Skip to content

Commit

Permalink
Rename internal variable (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored Sep 8, 2024
1 parent 4c209d1 commit e673b94
Show file tree
Hide file tree
Showing 15 changed files with 214 additions and 214 deletions.
4 changes: 2 additions & 2 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {getResult} from './result.js';
import {handlePipe} from './pipe.js';
import {lineIterator, combineAsyncIterators} from './iterable.js';

export default function nanoSpawn(file, second, third, previous) {
export default function spawn(file, second, third, previous) {
const [commandArguments = [], options = {}] = Array.isArray(second) ? [second, third] : [[], second];
const context = getContext([file, ...commandArguments]);
const spawnOptions = getOptions(options);
Expand All @@ -21,6 +21,6 @@ export default function nanoSpawn(file, second, third, previous) {
stdout,
stderr,
[Symbol.asyncIterator]: () => combineAsyncIterators(stdout, stderr),
pipe: (file, second, third) => nanoSpawn(file, second, third, subprocess),
pipe: (file, second, third) => spawn(file, second, third, subprocess),
});
}
144 changes: 72 additions & 72 deletions source/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import {
expectNotAssignable,
expectError,
} from 'tsd';
import nanoSpawn, {
import spawn, {
type Options,
type Result,
type SubprocessError,
type Subprocess,
} from './index.js';

try {
const result = await nanoSpawn('test');
const result = await spawn('test');
expectType<Result>(result);
expectType<string>(result.stdout);
expectType<string>(result.stderr);
Expand Down Expand Up @@ -48,101 +48,101 @@ expectAssignable<Options>({argv0: 'test'} as const);
expectNotAssignable<Options>({other: 'test'} as const);
expectNotAssignable<Options>('test');

await nanoSpawn('test', {argv0: 'test'} as const);
expectError(await nanoSpawn('test', {argv0: true} as const));
await nanoSpawn('test', {preferLocal: true} as const);
expectError(await nanoSpawn('test', {preferLocal: 'true'} as const));
await nanoSpawn('test', {env: {}} as const);
await spawn('test', {argv0: 'test'} as const);
expectError(await spawn('test', {argv0: true} as const));
await spawn('test', {preferLocal: true} as const);
expectError(await spawn('test', {preferLocal: 'true'} as const));
await spawn('test', {env: {}} as const);
// eslint-disable-next-line @typescript-eslint/naming-convention
await nanoSpawn('test', {env: {TEST: 'test'}} as const);
expectError(await nanoSpawn('test', {env: true} as const));
await spawn('test', {env: {TEST: 'test'}} as const);
expectError(await spawn('test', {env: true} as const));
// eslint-disable-next-line @typescript-eslint/naming-convention
expectError(await nanoSpawn('test', {env: {TEST: true}} as const));
await nanoSpawn('test', {stdin: 'pipe'} as const);
await nanoSpawn('test', {stdin: {string: 'test'} as const} as const);
expectError(await nanoSpawn('test', {stdin: {string: true} as const} as const));
expectError(await nanoSpawn('test', {stdin: {other: 'test'} as const} as const));
expectError(await nanoSpawn('test', {stdin: true} as const));
await nanoSpawn('test', {stdout: 'pipe'} as const);
expectError(await nanoSpawn('test', {stdout: {string: 'test'} as const} as const));
expectError(await nanoSpawn('test', {stdout: true} as const));
await nanoSpawn('test', {stderr: 'pipe'} as const);
expectError(await nanoSpawn('test', {stderr: {string: 'test'} as const} as const));
expectError(await nanoSpawn('test', {stderr: true} as const));
await nanoSpawn('test', {stdio: ['pipe', 'pipe', 'pipe'] as const} as const);
await nanoSpawn('test', {stdio: [{string: 'test'} as const, 'pipe', 'pipe'] as const} as const);
expectError(await nanoSpawn('test', {stdio: ['pipe', {string: 'test'} as const, 'pipe'] as const} as const));
expectError(await nanoSpawn('test', {stdio: ['pipe', 'pipe', {string: 'test'} as const] as const} as const));
expectError(await nanoSpawn('test', {stdio: [{string: true} as const, 'pipe', 'pipe'] as const} as const));
expectError(await nanoSpawn('test', {stdio: [{other: 'test'} as const, 'pipe', 'pipe'] as const} as const));
expectError(await nanoSpawn('test', {stdio: [true, true, true] as const} as const));
await nanoSpawn('test', {stdio: 'pipe'} as const);
expectError(await nanoSpawn('test', {stdio: true} as const));
expectError(await nanoSpawn('test', {other: 'test'} as const));
expectError(await spawn('test', {env: {TEST: true}} as const));
await spawn('test', {stdin: 'pipe'} as const);
await spawn('test', {stdin: {string: 'test'} as const} as const);
expectError(await spawn('test', {stdin: {string: true} as const} as const));
expectError(await spawn('test', {stdin: {other: 'test'} as const} as const));
expectError(await spawn('test', {stdin: true} as const));
await spawn('test', {stdout: 'pipe'} as const);
expectError(await spawn('test', {stdout: {string: 'test'} as const} as const));
expectError(await spawn('test', {stdout: true} as const));
await spawn('test', {stderr: 'pipe'} as const);
expectError(await spawn('test', {stderr: {string: 'test'} as const} as const));
expectError(await spawn('test', {stderr: true} as const));
await spawn('test', {stdio: ['pipe', 'pipe', 'pipe'] as const} as const);
await spawn('test', {stdio: [{string: 'test'} as const, 'pipe', 'pipe'] as const} as const);
expectError(await spawn('test', {stdio: ['pipe', {string: 'test'} as const, 'pipe'] as const} as const));
expectError(await spawn('test', {stdio: ['pipe', 'pipe', {string: 'test'} as const] as const} as const));
expectError(await spawn('test', {stdio: [{string: true} as const, 'pipe', 'pipe'] as const} as const));
expectError(await spawn('test', {stdio: [{other: 'test'} as const, 'pipe', 'pipe'] as const} as const));
expectError(await spawn('test', {stdio: [true, true, true] as const} as const));
await spawn('test', {stdio: 'pipe'} as const);
expectError(await spawn('test', {stdio: true} as const));
expectError(await spawn('test', {other: 'test'} as const));

expectError(await nanoSpawn());
expectError(await nanoSpawn(true));
await nanoSpawn('test', [] as const);
await nanoSpawn('test', ['one'] as const);
expectError(await nanoSpawn('test', [true] as const));
await nanoSpawn('test', {} as const);
expectError(await nanoSpawn('test', true));
await nanoSpawn('test', ['one'] as const, {} as const);
expectError(await nanoSpawn('test', ['one'] as const, true));
expectError(await nanoSpawn('test', ['one'] as const, {} as const, true));
expectError(await spawn());
expectError(await spawn(true));
await spawn('test', [] as const);
await spawn('test', ['one'] as const);
expectError(await spawn('test', [true] as const));
await spawn('test', {} as const);
expectError(await spawn('test', true));
await spawn('test', ['one'] as const, {} as const);
expectError(await spawn('test', ['one'] as const, true));
expectError(await spawn('test', ['one'] as const, {} as const, true));

expectError(await nanoSpawn('test').pipe());
expectError(await nanoSpawn('test').pipe(true));
await nanoSpawn('test').pipe('test', [] as const);
await nanoSpawn('test').pipe('test', ['one'] as const);
expectError(await nanoSpawn('test').pipe('test', [true] as const));
await nanoSpawn('test').pipe('test', {} as const);
expectError(await nanoSpawn('test').pipe('test', true));
await nanoSpawn('test').pipe('test', ['one'] as const, {} as const);
expectError(await nanoSpawn('test').pipe('test', ['one'] as const, true));
expectError(await nanoSpawn('test').pipe('test', ['one'] as const, {} as const, true));
expectError(await spawn('test').pipe());
expectError(await spawn('test').pipe(true));
await spawn('test').pipe('test', [] as const);
await spawn('test').pipe('test', ['one'] as const);
expectError(await spawn('test').pipe('test', [true] as const));
await spawn('test').pipe('test', {} as const);
expectError(await spawn('test').pipe('test', true));
await spawn('test').pipe('test', ['one'] as const, {} as const);
expectError(await spawn('test').pipe('test', ['one'] as const, true));
expectError(await spawn('test').pipe('test', ['one'] as const, {} as const, true));

expectError(await nanoSpawn('test').pipe('test').pipe());
expectError(await nanoSpawn('test').pipe('test').pipe(true));
await nanoSpawn('test').pipe('test').pipe('test', [] as const);
await nanoSpawn('test').pipe('test').pipe('test', ['one'] as const);
expectError(await nanoSpawn('test').pipe('test').pipe('test', [true] as const));
await nanoSpawn('test').pipe('test').pipe('test', {} as const);
expectError(await nanoSpawn('test').pipe('test').pipe('test', true));
await nanoSpawn('test').pipe('test').pipe('test', ['one'] as const, {} as const);
expectError(await nanoSpawn('test').pipe('test').pipe('test', ['one'] as const, true));
expectError(await nanoSpawn('test').pipe('test').pipe('test', ['one'] as const, {} as const, true));
expectError(await spawn('test').pipe('test').pipe());
expectError(await spawn('test').pipe('test').pipe(true));
await spawn('test').pipe('test').pipe('test', [] as const);
await spawn('test').pipe('test').pipe('test', ['one'] as const);
expectError(await spawn('test').pipe('test').pipe('test', [true] as const));
await spawn('test').pipe('test').pipe('test', {} as const);
expectError(await spawn('test').pipe('test').pipe('test', true));
await spawn('test').pipe('test').pipe('test', ['one'] as const, {} as const);
expectError(await spawn('test').pipe('test').pipe('test', ['one'] as const, true));
expectError(await spawn('test').pipe('test').pipe('test', ['one'] as const, {} as const, true));

expectType<Subprocess>(nanoSpawn('test').pipe('test'));
expectType<Subprocess>(nanoSpawn('test').pipe('test').pipe('test'));
expectType<Result>(await nanoSpawn('test').pipe('test'));
expectType<Result>(await nanoSpawn('test').pipe('test').pipe('test'));
expectType<Subprocess>(spawn('test').pipe('test'));
expectType<Subprocess>(spawn('test').pipe('test').pipe('test'));
expectType<Result>(await spawn('test').pipe('test'));
expectType<Result>(await spawn('test').pipe('test').pipe('test'));

for await (const line of nanoSpawn('test')) {
for await (const line of spawn('test')) {
expectType<string>(line);
}

for await (const line of nanoSpawn('test').pipe('test')) {
for await (const line of spawn('test').pipe('test')) {
expectType<string>(line);
}

for await (const line of nanoSpawn('test').stdout) {
for await (const line of spawn('test').stdout) {
expectType<string>(line);
}

for await (const line of nanoSpawn('test').pipe('test').stdout) {
for await (const line of spawn('test').pipe('test').stdout) {
expectType<string>(line);
}

for await (const line of nanoSpawn('test').stderr) {
for await (const line of spawn('test').stderr) {
expectType<string>(line);
}

for await (const line of nanoSpawn('test').pipe('test').stderr) {
for await (const line of spawn('test').pipe('test').stderr) {
expectType<string>(line);
}

const subprocess = nanoSpawn('test');
const subprocess = spawn('test');
expectType<Subprocess>(subprocess);

const nodeChildProcess = await subprocess.nodeChildProcess;
Expand Down
2 changes: 1 addition & 1 deletion source/iterable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const lineIterator = async function * (subprocess, {state}, streamName) {
// This would defeat one of the main goals of iterating: low memory consumption.
if (state.isIterating === false) {
throw new Error(`The subprocess must be iterated right away, for example:
for await (const line of nanoSpawn(...)) { ... }`);
for await (const line of spawn(...)) { ... }`);
}

state.isIterating = true;
Expand Down
4 changes: 2 additions & 2 deletions source/pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ const pipeStreams = async subprocesses => {
try {
const [{stdout}, {stdin}] = await Promise.all(subprocesses.map(({nodeChildProcess}) => nodeChildProcess));
if (stdin === null) {
throw new Error('The "stdin" option must be set on the first "nanoSpawn()" call in the pipeline.');
throw new Error('The "stdin" option must be set on the first "spawn()" call in the pipeline.');
}

if (stdout === null) {
throw new Error('The "stdout" option must be set on the last "nanoSpawn()" call in the pipeline.');
throw new Error('The "stdout" option must be set on the last "spawn()" call in the pipeline.');
}

// Do not `await` nor handle stream errors since this is already done by each subprocess
Expand Down
10 changes: 5 additions & 5 deletions test/context.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import test from 'ava';
import {red} from 'yoctocolors';
import nanoSpawn from '../source/index.js';
import spawn from '../source/index.js';
import {testString} from './helpers/arguments.js';
import {assertDurationMs} from './helpers/assert.js';
import {nodePrint, nodePrintFail, nodePrintStdout} from './helpers/commands.js';

test('result.command does not quote normal arguments', async t => {
const {command} = await nanoSpawn('node', ['--version']);
const {command} = await spawn('node', ['--version']);
t.is(command, 'node --version');
});

const testCommandEscaping = async (t, input, expectedCommand) => {
const {command, stdout} = await nanoSpawn(...nodePrint(`"${input}"`));
const {command, stdout} = await spawn(...nodePrint(`"${input}"`));
t.is(command, `node -p '"${expectedCommand}"'`);
t.is(stdout, input);
};
Expand All @@ -22,11 +22,11 @@ test('result.command quotes unusual characters', testCommandEscaping, ',', ',');
test('result.command strips ANSI sequences', testCommandEscaping, red(testString), testString);

test('result.durationMs is set', async t => {
const {durationMs} = await nanoSpawn(...nodePrintStdout);
const {durationMs} = await spawn(...nodePrintStdout);
assertDurationMs(t, durationMs);
});

test('error.durationMs is set', async t => {
const {durationMs} = await t.throwsAsync(nanoSpawn(...nodePrintFail));
const {durationMs} = await t.throwsAsync(spawn(...nodePrintFail));
assertDurationMs(t, durationMs);
});
4 changes: 2 additions & 2 deletions test/fixtures/node-flags-path.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import process from 'node:process';
import nanoSpawn from '../../source/index.js';
import spawn from '../../source/index.js';

await nanoSpawn(process.execPath, ['-p', 'process.execArgv'], {stdout: 'inherit'});
await spawn(process.execPath, ['-p', 'process.execArgv'], {stdout: 'inherit'});
4 changes: 2 additions & 2 deletions test/fixtures/node-flags.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node
import nanoSpawn from '../../source/index.js';
import spawn from '../../source/index.js';

await nanoSpawn('node', ['-p', 'process.execArgv'], {stdout: 'inherit'});
await spawn('node', ['-p', 'process.execArgv'], {stdout: 'inherit'});
4 changes: 2 additions & 2 deletions test/fixtures/node-version.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node
import nanoSpawn from '../../source/index.js';
import spawn from '../../source/index.js';

await nanoSpawn('node', ['--version'], {stdout: 'inherit'});
await spawn('node', ['--version'], {stdout: 'inherit'});
12 changes: 6 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import test from 'ava';
import nanoSpawn from '../source/index.js';
import spawn from '../source/index.js';
import {assertSigterm} from './helpers/assert.js';
import {nodePrintStdout, nodeHanging, nodePrint} from './helpers/commands.js';

test('Can pass no arguments', async t => {
const error = await t.throwsAsync(nanoSpawn(...nodeHanging, {timeout: 1}));
const error = await t.throwsAsync(spawn(...nodeHanging, {timeout: 1}));
assertSigterm(t, error);
});

test('Can pass no arguments nor options', async t => {
const subprocess = nanoSpawn(...nodeHanging);
const subprocess = spawn(...nodeHanging);
const nodeChildProcess = await subprocess.nodeChildProcess;
nodeChildProcess.kill();
const error = await t.throwsAsync(subprocess);
assertSigterm(t, error);
});

test('Returns a promise', async t => {
const subprocess = nanoSpawn(...nodePrintStdout);
const subprocess = spawn(...nodePrintStdout);
t.false(Object.prototype.propertyIsEnumerable.call(subprocess, 'then'));
t.false(Object.hasOwn(subprocess, 'then'));
t.true(subprocess instanceof Promise);
await subprocess;
});

test('subprocess.nodeChildProcess is set', async t => {
const subprocess = nanoSpawn(...nodePrintStdout);
const subprocess = spawn(...nodePrintStdout);
const nodeChildProcess = await subprocess.nodeChildProcess;
t.true(Number.isInteger(nodeChildProcess.pid));
await subprocess;
Expand All @@ -35,6 +35,6 @@ const PARALLEL_COUNT = 100;

test.serial('Can run many times at once', async t => {
const inputs = Array.from({length: PARALLEL_COUNT}, (_, index) => `${index}`);
const results = await Promise.all(inputs.map(input => nanoSpawn(...nodePrint(input))));
const results = await Promise.all(inputs.map(input => spawn(...nodePrint(input))));
t.deepEqual(results.map(({output}) => output), inputs);
});
Loading

0 comments on commit e673b94

Please sign in to comment.