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: multiple conversions #58

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
## 📜 Docs

```js
import convert from 'npm-to-yarn'
import { convert, convertMultiple } from 'npm-to-yarn'

// or
// var convert = require('npm-to-yarn')
Expand All @@ -34,9 +34,26 @@ convert('npm install squirrelly', 'yarn')
// yarn add squirrelly

// npx conversions

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

// one to many conversions
convertMultiple("npm i next", ["pnpm", "bun"])
// ["pnpm add next", "bun add next"]

// many to one
convertMultiple(["npm i eslint", "pnpm add react"], "yarn")
// ["yarn add eslint", "yarn add react"]

// many to many
convertMultiple(["bun add rollup", "npm i express"], ["yarn", "pnpm"])
/*
[
["yarn add rollup", "yarn add express"],
["pnpm add rollup", "pnpm add express"]
]
*/

```

`npm-to-yarn` exposes a UMD build, so you can also install it with a CDN (it exposes global variable `n2y`)
Expand All @@ -47,7 +64,8 @@ convert('npx create-next-app', 'yarn')
/**
* Converts between npm and yarn command
*/
export default function convert (str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun'): string
export function convert (str: string, to: Command): string
export function convertMultiple = (str: string | string[], to: Command | Command[]): string[]
```

## ✔️ Tests
Expand Down
26 changes: 25 additions & 1 deletion dist/npm-to-yarn.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,30 @@ function convert(str, to) {
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToYarn);
}
}
function convertMultiple(str, to) {
var commands = [];
// one to many
if (typeof str === 'string' && Array.isArray(to)) {
to.forEach(function (t) {
commands.push(convert(str, t));
});
}
// many to one
else if (Array.isArray(str) && typeof to === 'string') {
str.forEach(function (s) {
commands.push(convert(s, to));
});
}
// many to many
else if (Array.isArray(str) && Array.isArray(to)) {
to.forEach(function (t) {
str.forEach(function (s) {
commands.push(convert(s, t));
});
});
}
return commands;
}

export { convert as default };
export { convert, convertMultiple };
//# sourceMappingURL=npm-to-yarn.mjs.map
2 changes: 1 addition & 1 deletion dist/npm-to-yarn.mjs.map

Large diffs are not rendered by default.

35 changes: 30 additions & 5 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.

5 changes: 4 additions & 1 deletion dist/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
type Command = 'npm' | 'yarn' | 'pnpm' | 'bun';
/**
* Converts between npm and yarn command
*/
export default function convert(str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun'): string;
export declare function convert(str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun'): string;
export declare function convertMultiple(str: string | string[], to: Command | Command[]): string[];
export type { Command };
37 changes: 35 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { yarnToNPM } from './yarnToNpm'
import { npmToYarn } from './npmToYarn'
import { npmToPnpm } from './npmToPnpm'
import { npmToBun } from './npmToBun'

import { executorCommands } from './utils'

type Command = 'npm' | 'yarn' | 'pnpm' | 'bun'

/**
* Converts between npm and yarn command
*/
export default function convert (str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun'): string {

export function convert (str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun'): string {
if (
str.includes('npx') ||
str.includes('yarn dlx') ||
Expand All @@ -24,6 +26,7 @@ export default function convert (str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun
: '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 @@ -33,3 +36,33 @@ export default function convert (str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToYarn)
}
}


export function convertMultiple (str: string | string[], to: Command | Command[]): string[] {
const commands : string[] = []

// one to many
if (typeof str === 'string' && Array.isArray(to)) {
to.forEach((t) => {
commands.push(convert(str, t))
})
}
// many to one
else if (Array.isArray(str) && typeof to === 'string') {
str.forEach((s) => {
commands.push(convert(s, to))
})
}
// many to many
else if (Array.isArray(str) && Array.isArray(to)) {
to.forEach((t) => {
str.forEach((s) => {
commands.push(convert(s, t))
})
})
}

return commands
}

export type { Command }
119 changes: 118 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global it, expect, describe */

import convert from '../src'
import { convert, convertMultiple, type Command } from '../src'

describe('NPM tests', () => {
const tests: [npm: string, yarn: string, pnpm: string, bun: string][] = [
Expand Down Expand Up @@ -428,6 +428,123 @@ describe('Yarn to NPM tests', () => {
})
})

describe("Multiple Convert Tests", () => {
const oneToManyTests: {
command: string,
managers: Command[],
result: string[]
}[] = [
{
command: "npm i react",
managers: ["bun", "pnpm"],
result: ["bun add react", "pnpm add react"]
},
{
command: "npm install squirrelly --save",
managers: ["pnpm", "bun"],
result: ["pnpm add squirrelly", "bun add squirrelly"]
},
{
command: "npm install squirrelly --save-optional",
managers: ["yarn", "pnpm"],
result: ["yarn add squirrelly --optional", "pnpm add squirrelly --save-optional"]
},
{
command: "npm install squirrelly -E",
managers: ["yarn", "pnpm", "bun"],
result: ["yarn add squirrelly --exact", "pnpm add squirrelly -E", "bun add squirrelly --exact"]
},
{
command: "npm install squirrelly --save-prod",
managers: ["yarn", "bun"],
result: ["yarn add squirrelly --production", "bun add squirrelly --production"]
},
]

const manyToOneTests: {
command: string[],
managers: Command,
result: string[]
}[] = [
{
command: ["npm install react", "npm install react-dom"],
managers: "yarn",
result: ["yarn add react", "yarn add react-dom"]
},
{
command: ["npm install squirrelly --save", "npm install react --save-optional"],
managers: "pnpm",
result: ["pnpm add squirrelly", "pnpm add react --save-optional"]
},
{
command: ["npm install squirrelly -E", "npm install react --save-prod"],
managers: "bun",
result: ["bun add squirrelly --exact", "bun add react --production"]
},
{
command: ["npm install squirrelly --save-optional", "npm install react --save-prod"],
managers: "yarn",
result: ["yarn add squirrelly --optional", "yarn add react --production"]
},
{
command: ["npm outdated", "npm outdated react"],
managers: "pnpm",
result: ["pnpm outdated", "pnpm outdated react"]
},
]


const manyToManyTests: {
command: string[],
managers: Command[],
result: string[]
}[] = [
{
command: ["npm install react", "npm install react-dom"],
managers: ["yarn", "pnpm"],
result: ["yarn add react", "yarn add react-dom", "pnpm add react", "pnpm add react-dom"]
},
{
command: ["npm install squirrelly --save", "npm install react --save-optional"],
managers: ["yarn", "bun"],
result: ["yarn add squirrelly", "yarn add react --optional", "bun add squirrelly", "bun add react --optional"]
},
{
command: ["npm install squirrelly -E", "npm install react --save-prod"],
managers: ["pnpm", "bun"],
result: ["pnpm add squirrelly -E", "pnpm add react --save-prod", "bun add squirrelly --exact", "bun add react --production"]
},
{
command: ["npm install squirrelly --save-optional", "npm install react --save-prod", "npm install squirrelly --save-prod"],
managers: ["yarn", "pnpm"],
result: ["yarn add squirrelly --optional", "yarn add react --production", "yarn add squirrelly --production", "pnpm add squirrelly --save-optional", "pnpm add react --save-prod", "pnpm add squirrelly --save-prod"]
},
{
command: ["npm outdated", "npm outdated react"],
managers: ["yarn", "pnpm"],
result: ["yarn outdated", "yarn outdated react", "pnpm outdated", "pnpm outdated react"]
}
]

describe("One to Many", () => {
it.each(oneToManyTests)('%s', (test) => {
expect(convertMultiple(test.command, test.managers)).toEqual(test.result)
})
})

describe("Many to One", () => {
it.each(manyToOneTests)('%s', (test) => {
expect(convertMultiple(test.command, test.managers)).toEqual(test.result)
})
})

describe("Many to Many", () => {
it.each(manyToManyTests)('%s', (test) => {
expect(convertMultiple(test.command, test.managers)).toEqual(test.result)
})
})
})

describe('to yarn dlx tests', () => {
const tests: [npm: string, yarn: string][] = [
// npx -> ...
Expand Down
Loading