Skip to content

Commit

Permalink
ZETA-6608: Add save-dev support for npm install. (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieGreenman authored Aug 31, 2023
1 parent ccb2537 commit f6bf2f4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
25 changes: 24 additions & 1 deletion src/npm/npm-install/npm-install.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion src/npm/npm-install/npm-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/utils/command-search/command-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit f6bf2f4

Please sign in to comment.