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