Skip to content

Commit

Permalink
fix: add missing shortcut support npm i, -E -D -S
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed Feb 12, 2023
1 parent 9171a85 commit fe3a782
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 16 deletions.
9 changes: 5 additions & 4 deletions dist/npm-to-yarn.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ var yarnToNpmTable = {
return ['rebuild'];
}
args[0] = 'install';
if (!args.includes('--dev')) {
if (!args.includes('--dev') && !args.includes('--exact') && !args.includes('--optional')) {
args.push('--save');
}
return convertAddRemoveArgs(args);
Expand Down Expand Up @@ -188,22 +188,23 @@ var npmToYarnTable = {
args.unshift('global');
}
return args.map(function (item) {
if (item === '--save-dev')
if (item === '--save-dev' || item === '-D')
return '--dev';
else if (item === '--save')
else if (item === '--save' || item === '-S')
return '';
else if (item === '--no-package-lock')
return '--no-lockfile';
else if (item === '--save-optional')
return '--optional';
else if (item === '--save-exact')
else if (item === '--save-exact' || item === '-E')
return '--exact';
else if (item === '--global' || item === '-g')
return '';
return item;
});
},
i: function (args) {
args[0] = 'install';
return npmToYarnTable.install(args);
},
uninstall: function (args) {
Expand Down
2 changes: 1 addition & 1 deletion dist/npm-to-yarn.mjs.map

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions dist/npm-to-yarn.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/npm-to-yarn.umd.js.map

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/npmToYarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ const npmToYarnTable = {
}

return args.map((item) => {
if (item === '--save-dev') return '--dev'
else if (item === '--save') return ''
if (item === '--save-dev' || item === '-D') return '--dev'
else if (item === '--save' || item === '-S') return ''
else if (item === '--no-package-lock') return '--no-lockfile'
else if (item === '--save-optional') return '--optional'
else if (item === '--save-exact') return '--exact'
else if (item === '--save-exact' || item === '-E') return '--exact'
else if (item === '--global' || item === '-g') return ''
return item
})
},
i(args: string[]) {
args[0] = 'install'
return npmToYarnTable.install(args)
},
uninstall(args: string[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/yarnToNpm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const yarnToNpmTable = {
return ['rebuild']
}
args[0] = 'install'
if (!args.includes('--dev')) {
if (!args.includes('--dev') && !args.includes('--exact') && !args.includes('--optional')) {
args.push('--save')
}
return convertAddRemoveArgs(args)
Expand Down
23 changes: 21 additions & 2 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,32 @@ describe('NPM to Yarn tests', () => {

it('npm install', () => {
expect(convert('npm install', 'yarn')).toEqual('yarn install')
expect(convert('npm i', 'yarn')).toEqual('yarn install')
})

it('npm install --save', () => {
expect(convert('npm install --save test', 'yarn')).toEqual('yarn add test')
expect(convert('npm install -S test', 'yarn')).toEqual('yarn add test')
expect(convert('npm i --save test', 'yarn')).toEqual('yarn add test')
expect(convert('npm i -S test', 'yarn')).toEqual('yarn add test')
})

it('npm install --save-exact', () => {
expect(convert('npm install --save-exact', 'yarn')).toEqual('yarn add --exact')
expect(convert('npm install -E', 'yarn')).toEqual('yarn add --exact')
expect(convert('npm i --save-exact', 'yarn')).toEqual('yarn add --exact')
expect(convert('npm i -E', 'yarn')).toEqual('yarn add --exact')
})
it('npm install --save-optional', () => {
expect(convert('npm install --save-optional', 'yarn')).toEqual('yarn add --optional')
expect(convert('npm i --save-optional', 'yarn')).toEqual('yarn add --optional')
expect(convert('npm i --save-optional test', 'yarn')).toEqual('yarn add --optional test')
})
it('npm install --save-dev', () => {
expect(convert('npm install --save-dev', 'yarn')).toEqual('yarn add --dev')
expect(convert('npm install -D', 'yarn')).toEqual('yarn add --dev')
expect(convert('npm i --save-dev', 'yarn')).toEqual('yarn add --dev')
expect(convert('npm i -D', 'yarn')).toEqual('yarn add --dev')
})

it('npm rebuild', () => {
Expand Down Expand Up @@ -145,10 +164,10 @@ describe('Yarn to NPM tests', () => {
'npm install squirrelly --no-package-lock --save'
)
expect(convert('yarn add squirrelly --optional', 'npm')).toEqual(
'npm install squirrelly --save-optional --save'
'npm install squirrelly --save-optional'
)
expect(convert('yarn add squirrelly --exact', 'npm')).toEqual(
'npm install squirrelly --save-exact --save'
'npm install squirrelly --save-exact'
)
})

Expand Down

0 comments on commit fe3a782

Please sign in to comment.