This commit is contained in:
Yamozha
2021-04-02 02:24:13 +03:00
parent c23950b545
commit 7256d79e2c
31493 changed files with 3036630 additions and 0 deletions

View File

@ -0,0 +1 @@
export {};

View File

@ -0,0 +1,120 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const spawnAsync_1 = __importDefault(require("../spawnAsync"));
it(`receives output from completed processes`, async () => {
let result = await spawnAsync_1.default('echo', ['hi']);
expect(typeof result.pid).toBe('number');
expect(result.stdout).toBe('hi\n');
expect(result.stderr).toBe('');
expect(result.output[0]).toBe(result.stdout);
expect(result.output[1]).toBe(result.stderr);
expect(result.status).toBe(0);
expect(result.signal).toBe(null);
});
it(`throws errors when processes return non-zero exit codes`, async () => {
let didThrow = false;
try {
await spawnAsync_1.default('false');
}
catch (e) {
didThrow = true;
expect(typeof e.pid).toBe('number');
expect(e.status).toBe(1);
expect(e.signal).toBe(null);
}
expect(didThrow).toBe(true);
});
it(`returns when processes are killed with signals with non-zero exit codes`, async () => {
let didThrow = false;
try {
await spawnAsync_1.default(path_1.default.join(__dirname, 'signal-self.sh'));
}
catch (e) {
didThrow = true;
expect(typeof e.pid).toBe('number');
expect(e.status).toBe(null);
expect(e.signal).toBe('SIGKILL');
}
expect(didThrow).toBe(true);
});
it(`throws errors when processes don't exist`, async () => {
let didThrow = false;
try {
await spawnAsync_1.default('nonexistent-program');
}
catch (e) {
didThrow = true;
expect(e.pid).not.toBeDefined();
expect(e.code).toBe('ENOENT');
expect(e.status).toBe(null);
expect(e.signal).toBe(null);
}
expect(didThrow).toBe(true);
});
it(`exposes the child process through a property named "child"`, async () => {
let spawnTask = spawnAsync_1.default('echo', ['hi']);
let childProcess = spawnTask.child;
expect(childProcess).toBeDefined();
let result = await spawnTask;
expect(result.pid).toBe(childProcess.pid);
});
it(`runs extra listeners added to the child process`, async () => {
let spawnTask = spawnAsync_1.default('echo', ['hi']);
let mockExitListener = jest.fn();
let mockCloseListener = jest.fn();
spawnTask.child.on('exit', mockExitListener);
spawnTask.child.on('close', mockCloseListener);
await spawnTask;
expect(mockExitListener).toHaveBeenCalledTimes(1);
expect(mockCloseListener).toHaveBeenCalledTimes(1);
});
it(`runs extra error listeners added to the child process when there is an error`, async () => {
let spawnTask = spawnAsync_1.default('nonexistent-program');
let mockErrorListener = jest.fn();
spawnTask.child.on('error', mockErrorListener);
await expect(spawnTask).rejects.toThrowError();
expect(mockErrorListener).toHaveBeenCalledTimes(1);
});
it(`returns empty strings when ignoring stdio`, async () => {
let result = await spawnAsync_1.default('echo', ['hi'], { ignoreStdio: true });
expect(typeof result.pid).toBe('number');
expect(result.stdout).toBe('');
expect(result.stderr).toBe('');
expect(result.output[0]).toBe(result.stdout);
expect(result.output[1]).toBe(result.stderr);
expect(result.status).toBe(0);
expect(result.signal).toBe(null);
});
it(`returns even if stdout is open when ignoring stdio`, async () => {
// Without ignoring stdio, the promise will never resolve as stdout remains open indefinitely
let sourceTask = spawnAsync_1.default('yes', [], { ignoreStdio: true });
expect(sourceTask.child.listenerCount('exit')).toBe(1);
expect(sourceTask.child.listenerCount('close')).toBe(0);
// Create a sink that keeps the source's stdout open even after the source process exits
let sinkTask = spawnAsync_1.default('cat');
sourceTask.child.stdout.pipe(sinkTask.child.stdin);
sinkTask.child.stdin.cork();
// Allow the source's stdout to buffer with a short delay
await new Promise(resolve => setTimeout(resolve, 5));
// The source's stdout stays open even after killing the process
sourceTask.child.kill();
await expect(sourceTask).rejects.toThrowError();
// Destroy the sink's stdin stream to let the process exit
sinkTask.child.stdin.destroy();
await expect(sinkTask).resolves.toMatchObject({ status: 0, stdout: '', stderr: '' });
});
it('throws errors with preserved stack traces when processes return non-zero exit codes', async () => {
expect.assertions(2);
try {
await spawnAsync_1.default('false');
}
catch (e) {
expect(e.stack).toMatch(/\n \.\.\.\n/);
expect(e.stack).toMatch(/at Object\.spawnAsync/);
}
});
//# sourceMappingURL=spawnAsync-test.js.map

