Skip to content

Commit

Permalink
feat: add missing ls/list conversion
Browse files Browse the repository at this point in the history
armano2 committed Nov 24, 2021

Verified

This commit was signed with the committer’s verified signature.
lucasssvaz Lucas Saavedra Vaz
1 parent 846bb99 commit dc05ac5
Showing 3 changed files with 78 additions and 4 deletions.
42 changes: 39 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import {
// keys are NPM, vals are Yarn

interface Indexable {
[index: string]: string | Function
[index: string]: string | ((command: string) => string)
}

var npmToYarnTable: Indexable = {
@@ -63,7 +63,37 @@ var npmToYarnTable: Indexable = {
}
)
},
ls: 'why',
ls: function (command: string) {
return command.replace(/^(ls|list)(.*)$/, function (_1, _2: string, args: string): string {
var result = 'list'
if (args) {
var ended = false
var packages = []
var items = args.split(' ').filter(Boolean)
for (var item of items) {
if (ended) {
result += ' ' + item
} else if (item.startsWith('-')) {
result += ' --pattern "' + packages.join('|') + '"'
packages = []
ended = true
result += ' ' + item
} else {
packages.push(item)
}
}
if (packages.length > 0) {
result += ' --pattern "' + packages.join('|') + '"'
}
return result
} else {
return 'list'
}
})
},
list: function (command: string) {
return (npmToYarnTable.ls as Function)(command)
},
init: function (command: string) {
if (/^init (?!-).*$/.test(command)) {
return command.replace('init', 'create')
@@ -120,7 +150,13 @@ var yarnToNpmTable: Indexable = {
return command.replace(/--(major|minor|patch)/, '$1')
},
install: 'install',
why: 'ls',
list: function (command: string) {
return command
.replace(/--pattern ["']([^"']+)["']/, function (_, packages: string) {
return packages.split('|').join(' ')
})
.replace(/^list/, 'ls')
},
init: 'init',
create: 'init',
run: 'run',
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -49,7 +49,6 @@ export var uniqueYarnCLICommands = [
'info',
'licenses',
'link',
'list',
'lockfile',
'policies',
'remove',
@@ -60,6 +59,7 @@ export var uniqueYarnCLICommands = [
'upgrade',
'upgrade-interactive',
'versions',
'why',
'workspace',
'workspaces'
]
@@ -92,6 +92,7 @@ export var npmCLICommands = [
'install-test',
'install',
'ls',
'list',
'npm',
'org',
'outdated',
37 changes: 37 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -64,6 +64,32 @@ describe('NPM to Yarn tests', () => {
// with args
expect(convert('npm run custom -- --version', 'yarn')).toEqual('yarn custom --version')
})

it('npm list', () => {
expect(convert('npm list', 'yarn')).toEqual('yarn list')
expect(convert('npm list package', 'yarn')).toEqual('yarn list --pattern "package"')
expect(convert('npm list package package2', 'yarn')).toEqual(
'yarn list --pattern "package|package2"'
)
expect(convert('npm list @scope/package @scope/package2', 'yarn')).toEqual(
'yarn list --pattern "@scope/package|@scope/package2"'
)
expect(convert('npm list @scope/package @scope/package2 --depth=2', 'yarn')).toEqual(
'yarn list --pattern "@scope/package|@scope/package2" --depth=2'
)
// alias
expect(convert('npm ls', 'yarn')).toEqual('yarn list')
expect(convert('npm ls package', 'yarn')).toEqual('yarn list --pattern "package"')
expect(convert('npm ls package package2', 'yarn')).toEqual(
'yarn list --pattern "package|package2"'
)
expect(convert('npm ls @scope/package @scope/package2', 'yarn')).toEqual(
'yarn list --pattern "@scope/package|@scope/package2"'
)
expect(convert('npm ls @scope/package @scope/package2 --depth=2', 'yarn')).toEqual(
'yarn list --pattern "@scope/package|@scope/package2" --depth=2'
)
})
})

describe('Yarn to NPM tests', () => {
@@ -152,4 +178,15 @@ describe('Yarn to NPM tests', () => {
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(
'npm ls @scope/package @scope/package2'
)
})
})

0 comments on commit dc05ac5

Please sign in to comment.