Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add npx conversions #53

Closed
wants to merge 14 commits into from
Closed
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ import convert from 'npm-to-yarn'
// var convert = require('npm-to-yarn')

convert('npm install squirrelly', 'yarn')

// yarn add squirrelly

// npx conversions

convert('npx create-next-app', 'yarn')
// yarn dlx create-next-app
```

`npm-to-yarn` exposes a UMD build, so you can also install it with a CDN (it exposes global variable `n2y`)
Expand Down
12 changes: 11 additions & 1 deletion dist/npm-to-yarn.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ var yarnCLICommands = [
'workspace',
'workspaces'
];
var executorCommands = {
"npm": "npx",
"yarn": "yarn dlx",
"pnpm": "pnpm dlx",
"bun": "bun x"
};

function parse(command) {
var args = [];
Expand Down Expand Up @@ -619,7 +625,11 @@ function npmToBun(_m, command) {
* Converts between npm and yarn command
*/
function convert(str, to) {
if (to === 'npm') {
if (str.includes('npx') || str.includes('yarn dlx') || str.includes('pnpm dlx') || str.includes('bun x')) {
var executor = str.includes('npx') ? 'npx' : str.includes('yarn dlx') ? 'yarn dlx' : str.includes('pnpm dlx') ? 'pnpm dlx' : 'bun x';
return str.replace(executor, executorCommands[to]);
}
else if (to === 'npm') {
return str.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToNPM);
}
else if (to === 'pnpm') {
Expand Down
2 changes: 1 addition & 1 deletion dist/npm-to-yarn.mjs.map

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion 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.

6 changes: 6 additions & 0 deletions dist/types/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export declare const unchangedCLICommands: string[];
export declare const yarnCLICommands: string[];
export declare const npmCLICommands: string[];
export declare const executorCommands: {
npm: string;
yarn: string;
pnpm: string;
bun: string;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "npm-to-yarn",
"version": "2.2.1",
"version": "3.0.0",
"description": "Convert npm CLI commands to Yarn commands, and vice versa",
"keywords": [
"string convert",
Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import { npmToYarn } from './npmToYarn'
import { npmToPnpm } from './npmToPnpm'
import { npmToBun } from './npmToBun'

import { executorCommands } from './utils'

/**
* Converts between npm and yarn command
*/
export default function convert (str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun'): string {
if (to === 'npm') {
export default function convert(str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun'): string {
if (str.includes('npx') || str.includes('yarn dlx') || str.includes('pnpm dlx') || str.includes('bun x')) {
const executor = str.includes('npx') ? 'npx' : str.includes('yarn dlx') ? 'yarn dlx' : str.includes('pnpm dlx') ? 'pnpm dlx' : 'bun x'
return str.replace(executor, executorCommands[to])
} else if (to === 'npm') {
return str.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToNPM)
} else if (to === 'pnpm') {
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToPnpm)
Expand All @@ -16,4 +21,4 @@ export default function convert (str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun
} else {
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToYarn)
}
}
}
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,10 @@ export const npmCLICommands = [
'view',
'whoami'
]

export const executorCommands = {
"npm": "npx",
"yarn": "yarn dlx",
"pnpm": "pnpm dlx",
"bun": "bun x"
}
222 changes: 222 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,225 @@ describe('Yarn to NPM tests', () => {
expect(convert(yarnValue, 'npm')).toEqual(npmValue)
})
})

describe('to yarn dlx tests', () => {
const tests: [npm: string, yarn: string][] = [
// npx -> ...
[
'npx create-next-app',
'yarn dlx create-next-app',
],
[
'npx prettier --help',
'yarn dlx prettier --help',
],
[
'npx prettier -w .',
'yarn dlx prettier -w .',
],
[
'npx @neutrinojs/create-project my-app',
'yarn dlx @neutrinojs/create-project my-app',
],
[
'npx create-react-app my-app --template typescript',
'yarn dlx create-react-app my-app --template typescript',
],
// pnpm dlx -> ...
[
'pnpm dlx create-next-app',
'yarn dlx create-next-app',
],
[
'pnpm dlx prettier --help',
'yarn dlx prettier --help',
],
[
'pnpm dlx prettier -w .',
'yarn dlx prettier -w .',
],
[
'pnpm dlx @neutrinojs/create-project my-app',
'yarn dlx @neutrinojs/create-project my-app',
],
[
'pnpm dlx create-react-app my-app --template typescript',
'yarn dlx create-react-app my-app --template typescript',
],
// bun x -> ...
[
'bun x create-next-app',
'yarn dlx create-next-app',
],
[
'bun x prettier --help',
'yarn dlx prettier --help',
],
[
'bun x prettier -w .',
'yarn dlx prettier -w .',
],
[
'bun x @neutrinojs/create-project my-app',
'yarn dlx @neutrinojs/create-project my-app',
],
[
'bun x create-react-app my-app --template typescript',
'yarn dlx create-react-app my-app --template typescript',
],
]

describe('to Yarn', () => {
it.each(tests)('%s', (npmValue, yarnValue) => {
expect(convert(npmValue, 'yarn')).toEqual(yarnValue)
})
})
})

describe('to pnpm dlx tests', () => {
const tests: [npm: string, pnpm: string][] = [
// npx -> ...
[
'npx create-next-app',
'pnpm dlx create-next-app',
],
[
'npx prettier --help',
'pnpm dlx prettier --help',
],
[
'npx prettier -w .',
'pnpm dlx prettier -w .',
],
[
'npx @neutrinojs/create-project my-app',
'pnpm dlx @neutrinojs/create-project my-app',
],
[
'npx create-react-app my-app --template typescript',
'pnpm dlx create-react-app my-app --template typescript',
],
// yarn dlx -> ...
[
'yarn dlx create-next-app',
'pnpm dlx create-next-app',
],
[
'yarn dlx prettier --help',
'pnpm dlx prettier --help',
],
[
'yarn dlx prettier -w .',
'pnpm dlx prettier -w .',
],
[
'yarn dlx @neutrinojs/create-project my-app',
'pnpm dlx @neutrinojs/create-project my-app',
],
[
'yarn dlx create-react-app my-app --template typescript',
'pnpm dlx create-react-app my-app --template typescript',
],
// bun x -> ...
[
'bun x create-next-app',
'pnpm dlx create-next-app',
],
[
'bun x prettier --help',
'pnpm dlx prettier --help',
],
[
'bun x prettier -w .',
'pnpm dlx prettier -w .',
],
[
'bun x @neutrinojs/create-project my-app',
'pnpm dlx @neutrinojs/create-project my-app',
],
[
'bun x create-react-app my-app --template typescript',
'pnpm dlx create-react-app my-app --template typescript',
],
]

describe('to PNPM', () => {
it.each(tests)('%s', (npmValue, pnpmValue) => {
expect(convert(npmValue, 'pnpm')).toEqual(pnpmValue)
})
})
})

describe('to bun x tests', () => {
const tests: [npm: string, bun: string][] = [
// npx -> ...
[
'npx create-next-app',
'bun x create-next-app',
],
[
'npx prettier --help',
'bun x prettier --help',
],
[
'npx prettier -w .',
'bun x prettier -w .',
],
[
'npx @neutrinojs/create-project my-app',
'bun x @neutrinojs/create-project my-app',
],
[
'npx create-react-app my-app --template typescript',
'bun x create-react-app my-app --template typescript',
],
// yarn dlx -> ...
[
'yarn dlx create-next-app',
'bun x create-next-app',
],
[
'yarn dlx prettier --help',
'bun x prettier --help',
],
[
'yarn dlx prettier -w .',
'bun x prettier -w .',
],
[
'yarn dlx @neutrinojs/create-project my-app',
'bun x @neutrinojs/create-project my-app',
],
[
'yarn dlx create-react-app my-app --template typescript',
'bun x create-react-app my-app --template typescript',
],
// pnpm dlx -> ...
[
'pnpm dlx create-next-app',
'bun x create-next-app',
],
[
'pnpm dlx prettier --help',
'bun x prettier --help',
],
[
'pnpm dlx prettier -w .',
'bun x prettier -w .',
],
[
'pnpm dlx @neutrinojs/create-project my-app',
'bun x @neutrinojs/create-project my-app',
],
[
'pnpm dlx create-react-app my-app --template typescript',
'bun x create-react-app my-app --template typescript',
],
]

describe('to Bun', () => {
it.each(tests)('%s', (npmValue, bunValue) => {
expect(convert(npmValue, 'bun')).toEqual(bunValue)
})
})
})
Loading