From f6bf2f4d4783f1b14e4a8acc16dc4e986e213177 Mon Sep 17 00:00:00 2001 From: Charlie Greenman Date: Thu, 31 Aug 2023 15:58:31 -0700 Subject: [PATCH] ZETA-6608: Add save-dev support for npm install. (#13) --- src/npm/npm-install/npm-install.spec.ts | 25 +++++++++++++++++++++- src/npm/npm-install/npm-install.ts | 2 +- src/utils/command-search/command-search.ts | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/npm/npm-install/npm-install.spec.ts b/src/npm/npm-install/npm-install.spec.ts index 234f00b..9d3ccd6 100644 --- a/src/npm/npm-install/npm-install.spec.ts +++ b/src/npm/npm-install/npm-install.spec.ts @@ -2,7 +2,7 @@ import { morphCommand } from "../../command-morph/command-morph"; describe('npmInstall', () => { it('should return npm install with needed codemod', async() => { - const npmInstallCommandText = 'npm install apollo-angular react --save'; + const npmInstallCommandText = 'npm install apollo-angular@5.0.0 react --save'; const mockPackageJson = { name: 'hello', dependencies: { @@ -18,6 +18,29 @@ describe('npmInstall', () => { "apollo-angular": "5.0.0", "react": "18.2.0" } +}`; + expect(result).toEqual(expected); + }); + + it('should return npm install with needed codemod if saving as dev dependency', async() => { + const npmInstallCommandText = 'npm install apollo-angular@5.0.0 react@18.2.0 --save-dev'; + const mockPackageJson = { + name: 'hello', + dependencies: { + "angular": "16.0.0" + } + }; + const stringifiedMockPackageJson = JSON.stringify(mockPackageJson); + const result = await morphCommand(npmInstallCommandText, stringifiedMockPackageJson); + const expected = `{ + "name": "hello", + "dependencies": { + "angular": "16.0.0" + }, + "devDependencies": { + "apollo-angular": "5.0.0", + "react": "18.2.0" + } }`; expect(result).toEqual(expected); }); diff --git a/src/npm/npm-install/npm-install.ts b/src/npm/npm-install/npm-install.ts index 5aabb07..fca1930 100644 --- a/src/npm/npm-install/npm-install.ts +++ b/src/npm/npm-install/npm-install.ts @@ -12,8 +12,8 @@ export async function npmInstallCodemorph(commandText: string, packageJsonString } const packageJsonParsed = JSON.parse(packageJsonString); - const packageJsonDependencies = packageJsonParsed && packageJsonParsed.dependencies; const dependencyType = determineDependencyType(commandText); + const packageJsonDependencies = packageJsonParsed && dependencyType === 'dependencies' ? packageJsonParsed.dependencies : packageJsonParsed.devDependencies; const editInput: EditInput = { fileType: 'json', diff --git a/src/utils/command-search/command-search.ts b/src/utils/command-search/command-search.ts index 03d903d..2574c0d 100644 --- a/src/utils/command-search/command-search.ts +++ b/src/utils/command-search/command-search.ts @@ -19,8 +19,8 @@ function containsDoubleAmpersand(input: string): boolean { export function extractPackageNames(commandText: string): string[] { const commandTextCleaned = commandText.replace(/npm/g, '') .replace(/install/g, '') - .replace(/--save/g, '') .replace(/--save-dev/g, '') + .replace(/--save/g, '') .replace(/;/g, '') .trim();