Skip to content

Commit

Permalink
feat: add basic yarn run conversion logic
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed Nov 23, 2021
1 parent d63d533 commit 846bb99
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
32 changes: 24 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ var npmToYarnTable: Indexable = {
rebuild: function (command: string) {
return command.replace('rebuild', 'add --force')
},
run: function (command: string) {
return command.replace(
/^run\s?([^\s]+)?(\s--\s--)?(.*)$/,
(_, data?: string, dash?: string, rest?: string): string => {
var result = ''
if (data && !unchangedCLICommands.includes(data) && !yarnCLICommands.includes(data)) {
result += data
} else {
result += 'run ' + (data || '')
}
if (dash) result += dash.replace(/^\s--/, '')
if (rest) result += rest
return result
}
)
},
ls: 'why',
init: function (command: string) {
if (/^init (?!-).*$/.test(command)) {
Expand Down Expand Up @@ -106,14 +122,14 @@ var yarnToNpmTable: Indexable = {
install: 'install',
why: 'ls',
init: 'init',
create: 'init'
}

yarnToNpmTable.global = function (command: string) {
if (/^global add/.test(command)) {
return (yarnToNpmTable.add as Function)(command.replace(/^global add/, 'add'), true)
} else if (/^global remove/.test(command)) {
return (yarnToNpmTable.remove as Function)(command.replace(/^global remove/, 'remove'), true)
create: 'init',
run: 'run',
global: function (command: string) {
if (/^global add/.test(command)) {
return (yarnToNpmTable.add as Function)(command.replace(/^global add/, 'add'), true)
} else if (/^global remove/.test(command)) {
return (yarnToNpmTable.remove as Function)(command.replace(/^global remove/, 'remove'), true)
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export var unchangedCLICommands = ['run', 'test', 'login', 'logout', 'link', 'publish', 'cache']
export var unchangedCLICommands = ['test', 'login', 'logout', 'link', 'publish', 'cache']

export var yarnCLICommands = [
'init',
'run',
'add',
'audit',
'autoclean',
Expand Down Expand Up @@ -63,6 +65,8 @@ export var uniqueYarnCLICommands = [
]

export var npmCLICommands = [
'init',
'run',
'access',
'adduser',
'audit',
Expand Down
21 changes: 21 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ describe('NPM to Yarn tests', () => {
'yarn create react-app ./my-react-app'
)
})

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

describe('Yarn to NPM tests', () => {
Expand Down Expand Up @@ -131,4 +140,16 @@ describe('Yarn to NPM tests', () => {
'npm init react-app ./my-react-app'
)
})

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')
})
})

0 comments on commit 846bb99

Please sign in to comment.