@php-wasm/util 0.6.3 → 0.6.4

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.
Files changed (2) hide show
  1. package/index.js +62 -2
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -106,13 +106,67 @@ function normalizePathsArray(parts, allowAboveRoot) {
106
106
  return parts;
107
107
  }
108
108
 
109
+ // packages/php-wasm/util/src/lib/split-shell-command.ts
110
+ function splitShellCommand(command) {
111
+ const MODE_UNQUOTED = 0;
112
+ const MODE_IN_QUOTE = 1;
113
+ let mode = MODE_UNQUOTED;
114
+ let quote = "";
115
+ const parts = [];
116
+ let currentPart = "";
117
+ for (let i = 0; i < command.length; i++) {
118
+ const char = command[i];
119
+ if (char === "\\") {
120
+ if (command[i + 1] === '"' || command[i + 1] === "'") {
121
+ i++;
122
+ }
123
+ currentPart += command[i];
124
+ } else if (mode === MODE_UNQUOTED) {
125
+ if (char === '"' || char === "'") {
126
+ mode = MODE_IN_QUOTE;
127
+ quote = char;
128
+ } else if (char.match(/\s/)) {
129
+ if (currentPart.trim().length) {
130
+ parts.push(currentPart.trim());
131
+ }
132
+ currentPart = char;
133
+ } else if (parts.length && !currentPart) {
134
+ currentPart = parts.pop() + char;
135
+ } else {
136
+ currentPart += char;
137
+ }
138
+ } else if (mode === MODE_IN_QUOTE) {
139
+ if (char === quote) {
140
+ mode = MODE_UNQUOTED;
141
+ quote = "";
142
+ } else {
143
+ currentPart += char;
144
+ }
145
+ }
146
+ }
147
+ if (currentPart) {
148
+ parts.push(currentPart.trim());
149
+ }
150
+ return parts;
151
+ }
152
+
109
153
  // packages/php-wasm/util/src/lib/create-spawn-handler.ts
110
154
  function createSpawnHandler(program) {
111
- return function(command) {
155
+ return function(command, argsArray = [], options = {}) {
112
156
  const childProcess = new ChildProcess();
113
157
  const processApi = new ProcessApi(childProcess);
114
158
  setTimeout(async () => {
115
- await program(command, processApi);
159
+ let commandArray = [];
160
+ if (argsArray.length) {
161
+ commandArray = [command, ...argsArray];
162
+ } else if (typeof command === "string") {
163
+ commandArray = splitShellCommand(command);
164
+ } else if (Array.isArray(command)) {
165
+ commandArray = command;
166
+ } else {
167
+ throw new Error("Invalid command ", command);
168
+ }
169
+ await program(commandArray, processApi, options);
116
170
  childProcess.emit("spawn", true);
117
171
  });
118
172
  return childProcess;
@@ -156,12 +210,18 @@ var ProcessApi = class extends EventEmitter {
156
210
  }
157
211
  this.childProcess.stdout.emit("data", data);
158
212
  }
213
+ stdoutEnd() {
214
+ this.childProcess.stdout.emit("end", {});
215
+ }
159
216
  stderr(data) {
160
217
  if (typeof data === "string") {
161
218
  data = new TextEncoder().encode(data);
162
219
  }
163
220
  this.childProcess.stderr.emit("data", data);
164
221
  }
222
+ stderrEnd() {
223
+ this.childProcess.stderr.emit("end", {});
224
+ }
165
225
  exit(code) {
166
226
  if (!this.exited) {
167
227
  this.exited = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@php-wasm/util",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
4
4
  "type": "commonjs",
5
5
  "typedoc": {
6
6
  "entryPoint": "./src/index.ts",
@@ -12,7 +12,7 @@
12
12
  "access": "public",
13
13
  "directory": "../../../dist/packages/php-wasm/util"
14
14
  },
15
- "gitHead": "0a8916ec08aa5f02de5fea308b287171e7948e4b",
15
+ "gitHead": "8b74852e9701f5083b367f9a294f34200230ce79",
16
16
  "engines": {
17
17
  "node": ">=18.18.2",
18
18
  "npm": ">=8.11.0"