@php-wasm/util
Advanced tools
64
index.js
@@ -109,9 +109,63 @@ // packages/php-wasm/util/src/lib/semaphore.ts | ||
// packages/php-wasm/util/src/lib/split-shell-command.ts | ||
function splitShellCommand(command) { | ||
const MODE_UNQUOTED = 0; | ||
const MODE_IN_QUOTE = 1; | ||
let mode = MODE_UNQUOTED; | ||
let quote = ""; | ||
const parts = []; | ||
let currentPart = ""; | ||
for (let i = 0; i < command.length; i++) { | ||
const char = command[i]; | ||
if (char === "\\") { | ||
if (command[i + 1] === '"' || command[i + 1] === "'") { | ||
i++; | ||
} | ||
currentPart += command[i]; | ||
} else if (mode === MODE_UNQUOTED) { | ||
if (char === '"' || char === "'") { | ||
mode = MODE_IN_QUOTE; | ||
quote = char; | ||
} else if (char.match(/\s/)) { | ||
if (currentPart.trim().length) { | ||
parts.push(currentPart.trim()); | ||
} | ||
currentPart = char; | ||
} else if (parts.length && !currentPart) { | ||
currentPart = parts.pop() + char; | ||
} else { | ||
currentPart += char; | ||
} | ||
} else if (mode === MODE_IN_QUOTE) { | ||
if (char === quote) { | ||
mode = MODE_UNQUOTED; | ||
quote = ""; | ||
} else { | ||
currentPart += char; | ||
} | ||
} | ||
} | ||
if (currentPart) { | ||
parts.push(currentPart.trim()); | ||
} | ||
return parts; | ||
} | ||
// packages/php-wasm/util/src/lib/create-spawn-handler.ts | ||
function createSpawnHandler(program) { | ||
return function(command) { | ||
return function(command, argsArray = [], options = {}) { | ||
const childProcess = new ChildProcess(); | ||
const processApi = new ProcessApi(childProcess); | ||
setTimeout(async () => { | ||
await program(command, processApi); | ||
let commandArray = []; | ||
if (argsArray.length) { | ||
commandArray = [command, ...argsArray]; | ||
} else if (typeof command === "string") { | ||
commandArray = splitShellCommand(command); | ||
} else if (Array.isArray(command)) { | ||
commandArray = command; | ||
} else { | ||
throw new Error("Invalid command ", command); | ||
} | ||
await program(commandArray, processApi, options); | ||
childProcess.emit("spawn", true); | ||
@@ -160,2 +214,5 @@ }); | ||
} | ||
stdoutEnd() { | ||
this.childProcess.stdout.emit("end", {}); | ||
} | ||
stderr(data) { | ||
@@ -167,2 +224,5 @@ if (typeof data === "string") { | ||
} | ||
stderrEnd() { | ||
this.childProcess.stderr.emit("end", {}); | ||
} | ||
exit(code) { | ||
@@ -169,0 +229,0 @@ if (!this.exited) { |
{ | ||
"name": "@php-wasm/util", | ||
"version": "0.6.3", | ||
"version": "0.6.4", | ||
"type": "commonjs", | ||
@@ -15,3 +15,3 @@ "typedoc": { | ||
}, | ||
"gitHead": "0a8916ec08aa5f02de5fea308b287171e7948e4b", | ||
"gitHead": "8b74852e9701f5083b367f9a294f34200230ce79", | ||
"engines": { | ||
@@ -18,0 +18,0 @@ "node": ">=18.18.2", |
7980
26.77%295
25%