olum-compiler 0.8.6 → 0.9.2
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/bin/compiler.js +1 -1
- package/bin/dev.js +1 -1
- package/bin/index.js +4 -1
- package/bin/watcher.js +1 -1
- package/lib/chokidar.js +344 -0
- package/lib/cross-spawn.js +318 -0
- package/lib/getImports.js +2 -2
- package/lib/helpers.js +17 -6
- package/lib/jsParser.js +1 -0
- package/lib/magic-string.js +213 -0
- package/lib/parser.js +13 -5
- package/lib/sourcemap.js +1 -1
- package/lib/websocket.js +347 -0
- package/package.json +3 -7
package/bin/compiler.js
CHANGED
package/bin/dev.js
CHANGED
|
@@ -2,7 +2,7 @@ const serve = require("./server");
|
|
|
2
2
|
const watch = require("./watcher");
|
|
3
3
|
const compile = require("./compiler");
|
|
4
4
|
const copySrc = require("./copySrc");
|
|
5
|
-
const WebSocket = require("
|
|
5
|
+
const WebSocket = require("../lib/websocket");
|
|
6
6
|
const path = require("path");
|
|
7
7
|
const fs = require("fs");
|
|
8
8
|
|
package/bin/index.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
const path = require("path");
|
|
3
|
-
const spawn = require("cross-spawn");
|
|
3
|
+
const spawn = require("../lib/cross-spawn");
|
|
4
4
|
const script = process.argv[2];
|
|
5
5
|
|
|
6
|
+
if (script == "dev") process.env.NODE_ENV = "development";
|
|
7
|
+
else if (script == "build") process.env.NODE_ENV = "production";
|
|
8
|
+
|
|
6
9
|
if (typeof script !== undefined && ["dev", "build"].includes(script)) {
|
|
7
10
|
const result = spawn.sync("node", [path.resolve(__dirname, script + ".js")], { stdio: "inherit" });
|
|
8
11
|
|
package/bin/watcher.js
CHANGED
package/lib/chokidar.js
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* chokidar.js — standalone single-file recursive file watcher
|
|
3
|
+
* Minimal replacement for the `chokidar` package, using only node built-ins
|
|
4
|
+
* (fs, path, events). Cross-platform: instead of fs.watch's `recursive` flag
|
|
5
|
+
* (whose support and event detail differ per OS), every directory gets its own
|
|
6
|
+
* non-recursive fs.watch and events trigger a rescan-and-diff of that
|
|
7
|
+
* directory against a stat snapshot — so add/change/unlink detection behaves
|
|
8
|
+
* identically on Linux, macOS and Windows.
|
|
9
|
+
*
|
|
10
|
+
* Covered (same API shape as chokidar@5.0.0):
|
|
11
|
+
* const chokidar = require("./chokidar");
|
|
12
|
+
* const watcher = chokidar.watch(paths, {
|
|
13
|
+
* persistent: true, // keep the process alive (default true)
|
|
14
|
+
* ignoreInitial: false, // suppress add/addDir for the initial scan
|
|
15
|
+
* ignored: regex|fn|array, // paths to skip entirely
|
|
16
|
+
* awaitWriteFinish: { stabilityThreshold, pollIntervall } | true,
|
|
17
|
+
* });
|
|
18
|
+
* watcher.on("add" | "change" | "unlink" | "addDir" | "unlinkDir" | "ready" | "error", ...)
|
|
19
|
+
* watcher.close();
|
|
20
|
+
*
|
|
21
|
+
* Watched roots that don't exist yet (or get deleted) are polled for and
|
|
22
|
+
* picked up when they appear, like chokidar does.
|
|
23
|
+
*
|
|
24
|
+
* KNOWN LIMITATIONS vs the real chokidar (future maintenance):
|
|
25
|
+
*
|
|
26
|
+
* 1. No `usePolling` fallback — NFS / Docker / VM compatibility.
|
|
27
|
+
* fs.watch relies on OS change events (inotify on Linux, FSEvents/kqueue on
|
|
28
|
+
* macOS, ReadDirectoryChangesW on Windows). On filesystems that don't
|
|
29
|
+
* propagate those events — NFS mounts, Docker bind-mounted volumes with a
|
|
30
|
+
* macOS/Windows host, some VM shared folders (VirtualBox vboxsf, older
|
|
31
|
+
* Vagrant/WSL1 setups) — the watcher goes silent: no errors, just no
|
|
32
|
+
* events. Real chokidar covers this with `usePolling: true` (fs.watchFile
|
|
33
|
+
* stat polling). If dev-inside-Docker/NFS ever becomes a use case, add a
|
|
34
|
+
* polling mode here: replace fs.watch per directory with a setInterval
|
|
35
|
+
* that calls _rescan(dir) (the diffing logic already works unchanged);
|
|
36
|
+
* only _watchDir needs the branch.
|
|
37
|
+
*
|
|
38
|
+
* 2. Whole-directory rescan per event burst.
|
|
39
|
+
* On any event we readdir+stat the affected directory instead of stat-ing
|
|
40
|
+
* just the named file like chokidar. Identical behavior, negligible cost
|
|
41
|
+
* for source trees (measured: 305-dir Next.js project ready in ~41ms,
|
|
42
|
+
* 1000-file burst handled 1:1 with chokidar@5), but a single flat
|
|
43
|
+
* directory with tens of thousands of entries would rescan slowly. If that
|
|
44
|
+
* happens, use the filename argument from fs.watch's callback to stat only
|
|
45
|
+
* that entry, falling back to a full rescan when it's null.
|
|
46
|
+
*
|
|
47
|
+
* 3. Linux inotify limits apply (as they do to chokidar).
|
|
48
|
+
* One watch descriptor per directory, capped by
|
|
49
|
+
* /proc/sys/fs/inotify/max_user_watches (typically 8k–128k). Always pass
|
|
50
|
+
* `ignored` for node_modules/.git/build-output dirs on big repos.
|
|
51
|
+
*
|
|
52
|
+
* 4. Not implemented (unused by this project): add()/unwatch()/getWatched(),
|
|
53
|
+
* depth, cwd, alwaysStat, atomic, followSymlinks:false (symlinked dirs are
|
|
54
|
+
* watched, with realpath-based cycle protection), and sub-mtime-resolution
|
|
55
|
+
* edge: a rewrite with identical size landing in the same mtime tick is
|
|
56
|
+
* undetectable by stat diffing (theoretical on ext4/APFS/NTFS — ns mtime).
|
|
57
|
+
*/
|
|
58
|
+
"use strict";
|
|
59
|
+
|
|
60
|
+
const fs = require("fs");
|
|
61
|
+
const path = require("path");
|
|
62
|
+
const { EventEmitter } = require("events");
|
|
63
|
+
|
|
64
|
+
const SCAN_DEBOUNCE = 25; // coalesce fs.watch event bursts per directory
|
|
65
|
+
const ROOT_POLL = 1000; // how often to look for a missing watched root
|
|
66
|
+
|
|
67
|
+
function normalizeIgnored(ignored) {
|
|
68
|
+
if (!ignored) return () => false;
|
|
69
|
+
const list = Array.isArray(ignored) ? ignored : [ignored];
|
|
70
|
+
return p =>
|
|
71
|
+
list.some(entry => {
|
|
72
|
+
if (typeof entry === "function") return entry(p);
|
|
73
|
+
if (entry instanceof RegExp) return entry.test(p);
|
|
74
|
+
return p === entry || p.startsWith(entry + path.sep); // plain path prefix
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
class FSWatcher extends EventEmitter {
|
|
79
|
+
constructor(paths, options = {}) {
|
|
80
|
+
super();
|
|
81
|
+
|
|
82
|
+
this._persistent = options.persistent !== false;
|
|
83
|
+
this._ignoreInitial = !!options.ignoreInitial;
|
|
84
|
+
this._ignored = normalizeIgnored(options.ignored);
|
|
85
|
+
|
|
86
|
+
const awf = options.awaitWriteFinish;
|
|
87
|
+
// chokidar defaults: threshold 2000ms, poll 100ms when awaitWriteFinish: true
|
|
88
|
+
this._awf = awf ? { threshold: awf.stabilityThreshold || 2000, poll: awf.pollInterval || 100 } : null;
|
|
89
|
+
|
|
90
|
+
this._roots = (Array.isArray(paths) ? paths : [paths]).map(p => path.resolve(p));
|
|
91
|
+
this._watchers = new Map(); // dir → fs.FSWatcher
|
|
92
|
+
this._snapshots = new Map(); // dir → Map(name → { isDir, mtimeMs, size })
|
|
93
|
+
this._scanTimers = new Map(); // dir → debounce timeout
|
|
94
|
+
this._pending = new Map(); // file → awaitWriteFinish poll state
|
|
95
|
+
this._rootTimers = new Map(); // missing root → poll interval
|
|
96
|
+
this._realPaths = new Set(); // real paths of watched dirs (symlink-cycle guard)
|
|
97
|
+
this._realByDir = new Map(); // dir → its real path, for cleanup
|
|
98
|
+
this._closed = false;
|
|
99
|
+
|
|
100
|
+
process.nextTick(() => {
|
|
101
|
+
if (this._closed) return;
|
|
102
|
+
for (const root of this._roots) this._addRoot(root, true);
|
|
103
|
+
this.emit("ready");
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ------------------------------------------------------------ setup
|
|
108
|
+
_addRoot(root, isInitial) {
|
|
109
|
+
let stat;
|
|
110
|
+
try {
|
|
111
|
+
stat = fs.statSync(root);
|
|
112
|
+
} catch (e) {
|
|
113
|
+
this._waitForRoot(root);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (stat.isDirectory()) {
|
|
117
|
+
// chokidar reports the watched root itself, subject to ignoreInitial
|
|
118
|
+
if (!(isInitial && this._ignoreInitial)) this.emit("addDir", root);
|
|
119
|
+
this._watchDir(root, isInitial);
|
|
120
|
+
}
|
|
121
|
+
// single-file roots: watch the parent dir but only report this file
|
|
122
|
+
else this._watchDir(path.dirname(root), isInitial, root);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
_waitForRoot(root) {
|
|
126
|
+
if (this._closed || this._rootTimers.has(root)) return;
|
|
127
|
+
const timer = setInterval(() => {
|
|
128
|
+
if (fs.existsSync(root)) {
|
|
129
|
+
clearInterval(timer);
|
|
130
|
+
this._rootTimers.delete(root);
|
|
131
|
+
this._addRoot(root, false); // appeared later → its contents are new adds
|
|
132
|
+
}
|
|
133
|
+
}, ROOT_POLL);
|
|
134
|
+
if (!this._persistent && timer.unref) timer.unref();
|
|
135
|
+
this._rootTimers.set(root, timer);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
_watchDir(dir, isInitial, onlyFile) {
|
|
139
|
+
if (this._closed || this._watchers.has(dir) || this._ignored(dir)) return;
|
|
140
|
+
|
|
141
|
+
// symlink-cycle guard: a link pointing back into the watched tree must not
|
|
142
|
+
// cause infinite recursion — track the real path of every watched dir
|
|
143
|
+
let real;
|
|
144
|
+
try {
|
|
145
|
+
real = fs.realpathSync(dir);
|
|
146
|
+
} catch (e) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (this._realPaths.has(real)) return;
|
|
150
|
+
this._realPaths.add(real);
|
|
151
|
+
this._realByDir.set(dir, real);
|
|
152
|
+
|
|
153
|
+
let watcher;
|
|
154
|
+
try {
|
|
155
|
+
watcher = fs.watch(dir, { persistent: this._persistent });
|
|
156
|
+
} catch (err) {
|
|
157
|
+
this.emit("error", err);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
// fs.watch errors (e.g. EPERM on windows when the dir is deleted) are not
|
|
161
|
+
// fatal: the parent directory's rescan tears this watcher down
|
|
162
|
+
watcher.on("error", () => watcher.close());
|
|
163
|
+
watcher.on("change", () => this._scheduleScan(dir));
|
|
164
|
+
|
|
165
|
+
this._watchers.set(dir, watcher);
|
|
166
|
+
this._snapshots.set(dir, new Map());
|
|
167
|
+
if (onlyFile) this._onlyFile = onlyFile;
|
|
168
|
+
this._rescan(dir, isInitial);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// ------------------------------------------------------------ scanning
|
|
172
|
+
_scheduleScan(dir) {
|
|
173
|
+
if (this._closed || this._scanTimers.has(dir)) return;
|
|
174
|
+
this._scanTimers.set(
|
|
175
|
+
dir,
|
|
176
|
+
setTimeout(() => {
|
|
177
|
+
this._scanTimers.delete(dir);
|
|
178
|
+
this._rescan(dir, false);
|
|
179
|
+
}, SCAN_DEBOUNCE)
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
_rescan(dir, isInitial) {
|
|
184
|
+
if (this._closed || !this._snapshots.has(dir)) return;
|
|
185
|
+
|
|
186
|
+
let names;
|
|
187
|
+
try {
|
|
188
|
+
names = fs.readdirSync(dir);
|
|
189
|
+
} catch (e) {
|
|
190
|
+
// directory itself vanished
|
|
191
|
+
this._teardownDir(dir, true);
|
|
192
|
+
if (this._roots.includes(dir)) this._waitForRoot(dir);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const old = this._snapshots.get(dir);
|
|
197
|
+
const next = new Map();
|
|
198
|
+
|
|
199
|
+
for (const name of names) {
|
|
200
|
+
const full = path.join(dir, name);
|
|
201
|
+
if (this._ignored(full)) continue;
|
|
202
|
+
|
|
203
|
+
let stat;
|
|
204
|
+
try {
|
|
205
|
+
stat = fs.statSync(full);
|
|
206
|
+
} catch (e) {
|
|
207
|
+
continue; // deleted between readdir and stat
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const entry = { isDir: stat.isDirectory(), mtimeMs: stat.mtimeMs, size: stat.size };
|
|
211
|
+
next.set(name, entry);
|
|
212
|
+
|
|
213
|
+
const prev = old.get(name);
|
|
214
|
+
const silent = isInitial && this._ignoreInitial;
|
|
215
|
+
|
|
216
|
+
if (!prev) {
|
|
217
|
+
if (entry.isDir) {
|
|
218
|
+
if (!silent) this.emit("addDir", full);
|
|
219
|
+
this._watchDir(full, isInitial); // walks and reports its contents
|
|
220
|
+
} else if (!silent) {
|
|
221
|
+
this._settle(full, "add");
|
|
222
|
+
}
|
|
223
|
+
} else if (prev.isDir !== entry.isDir) {
|
|
224
|
+
// replaced by the other kind: unlink the old, add the new
|
|
225
|
+
if (prev.isDir) this._teardownDir(full, true);
|
|
226
|
+
else this.emit("unlink", full);
|
|
227
|
+
if (entry.isDir) {
|
|
228
|
+
this.emit("addDir", full);
|
|
229
|
+
this._watchDir(full, false);
|
|
230
|
+
} else {
|
|
231
|
+
this._settle(full, "add");
|
|
232
|
+
}
|
|
233
|
+
} else if (!entry.isDir && (prev.mtimeMs !== entry.mtimeMs || prev.size !== entry.size)) {
|
|
234
|
+
this._settle(full, "change");
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
for (const [name, prev] of old) {
|
|
239
|
+
if (next.has(name)) continue;
|
|
240
|
+
const full = path.join(dir, name);
|
|
241
|
+
if (prev.isDir) this._teardownDir(full, true);
|
|
242
|
+
else this._dropFile(full);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
this._snapshots.set(dir, next);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
_dropFile(file) {
|
|
249
|
+
const pending = this._pending.get(file);
|
|
250
|
+
if (pending) {
|
|
251
|
+
clearInterval(pending.timer);
|
|
252
|
+
this._pending.delete(file);
|
|
253
|
+
// a file that vanished while its "add" was still stabilizing was never
|
|
254
|
+
// reported, so there is nothing to unlink
|
|
255
|
+
if (pending.event === "add") return;
|
|
256
|
+
}
|
|
257
|
+
this.emit("unlink", file);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
_teardownDir(dir, emitEvents) {
|
|
261
|
+
const watcher = this._watchers.get(dir);
|
|
262
|
+
if (watcher) watcher.close();
|
|
263
|
+
this._watchers.delete(dir);
|
|
264
|
+
|
|
265
|
+
const real = this._realByDir.get(dir);
|
|
266
|
+
if (real !== undefined) {
|
|
267
|
+
this._realPaths.delete(real);
|
|
268
|
+
this._realByDir.delete(dir);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const timer = this._scanTimers.get(dir);
|
|
272
|
+
if (timer) clearTimeout(timer);
|
|
273
|
+
this._scanTimers.delete(dir);
|
|
274
|
+
|
|
275
|
+
const snapshot = this._snapshots.get(dir);
|
|
276
|
+
this._snapshots.delete(dir);
|
|
277
|
+
if (!snapshot) return;
|
|
278
|
+
|
|
279
|
+
for (const [name, entry] of snapshot) {
|
|
280
|
+
const full = path.join(dir, name);
|
|
281
|
+
if (entry.isDir) this._teardownDir(full, emitEvents);
|
|
282
|
+
else if (emitEvents) this._dropFile(full);
|
|
283
|
+
}
|
|
284
|
+
if (emitEvents) this.emit("unlinkDir", dir);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// ------------------------------------------------- awaitWriteFinish
|
|
288
|
+
_settle(file, event) {
|
|
289
|
+
if (this._onlyFile && file !== this._onlyFile) return; // single-file root filter
|
|
290
|
+
|
|
291
|
+
if (!this._awf) return this.emit(event, file);
|
|
292
|
+
|
|
293
|
+
// an event already stabilizing keeps its original type: more writes while
|
|
294
|
+
// an "add" settles must still surface as a single "add", not a "change"
|
|
295
|
+
const existing = this._pending.get(file);
|
|
296
|
+
if (existing) {
|
|
297
|
+
existing.stableSince = Date.now();
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const state = { event, stableSince: Date.now(), mtimeMs: -1, size: -1, timer: null };
|
|
302
|
+
state.timer = setInterval(() => {
|
|
303
|
+
let stat;
|
|
304
|
+
try {
|
|
305
|
+
stat = fs.statSync(file);
|
|
306
|
+
} catch (e) {
|
|
307
|
+
clearInterval(state.timer); // vanished mid-write; unlink comes from the dir rescan
|
|
308
|
+
this._pending.delete(file);
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
if (stat.mtimeMs !== state.mtimeMs || stat.size !== state.size) {
|
|
312
|
+
state.mtimeMs = stat.mtimeMs;
|
|
313
|
+
state.size = stat.size;
|
|
314
|
+
state.stableSince = Date.now();
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
if (Date.now() - state.stableSince >= this._awf.threshold) {
|
|
318
|
+
clearInterval(state.timer);
|
|
319
|
+
this._pending.delete(file);
|
|
320
|
+
this.emit(state.event, file);
|
|
321
|
+
}
|
|
322
|
+
}, this._awf.poll);
|
|
323
|
+
this._pending.set(file, state);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// ------------------------------------------------------------ teardown
|
|
327
|
+
close() {
|
|
328
|
+
this._closed = true;
|
|
329
|
+
for (const watcher of this._watchers.values()) watcher.close();
|
|
330
|
+
for (const timer of this._scanTimers.values()) clearTimeout(timer);
|
|
331
|
+
for (const state of this._pending.values()) clearInterval(state.timer);
|
|
332
|
+
for (const timer of this._rootTimers.values()) clearInterval(timer);
|
|
333
|
+
this._watchers.clear();
|
|
334
|
+
this._scanTimers.clear();
|
|
335
|
+
this._pending.clear();
|
|
336
|
+
this._rootTimers.clear();
|
|
337
|
+
this._snapshots.clear();
|
|
338
|
+
this._realPaths.clear();
|
|
339
|
+
this._realByDir.clear();
|
|
340
|
+
return Promise.resolve();
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
module.exports = { watch: (paths, options) => new FSWatcher(paths, options), FSWatcher };
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* spawn.js — standalone single-file port of cross-spawn@7.0.6 (MIT)
|
|
3
|
+
* with its dependencies inlined: which@2.0.2, isexe@2.0.0, path-key@3.1.1,
|
|
4
|
+
* shebang-command@2.0.0, shebang-regex@3.0.0 (all MIT).
|
|
5
|
+
* Uses only node built-ins (child_process, fs, path).
|
|
6
|
+
*
|
|
7
|
+
* API (same as cross-spawn):
|
|
8
|
+
* spawn(command, [args], [options]) → ChildProcess
|
|
9
|
+
* spawn.sync(command, [args], [options]) → SpawnSyncReturns
|
|
10
|
+
*/
|
|
11
|
+
"use strict";
|
|
12
|
+
|
|
13
|
+
const cp = require("child_process");
|
|
14
|
+
const fs = require("fs");
|
|
15
|
+
const path = require("path");
|
|
16
|
+
|
|
17
|
+
const isWin = process.platform === "win32";
|
|
18
|
+
// `which`/`isexe` treat cygwin/msys shells as windows too
|
|
19
|
+
const isWindowsEnv = isWin || process.env.OSTYPE === "cygwin" || process.env.OSTYPE === "msys";
|
|
20
|
+
|
|
21
|
+
// ---------------------------------------------------------------- path-key
|
|
22
|
+
function pathKey(options = {}) {
|
|
23
|
+
const environment = options.env || process.env;
|
|
24
|
+
const platform = options.platform || process.platform;
|
|
25
|
+
if (platform !== "win32") return "PATH";
|
|
26
|
+
return Object.keys(environment).reverse().find(key => key.toUpperCase() === "PATH") || "Path";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ------------------------------------------------------------------- isexe
|
|
30
|
+
// windows flavor: a file is executable if its extension is in PATHEXT
|
|
31
|
+
function checkPathExt(file, options) {
|
|
32
|
+
let pathext = options.pathExt !== undefined ? options.pathExt : process.env.PATHEXT;
|
|
33
|
+
if (!pathext) return true;
|
|
34
|
+
pathext = pathext.split(";");
|
|
35
|
+
if (pathext.indexOf("") !== -1) return true;
|
|
36
|
+
for (let i = 0; i < pathext.length; i++) {
|
|
37
|
+
const p = pathext[i].toLowerCase();
|
|
38
|
+
if (p && file.substr(-p.length).toLowerCase() === p) return true;
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function isexeWindows(file, options) {
|
|
44
|
+
const stat = fs.statSync(file);
|
|
45
|
+
if (!stat.isSymbolicLink() && !stat.isFile()) return false;
|
|
46
|
+
return checkPathExt(file, options);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function isexePosix(file) {
|
|
50
|
+
const stat = fs.statSync(file);
|
|
51
|
+
if (!stat.isFile()) return false;
|
|
52
|
+
const mod = stat.mode;
|
|
53
|
+
const uid = stat.uid;
|
|
54
|
+
const gid = stat.gid;
|
|
55
|
+
const myUid = process.getuid && process.getuid();
|
|
56
|
+
const myGid = process.getgid && process.getgid();
|
|
57
|
+
const u = parseInt("100", 8);
|
|
58
|
+
const g = parseInt("010", 8);
|
|
59
|
+
const o = parseInt("001", 8);
|
|
60
|
+
const ug = u | g;
|
|
61
|
+
return (mod & o) || (mod & g && gid === myGid) || (mod & u && uid === myUid) || (mod & ug && myUid === 0);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function isexeSync(file, options) {
|
|
65
|
+
return isWindowsEnv ? isexeWindows(file, options) : isexePosix(file);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ------------------------------------------------------------------- which
|
|
69
|
+
function getPathInfo(cmd, opt) {
|
|
70
|
+
const colon = opt.colon || (isWindowsEnv ? ";" : ":");
|
|
71
|
+
|
|
72
|
+
// if cmd has a slash, don't search PATH — check the file itself
|
|
73
|
+
const pathEnv = cmd.match(/\//) || (isWindowsEnv && cmd.match(/\\/))
|
|
74
|
+
? [""]
|
|
75
|
+
: [
|
|
76
|
+
// windows always checks the cwd first
|
|
77
|
+
...(isWindowsEnv ? [process.cwd()] : []),
|
|
78
|
+
...(opt.path || process.env.PATH || "").split(colon),
|
|
79
|
+
];
|
|
80
|
+
const pathExtExe = isWindowsEnv ? opt.pathExt || process.env.PATHEXT || ".EXE;.CMD;.BAT;.COM" : "";
|
|
81
|
+
const pathExt = isWindowsEnv ? pathExtExe.split(colon) : [""];
|
|
82
|
+
|
|
83
|
+
if (isWindowsEnv) {
|
|
84
|
+
if (cmd.indexOf(".") !== -1 && pathExt[0] !== "") pathExt.unshift("");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return { pathEnv, pathExt, pathExtExe };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function whichSync(cmd, opt) {
|
|
91
|
+
opt = opt || {};
|
|
92
|
+
const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt);
|
|
93
|
+
|
|
94
|
+
for (let i = 0; i < pathEnv.length; i++) {
|
|
95
|
+
const ppRaw = pathEnv[i];
|
|
96
|
+
const pathPart = /^".*"$/.test(ppRaw) ? ppRaw.slice(1, -1) : ppRaw;
|
|
97
|
+
|
|
98
|
+
const pCmd = path.join(pathPart, cmd);
|
|
99
|
+
const p = !pathPart && /^\.[\\/]/.test(cmd) ? cmd.slice(0, 2) + pCmd : pCmd;
|
|
100
|
+
|
|
101
|
+
for (let j = 0; j < pathExt.length; j++) {
|
|
102
|
+
const cur = p + pathExt[j];
|
|
103
|
+
try {
|
|
104
|
+
if (isexeSync(cur, { pathExt: pathExtExe })) return cur;
|
|
105
|
+
} catch (ex) { /* Empty */ }
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (opt.nothrow) return null;
|
|
110
|
+
throw Object.assign(new Error("not found: " + cmd), { code: "ENOENT" });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// -------------------------------------------------- cross-spawn/lib/util/*
|
|
114
|
+
function resolveCommandAttempt(parsed, withoutPathExt) {
|
|
115
|
+
const env = parsed.options.env || process.env;
|
|
116
|
+
const cwd = process.cwd();
|
|
117
|
+
const hasCustomCwd = parsed.options.cwd != null;
|
|
118
|
+
// worker threads do not have process.chdir()
|
|
119
|
+
const shouldSwitchCwd = hasCustomCwd && process.chdir !== undefined && !process.chdir.disabled;
|
|
120
|
+
|
|
121
|
+
// `which` stats against the process cwd, so temporarily switch to the custom one
|
|
122
|
+
if (shouldSwitchCwd) {
|
|
123
|
+
try {
|
|
124
|
+
process.chdir(parsed.options.cwd);
|
|
125
|
+
} catch (err) { /* Empty */ }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
let resolved;
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
resolved = whichSync(parsed.command, {
|
|
132
|
+
path: env[pathKey({ env })],
|
|
133
|
+
pathExt: withoutPathExt ? path.delimiter : undefined,
|
|
134
|
+
});
|
|
135
|
+
} catch (e) { /* Empty */ } finally {
|
|
136
|
+
if (shouldSwitchCwd) process.chdir(cwd);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ensure an absolute path is returned (resolved against the custom cwd if any)
|
|
140
|
+
if (resolved) resolved = path.resolve(hasCustomCwd ? parsed.options.cwd : "", resolved);
|
|
141
|
+
|
|
142
|
+
return resolved;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function resolveCommand(parsed) {
|
|
146
|
+
return resolveCommandAttempt(parsed) || resolveCommandAttempt(parsed, true);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// shebang-command: "#!/usr/bin/env node" → "node", "#!/usr/bin/node -x" → "node -x"
|
|
150
|
+
function shebangCommand(string = "") {
|
|
151
|
+
const match = string.match(/^#!(.*)/);
|
|
152
|
+
if (!match) return null;
|
|
153
|
+
|
|
154
|
+
const [shebangPath, argument] = match[0].replace(/#! ?/, "").split(" ");
|
|
155
|
+
const binary = shebangPath.split("/").pop();
|
|
156
|
+
|
|
157
|
+
if (binary === "env") return argument;
|
|
158
|
+
return argument ? `${binary} ${argument}` : binary;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function readShebang(command) {
|
|
162
|
+
// read the first 150 bytes of the file
|
|
163
|
+
const size = 150;
|
|
164
|
+
const buffer = Buffer.alloc(size);
|
|
165
|
+
|
|
166
|
+
let fd;
|
|
167
|
+
try {
|
|
168
|
+
fd = fs.openSync(command, "r");
|
|
169
|
+
fs.readSync(fd, buffer, 0, size, 0);
|
|
170
|
+
fs.closeSync(fd);
|
|
171
|
+
} catch (e) { /* Empty */ }
|
|
172
|
+
|
|
173
|
+
return shebangCommand(buffer.toString());
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// see http://www.robvanderwoude.com/escapechars.php
|
|
177
|
+
const metaCharsRegExp = /([()\][%!^"`<>&|;, *?])/g;
|
|
178
|
+
|
|
179
|
+
function escapeCommand(arg) {
|
|
180
|
+
return arg.replace(metaCharsRegExp, "^$1");
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function escapeArgument(arg, doubleEscapeMetaChars) {
|
|
184
|
+
arg = `${arg}`;
|
|
185
|
+
|
|
186
|
+
// algorithm based on https://qntm.org/cmd, altered to avoid regex backtracking
|
|
187
|
+
// sequence of backslashes followed by a double quote: double the backslashes, escape the quote
|
|
188
|
+
arg = arg.replace(/(?=(\\+?)?)\1"/g, '$1$1\\"');
|
|
189
|
+
// sequence of backslashes at the end (a double quote follows once wrapped): double them
|
|
190
|
+
arg = arg.replace(/(?=(\\+?)?)\1$/, "$1$1");
|
|
191
|
+
|
|
192
|
+
// quote the whole thing, then escape cmd.exe meta chars
|
|
193
|
+
arg = `"${arg}"`;
|
|
194
|
+
arg = arg.replace(metaCharsRegExp, "^$1");
|
|
195
|
+
|
|
196
|
+
// cmd-shims are re-parsed by a second cmd.exe, so escape twice
|
|
197
|
+
if (doubleEscapeMetaChars) arg = arg.replace(metaCharsRegExp, "^$1");
|
|
198
|
+
|
|
199
|
+
return arg;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// --------------------------------------------------- cross-spawn/lib/parse
|
|
203
|
+
const isExecutableRegExp = /\.(?:com|exe)$/i;
|
|
204
|
+
const isCmdShimRegExp = /node_modules[\\/].bin[\\/][^\\/]+\.cmd$/i;
|
|
205
|
+
|
|
206
|
+
function detectShebang(parsed) {
|
|
207
|
+
parsed.file = resolveCommand(parsed);
|
|
208
|
+
|
|
209
|
+
const shebang = parsed.file && readShebang(parsed.file);
|
|
210
|
+
|
|
211
|
+
if (shebang) {
|
|
212
|
+
parsed.args.unshift(parsed.file);
|
|
213
|
+
parsed.command = shebang;
|
|
214
|
+
return resolveCommand(parsed);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return parsed.file;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function parseNonShell(parsed) {
|
|
221
|
+
if (!isWin) return parsed;
|
|
222
|
+
|
|
223
|
+
const commandFile = detectShebang(parsed);
|
|
224
|
+
|
|
225
|
+
// no shell needed if the command resolved to a real executable (.com/.exe)
|
|
226
|
+
const needsShell = !isExecutableRegExp.test(commandFile);
|
|
227
|
+
|
|
228
|
+
if (parsed.options.forceShell || needsShell) {
|
|
229
|
+
// cmd-shims in node_modules/.bin invoke a nested cmd.exe, requiring double escaping
|
|
230
|
+
const needsDoubleEscapeMetaChars = isCmdShimRegExp.test(commandFile);
|
|
231
|
+
|
|
232
|
+
// normalize posix paths (foo/bar → foo\bar) or cmd.exe fails with ENOENT
|
|
233
|
+
parsed.command = path.normalize(parsed.command);
|
|
234
|
+
|
|
235
|
+
parsed.command = escapeCommand(parsed.command);
|
|
236
|
+
parsed.args = parsed.args.map(arg => escapeArgument(arg, needsDoubleEscapeMetaChars));
|
|
237
|
+
|
|
238
|
+
const shellCommand = [parsed.command].concat(parsed.args).join(" ");
|
|
239
|
+
|
|
240
|
+
parsed.args = ["/d", "/s", "/c", `"${shellCommand}"`];
|
|
241
|
+
parsed.command = process.env.comspec || "cmd.exe";
|
|
242
|
+
parsed.options.windowsVerbatimArguments = true; // args are already escaped
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return parsed;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function parse(command, args, options) {
|
|
249
|
+
// normalize arguments, similar to nodejs
|
|
250
|
+
if (args && !Array.isArray(args)) {
|
|
251
|
+
options = args;
|
|
252
|
+
args = null;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
args = args ? args.slice(0) : [];
|
|
256
|
+
options = Object.assign({}, options);
|
|
257
|
+
|
|
258
|
+
const parsed = {
|
|
259
|
+
command,
|
|
260
|
+
args,
|
|
261
|
+
options,
|
|
262
|
+
file: undefined,
|
|
263
|
+
original: { command, args },
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
return options.shell ? parsed : parseNonShell(parsed);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// -------------------------------------------------- cross-spawn/lib/enoent
|
|
270
|
+
function notFoundError(original, syscall) {
|
|
271
|
+
return Object.assign(new Error(`${syscall} ${original.command} ENOENT`), {
|
|
272
|
+
code: "ENOENT",
|
|
273
|
+
errno: "ENOENT",
|
|
274
|
+
syscall: `${syscall} ${original.command}`,
|
|
275
|
+
path: original.command,
|
|
276
|
+
spawnargs: original.args,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// on windows a missing command exits cmd.exe with status 1 instead of erroring;
|
|
281
|
+
// convert that into the ENOENT error posix would have produced
|
|
282
|
+
function verifyENOENT(status, parsed, syscall) {
|
|
283
|
+
if (isWin && status === 1 && !parsed.file) return notFoundError(parsed.original, syscall);
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function hookChildProcess(child, parsed) {
|
|
288
|
+
if (!isWin) return;
|
|
289
|
+
|
|
290
|
+
const originalEmit = child.emit;
|
|
291
|
+
child.emit = function (name, arg1) {
|
|
292
|
+
if (name === "exit") {
|
|
293
|
+
const err = verifyENOENT(arg1, parsed, "spawn");
|
|
294
|
+
if (err) return originalEmit.call(child, "error", err);
|
|
295
|
+
}
|
|
296
|
+
return originalEmit.apply(child, arguments);
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// ------------------------------------------------------- cross-spawn/index
|
|
301
|
+
function spawn(command, args, options) {
|
|
302
|
+
const parsed = parse(command, args, options);
|
|
303
|
+
const spawned = cp.spawn(parsed.command, parsed.args, parsed.options);
|
|
304
|
+
hookChildProcess(spawned, parsed);
|
|
305
|
+
return spawned;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function spawnSync(command, args, options) {
|
|
309
|
+
const parsed = parse(command, args, options);
|
|
310
|
+
const result = cp.spawnSync(parsed.command, parsed.args, parsed.options);
|
|
311
|
+
result.error = result.error || verifyENOENT(result.status, parsed, "spawnSync");
|
|
312
|
+
return result;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
module.exports = spawn;
|
|
316
|
+
module.exports.spawn = spawn;
|
|
317
|
+
module.exports.sync = spawnSync;
|
|
318
|
+
module.exports._parse = parse;
|