Skip to content

Commit

Permalink
fix: add support for global list and correct handling of global add/r…
Browse files Browse the repository at this point in the history
…emove
  • Loading branch information
armano2 committed Feb 12, 2023
1 parent 920d81b commit 9c3b360
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 66 deletions.
34 changes: 24 additions & 10 deletions dist/npm-to-yarn.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var yarnCLICommands = [
'versions',
'why',
'workspace',
'workspaces',
'workspaces'
];

function parse(command) {
Expand Down Expand Up @@ -139,15 +139,29 @@ var yarnToNpmTable = {
stop: 'stop',
test: 'test',
global: function (args) {
if (args[1] === 'add' || args[1] === 'remove') {
args.splice(0, 2, args[1] === 'add' ? 'install' : 'uninstall');
args.push('--global');
return convertAddRemoveArgs(args);
switch (args[1]) {
case 'add':
args.shift();
args = yarnToNpmTable.add(args);
args.push('--global');
return args;
case 'remove':
args.shift();
args = yarnToNpmTable.remove(args);
args.push('--global');
return args;
case 'list':
args.shift();
args = yarnToNpmTable.list(args);
args.push('--global');
return args;
// case 'bin':
// case 'upgrade':
default:
args.push("\n# couldn't auto-convert command");
return args;
}
// TODO: find better way
args.push("\n# couldn't auto-convert command");
return args;
},
}
};
function yarnToNPM(_m, command) {
command = (command || '').trim();
Expand Down Expand Up @@ -271,7 +285,7 @@ var npmToYarnTable = {
},
start: 'start',
stop: 'stop',
test: 'test',
test: 'test'
};
function npmToYarn(_m, command) {
var args = parse((command || '').trim());
Expand Down
2 changes: 1 addition & 1 deletion dist/npm-to-yarn.mjs.map

Large diffs are not rendered by default.

34 changes: 24 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/command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function parse(command: string) {
export function parse (command: string) {
const args: string[] = []
let lastQuote: string | false = false
let escaped = false
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { npmToYarn } from './npmToYarn'
/**
* Converts between npm and yarn command
*/
export default function convert(str: string, to: 'npm' | 'yarn'): string {
export default function convert (str: string, to: 'npm' | 'yarn'): string {
if (to === 'npm') {
return str.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToNPM)
} else {
Expand Down
34 changes: 17 additions & 17 deletions src/npmToYarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { unchangedCLICommands, yarnCLICommands } from './utils'
import { parse } from './command'

const npmToYarnTable = {
install(args: string[]) {
install (args: string[]) {
if (args.length === 1) {
return args
}
Expand All @@ -12,7 +12,7 @@ const npmToYarnTable = {
args.unshift('global')
}

return args.map((item) => {
return args.map(item => {
if (item === '--save-dev' || item === '-D') return '--dev'
else if (item === '--save' || item === '-S') return ''
else if (item === '--no-package-lock') return '--no-lockfile'
Expand All @@ -22,46 +22,46 @@ const npmToYarnTable = {
return item
})
},
i(args: string[]) {
i (args: string[]) {
args[0] = 'install'
return npmToYarnTable.install(args)
},
uninstall(args: string[]) {
uninstall (args: string[]) {
args[0] = 'remove'

if (args.includes('--global') || args.includes('-g')) {
args.unshift('global')
}

return args.map((item) => {
return args.map(item => {
if (item === '--save-dev') return '--dev'
else if (item === '--save') return ''
else if (item === '--no-package-lock') return '--no-lockfile'
else if (item === '--global' || item === '-g') return ''
return item
})
},
version(args: string[]) {
return args.map((item) => {
version (args: string[]) {
return args.map(item => {
if (['major', 'minor', 'patch'].includes(item)) return `--${item}`
return item
})
},
rebuild(args: string[]) {
rebuild (args: string[]) {
args[0] = 'add --force'
return args
},
run(args: string[]) {
run (args: string[]) {
if (args[1] && !unchangedCLICommands.includes(args[1]) && !yarnCLICommands.includes(args[1])) {
args.splice(0, 1)
}
return args
},
exec(args: string[]) {
exec (args: string[]) {
args[0] = 'run'
return npmToYarnTable.run(args)
},
ls(args: string[]) {
ls (args: string[]) {
args[0] = 'list'

let ended = false
Expand All @@ -77,24 +77,24 @@ const npmToYarnTable = {
}
return args
},
list(args: string[]) {
list (args: string[]) {
return npmToYarnTable.ls(args)
},
init(args: string[]) {
init (args: string[]) {
if (args[1] && !args[1].startsWith('-')) {
args[0] = 'create'
}
return args.filter((item) => item !== '--scope')
return args.filter(item => item !== '--scope')
},
start: 'start',
stop: 'stop',
test: 'test',
test: 'test'
}

export function npmToYarn(_m: string, command: string): string {
export function npmToYarn (_m: string, command: string): string {
let args = parse((command || '').trim())

const index = args.findIndex((a) => a === '--')
const index = args.findIndex(a => a === '--')
if (index >= 0) {
args.splice(index, 1)
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const yarnCLICommands = [
'versions',
'why',
'workspace',
'workspaces',
'workspaces'
]

export const npmCLICommands = [
Expand Down Expand Up @@ -97,5 +97,5 @@ export const npmCLICommands = [
'update',
'version',
'view',
'whoami',
'whoami'
]
50 changes: 32 additions & 18 deletions src/yarnToNpm.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { unchangedCLICommands, yarnCLICommands } from './utils'
import { parse } from './command'

function convertAddRemoveArgs(args: string[]) {
return args.map((item) => {
function convertAddRemoveArgs (args: string[]) {
return args.map(item => {
switch (item) {
case '--no-lockfile':
return '--no-package-lock'
Expand All @@ -19,7 +19,7 @@ function convertAddRemoveArgs(args: string[]) {
}

const yarnToNpmTable = {
add(args: string[]) {
add (args: string[]) {
if (args[1] === '--force') {
return ['rebuild']
}
Expand All @@ -29,15 +29,15 @@ const yarnToNpmTable = {
}
return convertAddRemoveArgs(args)
},
remove(args: string[]) {
remove (args: string[]) {
args[0] = 'uninstall'
if (!args.includes('--dev')) {
args.push('--save')
}
return convertAddRemoveArgs(args)
},
version(args: string[]) {
return args.map((item) => {
version (args: string[]) {
return args.map(item => {
switch (item) {
case '--major':
return 'major'
Expand All @@ -51,9 +51,9 @@ const yarnToNpmTable = {
})
},
install: 'install',
list(args: string[]) {
list (args: string[]) {
args[0] = 'ls'
const patternIndex = args.findIndex((item) => item === '--pattern')
const patternIndex = args.findIndex(item => item === '--pattern')
if (patternIndex >= 0 && args[patternIndex + 1]) {
const packages = args[patternIndex + 1].replace(/["']([^"']+)["']/, '$1').split('|')
args.splice(patternIndex, 2, packages.join(' '))
Expand All @@ -66,19 +66,33 @@ const yarnToNpmTable = {
start: 'start',
stop: 'stop',
test: 'test',
global(args: string[]) {
if (args[1] === 'add' || args[1] === 'remove') {
args.splice(0, 2, args[1] === 'add' ? 'install' : 'uninstall')
args.push('--global')
return convertAddRemoveArgs(args)
global (args: string[]) {
switch (args[1]) {
case 'add':
args.shift()
args = yarnToNpmTable.add(args)
args.push('--global')
return args
case 'remove':
args.shift()
args = yarnToNpmTable.remove(args)
args.push('--global')
return args
case 'list':
args.shift()
args = yarnToNpmTable.list(args)
args.push('--global')
return args
// case 'bin':
// case 'upgrade':
default:
args.push("\n# couldn't auto-convert command")
return args
}
// TODO: find better way
args.push("\n# couldn't auto-convert command")
return args
},
}
}

export function yarnToNPM(_m: string, command: string): string {
export function yarnToNPM (_m: string, command: string): string {
command = (command || '').trim()
if (command === '') {
return 'npm install'
Expand Down
14 changes: 9 additions & 5 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ describe('NPM to Yarn tests', () => {
)
})

it('Global list', () => {
expect(convert('npm list --global', 'yarn')).toEqual('yarn list --global')
})

it('Unchanged command', () => {
expect(convert('npm cache clean', 'yarn')).toEqual('yarn cache clean')
})
Expand Down Expand Up @@ -196,16 +200,16 @@ describe('Yarn to NPM tests', () => {
})

it('Yarn global', () => {
expect(convert('yarn global add squirrelly', 'npm')).toEqual('npm install squirrelly --global')
})

it('Yarn global remove', () => {
expect(convert('yarn global add squirrelly', 'npm')).toEqual(
'npm install squirrelly --save --global'
)
expect(convert('yarn global remove squirrelly', 'npm')).toEqual(
'npm uninstall squirrelly --global'
'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`', () => {
Expand Down

0 comments on commit 9c3b360

Please sign in to comment.