Skip to content
Myr

Allow appending to files (#1166)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 091ed74 · Browse files · 10 files+160 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

10 files+160 added−6 removed

  • tests:+36 added−0 removed
  • other:+124 added−6 removed

10 files+160 added−6 removed

modified:docs/bash.md+21 added
603 unmodified lines
604```605606[More info.](output.md#file-output)607608### Append stdout to a file609610```sh611# Bash612npm run build >> output.txt613```614615```js616// zx617import {createWriteStream} from 'node:fs';618619await $`npm run build`.pipe(createWriteStream('output.txt', {flags: 'a'}));620```621622```js623// Execa624await $({stdout: {file: 'output.txt', append: true}})`npm run build`;625```626627[More info.](output.md#file-output)628629### Piping interleaved stdout and stderr to a file630631```sh632# Bash
663 unmodified lines
modified:docs/output.md+7 added
30 unmodified lines
31```js32await execa({stdout: {file: 'output.txt'}})`npm run build`;33// Or:34await execa({stdout: new URL('file:///path/to/output.txt')})`npm run build`;35```3637```js38// Redirect interleaved stdout and stderr to same file39const output = {file: 'output.txt'};40await execa({stdout: output, stderr: output})`npm run build`;41```4243```js44// Append instead of overwriting45await execa({stdout: {file: 'output.txt', append: true}})`npm run build`;46```4748## Terminal output4950The parent process' output can be re-used in the subprocess by passing `'inherit'`. This is especially useful to print to the terminal in command line applications.51
154 unmodified lines
modified:lib/io/output-sync.js+2 added-2 removed
modified:lib/stdio/handle-async.js+1 added-1 removed
modified:lib/stdio/handle-sync.js+1 added-1 removed
46 unmodified lines
47 },48 output: {49 ...addProperties,50 fileUrl: ({value}) => ({path: value}),51 filePath: ({value: {file}}) => ({path: file}),51 filePath: ({value: {file, append}}) => ({path: file, append}),52 fileNumber: ({value}) => ({path: value}),53 iterable: forbiddenIfSync,54 string: forbiddenIfSync,55 uint8Array: forbiddenIfSync,
2 unmodified lines
modified:lib/stdio/type.js+3 added-1 removed
122 unmodified lines
123export const isUrl = value => Object.prototype.toString.call(value) === '[object URL]';124export const isRegularUrl = value => isUrl(value) && value.protocol !== 'file:';125126const isFilePathObject = value => isPlainObj(value)127 && Object.keys(value).length === 1127 && Object.keys(value).length > 0128 && Object.keys(value).every(key => FILE_PATH_KEYS.has(key))129 && isFilePathString(value.file);130const FILE_PATH_KEYS = new Set(['file', 'append']);131export const isFilePathString = file => typeof file === 'string';132133export const isUnknownStdioString = (type, value) => type === 'native'134 && typeof value === 'string'
39 unmodified lines
modified:test/stdio/file-path-main.js+36 added
156 unmodified lines
157};158159test('Passing an input file path when subprocess exits does not make promise hang', testInputFileHanging, getAbsolutePath);160test('Passing an input file URL when subprocess exits does not make promise hang', testInputFileHanging, pathToFileURL);161162const testOverwriteFile = async (t, fdNumber, execaMethod, append) => {163 const filePath = tempfile();164 await writeFile(filePath, '.');165 await execaMethod('noop-fd.js', [`${fdNumber}`, foobarString], getStdio(fdNumber, {file: filePath, append}));166 t.is(await readFile(filePath, 'utf8'), foobarString);167 await rm(filePath);168};169170test('Overwrite by default to stdout', testOverwriteFile, 1, execa, undefined);171test('Overwrite by default to stderr', testOverwriteFile, 2, execa, undefined);172test('Overwrite by default to stdio[*]', testOverwriteFile, 3, execa, undefined);173test('Overwrite by default to stdout - sync', testOverwriteFile, 1, execaSync, undefined);174test('Overwrite by default to stderr - sync', testOverwriteFile, 2, execaSync, undefined);175test('Overwrite by default to stdio[*] - sync', testOverwriteFile, 3, execaSync, undefined);176test('Overwrite with append false to stdout', testOverwriteFile, 1, execa, false);177test('Overwrite with append false to stderr', testOverwriteFile, 2, execa, false);178test('Overwrite with append false to stdio[*]', testOverwriteFile, 3, execa, false);179test('Overwrite with append false to stdout - sync', testOverwriteFile, 1, execaSync, false);180test('Overwrite with append false to stderr - sync', testOverwriteFile, 2, execaSync, false);181test('Overwrite with append false to stdio[*] - sync', testOverwriteFile, 3, execaSync, false);182183const testAppendFile = async (t, fdNumber, execaMethod) => {184 const filePath = tempfile();185 await writeFile(filePath, '.');186 await execaMethod('noop-fd.js', [`${fdNumber}`, foobarString], getStdio(fdNumber, {file: filePath, append: true}));187 t.is(await readFile(filePath, 'utf8'), `.${foobarString}`);188 await rm(filePath);189};190191test('Can append to stdout', testAppendFile, 1, execa);192test('Can append to stderr', testAppendFile, 2, execa);193test('Can append to stdio[*]', testAppendFile, 3, execa);194test('Can append to stdout - sync', testAppendFile, 1, execaSync);195test('Can append to stderr - sync', testAppendFile, 2, execaSync);196test('Can append to stdio[*] - sync', testAppendFile, 3, execaSync);
added:test-d/stdio/option/file-append-invalid.test-d.ts+44 added
1import {expectError, expectNotAssignable} from 'tsd';2import {3 execa,4 execaSync,5 type StdinOption,6 type StdinSyncOption,7 type StdoutStderrOption,8 type StdoutStderrSyncOption,9} from '../../../index.js';1011const invalidFileAppend = {file: './test', append: 'true'} as const;1213expectError(await execa('unicorns', {stdin: invalidFileAppend}));14expectError(execaSync('unicorns', {stdin: invalidFileAppend}));15expectError(await execa('unicorns', {stdin: [invalidFileAppend]}));16expectError(execaSync('unicorns', {stdin: [invalidFileAppend]}));1718expectError(await execa('unicorns', {stdout: invalidFileAppend}));19expectError(execaSync('unicorns', {stdout: invalidFileAppend}));20expectError(await execa('unicorns', {stdout: [invalidFileAppend]}));21expectError(execaSync('unicorns', {stdout: [invalidFileAppend]}));2223expectError(await execa('unicorns', {stderr: invalidFileAppend}));24expectError(execaSync('unicorns', {stderr: invalidFileAppend}));25expectError(await execa('unicorns', {stderr: [invalidFileAppend]}));26expectError(execaSync('unicorns', {stderr: [invalidFileAppend]}));2728expectError(await execa('unicorns', {stdio: invalidFileAppend}));29expectError(execaSync('unicorns', {stdio: invalidFileAppend}));3031expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', invalidFileAppend]}));32expectError(execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', invalidFileAppend]}));33expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [invalidFileAppend]]}));34expectError(execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [invalidFileAppend]]}));3536expectNotAssignable<StdinOption>(invalidFileAppend);37expectNotAssignable<StdinSyncOption>(invalidFileAppend);38expectNotAssignable<StdinOption>([invalidFileAppend]);39expectNotAssignable<StdinSyncOption>([invalidFileAppend]);4041expectNotAssignable<StdoutStderrOption>(invalidFileAppend);42expectNotAssignable<StdoutStderrSyncOption>(invalidFileAppend);43expectNotAssignable<StdoutStderrOption>([invalidFileAppend]);44expectNotAssignable<StdoutStderrSyncOption>([invalidFileAppend]);
added:test-d/stdio/option/file-append.test-d.ts+44 added
1import {expectError, expectAssignable} from 'tsd';2import {3 execa,4 execaSync,5 type StdinOption,6 type StdinSyncOption,7 type StdoutStderrOption,8 type StdoutStderrSyncOption,9} from '../../../index.js';1011const fileAppend = {file: './test', append: true} as const;1213await execa('unicorns', {stdin: fileAppend});14execaSync('unicorns', {stdin: fileAppend});15await execa('unicorns', {stdin: [fileAppend]});16execaSync('unicorns', {stdin: [fileAppend]});1718await execa('unicorns', {stdout: fileAppend});19execaSync('unicorns', {stdout: fileAppend});20await execa('unicorns', {stdout: [fileAppend]});21execaSync('unicorns', {stdout: [fileAppend]});2223await execa('unicorns', {stderr: fileAppend});24execaSync('unicorns', {stderr: fileAppend});25await execa('unicorns', {stderr: [fileAppend]});26execaSync('unicorns', {stderr: [fileAppend]});2728expectError(await execa('unicorns', {stdio: fileAppend}));29expectError(execaSync('unicorns', {stdio: fileAppend}));3031await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', fileAppend]});32execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', fileAppend]});33await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [fileAppend]]});34execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [fileAppend]]});3536expectAssignable<StdinOption>(fileAppend);37expectAssignable<StdinSyncOption>(fileAppend);38expectAssignable<StdinOption>([fileAppend]);39expectAssignable<StdinSyncOption>([fileAppend]);4041expectAssignable<StdoutStderrOption>(fileAppend);42expectAssignable<StdoutStderrSyncOption>(fileAppend);43expectAssignable<StdoutStderrOption>([fileAppend]);44expectAssignable<StdoutStderrSyncOption>([fileAppend]);
modified:types/stdio/type.d.ts+1 added-1 removed
47 unmodified lines
48 IsArray extends boolean,49> =50 | SimpleStdioOption<IsSync, IsExtra, IsArray>51 | URL52 | {readonly file: string}52 | {readonly file: string; readonly append?: boolean}53 | GeneratorTransform<IsSync>54 | GeneratorTransformFull<IsSync>55 | Unless<And<Not<IsSync>, IsArray>, 3 | 4 | 5 | 6 | 7 | 8 | 9>56 | Unless<Or<IsSync, IsArray>, 'ipc'>
114 unmodified lines