@zkochan/cmd-shim 5.2.0 → 5.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +27 -27
- package/README.md +52 -51
- package/index.d.ts +73 -72
- package/index.d.ts.map +1 -1
- package/index.js +518 -489
- package/index.js.map +1 -1
- package/package.json +2 -1
package/index.js
CHANGED
|
@@ -1,490 +1,519 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
cmdShim.ifExists = cmdShimIfExists;
|
|
3
|
-
const util_1 = require("util");
|
|
4
|
-
const path = require("path");
|
|
5
|
-
const isWindows = require("is-windows");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
*
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
['.
|
|
20
|
-
['.
|
|
21
|
-
['.
|
|
22
|
-
['.
|
|
23
|
-
['.
|
|
24
|
-
['.
|
|
25
|
-
]
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* @param
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* @
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
await
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* @param
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* @param
|
|
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
|
-
* @param
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
* @param
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
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
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
// If
|
|
155
|
-
//
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
* @param
|
|
174
|
-
* @param
|
|
175
|
-
* @param
|
|
176
|
-
* @param
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
//
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
await
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
* @param
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
* @
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
let
|
|
207
|
-
let
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
//
|
|
226
|
-
//
|
|
227
|
-
//
|
|
228
|
-
//
|
|
229
|
-
//
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
let
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
shProg =
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
//
|
|
292
|
-
//
|
|
293
|
-
//
|
|
294
|
-
//
|
|
295
|
-
//
|
|
296
|
-
//
|
|
297
|
-
//
|
|
298
|
-
//
|
|
299
|
-
//
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
//
|
|
383
|
-
//
|
|
384
|
-
//
|
|
385
|
-
//
|
|
386
|
-
//
|
|
387
|
-
//
|
|
388
|
-
// #
|
|
389
|
-
//
|
|
390
|
-
//
|
|
391
|
-
//
|
|
392
|
-
//
|
|
393
|
-
//
|
|
394
|
-
// $
|
|
395
|
-
//
|
|
396
|
-
//
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
$
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
$
|
|
403
|
-
$
|
|
404
|
-
|
|
405
|
-
$
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
$
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
$
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
if (
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
$
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
cmdShim.ifExists = cmdShimIfExists;
|
|
3
|
+
const util_1 = require("util");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const isWindows = require("is-windows");
|
|
6
|
+
const CMD_EXTENSION = require("cmd-extension");
|
|
7
|
+
const shebangExpr = /^#!\s*(?:\/usr\/bin\/env)?\s*([^ \t]+)(.*)$/;
|
|
8
|
+
const DEFAULT_OPTIONS = {
|
|
9
|
+
// Create PowerShell file by default if the option hasn't been specified
|
|
10
|
+
createPwshFile: true,
|
|
11
|
+
createCmdFile: isWindows(),
|
|
12
|
+
fs: require('fs')
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Map from extensions of files that this module is frequently used for to their runtime.
|
|
16
|
+
* @type {Map<string, string>}
|
|
17
|
+
*/
|
|
18
|
+
const extensionToProgramMap = new Map([
|
|
19
|
+
['.js', 'node'],
|
|
20
|
+
['.cjs', 'node'],
|
|
21
|
+
['.mjs', 'node'],
|
|
22
|
+
['.cmd', 'cmd'],
|
|
23
|
+
['.bat', 'cmd'],
|
|
24
|
+
['.ps1', 'pwsh'],
|
|
25
|
+
['.sh', 'sh']
|
|
26
|
+
]);
|
|
27
|
+
function ingestOptions(opts) {
|
|
28
|
+
const opts_ = { ...DEFAULT_OPTIONS, ...opts };
|
|
29
|
+
const fs = opts_.fs;
|
|
30
|
+
opts_.fs_ = {
|
|
31
|
+
chmod: fs.chmod ? util_1.promisify(fs.chmod) : (async () => { }),
|
|
32
|
+
mkdir: util_1.promisify(fs.mkdir),
|
|
33
|
+
readFile: util_1.promisify(fs.readFile),
|
|
34
|
+
stat: util_1.promisify(fs.stat),
|
|
35
|
+
unlink: util_1.promisify(fs.unlink),
|
|
36
|
+
writeFile: util_1.promisify(fs.writeFile)
|
|
37
|
+
};
|
|
38
|
+
return opts_;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Try to create shims.
|
|
42
|
+
*
|
|
43
|
+
* @param src Path to program (executable or script).
|
|
44
|
+
* @param to Path to shims.
|
|
45
|
+
* Don't add an extension if you will create multiple types of shims.
|
|
46
|
+
* @param opts Options.
|
|
47
|
+
* @throws If `src` is missing.
|
|
48
|
+
*/
|
|
49
|
+
async function cmdShim(src, to, opts) {
|
|
50
|
+
const opts_ = ingestOptions(opts);
|
|
51
|
+
await opts_.fs_.stat(src);
|
|
52
|
+
await cmdShim_(src, to, opts_);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Try to create shims.
|
|
56
|
+
*
|
|
57
|
+
* Does nothing if `src` doesn't exist.
|
|
58
|
+
*
|
|
59
|
+
* @param src Path to program (executable or script).
|
|
60
|
+
* @param to Path to shims.
|
|
61
|
+
* Don't add an extension if you will create multiple types of shims.
|
|
62
|
+
* @param opts Options.
|
|
63
|
+
*/
|
|
64
|
+
function cmdShimIfExists(src, to, opts) {
|
|
65
|
+
return cmdShim(src, to, opts).catch(() => { });
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Try to unlink, but ignore errors.
|
|
69
|
+
* Any problems will surface later.
|
|
70
|
+
*
|
|
71
|
+
* @param path File to be removed.
|
|
72
|
+
*/
|
|
73
|
+
function rm(path, opts) {
|
|
74
|
+
return opts.fs_.unlink(path).catch(() => { });
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Try to create shims **even if `src` is missing**.
|
|
78
|
+
*
|
|
79
|
+
* @param src Path to program (executable or script).
|
|
80
|
+
* @param to Path to shims.
|
|
81
|
+
* Don't add an extension if you will create multiple types of shims.
|
|
82
|
+
* @param opts Options.
|
|
83
|
+
*/
|
|
84
|
+
async function cmdShim_(src, to, opts) {
|
|
85
|
+
const srcRuntimeInfo = await searchScriptRuntime(src, opts);
|
|
86
|
+
// Always tries to create all types of shims by calling `writeAllShims` as of now.
|
|
87
|
+
// Append your code here to change the behavior in response to `srcRuntimeInfo`.
|
|
88
|
+
// Create 3 shims for (Ba)sh in Cygwin / MSYS, no extension) & CMD (.cmd) & PowerShell (.ps1)
|
|
89
|
+
await writeShimsPreCommon(to, opts);
|
|
90
|
+
return writeAllShims(src, to, srcRuntimeInfo, opts);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Do processes before **all** shims are created.
|
|
94
|
+
* This must be called **only once** for one call of `cmdShim(IfExists)`.
|
|
95
|
+
*
|
|
96
|
+
* @param target Path of shims that are going to be created.
|
|
97
|
+
*/
|
|
98
|
+
function writeShimsPreCommon(target, opts) {
|
|
99
|
+
return opts.fs_.mkdir(path.dirname(target), { recursive: true });
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Write all types (sh & cmd & pwsh) of shims to files.
|
|
103
|
+
* Extensions (`.cmd` and `.ps1`) are appended to cmd and pwsh shims.
|
|
104
|
+
*
|
|
105
|
+
*
|
|
106
|
+
* @param src Path to program (executable or script).
|
|
107
|
+
* @param to Path to shims **without extensions**.
|
|
108
|
+
* Extensions are added for CMD and PowerShell shims.
|
|
109
|
+
* @param srcRuntimeInfo Return value of `await searchScriptRuntime(src)`.
|
|
110
|
+
* @param opts Options.
|
|
111
|
+
*/
|
|
112
|
+
function writeAllShims(src, to, srcRuntimeInfo, opts) {
|
|
113
|
+
const opts_ = ingestOptions(opts);
|
|
114
|
+
const generatorAndExts = [{ generator: generateShShim, extension: '' }];
|
|
115
|
+
if (opts_.createCmdFile) {
|
|
116
|
+
generatorAndExts.push({ generator: generateCmdShim, extension: CMD_EXTENSION });
|
|
117
|
+
}
|
|
118
|
+
if (opts_.createPwshFile) {
|
|
119
|
+
generatorAndExts.push({ generator: generatePwshShim, extension: '.ps1' });
|
|
120
|
+
}
|
|
121
|
+
return Promise.all(generatorAndExts.map((generatorAndExt) => writeShim(src, to + generatorAndExt.extension, srcRuntimeInfo, generatorAndExt.generator, opts_)));
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Do processes before writing shim.
|
|
125
|
+
*
|
|
126
|
+
* @param target Path to shim that is going to be created.
|
|
127
|
+
*/
|
|
128
|
+
function writeShimPre(target, opts) {
|
|
129
|
+
return rm(target, opts);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Do processes after writing the shim.
|
|
133
|
+
*
|
|
134
|
+
* @param target Path to just created shim.
|
|
135
|
+
*/
|
|
136
|
+
function writeShimPost(target, opts) {
|
|
137
|
+
// Only chmoding shims as of now.
|
|
138
|
+
// Some other processes may be appended.
|
|
139
|
+
return chmodShim(target, opts);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Look into runtime (e.g. `node` & `sh` & `pwsh`) and its arguments
|
|
143
|
+
* of the target program (script or executable).
|
|
144
|
+
*
|
|
145
|
+
* @param target Path to the executable or script.
|
|
146
|
+
* @return Promise of infomation of runtime of `target`.
|
|
147
|
+
*/
|
|
148
|
+
async function searchScriptRuntime(target, opts) {
|
|
149
|
+
const data = await opts.fs_.readFile(target, 'utf8');
|
|
150
|
+
// First, check if the bin is a #! of some sort.
|
|
151
|
+
const firstLine = data.trim().split(/\r*\n/)[0];
|
|
152
|
+
const shebang = firstLine.match(shebangExpr);
|
|
153
|
+
if (!shebang) {
|
|
154
|
+
// If not, infer script type from its extension.
|
|
155
|
+
// If the inference fails, it's something that'll be compiled, or some other
|
|
156
|
+
// sort of script, and just call it directly.
|
|
157
|
+
const targetExtension = path.extname(target).toLowerCase();
|
|
158
|
+
return {
|
|
159
|
+
// undefined if extension is unknown but it's converted to null.
|
|
160
|
+
program: extensionToProgramMap.get(targetExtension) || null,
|
|
161
|
+
additionalArgs: ''
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
program: shebang[1],
|
|
166
|
+
additionalArgs: shebang[2]
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Write shim to the file system while executing the pre- and post-processes
|
|
171
|
+
* defined in `WriteShimPre` and `WriteShimPost`.
|
|
172
|
+
*
|
|
173
|
+
* @param src Path to the executable or script.
|
|
174
|
+
* @param to Path to the (sh) shim(s) that is going to be created.
|
|
175
|
+
* @param srcRuntimeInfo Result of `await searchScriptRuntime(src)`.
|
|
176
|
+
* @param generateShimScript Generator of shim script.
|
|
177
|
+
* @param opts Other options.
|
|
178
|
+
*/
|
|
179
|
+
async function writeShim(src, to, srcRuntimeInfo, generateShimScript, opts) {
|
|
180
|
+
const defaultArgs = opts.preserveSymlinks ? '--preserve-symlinks' : '';
|
|
181
|
+
// `Array.prototype.filter` removes ''.
|
|
182
|
+
// ['--foo', '--bar'].join(' ') and [].join(' ') returns '--foo --bar' and '' respectively.
|
|
183
|
+
const args = [srcRuntimeInfo.additionalArgs, defaultArgs].filter(arg => arg).join(' ');
|
|
184
|
+
opts = Object.assign({}, opts, {
|
|
185
|
+
prog: srcRuntimeInfo.program,
|
|
186
|
+
args: args
|
|
187
|
+
});
|
|
188
|
+
await writeShimPre(to, opts);
|
|
189
|
+
await opts.fs_.writeFile(to, generateShimScript(src, to, opts), 'utf8');
|
|
190
|
+
return writeShimPost(to, opts);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Generate the content of a shim for CMD.
|
|
194
|
+
*
|
|
195
|
+
* @param src Path to the executable or script.
|
|
196
|
+
* @param to Path to the shim to be created.
|
|
197
|
+
* It is highly recommended to end with `.cmd` (or `.bat`).
|
|
198
|
+
* @param opts Options.
|
|
199
|
+
* @return The content of shim.
|
|
200
|
+
*/
|
|
201
|
+
function generateCmdShim(src, to, opts) {
|
|
202
|
+
// `shTarget` is not used to generate the content.
|
|
203
|
+
const shTarget = path.relative(path.dirname(to), src);
|
|
204
|
+
let target = shTarget.split('/').join('\\');
|
|
205
|
+
const quotedPathToTarget = path.isAbsolute(target) ? `"${target}"` : `"%~dp0\\${target}"`;
|
|
206
|
+
let longProg;
|
|
207
|
+
let prog = opts.prog;
|
|
208
|
+
let args = opts.args || '';
|
|
209
|
+
const nodePath = normalizePathEnvVar(opts.nodePath).win32;
|
|
210
|
+
const prependToPath = normalizePathEnvVar(opts.prependToPath).win32;
|
|
211
|
+
if (!prog) {
|
|
212
|
+
prog = quotedPathToTarget;
|
|
213
|
+
args = '';
|
|
214
|
+
target = '';
|
|
215
|
+
}
|
|
216
|
+
else if (prog === 'node' && opts.nodeExecPath) {
|
|
217
|
+
prog = `"${opts.nodeExecPath}"`;
|
|
218
|
+
target = quotedPathToTarget;
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
longProg = `"%~dp0\\${prog}.exe"`;
|
|
222
|
+
target = quotedPathToTarget;
|
|
223
|
+
}
|
|
224
|
+
let progArgs = opts.progArgs ? `${opts.progArgs.join(` `)} ` : '';
|
|
225
|
+
// @IF EXIST "%~dp0\node.exe" (
|
|
226
|
+
// "%~dp0\node.exe" "%~dp0\.\node_modules\npm\bin\npm-cli.js" %*
|
|
227
|
+
// ) ELSE (
|
|
228
|
+
// SETLOCAL
|
|
229
|
+
// SET PATHEXT=%PATHEXT:;.JS;=;%
|
|
230
|
+
// node "%~dp0\.\node_modules\npm\bin\npm-cli.js" %*
|
|
231
|
+
// )
|
|
232
|
+
let cmd = '@SETLOCAL\r\n';
|
|
233
|
+
if (prependToPath) {
|
|
234
|
+
cmd += `@SET "PATH=${prependToPath}:%PATH%"\r\n`;
|
|
235
|
+
}
|
|
236
|
+
if (nodePath) {
|
|
237
|
+
cmd += `\
|
|
238
|
+
@IF NOT DEFINED NODE_PATH (\r
|
|
239
|
+
@SET "NODE_PATH=${nodePath}"\r
|
|
240
|
+
) ELSE (\r
|
|
241
|
+
@SET "NODE_PATH=%NODE_PATH%;${nodePath}"\r
|
|
242
|
+
)\r
|
|
243
|
+
`;
|
|
244
|
+
}
|
|
245
|
+
if (longProg) {
|
|
246
|
+
cmd += `\
|
|
247
|
+
@IF EXIST ${longProg} (\r
|
|
248
|
+
${longProg} ${args} ${target} ${progArgs}%*\r
|
|
249
|
+
) ELSE (\r
|
|
250
|
+
@SET PATHEXT=%PATHEXT:;.JS;=;%\r
|
|
251
|
+
${prog} ${args} ${target} ${progArgs}%*\r
|
|
252
|
+
)\r
|
|
253
|
+
`;
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
cmd += `@${prog} ${args} ${target} ${progArgs}%*\r\n`;
|
|
257
|
+
}
|
|
258
|
+
return cmd;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Generate the content of a shim for (Ba)sh in, for example, Cygwin and MSYS(2).
|
|
262
|
+
*
|
|
263
|
+
* @param src Path to the executable or script.
|
|
264
|
+
* @param to Path to the shim to be created.
|
|
265
|
+
* It is highly recommended to end with `.sh` or to contain no extension.
|
|
266
|
+
* @param opts Options.
|
|
267
|
+
* @return The content of shim.
|
|
268
|
+
*/
|
|
269
|
+
function generateShShim(src, to, opts) {
|
|
270
|
+
let shTarget = path.relative(path.dirname(to), src);
|
|
271
|
+
let shProg = opts.prog && opts.prog.split('\\').join('/');
|
|
272
|
+
let shLongProg;
|
|
273
|
+
shTarget = shTarget.split('\\').join('/');
|
|
274
|
+
const quotedPathToTarget = path.isAbsolute(shTarget) ? `"${shTarget}"` : `"$basedir/${shTarget}"`;
|
|
275
|
+
let args = opts.args || '';
|
|
276
|
+
const shNodePath = normalizePathEnvVar(opts.nodePath).posix;
|
|
277
|
+
if (!shProg) {
|
|
278
|
+
shProg = quotedPathToTarget;
|
|
279
|
+
args = '';
|
|
280
|
+
shTarget = '';
|
|
281
|
+
}
|
|
282
|
+
else if (opts.prog === 'node' && opts.nodeExecPath) {
|
|
283
|
+
shProg = `"${opts.nodeExecPath}"`;
|
|
284
|
+
shTarget = quotedPathToTarget;
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
shLongProg = `"$basedir/${opts.prog}"`;
|
|
288
|
+
shTarget = quotedPathToTarget;
|
|
289
|
+
}
|
|
290
|
+
let progArgs = opts.progArgs ? `${opts.progArgs.join(` `)} ` : '';
|
|
291
|
+
// #!/bin/sh
|
|
292
|
+
// basedir=`dirname "$0"`
|
|
293
|
+
//
|
|
294
|
+
// case `uname` in
|
|
295
|
+
// *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
|
|
296
|
+
// esac
|
|
297
|
+
//
|
|
298
|
+
// export NODE_PATH="<nodepath>"
|
|
299
|
+
//
|
|
300
|
+
// if [ -x "$basedir/node.exe" ]; then
|
|
301
|
+
// exec "$basedir/node.exe" "$basedir/node_modules/npm/bin/npm-cli.js" "$@"
|
|
302
|
+
// else
|
|
303
|
+
// exec node "$basedir/node_modules/npm/bin/npm-cli.js" "$@"
|
|
304
|
+
// fi
|
|
305
|
+
let sh = `\
|
|
306
|
+
#!/bin/sh
|
|
307
|
+
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
|
|
308
|
+
|
|
309
|
+
case \`uname\` in
|
|
310
|
+
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
|
|
311
|
+
esac
|
|
312
|
+
|
|
313
|
+
`;
|
|
314
|
+
if (opts.prependToPath) {
|
|
315
|
+
sh += `\
|
|
316
|
+
export PATH="${opts.prependToPath}:$PATH"
|
|
317
|
+
`;
|
|
318
|
+
}
|
|
319
|
+
if (shNodePath) {
|
|
320
|
+
sh += `\
|
|
321
|
+
if [ -z "$NODE_PATH" ]; then
|
|
322
|
+
export NODE_PATH="${shNodePath}"
|
|
323
|
+
else
|
|
324
|
+
export NODE_PATH="$NODE_PATH:${shNodePath}"
|
|
325
|
+
fi
|
|
326
|
+
`;
|
|
327
|
+
}
|
|
328
|
+
if (shLongProg) {
|
|
329
|
+
sh += `\
|
|
330
|
+
if [ -x ${shLongProg} ]; then
|
|
331
|
+
exec ${shLongProg} ${args} ${shTarget} ${progArgs}"$@"
|
|
332
|
+
else
|
|
333
|
+
exec ${shProg} ${args} ${shTarget} ${progArgs}"$@"
|
|
334
|
+
fi
|
|
335
|
+
`;
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
sh += `\
|
|
339
|
+
${shProg} ${args} ${shTarget} ${progArgs}"$@"
|
|
340
|
+
exit $?
|
|
341
|
+
`;
|
|
342
|
+
}
|
|
343
|
+
return sh;
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Generate the content of a shim for PowerShell.
|
|
347
|
+
*
|
|
348
|
+
* @param src Path to the executable or script.
|
|
349
|
+
* @param to Path to the shim to be created.
|
|
350
|
+
* It is highly recommended to end with `.ps1`.
|
|
351
|
+
* @param opts Options.
|
|
352
|
+
* @return The content of shim.
|
|
353
|
+
*/
|
|
354
|
+
function generatePwshShim(src, to, opts) {
|
|
355
|
+
let shTarget = path.relative(path.dirname(to), src);
|
|
356
|
+
const shProg = opts.prog && opts.prog.split('\\').join('/');
|
|
357
|
+
let pwshProg = shProg && `"${shProg}$exe"`;
|
|
358
|
+
let pwshLongProg;
|
|
359
|
+
shTarget = shTarget.split('\\').join('/');
|
|
360
|
+
const quotedPathToTarget = path.isAbsolute(shTarget) ? `"${shTarget}"` : `"$basedir/${shTarget}"`;
|
|
361
|
+
let args = opts.args || '';
|
|
362
|
+
let normalizedNodePathEnvVar = normalizePathEnvVar(opts.nodePath);
|
|
363
|
+
const nodePath = normalizedNodePathEnvVar.win32;
|
|
364
|
+
const shNodePath = normalizedNodePathEnvVar.posix;
|
|
365
|
+
let normalizedPrependPathEnvVar = normalizePathEnvVar(opts.prependToPath);
|
|
366
|
+
const prependPath = normalizedPrependPathEnvVar.win32;
|
|
367
|
+
const shPrependPath = normalizedPrependPathEnvVar.posix;
|
|
368
|
+
if (!pwshProg) {
|
|
369
|
+
pwshProg = quotedPathToTarget;
|
|
370
|
+
args = '';
|
|
371
|
+
shTarget = '';
|
|
372
|
+
}
|
|
373
|
+
else if (opts.prog === 'node' && opts.nodeExecPath) {
|
|
374
|
+
pwshProg = `"${opts.nodeExecPath}"`;
|
|
375
|
+
shTarget = quotedPathToTarget;
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
pwshLongProg = `"$basedir/${opts.prog}$exe"`;
|
|
379
|
+
shTarget = quotedPathToTarget;
|
|
380
|
+
}
|
|
381
|
+
let progArgs = opts.progArgs ? `${opts.progArgs.join(` `)} ` : '';
|
|
382
|
+
// #!/usr/bin/env pwsh
|
|
383
|
+
// $basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
|
|
384
|
+
//
|
|
385
|
+
// $ret=0
|
|
386
|
+
// $exe = ""
|
|
387
|
+
// if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
|
|
388
|
+
// # Fix case when both the Windows and Linux builds of Node
|
|
389
|
+
// # are installed in the same directory
|
|
390
|
+
// $exe = ".exe"
|
|
391
|
+
// }
|
|
392
|
+
// if (Test-Path "$basedir/node") {
|
|
393
|
+
// # Support pipeline input
|
|
394
|
+
// if ($MyInvocation.ExpectingInput) {
|
|
395
|
+
// $input | & "$basedir/node$exe" "$basedir/node_modules/npm/bin/npm-cli.js" $args
|
|
396
|
+
// } else {
|
|
397
|
+
// & "$basedir/node$exe" "$basedir/node_modules/npm/bin/npm-cli.js" $args
|
|
398
|
+
// }
|
|
399
|
+
// $ret=$LASTEXITCODE
|
|
400
|
+
// } else {
|
|
401
|
+
// # Support pipeline input
|
|
402
|
+
// if ($MyInvocation.ExpectingInput) {
|
|
403
|
+
// $input | & "node$exe" "$basedir/node_modules/npm/bin/npm-cli.js" $args
|
|
404
|
+
// } else {
|
|
405
|
+
// & "node$exe" "$basedir/node_modules/npm/bin/npm-cli.js" $args
|
|
406
|
+
// }
|
|
407
|
+
// $ret=$LASTEXITCODE
|
|
408
|
+
// }
|
|
409
|
+
// exit $ret
|
|
410
|
+
let pwsh = `\
|
|
411
|
+
#!/usr/bin/env pwsh
|
|
412
|
+
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
|
|
413
|
+
|
|
414
|
+
$exe=""
|
|
415
|
+
${(nodePath || prependPath) ? '$pathsep=":"\n' : ''}\
|
|
416
|
+
${nodePath ? `\
|
|
417
|
+
$env_node_path=$env:NODE_PATH
|
|
418
|
+
$new_node_path="${nodePath}"
|
|
419
|
+
` : ''}\
|
|
420
|
+
${prependPath ? `\
|
|
421
|
+
$env_path=$env:PATH
|
|
422
|
+
$prepend_path="${prependPath}"
|
|
423
|
+
` : ''}\
|
|
424
|
+
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
|
|
425
|
+
# Fix case when both the Windows and Linux builds of Node
|
|
426
|
+
# are installed in the same directory
|
|
427
|
+
$exe=".exe"
|
|
428
|
+
${(nodePath || prependPath) ? ' $pathsep=";"\n' : ''}\
|
|
429
|
+
}`;
|
|
430
|
+
if (shNodePath || shPrependPath) {
|
|
431
|
+
pwsh += `\
|
|
432
|
+
else {
|
|
433
|
+
${shNodePath ? ` $new_node_path="${shNodePath}"\n` : ''}\
|
|
434
|
+
${shPrependPath ? ` $prepend_path="${shPrependPath}"\n` : ''}\
|
|
435
|
+
}
|
|
436
|
+
`;
|
|
437
|
+
}
|
|
438
|
+
if (shNodePath) {
|
|
439
|
+
pwsh += `\
|
|
440
|
+
if ([string]::IsNullOrEmpty($env_node_path)) {
|
|
441
|
+
$env:NODE_PATH=$new_node_path
|
|
442
|
+
} else {
|
|
443
|
+
$env:NODE_PATH="$env_node_path$pathsep$new_node_path"
|
|
444
|
+
}
|
|
445
|
+
`;
|
|
446
|
+
}
|
|
447
|
+
if (opts.prependToPath) {
|
|
448
|
+
pwsh += `
|
|
449
|
+
$env:PATH="$prepend_path$pathsep$env:PATH"
|
|
450
|
+
`;
|
|
451
|
+
}
|
|
452
|
+
if (pwshLongProg) {
|
|
453
|
+
pwsh += `
|
|
454
|
+
$ret=0
|
|
455
|
+
if (Test-Path ${pwshLongProg}) {
|
|
456
|
+
# Support pipeline input
|
|
457
|
+
if ($MyInvocation.ExpectingInput) {
|
|
458
|
+
$input | & ${pwshLongProg} ${args} ${shTarget} ${progArgs}$args
|
|
459
|
+
} else {
|
|
460
|
+
& ${pwshLongProg} ${args} ${shTarget} ${progArgs}$args
|
|
461
|
+
}
|
|
462
|
+
$ret=$LASTEXITCODE
|
|
463
|
+
} else {
|
|
464
|
+
# Support pipeline input
|
|
465
|
+
if ($MyInvocation.ExpectingInput) {
|
|
466
|
+
$input | & ${pwshProg} ${args} ${shTarget} ${progArgs}$args
|
|
467
|
+
} else {
|
|
468
|
+
& ${pwshProg} ${args} ${shTarget} ${progArgs}$args
|
|
469
|
+
}
|
|
470
|
+
$ret=$LASTEXITCODE
|
|
471
|
+
}
|
|
472
|
+
${nodePath ? '$env:NODE_PATH=$env_node_path\n' : ''}\
|
|
473
|
+
${prependPath ? '$env:PATH=$env_path\n' : ''}\
|
|
474
|
+
exit $ret
|
|
475
|
+
`;
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
pwsh += `
|
|
479
|
+
# Support pipeline input
|
|
480
|
+
if ($MyInvocation.ExpectingInput) {
|
|
481
|
+
$input | & ${pwshProg} ${args} ${shTarget} ${progArgs}$args
|
|
482
|
+
} else {
|
|
483
|
+
& ${pwshProg} ${args} ${shTarget} ${progArgs}$args
|
|
484
|
+
}
|
|
485
|
+
${nodePath ? '$env:NODE_PATH=$env_node_path\n' : ''}\
|
|
486
|
+
${prependPath ? '$env:PATH=$env_path\n' : ''}\
|
|
487
|
+
exit $LASTEXITCODE
|
|
488
|
+
`;
|
|
489
|
+
}
|
|
490
|
+
return pwsh;
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Chmod just created shim and make it executable
|
|
494
|
+
*
|
|
495
|
+
* @param to Path to shim.
|
|
496
|
+
*/
|
|
497
|
+
function chmodShim(to, opts) {
|
|
498
|
+
return opts.fs_.chmod(to, 0o755);
|
|
499
|
+
}
|
|
500
|
+
function normalizePathEnvVar(nodePath) {
|
|
501
|
+
if (!nodePath || !nodePath.length) {
|
|
502
|
+
return {
|
|
503
|
+
win32: '',
|
|
504
|
+
posix: ''
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
let split = (typeof nodePath === 'string' ? nodePath.split(path.delimiter) : Array.from(nodePath));
|
|
508
|
+
let result = {};
|
|
509
|
+
for (let i = 0; i < split.length; i++) {
|
|
510
|
+
const win32 = split[i].split('/').join('\\');
|
|
511
|
+
const posix = isWindows() ? split[i].split('\\').join('/').replace(/^([^:\\/]*):/, (_, $1) => `/mnt/${$1.toLowerCase()}`) : split[i];
|
|
512
|
+
result.win32 = result.win32 ? `${result.win32};${win32}` : win32;
|
|
513
|
+
result.posix = result.posix ? `${result.posix}:${posix}` : posix;
|
|
514
|
+
result[i] = { win32, posix };
|
|
515
|
+
}
|
|
516
|
+
return result;
|
|
517
|
+
}
|
|
518
|
+
module.exports = cmdShim;
|
|
490
519
|
//# sourceMappingURL=index.js.map
|