File diff suppressed because one or more lines are too long

17
node_modules/@expo/spawn-async/build/spawnAsync.d.ts generated vendored Normal file
View File

@ -0,0 +1,17 @@
/// <reference types="node" />
import { ChildProcess, SpawnOptions as NodeSpawnOptions } from 'child_process';
export interface SpawnOptions extends NodeSpawnOptions {
ignoreStdio?: boolean;
}
export interface SpawnPromise<T> extends Promise<T> {
child: ChildProcess;
}
export interface SpawnResult {
pid: number;
output: string[];
stdout: string;
stderr: string;
status: number | null;
signal: string | null;
}
export default function spawnAsync(command: string, args?: ReadonlyArray<string>, options?: SpawnOptions): SpawnPromise<SpawnResult>;

94
node_modules/@expo/spawn-async/build/spawnAsync.js generated vendored Normal file
View File

@ -0,0 +1,94 @@
"use strict";
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cross_spawn_1 = __importDefault(require("cross-spawn"));
function spawnAsync(command, args, options = {}) {
const fakeErr = new Error('fake error just to preserve stacktrace');
const previousStack = fakeErr.stack && fakeErr.stack.split('\n').splice(1);
const previousStackString = previousStack && [' ...', ...previousStack].join('\n');
let child;
let promise = new Promise((resolve, reject) => {
let { ignoreStdio } = options, nodeOptions = __rest(options, ["ignoreStdio"]);
// @ts-ignore: cross-spawn declares "args" to be a regular array instead of a read-only one
child = cross_spawn_1.default(command, args, nodeOptions);
let stdout = '';
let stderr = '';
if (!ignoreStdio) {
if (child.stdout) {
child.stdout.on('data', data => {
stdout += data;
});
}
if (child.stderr) {
child.stderr.on('data', data => {
stderr += data;
});
}
}
let completionListener = (code, signal) => {
child.removeListener('error', errorListener);
let result = {
pid: child.pid,
output: [stdout, stderr],
stdout,
stderr,
status: code,
signal,
};
if (code !== 0) {
let error = signal
? new Error(`${command} exited with signal: ${signal}`)
: new Error(`${command} exited with non-zero code: ${code}`);
if (error.stack && previousStackString) {
error.stack += `\n${previousStackString}`;
}
Object.assign(error, result);
reject(error);
}
else {
resolve(result);
}
};
let errorListener = (error) => {
if (ignoreStdio) {
child.removeListener('exit', completionListener);
}
else {
child.removeListener('close', completionListener);
}
Object.assign(error, {
pid: child.pid,
output: [stdout, stderr],
stdout,
stderr,
status: null,
signal: null,
});
reject(error);
};
if (ignoreStdio) {
child.once('exit', completionListener);
}
else {
child.once('close', completionListener);
}
child.once('error', errorListener);
});
// @ts-ignore: TypeScript isn't aware the Promise constructor argument runs synchronously and
// thinks `child` is not yet defined
promise.child = child;
return promise;
}
exports.default = spawnAsync;
//# sourceMappingURL=spawnAsync.js.map

File diff suppressed because one or more lines are too long