Skip to content

Commit

Permalink
Merge branch 'master' into feat/multiple-conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay-Karia authored Sep 5, 2024
2 parents 0d6fcef + 0b7979c commit 071194f
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 10 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { convert, convertMultiple } from 'npm-to-yarn'
// var convert = require('npm-to-yarn')

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

// yarn add squirrelly

// one to many conversions
Expand All @@ -50,6 +49,11 @@ convertMultiple(["bun add rollup", "npm i express"], ["yarn", "pnpm"])
["pnpm add rollup", "pnpm add express"]
]
*/

// 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 All @@ -71,6 +75,7 @@ Tests can be run with `npm test`. Multiple tests check that parsing, rendering,
## 📦 Contributing to `npm-to-yarn` - Setup Guide

Install Dependencies

```sh copy
npm install
```
Expand All @@ -83,11 +88,12 @@ npm run start

A new file: `npm-to-yarn.mjs` is created in `dist` folder. <br>
Open `node` inside the terminal and write the following code to test new changes

```js
const npmToYarn = await import("./dist/npm-to-yarn.mjs")
const npmToYarn = await import('./dist/npm-to-yarn.mjs')
const convert = npmToYarn.default
convert("npm install react", "bun")
convert('npm install react', 'bun')
```

## Resources
Expand Down
21 changes: 20 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,20 @@ 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.

21 changes: 20 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;
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
20 changes: 20 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,34 @@ 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 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 Down
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'
}
Loading

0 comments on commit 071194f

Please sign in to comment.