-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 62b1082
Showing
13 changed files
with
310 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = tab | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.yml] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: CI | ||
on: | ||
- push | ||
- pull_request | ||
jobs: | ||
test: | ||
name: Node.js ${{ matrix.node-version }} | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
node-version: | ||
- 22 | ||
- 20 | ||
- 18 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- run: npm install | ||
- run: npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export type Options = { | ||
readonly timeout: number; | ||
readonly signal: AbortSignal; | ||
// Readonly nativeOptions; | ||
}; | ||
|
||
// TODO: Finish this when the API is decided on. | ||
export function nanoSpawn( | ||
command: string, | ||
arguments: readonly string[], | ||
options?: Options | ||
): Promise<void>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {spawn} from 'node:child_process'; | ||
import {lineIterator, combineAsyncIterables} from './utilities.js'; | ||
|
||
export default function nanoSpawn(command, arguments_ = [], {signal, timeout, nativeOptions} = {}) { | ||
const subprocess = spawn(command, arguments_, {...nativeOptions, signal, timeout}); | ||
|
||
// eslint-disable-next-line no-async-promise-executor | ||
const promise = new Promise(async (resolve, reject) => { | ||
try { | ||
subprocess.on('close', exitCode => { | ||
// TODO: Pass in `stdin` and `stdout` strings here. | ||
resolve({ | ||
exitCode, | ||
}); | ||
}); | ||
|
||
subprocess.on('error', error => { | ||
reject(error); | ||
}); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
|
||
const stdoutLines = lineIterator(subprocess.stdout); | ||
const stderrLines = lineIterator(subprocess.stderr); | ||
|
||
return { | ||
[Symbol.asyncIterator]: () => combineAsyncIterables(stdoutLines, stderrLines), | ||
stdout: stdoutLines, | ||
stderr: stderrLines, | ||
/* eslint-disable promise/prefer-await-to-then */ | ||
then(onFulfilled, onRejected) { // eslint-disable-line unicorn/no-thenable | ||
return promise.then(onFulfilled, onRejected); | ||
}, | ||
catch(onRejected) { | ||
return promise.catch(onRejected); | ||
}, | ||
finally(onFinally) { | ||
return promise.finally(onFinally); | ||
}, | ||
/* eslint-enable promise/prefer-await-to-then */ | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
MIT License | ||
|
||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"name": "nano-spawn", | ||
"version": "0.0.0", | ||
"description": "Tiny process execution for humans — a better child_process", | ||
"license": "MIT", | ||
"repository": "sindresorhus/nano-spawn", | ||
"funding": "https://github.com/sindresorhus/nano-spawn?sponsor=1", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": { | ||
"types": "./index.d.ts", | ||
"default": "./index.js" | ||
}, | ||
"sideEffects": false, | ||
"engines": { | ||
"node": ">=18.19" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsc index.d.ts" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts", | ||
"utilities.js" | ||
], | ||
"keywords": [ | ||
"spawn", | ||
"exec", | ||
"child", | ||
"process", | ||
"subprocess", | ||
"execute", | ||
"fork", | ||
"execfile", | ||
"file", | ||
"shell", | ||
"bin", | ||
"binary", | ||
"binaries", | ||
"npm", | ||
"path", | ||
"local", | ||
"zx", | ||
"execa" | ||
], | ||
"devDependencies": { | ||
"ava": "^6.1.3", | ||
"typescript": "^5.5.4", | ||
"xo": "^0.59.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<h1 align="center" title="nano-spawn"> | ||
<img src="media/logo.jpg" alt="nano-spawn logo"> | ||
</h1> | ||
|
||
> Tiny process execution for humans — a better [`child_process`](https://nodejs.org/api/child_process.html) | ||
> [!WARNING] | ||
> This package is still a work in progress. | ||
Check out [`execa`](https://github.com/sindresorhus/execa) for more features. | ||
|
||
## Features | ||
|
||
- Outputs combined result of stdout and stderr, similar to what you get in terminals | ||
- Outputs lines | ||
- No dependencies | ||
|
||
## Install | ||
|
||
```sh | ||
npm install nano-spawn | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
import $ from 'nano-spawn'; | ||
|
||
const result = await $('echo', ['🦄']); | ||
|
||
console.log(result.exitCode); | ||
//=> 0 | ||
``` | ||
|
||
```js | ||
import $ from 'nano-spawn'; | ||
|
||
for await (const line of $('ls', ['--oneline'])) { | ||
console.log(line); | ||
} | ||
//=> index.d.ts | ||
//=> index.js | ||
//=> … | ||
``` | ||
|
||
## API | ||
|
||
See the [types](index.d.ts) for now. | ||
|
||
## Limitations | ||
|
||
- It does not handle binary output. Use [`execa`](https://github.com/sindresorhus/execa) for that. | ||
|
||
## Related | ||
|
||
- [execa](https://github.com/sindresorhus/execa) - Process execution for humans | ||
- [unicorn-magic](https://github.com/sindresorhus/unicorn-magic/blob/6614e1e82a19f41d7cc8f04df7c90a4dfe781741/node.d.ts#L77-L125) - Slightly improved `child_process#execFile` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import test from 'ava'; | ||
import nanoSpawn from './index.js'; | ||
|
||
test('can be awaited', async t => { | ||
const result = await nanoSpawn('echo', ['🦄']); | ||
// TODO | ||
// t.is(result.stdout, '🦄'); | ||
t.is(result.exitCode, 0); | ||
}); | ||
|
||
test('stdout produces correct output', async t => { | ||
const result = nanoSpawn('echo', ['Hello\nWorld']); | ||
|
||
const lines = []; | ||
for await (const chunk of result.stdout) { | ||
lines.push(chunk.toString()); | ||
} | ||
|
||
t.deepEqual(lines, ['Hello', 'World']); | ||
}); | ||
|
||
test('stderr produces correct output', async t => { | ||
const result = nanoSpawn('ls', ['non-existent-file']); | ||
|
||
const lines = []; | ||
for await (const line of result.stderr) { | ||
lines.push(line); | ||
} | ||
|
||
t.is(lines.length, 1); | ||
t.regex(lines[0], /No such file/); | ||
}); | ||
|
||
test('combines stdout and stderr correctly', async t => { | ||
const result = nanoSpawn('bash', ['-c', 'echo "stdout\nstdout2"; echo "stderr\nstderr2" 1>&2']); | ||
|
||
const lines = []; | ||
for await (const line of result) { | ||
lines.push(line); | ||
} | ||
|
||
t.deepEqual(lines, ['stdout', 'stderr', 'stdout2', 'stderr2']); | ||
}); | ||
|
||
test('rejects on error', async t => { | ||
await t.throwsAsync( | ||
nanoSpawn('non-existent-command'), | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
export function streamToIterable(stream) { | ||
return { | ||
[Symbol.asyncIterator]: stream[Symbol.asyncIterator].bind(stream), | ||
}; | ||
} | ||
|
||
export async function * combineAsyncIterables(iterable1, iterable2) { | ||
const iterator1 = iterable1[Symbol.asyncIterator](); | ||
const iterator2 = iterable2[Symbol.asyncIterator](); | ||
|
||
while (true) { | ||
// eslint-disable-next-line no-await-in-loop | ||
const [result1, result2] = await Promise.all([ | ||
iterator1.next(), | ||
iterator2.next(), | ||
]); | ||
|
||
if (result1.done && result2.done) { | ||
break; | ||
} | ||
|
||
if (!result1.done) { | ||
yield result1.value; | ||
} | ||
|
||
if (!result2.done) { | ||
yield result2.value; | ||
} | ||
} | ||
} | ||
|
||
export async function * lineIterator(iterable) { | ||
let buffer = ''; | ||
for await (const chunk of iterable) { | ||
buffer += chunk; | ||
const lines = buffer.split('\n'); | ||
buffer = lines.pop(); // Keep last line in buffer as it may not be complete | ||
for (const line of lines) { | ||
yield line; | ||
} | ||
} | ||
|
||
if (buffer) { | ||
yield buffer; // Yield any remaining data as the last line | ||
} | ||
} |