Skip to content

Commit

Permalink
Merge pull request #42 from colinhacks/colinhacks/add-bun
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug authored Sep 6, 2023
2 parents 5e69e30 + bcf26e5 commit 13c221e
Show file tree
Hide file tree
Showing 9 changed files with 643 additions and 100 deletions.
135 changes: 132 additions & 3 deletions dist/npm-to-yarn.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function yarnToNPM(_m, command) {
}
}

function convertInstallArgs(args) {
function convertInstallArgs$1(args) {
if (args.includes('--global') || args.includes('-g')) {
args.unshift('global');
}
Expand Down Expand Up @@ -239,14 +239,14 @@ var npmToYarnTable = {
return ['install'];
}
args[0] = 'add';
return convertInstallArgs(args);
return convertInstallArgs$1(args);
},
i: function (args) {
return npmToYarnTable.install(args);
},
uninstall: function (args) {
args[0] = 'remove';
return convertInstallArgs(args);
return convertInstallArgs$1(args);
},
un: function (args) {
return npmToYarnTable.uninstall(args);
Expand Down Expand Up @@ -485,6 +485,132 @@ function npmToPnpm(_m, command) {
}
}

function convertInstallArgs(args) {
// bun uses -g and --global flags
// bun mostly conforms to Yarn's CLI
return args.map(function (item) {
switch (item) {
case '--save-dev':
case '--development':
case '-D':
return '--dev';
case '--save-prod':
case '-P':
return '--production';
case '--no-package-lock':
return '--no-save';
case '--save-optional':
case '-O':
return '--optional';
case '--save-exact':
case '-E':
return '--exact';
case '--save':
case '-S':
// this is default in bun
return '';
case '--global':
case '-g':
return '--global';
default:
return item;
}
});
}
function npmToBun(_m, command) {
var args = parse((command || '').trim());
var index = args.findIndex(function (a) { return a === '--'; });
if (index >= 0) {
args.splice(index, 1);
}
var cmd = 'bun';
switch (args[0]) {
case 'install':
case 'i':
if (args.length === 1) {
args = ['install'];
}
else {
args[0] = 'add';
}
args = convertInstallArgs(args);
break;
case 'uninstall':
case 'un':
case 'remove':
case 'r':
case 'rm':
args[0] = 'remove';
args = convertInstallArgs(args);
break;
case 'cache':
if (args[1] === 'clean') {
args = ['pm', 'cache', 'rm'].concat(args.slice(2));
}
else {
cmd = 'npm';
}
break;
case 'rebuild':
case 'rb':
args[0] = 'add';
args.push('--force');
break;
case 'run':
break;
case 'list':
case 'ls':
// 'npm ls' => 'bun pm ls'
args = convertInstallArgs(args);
args[0] = 'ls';
args.unshift('pm');
break;
case 'init':
case 'create':
if (args[1]) {
if (args[1].startsWith('@')) {
cmd = 'bunx';
args[1] = args[1].replace('/', '/create-');
args = args.slice(1);
}
else if (!args[1].startsWith('-')) {
cmd = 'bunx';
args[1] = "create-".concat(args[1]);
args = args.slice(1);
}
else {
args[0] = 'init';
}
}
break;
case 'link':
case 'ln':
args = convertInstallArgs(args);
args[0] = 'link';
break;
case 'stop':
case 'start':
case 'unlink':
break;
case 'test':
case 't':
case 'tst':
args[0] = 'test';
args.unshift('run');
break;
case 'exec':
cmd = 'bunx';
args.splice(0, 1);
break;
default:
// null == keep `npm` command
cmd = 'npm';
break;
}
var filtered = args.filter(Boolean).filter(function (arg) { return arg !== '--'; });
return "".concat(cmd, " ").concat(filtered.join(' ')).concat(cmd === 'npm' ? "\n# couldn't auto-convert command" : '').replace('=', ' ');
}

/**
* Converts between npm and yarn command
*/
Expand All @@ -495,6 +621,9 @@ function convert(str, to) {
else if (to === 'pnpm') {
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToPnpm);
}
else if (to === 'bun') {
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToBun);
}
else {
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToYarn);
}
Expand Down
2 changes: 1 addition & 1 deletion dist/npm-to-yarn.mjs.map

Large diffs are not rendered by default.

135 changes: 132 additions & 3 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.

2 changes: 1 addition & 1 deletion dist/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* Converts between npm and yarn command
*/
export default function convert(str: string, to: 'npm' | 'yarn' | 'pnpm'): string;
export default function convert(str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun'): string;
2 changes: 2 additions & 0 deletions dist/types/npmToBun.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare const bunCLICommands: readonly ["init", "run", "add", "pm", "help", "install", "remove", "upgrade", "version"];
export declare function npmToBun(_m: string, command: string): string;
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { yarnToNPM } from './yarnToNpm'
import { npmToYarn } from './npmToYarn'
import { npmToPnpm } from './npmToPnpm'
import { npmToBun } from './npmToBun'

/**
* Converts between npm and yarn command
*/
export default function convert (str: string, to: 'npm' | 'yarn' | 'pnpm'): string {
export default function convert (str: string, to: 'npm' | 'yarn' | 'pnpm' | 'bun'): string {
if (to === 'npm') {
return str.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToNPM)
} else if (to === 'pnpm') {
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToPnpm)
} else if (to === 'bun') {
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToBun)
} else {
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToYarn)
}
Expand Down
Loading

0 comments on commit 13c221e

Please sign in to comment.