-
Notifications
You must be signed in to change notification settings - Fork 0
/
npm-adaptor.ts
156 lines (135 loc) · 4.37 KB
/
npm-adaptor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env node
import * as child from 'child_process';
import { promisify } from 'util';
import { SpawnOptions } from 'child_process';
const spawn = <(command: string, opts: SpawnOptions) => Promise<child.ChildProcess>>promisify(child.spawn);
const options: SpawnOptions = {
stdio: 'inherit',
shell: true
};
interface NPMExpressionShape {
exe?: string;
run?: string;
command: string;
pkgdetails?: string;
options?: string;
}
/**
* @external https://regex101.com/library/IwsYRm
*/
const regex = new RegExp([
'^(?<exe>npm)?\\ ?',
'(?<run>(?<=\\k<exe> )run(?:-script)?)?\\ ?',
'(?<command>(?<=\\k<run> )[a-z]+(?:[-|:|.|_][a-z]+)?|(?<!\\k<run> )[a-z]+(?:[-][a-z]+)?)?\\ ?',
'(?<pkgdetails>[a-z0-9\\>\\=\\:\\+\\#\\^\\.\\@\\/][a-z0-9\\>\\=\\:\\+\\#\\^\\~\\.\\@\\/\\-]+)?\\ ?',
'(?<options>(?:\\ [-]{1,2}[a-zA-Z]+(?:[-][a-z]+)*)*)?$'
].join(''));
function getIsoMorphedExpression(npmex: NPMExpressionShape): string {
const commmandsEquivalenceTable = {
'install': 'add',
'i': 'add',
'uninstall': 'remove',
'view': 'info'
}
const optionsEquivalenceTable = {
'--no-package-lock': '--no-lockfile',
'--production': '',
'--save': '',
'--save-prod': '',
'-P': '',
'--save-dev': '--dev',
'-D': '--dev',
'--save-optional': '--optional',
'-O': '--optional',
'--save-exact': '--exact',
'-E': '--exact',
'--global': ''
};
const map = (table, value: string | string[]) => {
if (typeof value === "string") {
value = [value];
}
return value.map((val) => {
// if the mapped val is undefined, return original val
const mappedval = table[val];
return (mappedval !== undefined) ? mappedval : val;
});
};
const macromap = () => {
if (npmex.command) {
npmex.command = map(commmandsEquivalenceTable, npmex.command)[0];
}
options = map(optionsEquivalenceTable, options);
};
npmex.exe = 'yarn';
let options: string[] = (npmex.options) ? npmex.options.split(' ').filter(value => value.length) : [];
if (npmex.command && !npmex.pkgdetails) {
if (npmex.command === 'update' && options.indexOf('--global') !== -1) {
npmex.command = 'global upgrade';
options = options.filter((value) => value !== '--global');
} else if (npmex.command === 'rebuild') {
npmex.command = 'add';
options.push('--force');
} else if (options.length) {
if (options.indexOf('--no-package-lock') === -1) {
npmex.command = map(commmandsEquivalenceTable, npmex.command)[0];
} else {
npmex.command = 'install';
}
options = map(optionsEquivalenceTable, options);
}
} else if (npmex.command && npmex.pkgdetails) {
if (npmex.command === 'install' && options.indexOf('--global') !== -1) {
npmex.command = 'global add';
options = options.filter((value) => value !== '--global');
} else if (npmex.command === 'version') {
options = ['--' + npmex.pkgdetails];
npmex.pkgdetails = '';
} else {
macromap();
}
macromap();
}
return [npmex.exe, npmex.run, npmex.command, npmex.pkgdetails]
.concat(options)
.filter((value) => {
if (value != '' || value != undefined) {
return value;
}
})
.join(' ');
};
const exe = async (npmExpression, yarnExpression) => {
if (verboseEnabled) {
console.log('[spypkg] The npm expression below has been transformed into the followed yarn expression:');
console.log(npmExpression);
console.log(yarnExpression);
}
return await spawn(yarnExpression, options)
.then((onfulfilled) => {
if (onfulfilled.stdout) {
console.log(onfulfilled.stdout);
}
if (onfulfilled.stderr) {
console.log(onfulfilled.stderr);
}
})
.catch((reason) => {
console.log(reason);
});
};
let verboseEnabled: boolean;
let argument: string = '';
// prepare argv values into argument, so that regex can parse as expected
for (let j = 2; j < process.argv.length; j++) {
if (process.argv[j] === '--verbose') {
verboseEnabled = true;
} else {
argument += ' ' + process.argv[j];
}
}
argument = argument.trimLeft();
let prefix: string = (process.platform === 'win32') ? 'cmd /c' : '';
const parsedArg: NPMExpressionShape = regex.exec(argument)['groups'];
const transformedExpression: string = getIsoMorphedExpression(parsedArg);
exe(argument, (prefix + ' ' + transformedExpression).trimLeft());