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
Provenance
- Commit
94a75df- Tree
87be621- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
091ed74
10 files changed+160 added−6 removed
modified:docs/bash.md+21 added
603 unmodified lines604 ```605 606 [More info.](output.md#file-output)607 608+### Append stdout to a file609+610+```sh611+# Bash612+npm run build >> output.txt613+```614+615+```js616+// zx617+import {createWriteStream} from 'node:fs';618+619+await $`npm run build`.pipe(createWriteStream('output.txt', {flags: 'a'}));620+```621+622+```js623+// Execa624+await $({stdout: {file: 'output.txt', append: true}})`npm run build`;625+```626+627+[More info.](output.md#file-output)628+629 ### Piping interleaved stdout and stderr to a file630 631 ```sh632 # Bash663 unmodified linesmodified:docs/output.md+7 added
30 unmodified lines31 ```js32 await execa({stdout: {file: 'output.txt'}})`npm run build`;33 // Or:34 await execa({stdout: new URL('file:///path/to/output.txt')})`npm run build`;35+```36 37+```js38 // Redirect interleaved stdout and stderr to same file39 const output = {file: 'output.txt'};40 await execa({stdout: output, stderr: output})`npm run build`;41 ```42 43+```js44+// Append instead of overwriting45+await execa({stdout: {file: 'output.txt', append: true}})`npm run build`;46+```47+48 ## Terminal output49 50 The 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 linesmodified:lib/io/output-sync.js+2 added-2 removed
121 unmodified linesExpand all1221231241251261261271281281291301311323 unmodified lines121 unmodified linesExpand all};
// When the `std*` target is a file path/URL or a file descriptorconst writeToFiles = (serializedResult, stdioItems, outputFiles) => { for (const {path} of stdioItems.filter(({type}) => FILE_TYPES.has(type))) { for (const {path, append} of stdioItems.filter(({type}) => FILE_TYPES.has(type))) { const pathString = typeof path === 'string' ? path : path.toString(); if (outputFiles.has(pathString)) { if (append || outputFiles.has(pathString)) { appendFileSync(path, serializedResult); } else { outputFiles.add(pathString); writeFileSync(path, serializedResult);3 unmodified linesmodified:lib/stdio/handle-async.js+1 added-1 removed
40 unmodified lines414243444545464748493 unmodified lines40 unmodified lines }, output: { ...addProperties, fileUrl: ({value}) => ({stream: createWriteStream(value)}), filePath: ({value: {file}}) => ({stream: createWriteStream(file)}), filePath: ({value: {file, append}}) => ({stream: createWriteStream(file, append ? {flags: 'a'} : {})}), webStream: ({value}) => ({stream: Writable.fromWeb(value)}), iterable: forbiddenIfAsync, asyncIterable: forbiddenIfAsync, string: forbiddenIfAsync,3 unmodified linesmodified:lib/stdio/handle-sync.js+1 added-1 removed
46 unmodified lines47 },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 linesmodified:lib/stdio/type.js+3 added-1 removed
122 unmodified lines123 export const isUrl = value => Object.prototype.toString.call(value) === '[object URL]';124 export const isRegularUrl = value => isUrl(value) && value.protocol !== 'file:';125 126 const 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);130+const FILE_PATH_KEYS = new Set(['file', 'append']);131 export const isFilePathString = file => typeof file === 'string';132 133 export const isUnknownStdioString = (type, value) => type === 'native'134 && typeof value === 'string'39 unmodified linesmodified:test/stdio/file-path-main.js+36 added
156 unmodified lines157 };158 159 test('Passing an input file path when subprocess exits does not make promise hang', testInputFileHanging, getAbsolutePath);160 test('Passing an input file URL when subprocess exits does not make promise hang', testInputFileHanging, pathToFileURL);161+162+const 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+};169+170+test('Overwrite by default to stdout', testOverwriteFile, 1, execa, undefined);171+test('Overwrite by default to stderr', testOverwriteFile, 2, execa, undefined);172+test('Overwrite by default to stdio[*]', testOverwriteFile, 3, execa, undefined);173+test('Overwrite by default to stdout - sync', testOverwriteFile, 1, execaSync, undefined);174+test('Overwrite by default to stderr - sync', testOverwriteFile, 2, execaSync, undefined);175+test('Overwrite by default to stdio[*] - sync', testOverwriteFile, 3, execaSync, undefined);176+test('Overwrite with append false to stdout', testOverwriteFile, 1, execa, false);177+test('Overwrite with append false to stderr', testOverwriteFile, 2, execa, false);178+test('Overwrite with append false to stdio[*]', testOverwriteFile, 3, execa, false);179+test('Overwrite with append false to stdout - sync', testOverwriteFile, 1, execaSync, false);180+test('Overwrite with append false to stderr - sync', testOverwriteFile, 2, execaSync, false);181+test('Overwrite with append false to stdio[*] - sync', testOverwriteFile, 3, execaSync, false);182+183+const 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+};190+191+test('Can append to stdout', testAppendFile, 1, execa);192+test('Can append to stderr', testAppendFile, 2, execa);193+test('Can append to stdio[*]', testAppendFile, 3, execa);194+test('Can append to stdout - sync', testAppendFile, 1, execaSync);195+test('Can append to stderr - sync', testAppendFile, 2, execaSync);196+test('Can append to stdio[*] - sync', testAppendFile, 3, execaSync);added:test-d/stdio/option/file-append-invalid.test-d.ts+44 added
1+import {expectError, expectNotAssignable} from 'tsd';2+import {3+ execa,4+ execaSync,5+ type StdinOption,6+ type StdinSyncOption,7+ type StdoutStderrOption,8+ type StdoutStderrSyncOption,9+} from '../../../index.js';10+11+const invalidFileAppend = {file: './test', append: 'true'} as const;12+13+expectError(await execa('unicorns', {stdin: invalidFileAppend}));14+expectError(execaSync('unicorns', {stdin: invalidFileAppend}));15+expectError(await execa('unicorns', {stdin: [invalidFileAppend]}));16+expectError(execaSync('unicorns', {stdin: [invalidFileAppend]}));17+18+expectError(await execa('unicorns', {stdout: invalidFileAppend}));19+expectError(execaSync('unicorns', {stdout: invalidFileAppend}));20+expectError(await execa('unicorns', {stdout: [invalidFileAppend]}));21+expectError(execaSync('unicorns', {stdout: [invalidFileAppend]}));22+23+expectError(await execa('unicorns', {stderr: invalidFileAppend}));24+expectError(execaSync('unicorns', {stderr: invalidFileAppend}));25+expectError(await execa('unicorns', {stderr: [invalidFileAppend]}));26+expectError(execaSync('unicorns', {stderr: [invalidFileAppend]}));27+28+expectError(await execa('unicorns', {stdio: invalidFileAppend}));29+expectError(execaSync('unicorns', {stdio: invalidFileAppend}));30+31+expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', invalidFileAppend]}));32+expectError(execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', invalidFileAppend]}));33+expectError(await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [invalidFileAppend]]}));34+expectError(execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [invalidFileAppend]]}));35+36+expectNotAssignable<StdinOption>(invalidFileAppend);37+expectNotAssignable<StdinSyncOption>(invalidFileAppend);38+expectNotAssignable<StdinOption>([invalidFileAppend]);39+expectNotAssignable<StdinSyncOption>([invalidFileAppend]);40+41+expectNotAssignable<StdoutStderrOption>(invalidFileAppend);42+expectNotAssignable<StdoutStderrSyncOption>(invalidFileAppend);43+expectNotAssignable<StdoutStderrOption>([invalidFileAppend]);44+expectNotAssignable<StdoutStderrSyncOption>([invalidFileAppend]);added:test-d/stdio/option/file-append.test-d.ts+44 added
1+import {expectError, expectAssignable} from 'tsd';2+import {3+ execa,4+ execaSync,5+ type StdinOption,6+ type StdinSyncOption,7+ type StdoutStderrOption,8+ type StdoutStderrSyncOption,9+} from '../../../index.js';10+11+const fileAppend = {file: './test', append: true} as const;12+13+await execa('unicorns', {stdin: fileAppend});14+execaSync('unicorns', {stdin: fileAppend});15+await execa('unicorns', {stdin: [fileAppend]});16+execaSync('unicorns', {stdin: [fileAppend]});17+18+await execa('unicorns', {stdout: fileAppend});19+execaSync('unicorns', {stdout: fileAppend});20+await execa('unicorns', {stdout: [fileAppend]});21+execaSync('unicorns', {stdout: [fileAppend]});22+23+await execa('unicorns', {stderr: fileAppend});24+execaSync('unicorns', {stderr: fileAppend});25+await execa('unicorns', {stderr: [fileAppend]});26+execaSync('unicorns', {stderr: [fileAppend]});27+28+expectError(await execa('unicorns', {stdio: fileAppend}));29+expectError(execaSync('unicorns', {stdio: fileAppend}));30+31+await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', fileAppend]});32+execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', fileAppend]});33+await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [fileAppend]]});34+execaSync('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [fileAppend]]});35+36+expectAssignable<StdinOption>(fileAppend);37+expectAssignable<StdinSyncOption>(fileAppend);38+expectAssignable<StdinOption>([fileAppend]);39+expectAssignable<StdinSyncOption>([fileAppend]);40+41+expectAssignable<StdoutStderrOption>(fileAppend);42+expectAssignable<StdoutStderrSyncOption>(fileAppend);43+expectAssignable<StdoutStderrOption>([fileAppend]);44+expectAssignable<StdoutStderrSyncOption>([fileAppend]);modified:types/stdio/type.d.ts+1 added-1 removed
47 unmodified lines48 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