mol_plot_all 1.2.1220 → 1.2.1222

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/node.mjs CHANGED
@@ -2149,97 +2149,115 @@ var $;
2149
2149
  class $mol_run_error extends $mol_error_mix {
2150
2150
  }
2151
2151
  $.$mol_run_error = $mol_run_error;
2152
- const child_process = $node['child_process'];
2153
- $.$mol_run_spawn = child_process.spawn.bind(child_process);
2154
- $.$mol_run_spawn_sync = child_process.spawnSync.bind(child_process);
2155
- function $mol_run_async({ dir, timeout, command, env }) {
2156
- const args_raw = typeof command === 'string' ? command.split(' ') : command;
2157
- const [app, ...args] = args_raw;
2158
- if (!env?.MOL_RUN_ASYNC) {
2159
- this.$mol_log3_come({
2160
- place: '$mol_run_sync',
2152
+ $.$mol_run_spawn = (...args) => $node['child_process'].spawn(...args);
2153
+ $.$mol_run_spawn_sync = (...args) => $node['child_process'].spawnSync(...args);
2154
+ class $mol_run extends $mol_object {
2155
+ static async_enabled() {
2156
+ return Boolean(this.$.$mol_env()['MOL_RUN_ASYNC']);
2157
+ }
2158
+ static spawn(options) {
2159
+ const sync = !this.async_enabled() || !Boolean($mol_wire_auto());
2160
+ const env = options.env ?? this.$.$mol_env();
2161
+ return $mol_wire_sync(this).spawn_async({ ...options, sync, env });
2162
+ }
2163
+ static spawn_async({ dir, sync, timeout, command, env }) {
2164
+ const args_raw = typeof command === 'string' ? command.split(' ') : command;
2165
+ const [app, ...args] = args_raw;
2166
+ const opts = { shell: true, cwd: dir, env };
2167
+ const log_object = {
2168
+ place: `${this}.spawn()`,
2161
2169
  message: 'Run',
2162
2170
  command: args_raw.join(' '),
2163
2171
  dir: $node.path.relative('', dir),
2172
+ };
2173
+ if (sync) {
2174
+ this.$.$mol_log3_come({
2175
+ hint: 'Run inside fiber',
2176
+ ...log_object
2177
+ });
2178
+ let error;
2179
+ let res;
2180
+ try {
2181
+ res = this.$.$mol_run_spawn_sync(app, args, opts);
2182
+ error = res.error;
2183
+ }
2184
+ catch (err) {
2185
+ error = err;
2186
+ }
2187
+ if (!res || error || res.status) {
2188
+ throw new $mol_run_error(this.error_message(res), { ...log_object, status: res?.status, signal: res?.signal }, ...(error ? [error] : []));
2189
+ }
2190
+ return res;
2191
+ }
2192
+ let sub;
2193
+ try {
2194
+ sub = this.$.$mol_run_spawn(app, args, {
2195
+ ...opts,
2196
+ stdio: ['pipe', 'inherit', 'inherit'],
2197
+ });
2198
+ }
2199
+ catch (error) {
2200
+ throw new $mol_run_error(this.error_message(undefined), log_object, error);
2201
+ }
2202
+ const pid = sub.pid ?? 0;
2203
+ this.$.$mol_log3_come({
2204
+ ...log_object,
2205
+ pid,
2164
2206
  });
2165
- const res = this.$mol_run_spawn_sync(app, args, { shell: true, cwd: dir, env });
2166
- if (res.status)
2167
- $mol_fail(new Error(res.stderr.toString() || 'Exit(' + res.status + ')'));
2168
- return res;
2169
- }
2170
- const sub = this.$mol_run_spawn(app, args, {
2171
- shell: true,
2172
- cwd: dir,
2173
- env
2174
- });
2175
- this.$mol_log3_come({
2176
- place: '$mol_run_async',
2177
- pid: sub.pid,
2178
- message: 'Run',
2179
- command: args_raw.join(' '),
2180
- dir: $node.path.relative('', dir),
2181
- });
2182
- let killed = false;
2183
- let timer;
2184
- const std_data = [];
2185
- const error_data = [];
2186
- const add = (std_chunk, error_chunk) => {
2187
- if (std_chunk)
2188
- std_data.push(std_chunk);
2189
- if (error_chunk)
2190
- error_data.push(error_chunk);
2191
- if (!timeout)
2192
- return;
2193
- clearTimeout(timer);
2194
- timer = setTimeout(() => {
2195
- const signal = killed ? 'SIGKILL' : 'SIGTERM';
2196
- killed = true;
2197
- add();
2198
- sub.kill(signal);
2199
- }, timeout);
2200
- };
2201
- add();
2202
- sub.stdout?.on('data', data => add(data));
2203
- sub.stderr?.on('data', data => add(undefined, data));
2204
- const promise = new Promise((done, fail) => {
2205
- const close = (error, status = null, signal = null) => {
2206
- if (!timer && timeout)
2207
+ let timeout_kill = false;
2208
+ let timer;
2209
+ const std_data = [];
2210
+ const error_data = [];
2211
+ const add = (std_chunk, error_chunk) => {
2212
+ if (std_chunk)
2213
+ std_data.push(std_chunk);
2214
+ if (error_chunk)
2215
+ error_data.push(error_chunk);
2216
+ if (!timeout)
2207
2217
  return;
2208
2218
  clearTimeout(timer);
2209
- timer = undefined;
2210
- const res = {
2211
- pid: sub.pid,
2212
- status,
2213
- signal,
2214
- get stdout() { return Buffer.concat(std_data); },
2215
- get stderr() { return Buffer.concat(error_data); }
2216
- };
2217
- this.$mol_log3_done({
2218
- place: '$mol_run_async',
2219
- pid: sub.pid,
2220
- message: 'Run',
2221
- status,
2222
- command: args_raw.join(' '),
2223
- dir: $node.path.relative('', dir),
2224
- });
2225
- if (error || status || killed)
2226
- return fail(new $mol_run_error((res.stderr.toString() || res.stdout.toString() || 'Run error') + (killed ? ', timeout' : ''), { signal, timeout: killed }, ...error ? [error] : []));
2227
- done(res);
2219
+ timer = setTimeout(() => {
2220
+ const signal = timeout_kill ? 'SIGKILL' : 'SIGTERM';
2221
+ timeout_kill = true;
2222
+ add();
2223
+ sub.kill(signal);
2224
+ }, timeout);
2228
2225
  };
2229
- sub.on('disconnect', () => close(new Error('Disconnected')));
2230
- sub.on('error', err => close(err));
2231
- sub.on('exit', (status, signal) => close(null, status, signal));
2232
- });
2233
- return Object.assign(promise, { destructor: () => {
2234
- clearTimeout(timer);
2235
- sub.kill('SIGKILL');
2236
- } });
2237
- }
2238
- $.$mol_run_async = $mol_run_async;
2239
- function $mol_run(options) {
2240
- if (!options.env)
2241
- options = { ...options, env: this.$mol_env() };
2242
- return $mol_wire_sync(this).$mol_run_async(options);
2226
+ add();
2227
+ sub.stdout?.on('data', data => add(data));
2228
+ sub.stderr?.on('data', data => add(undefined, data));
2229
+ const result_promise = new Promise((done, fail) => {
2230
+ const close = (error, status = null, signal = null) => {
2231
+ if (!timer && timeout)
2232
+ return;
2233
+ clearTimeout(timer);
2234
+ timer = undefined;
2235
+ const res = {
2236
+ pid,
2237
+ signal,
2238
+ get stdout() { return Buffer.concat(std_data); },
2239
+ get stderr() { return Buffer.concat(error_data); }
2240
+ };
2241
+ if (error || status || timeout_kill)
2242
+ return fail(new $mol_run_error(this.error_message(res) + (timeout_kill ? ', timeout' : ''), { ...log_object, pid, status, signal, timeout_kill }, ...error ? [error] : []));
2243
+ this.$.$mol_log3_done({
2244
+ ...log_object,
2245
+ pid,
2246
+ });
2247
+ done(res);
2248
+ };
2249
+ sub.on('disconnect', () => close(new Error('Disconnected')));
2250
+ sub.on('error', err => close(err));
2251
+ sub.on('exit', (status, signal) => close(null, status, signal));
2252
+ });
2253
+ return Object.assign(result_promise, { destructor: () => {
2254
+ clearTimeout(timer);
2255
+ sub.kill('SIGKILL');
2256
+ } });
2257
+ }
2258
+ static error_message(res) {
2259
+ return res?.stderr.toString() || res?.stdout.toString() || 'Run error';
2260
+ }
2243
2261
  }
2244
2262
  $.$mol_run = $mol_run;
2245
2263
  })($ || ($ = {}));
@@ -2249,7 +2267,7 @@ var $;
2249
2267
  var $;
2250
2268
  (function ($) {
2251
2269
  function $mol_exec(dir, command, ...args) {
2252
- return this.$mol_run({ command: [command, ...args], dir });
2270
+ return this.$mol_run.spawn({ command: [command, ...args], dir });
2253
2271
  }
2254
2272
  $.$mol_exec = $mol_exec;
2255
2273
  })($ || ($ = {}));