Skip to content

Commit 4a13cc8

Browse files
committed
Fix CommonJS resolution
1 parent f9e3f7f commit 4a13cc8

File tree

4 files changed

+279
-7
lines changed

4 files changed

+279
-7
lines changed

dist/npm-to-yarn.cjs

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
'use strict';
2+
3+
var unchangedCLICommands = ['test', 'login', 'logout', 'link', 'publish', 'cache'];
4+
var yarnCLICommands = [
5+
'init',
6+
'run',
7+
'add',
8+
'audit',
9+
'autoclean',
10+
'bin',
11+
'check',
12+
'config',
13+
'create',
14+
'dedupe',
15+
'generate-lock-entry',
16+
'global',
17+
'help',
18+
'import',
19+
'info',
20+
'install',
21+
'licenses',
22+
'list',
23+
'lockfile',
24+
'outdated',
25+
'owner',
26+
'pack',
27+
'policies',
28+
'prune',
29+
'remove',
30+
'self-update',
31+
'tag',
32+
'team',
33+
'unlink',
34+
'upgrade',
35+
'upgrade-interactive',
36+
'version',
37+
'versions',
38+
'why',
39+
'workspace',
40+
'workspaces'
41+
];
42+
43+
var npmToYarnTable = {
44+
install: function (command) {
45+
if (/^install *$/.test(command)) {
46+
return 'install';
47+
}
48+
var ret = command
49+
.replace('install', 'add')
50+
.replace('--save-dev', '--dev')
51+
.replace('--save-exact', '--exact')
52+
.replace('--save-optional', '--optional')
53+
.replace(/\s*--save/, '')
54+
.replace('--no-package-lock', '--no-lockfile');
55+
if (/ -(?:-global|g)(?![^\b])/.test(ret)) {
56+
ret = ret.replace(/ -(?:-global|g)(?![^\b])/, '');
57+
ret = 'global ' + ret;
58+
}
59+
return ret;
60+
},
61+
uninstall: function (command) {
62+
var ret = command
63+
.replace('uninstall', 'remove')
64+
.replace('--save-dev', '--dev')
65+
.replace(/\s*--save/, '')
66+
.replace('--no-package-lock', '--no-lockfile');
67+
if (/ -(?:-global|g)(?![^\b])/.test(ret)) {
68+
ret = ret.replace(/ -(?:-global|g)(?![^\b])/, '');
69+
ret = 'global ' + ret;
70+
}
71+
return ret;
72+
},
73+
version: function (command) {
74+
return command.replace(/(major|minor|patch)/, '--$1');
75+
},
76+
rebuild: function (command) {
77+
return command.replace('rebuild', 'add --force');
78+
},
79+
run: function (command) {
80+
return command.replace(/^run\s?([^\s]+)?(\s--\s--)?(.*)$/, function (_, data, dash, rest) {
81+
var result = '';
82+
if (data && !unchangedCLICommands.includes(data) && !yarnCLICommands.includes(data)) {
83+
result += data;
84+
}
85+
else {
86+
result += 'run ' + (data || '');
87+
}
88+
if (dash)
89+
result += dash.replace(/^\s--/, '');
90+
if (rest)
91+
result += rest;
92+
return result;
93+
});
94+
},
95+
ls: function (command) {
96+
return command.replace(/^(ls|list)(.*)$/, function (_1, _2, args) {
97+
var result = 'list';
98+
if (args) {
99+
var ended = false;
100+
var packages = [];
101+
var items = args.split(' ').filter(Boolean);
102+
for (var _i = 0, items_1 = items; _i < items_1.length; _i++) {
103+
var item = items_1[_i];
104+
if (ended) {
105+
result += ' ' + item;
106+
}
107+
else if (item.startsWith('-')) {
108+
result += ' --pattern "' + packages.join('|') + '"';
109+
packages = [];
110+
ended = true;
111+
result += ' ' + item;
112+
}
113+
else {
114+
packages.push(item);
115+
}
116+
}
117+
if (packages.length > 0) {
118+
result += ' --pattern "' + packages.join('|') + '"';
119+
}
120+
return result;
121+
}
122+
else {
123+
return 'list';
124+
}
125+
});
126+
},
127+
list: function (command) {
128+
return npmToYarnTable.ls(command);
129+
},
130+
init: function (command) {
131+
if (/^init (?!-).*$/.test(command)) {
132+
return command.replace('init', 'create');
133+
}
134+
return command.replace(' --scope', '');
135+
}
136+
};
137+
var yarnToNpmTable = {
138+
add: function (command, global) {
139+
var dev;
140+
if (command === 'add --force') {
141+
return 'rebuild';
142+
}
143+
var ret = command
144+
.replace('add', 'install')
145+
.replace(/\s*--dev/, function () {
146+
dev = true;
147+
return '';
148+
})
149+
.replace('--no-lockfile', '--no-package-lock')
150+
.replace('--optional', '--save-optional')
151+
.replace('--exact', '--save-exact');
152+
if (dev) {
153+
ret += ' --save-dev';
154+
}
155+
else if (global) {
156+
ret += ' --global';
157+
}
158+
else {
159+
ret += ' --save';
160+
}
161+
return ret;
162+
},
163+
remove: function (command, global) {
164+
var dev;
165+
var ret = command
166+
.replace('remove', 'uninstall')
167+
.replace(/\s*--dev/, function () {
168+
dev = true;
169+
return '';
170+
})
171+
.replace('--no-lockfile', '--no-package-lock')
172+
.replace('--optional', '--save-optional')
173+
.replace('--exact', '--save-exact');
174+
if (dev) {
175+
ret += ' --save-dev';
176+
}
177+
else if (global) {
178+
ret += ' --global';
179+
}
180+
else {
181+
ret += ' --save';
182+
}
183+
return ret;
184+
},
185+
version: function (command) {
186+
return command.replace(/--(major|minor|patch)/, '$1');
187+
},
188+
install: 'install',
189+
list: function (command) {
190+
return command
191+
.replace(/--pattern ["']([^"']+)["']/, function (_, packages) {
192+
return packages.split('|').join(' ');
193+
})
194+
.replace(/^list/, 'ls');
195+
},
196+
init: 'init',
197+
create: 'init',
198+
run: 'run',
199+
global: function (command) {
200+
if (/^global add/.test(command)) {
201+
return yarnToNpmTable.add(command.replace(/^global add/, 'add'), true);
202+
}
203+
else if (/^global remove/.test(command)) {
204+
return yarnToNpmTable.remove(command.replace(/^global remove/, 'remove'), true);
205+
}
206+
}
207+
};
208+
function convert(str, to) {
209+
var returnStr = str;
210+
if (to === 'npm') {
211+
return returnStr.replace(/yarn(?: +([^&\n\r]*))?/gm, yarnToNPM);
212+
}
213+
else {
214+
return returnStr.replace(/npm(?: +([^&\n\r]*))?/gm, npmToYarn);
215+
}
216+
}
217+
function npmToYarn(m, command) {
218+
command = (command || '').trim();
219+
var firstCommand = (/\w+/.exec(command) || [''])[0];
220+
if (unchangedCLICommands.includes(firstCommand)) {
221+
return 'yarn ' + command;
222+
}
223+
else if (Object.prototype.hasOwnProperty.call(npmToYarnTable, firstCommand) &&
224+
npmToYarnTable[firstCommand]) {
225+
if (typeof npmToYarnTable[firstCommand] === 'function') {
226+
return 'yarn ' + npmToYarnTable[firstCommand](command);
227+
}
228+
else {
229+
return 'yarn ' + command.replace(firstCommand, npmToYarnTable[firstCommand]);
230+
}
231+
}
232+
else {
233+
return 'yarn ' + command + "\n# couldn't auto-convert command";
234+
}
235+
}
236+
function yarnToNPM(m, command) {
237+
command = (command || '').trim();
238+
if (command === '') {
239+
return 'npm install';
240+
}
241+
var firstCommand = (/\w+/.exec(command) || [''])[0];
242+
if (unchangedCLICommands.includes(firstCommand)) {
243+
return 'npm ' + command;
244+
}
245+
else if (Object.prototype.hasOwnProperty.call(yarnToNpmTable, firstCommand) &&
246+
yarnToNpmTable[firstCommand]) {
247+
if (typeof yarnToNpmTable[firstCommand] === 'function') {
248+
return 'npm ' + yarnToNpmTable[firstCommand](command);
249+
}
250+
else {
251+
return 'npm ' + command.replace(firstCommand, yarnToNpmTable[firstCommand]);
252+
}
253+
}
254+
else if (!yarnCLICommands.includes(firstCommand)) {
255+
// i.e., yarn grunt -> npm run grunt
256+
return 'npm run ' + command;
257+
}
258+
else {
259+
return 'npm ' + command + "\n# couldn't auto-convert command";
260+
}
261+
}
262+
263+
module.exports = convert;
264+
//# sourceMappingURL=npm-to-yarn.cjs.map

0 commit comments

Comments
 (0)