Skip to content

Commit 57a9cd9

Browse files
authored
Merge pull request #37 from armano2/feat/npm-to-pnpm
2 parents 6d1b43e + ca64dce commit 57a9cd9

14 files changed

+782
-346
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dist
2+
build
23
coverage
34
.rpt2_cache
45
node_modules

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ convert('npm install squirrelly', 'yarn')
4444
/**
4545
* Converts between npm and yarn command
4646
*/
47-
export default function convert(str: string, to: 'npm' | 'yarn'): string;
47+
export default function convert(str: string, to: 'npm' | 'yarn' | 'pnpm'): string;
4848
```
4949

5050
## ✔️ Tests

dist/npm-to-yarn.mjs

Lines changed: 160 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,10 @@ var yarnToNpmTable = {
107107
return ['rebuild'];
108108
}
109109
args[0] = 'install';
110-
if (!args.includes('--dev') &&
111-
!args.includes('--force') &&
112-
!args.includes('--exact') &&
113-
!args.includes('--optional') &&
114-
!args.includes('--production')) {
115-
args.push('--save');
116-
}
117110
return convertAddRemoveArgs(args);
118111
},
119112
remove: function (args) {
120113
args[0] = 'uninstall';
121-
if (!args.includes('--dev')) {
122-
args.push('--save');
123-
}
124114
return convertAddRemoveArgs(args);
125115
},
126116
version: function (args) {
@@ -149,6 +139,7 @@ var yarnToNpmTable = {
149139
},
150140
init: 'init',
151141
create: 'init',
142+
outdated: 'outdated',
152143
run: 'run',
153144
global: function (args) {
154145
switch (args[1]) {
@@ -173,6 +164,14 @@ var yarnToNpmTable = {
173164
args.push("\n# couldn't auto-convert command");
174165
return args;
175166
}
167+
},
168+
pack: function (args) {
169+
return args.map(function (item) {
170+
if (item === '--filename') {
171+
return '--pack-destination';
172+
}
173+
return item;
174+
});
176175
}
177176
};
178177
function yarnToNPM(_m, command) {
@@ -249,6 +248,9 @@ var npmToYarnTable = {
249248
args[0] = 'remove';
250249
return convertInstallArgs(args);
251250
},
251+
un: function (args) {
252+
return npmToYarnTable.uninstall(args);
253+
},
252254
remove: function (args) {
253255
return npmToYarnTable.uninstall(args);
254256
},
@@ -315,7 +317,17 @@ var npmToYarnTable = {
315317
return args.filter(function (item) { return item !== '--scope'; });
316318
},
317319
ln: 'link',
318-
un: 'unlink'
320+
t: 'test',
321+
tst: 'test',
322+
outdated: 'outdated',
323+
pack: function (args) {
324+
return args.map(function (item) {
325+
if (item.startsWith('--pack-destination')) {
326+
return item.replace(/^--pack-destination[\s=]/, '--filename ');
327+
}
328+
return item;
329+
});
330+
}
319331
};
320332
function npmToYarn(_m, command) {
321333
var args = parse((command || '').trim());
@@ -324,7 +336,7 @@ function npmToYarn(_m, command) {
324336
args.splice(index, 1);
325337
}
326338
if (unchangedCLICommands.includes(args[0])) {
327-
return 'yarn ' + args.join(' ');
339+
return 'yarn ' + args.filter(Boolean).join(' ');
328340
}
329341
else if (args[0] in npmToYarnTable) {
330342
var converter = npmToYarnTable[args[0]];
@@ -337,7 +349,139 @@ function npmToYarn(_m, command) {
337349
return 'yarn ' + args.filter(Boolean).join(' ');
338350
}
339351
else {
340-
return 'yarn ' + command + "\n# couldn't auto-convert command";
352+
return 'npm ' + command + "\n# couldn't auto-convert command";
353+
}
354+
}
355+
356+
function convertPnpmInstallArgs(args) {
357+
return args.map(function (item) {
358+
switch (item) {
359+
case '--save':
360+
case '-S':
361+
return '';
362+
case '--no-package-lock':
363+
return '--frozen-lockfile';
364+
// case '--save-dev':
365+
// case '-D':
366+
// case '--save-prod':
367+
// case '-P':
368+
// case '--save-optional':
369+
// case '-O':
370+
// case '--save-exact':
371+
// case '-E':
372+
// case '--global':
373+
// case '-g':
374+
default:
375+
return item;
376+
}
377+
});
378+
}
379+
function convertFilterArg(args) {
380+
if (args.length > 1) {
381+
var filter = args.filter(function (item, index) { return index !== 0 && !item.startsWith('-'); });
382+
if (filter.length > 0) {
383+
args = args.filter(function (item, index) { return index === 0 || item.startsWith('-'); });
384+
args.push('--filter');
385+
args.push(filter.join(' '));
386+
}
387+
}
388+
return args;
389+
}
390+
var npmToPnpmTable = {
391+
// ------------------------------
392+
install: function (args) {
393+
if (args.length > 1 && args.filter(function (item) { return !item.startsWith('-'); }).length > 1) {
394+
args[0] = 'add';
395+
}
396+
return convertPnpmInstallArgs(args);
397+
},
398+
i: function (args) {
399+
return npmToPnpmTable.install(args);
400+
},
401+
// ------------------------------
402+
uninstall: function (args) {
403+
args[0] = 'remove';
404+
return convertPnpmInstallArgs(args);
405+
},
406+
un: function (args) {
407+
return npmToPnpmTable.uninstall(args);
408+
},
409+
remove: function (args) {
410+
return npmToPnpmTable.uninstall(args);
411+
},
412+
r: function (args) {
413+
return npmToPnpmTable.uninstall(args);
414+
},
415+
rm: function (args) {
416+
return npmToPnpmTable.uninstall(args);
417+
},
418+
// ------------------------------
419+
rb: function (args) {
420+
return npmToPnpmTable.rebuild(args);
421+
},
422+
rebuild: function (args) {
423+
args[0] = 'rebuild';
424+
return convertFilterArg(args);
425+
},
426+
run: 'run',
427+
exec: 'exec',
428+
ls: function (args) {
429+
return npmToPnpmTable.list(args);
430+
},
431+
list: function (args) {
432+
return args.map(function (item) {
433+
if (item.startsWith('--depth=')) {
434+
return "--depth ".concat(item.split('=')[1]);
435+
}
436+
switch (item) {
437+
case '--production':
438+
return '--prod';
439+
case '--development':
440+
return '--dev';
441+
default:
442+
return item;
443+
}
444+
});
445+
},
446+
init: function (args) {
447+
if (args[1] && !args[1].startsWith('-')) {
448+
args[0] = 'create';
449+
}
450+
return args.filter(function (item) { return item !== '--scope'; });
451+
},
452+
ln: 'link',
453+
t: 'test',
454+
test: 'test',
455+
tst: 'test',
456+
start: 'start',
457+
link: 'link',
458+
unlink: function (args) {
459+
return convertFilterArg(args);
460+
},
461+
outdated: 'outdated',
462+
pack: function (args) {
463+
return args.map(function (item) {
464+
if (item.startsWith('--pack-destination')) {
465+
return item.replace(/^--pack-destination[\s=]/, '--pack-destination ');
466+
}
467+
return item;
468+
});
469+
}
470+
};
471+
function npmToPnpm(_m, command) {
472+
var args = parse((command || '').trim());
473+
if (args[0] in npmToPnpmTable) {
474+
var converter = npmToPnpmTable[args[0]];
475+
if (typeof converter === 'function') {
476+
args = converter(args);
477+
}
478+
else {
479+
args[0] = converter;
480+
}
481+
return 'pnpm ' + args.filter(Boolean).join(' ');
482+
}
483+
else {
484+
return 'npm ' + command + "\n# couldn't auto-convert command";
341485
}
342486
}
343487

@@ -348,6 +492,9 @@ function convert(str, to) {
348492
if (to === 'npm') {
349493
return str.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToNPM);
350494
}
495+
else if (to === 'pnpm') {
496+
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToPnpm);
497+
}
351498
else {
352499
return str.replace(/npm(?: +([^&\n\r]*))?/gm, npmToYarn);
353500
}

dist/npm-to-yarn.mjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)