Skip to content

Commit

Permalink
fix: add missing yarn pack and outdated, and remove incorrect --save
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed Feb 19, 2023
1 parent 4304218 commit ca64dce
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 158 deletions.
19 changes: 9 additions & 10 deletions dist/npm-to-yarn.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,10 @@ var yarnToNpmTable = {
return ['rebuild'];
}
args[0] = 'install';
if (!args.includes('--dev') &&
!args.includes('--force') &&
!args.includes('--exact') &&
!args.includes('--optional') &&
!args.includes('--production')) {
args.push('--save');
}
return convertAddRemoveArgs(args);
},
remove: function (args) {
args[0] = 'uninstall';
if (!args.includes('--dev')) {
args.push('--save');
}
return convertAddRemoveArgs(args);
},
version: function (args) {
Expand Down Expand Up @@ -149,6 +139,7 @@ var yarnToNpmTable = {
},
init: 'init',
create: 'init',
outdated: 'outdated',
run: 'run',
global: function (args) {
switch (args[1]) {
Expand All @@ -173,6 +164,14 @@ var yarnToNpmTable = {
args.push("\n# couldn't auto-convert command");
return args;
}
},
pack: function (args) {
return args.map(function (item) {
if (item === '--filename') {
return '--pack-destination';
}
return item;
});
}
};
function yarnToNPM(_m, command) {
Expand Down
2 changes: 1 addition & 1 deletion dist/npm-to-yarn.mjs.map

Large diffs are not rendered by default.

19 changes: 9 additions & 10 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.

2 changes: 1 addition & 1 deletion src/npmToYarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const npmToYarnTable = {
t: 'test',
tst: 'test',
outdated: 'outdated',
pack: (args: string[]) => {
pack (args: string[]) {
return args.map(item => {
if (item.startsWith('--pack-destination')) {
return item.replace(/^--pack-destination[\s=]/, '--filename ')
Expand Down
21 changes: 9 additions & 12 deletions src/yarnToNpm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,10 @@ const yarnToNpmTable = {
return ['rebuild']
}
args[0] = 'install'
if (
!args.includes('--dev') &&
!args.includes('--force') &&
!args.includes('--exact') &&
!args.includes('--optional') &&
!args.includes('--production')
) {
args.push('--save')
}
return convertAddRemoveArgs(args)
},
remove (args: string[]) {
args[0] = 'uninstall'
if (!args.includes('--dev')) {
args.push('--save')
}
return convertAddRemoveArgs(args)
},
version (args: string[]) {
Expand Down Expand Up @@ -70,6 +58,7 @@ const yarnToNpmTable = {
},
init: 'init',
create: 'init',
outdated: 'outdated',
run: 'run',
global (args: string[]) {
switch (args[1]) {
Expand All @@ -94,6 +83,14 @@ const yarnToNpmTable = {
args.push("\n# couldn't auto-convert command")
return args
}
},
pack (args: string[]) {
return args.map(item => {
if (item === '--filename') {
return '--pack-destination'
}
return item
})
}
}

Expand Down
208 changes: 85 additions & 123 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,130 +202,92 @@ describe('NPM tests', () => {
})

describe('Yarn to NPM tests', () => {
it('Simple convert works', () => {
expect(convert('yarn add squirrelly', 'npm')).toEqual('npm install squirrelly --save')
expect(convert('yarn add squirrelly --no-lockfile', 'npm')).toEqual(
'npm install squirrelly --no-package-lock --save'
)
expect(convert('yarn add squirrelly --optional', 'npm')).toEqual(
'npm install squirrelly --save-optional'
)
expect(convert('yarn add squirrelly --exact', 'npm')).toEqual(
'npm install squirrelly --save-exact'
)
expect(convert('yarn add test --production', 'npm')).toEqual('npm install test --save-prod')
})

it('Convert with dev works', () => {
expect(convert('yarn add squirrelly --dev', 'npm')).toEqual('npm install squirrelly --save-dev')
})

it('Convert with remove works', () => {
expect(convert('yarn remove squirrelly --dev', 'npm')).toEqual(
'npm uninstall squirrelly --save-dev'
)
})

it('Convert with cache clean works', () => {
expect(convert('yarn cache clean', 'npm')).toEqual('npm cache clean')
})

it('Yarn implied run', () => {
expect(convert('yarn grunt', 'npm')).toEqual('npm run grunt')
})

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

it('Yarn global', () => {
expect(convert('yarn global add squirrelly', 'npm')).toEqual(
'npm install squirrelly --save --global'
)
expect(convert('yarn global remove squirrelly', 'npm')).toEqual(
'npm uninstall squirrelly --save --global'
)
expect(convert('yarn global squirrelly', 'npm')).toEqual(
'npm global squirrelly \n' + "# couldn't auto-convert command"
)
expect(convert('yarn global list', 'npm')).toEqual('npm ls --global')
})

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

it('yarn add --force', () => {
expect(convert('yarn add --force', 'npm')).toEqual('npm rebuild')
expect(convert('yarn add package --force', 'npm')).toEqual('npm install package --force')
})

it('Version works', () => {
expect(convert('yarn version', 'npm')).toEqual('npm version')
expect(convert('yarn version --major', 'npm')).toEqual('npm version major')
expect(convert('yarn version --minor', 'npm')).toEqual('npm version minor')
expect(convert('yarn version --patch', 'npm')).toEqual('npm version patch')
})

it('Yarn remove', () => {
expect(convert('yarn remove squirrelly', 'npm')).toEqual('npm uninstall squirrelly --save')
})

it("Yarn upgrade-interactive can't auto-convert", () => {
expect(convert('yarn upgrade-interactive', 'npm')).toEqual(
"npm upgrade-interactive\n# couldn't auto-convert command"
)
})

it('yarn init', () => {
expect(convert('yarn init', 'npm')).toEqual('npm init')
expect(convert('yarn init -y', 'npm')).toEqual('npm init -y')
expect(convert('yarn init --yes', 'npm')).toEqual('npm init --yes')
expect(convert('yarn init --private', 'npm')).toEqual('npm init --private')
expect(convert('yarn init --unknown-arg', 'npm')).toEqual('npm init --unknown-arg')
})

it('yarn create', () => {
expect(convert('yarn create esm --yes', 'npm')).toEqual('npm init esm --yes')
expect(convert('yarn create @scope/my-package', 'npm')).toEqual('npm init @scope/my-package')
expect(convert('yarn create react-app ./my-react-app', 'npm')).toEqual(
'npm init react-app ./my-react-app'
)
})

it('npm custom', () => {
expect(convert('yarn start', 'npm')).toEqual('npm start')
expect(convert('yarn stop', 'npm')).toEqual('npm stop')
expect(convert('yarn test', 'npm')).toEqual('npm test')
})

it('yarn run', () => {
expect(convert('yarn custom', 'npm')).toEqual('npm run custom')
expect(convert('yarn run custom', 'npm')).toEqual('npm run custom')
expect(convert('yarn run add', 'npm')).toEqual('npm run add')
expect(convert('yarn run install', 'npm')).toEqual('npm run install')
expect(convert('yarn run run', 'npm')).toEqual('npm run run')
// with args
expect(convert('yarn custom -- --version', 'npm')).toEqual('npm run custom -- --version')
expect(convert('yarn run custom --version', 'npm')).toEqual('npm run custom --version')
expect(convert('yarn run custom -- --version', 'npm')).toEqual('npm run custom -- --version')
})

it('yarn list', () => {
expect(convert('yarn list', 'npm')).toEqual('npm ls')
expect(convert('yarn list --pattern "package"', 'npm')).toEqual('npm ls package')
expect(convert('yarn list --pattern "package|package2"', 'npm')).toEqual(
'npm ls package package2'
)
expect(convert('yarn list --pattern "@scope/package|@scope/package2"', 'npm')).toEqual(
const tests = [
// install
['yarn', 'npm install'],
['yarn install', 'npm install'],
// add
['yarn add squirrelly', 'npm install squirrelly'],
['yarn add squirrelly --no-lockfile', 'npm install squirrelly --no-package-lock'],
['yarn add squirrelly --optional', 'npm install squirrelly --save-optional'],
['yarn add squirrelly --exact', 'npm install squirrelly --save-exact'],
['yarn add squirrelly --production', 'npm install squirrelly --save-prod'],
['yarn add squirrelly --dev', 'npm install squirrelly --save-dev'],
['yarn add --force', 'npm rebuild'],
['yarn add package --force', 'npm install package --force'],
// remove
['yarn remove squirrelly', 'npm uninstall squirrelly'],
['yarn remove squirrelly --dev', 'npm uninstall squirrelly --save-dev'],
// cache
['yarn cache clean', 'npm cache clean'],
// implied run
['yarn grunt', 'npm run grunt'],
// global
['yarn global add squirrelly', 'npm install squirrelly --global'],
['yarn global remove squirrelly', 'npm uninstall squirrelly --global'],
['yarn global squirrelly', "npm global squirrelly \n# couldn't auto-convert command"],
['yarn global list', 'npm ls --global'],
// version
['yarn version', 'npm version'],
['yarn version --major', 'npm version major'],
['yarn version --minor', 'npm version minor'],
['yarn version --patch', 'npm version patch'],
// init
['yarn init', 'npm init'],
['yarn init -y', 'npm init -y'],
['yarn init --yes', 'npm init --yes'],
['yarn init --private', 'npm init --private'],
['yarn init --unknown-arg', 'npm init --unknown-arg'],
// create
['yarn create esm --yes', 'npm init esm --yes'],
['yarn create @scope/my-package', 'npm init @scope/my-package'],
['yarn create react-app ./my-react-app', 'npm init react-app ./my-react-app'],
// unchanged
['yarn start', 'npm start'],
['yarn stop', 'npm stop'],
['yarn test', 'npm test'],
// run
['yarn run', 'npm run'],
['yarn custom', 'npm run custom'],
['yarn run custom', 'npm run custom'],
['yarn run add', 'npm run add'],
['yarn run install', 'npm run install'],
['yarn run run', 'npm run run'],
['yarn run --silent', 'npm run --silent'],
['yarn custom -- --version', 'npm run custom -- --version'],
['yarn run custom -- --version', 'npm run custom -- --version'],
['yarn run custom --version', 'npm run custom --version'],
// list
['yarn list', 'npm ls'],
['yarn list --pattern "package"', 'npm ls package'],
['yarn list --pattern "package|package2"', 'npm ls package package2'],
[
'yarn list --pattern "@scope/package|@scope/package2"',
'npm ls @scope/package @scope/package2'
)
expect(convert('yarn list --production', 'npm')).toEqual('npm ls --production')
expect(convert('yarn list --development', 'npm')).toEqual('npm ls --development')
})
],
['yarn list --depth 2', 'npm ls --depth 2'],
['yarn list --json', 'npm ls --json'],
['yarn list --production', 'npm ls --production'],
['yarn list --development', 'npm ls --development'],
// link/unlink
['yarn link', 'npm link'],
['yarn link custom', 'npm link custom'],
['yarn unlink', 'npm unlink'],
['yarn unlink custom', 'npm unlink custom'],
// outdated
['yarn outdated', 'npm outdated'],
['yarn outdated --json', 'npm outdated --json'],
['yarn outdated --long', 'npm outdated --long'],
['yarn outdated lodash', 'npm outdated lodash'],
// pack
['yarn pack', 'npm pack'],
['yarn pack --filename foobar', 'npm pack --pack-destination foobar'],
// unsupported
['yarn why', "npm why\n# couldn't auto-convert command"],
['yarn upgrade-interactive', "npm upgrade-interactive\n# couldn't auto-convert command"]
]

it('npm link/unlink', () => {
expect(convert('yarn link custom', 'npm')).toEqual('npm link custom')
expect(convert('yarn unlink custom', 'npm')).toEqual('npm unlink custom')
it.each(tests)('%s', (yarnValue, npmValue) => {
expect(convert(yarnValue, 'npm')).toEqual(npmValue)
})
})

0 comments on commit ca64dce

Please sign in to comment.