-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use "spawnAsync" to stream publish script
- Loading branch information
1 parent
b11b9cf
commit dd48334
Showing
5 changed files
with
94 additions
and
25 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
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
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
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,59 @@ | ||
import { ChildProcess, SpawnOptions, spawn } from 'node:child_process' | ||
import { format } from 'outvariant' | ||
import { DeferredPromise } from '@open-draft/deferred-promise' | ||
|
||
type SpawnAsyncFunction = { | ||
(command: string, options?: SpawnOptions): [ | ||
ChildProcess, | ||
DeferredPromise<void>, | ||
] | ||
|
||
contextOptions: SpawnOptions | ||
mockContext(options: SpawnOptions): void | ||
restoreContext(): void | ||
} | ||
|
||
const DEFAULT_SPAWN_CONTEXT: Partial<SpawnOptions> = { | ||
cwd: process.cwd(), | ||
} | ||
|
||
/** | ||
* Spawns the given `command` in a new process and gives that | ||
* child process' reference as well as the command exit promise. | ||
*/ | ||
export const spawnAsync = <SpawnAsyncFunction>((command, options) => { | ||
const commandPromise = new DeferredPromise<void>() | ||
const [commandName, ...args] = command.split(' ') | ||
const io = spawn(commandName, args, { | ||
...spawnAsync.contextOptions, | ||
...options, | ||
}) | ||
|
||
io.once('exit', (exitCode) => { | ||
if (exitCode !== 0) { | ||
return commandPromise.reject( | ||
new Error( | ||
format( | ||
'Running "%s" failed: process exited with code %d', | ||
command, | ||
exitCode, | ||
), | ||
), | ||
) | ||
} | ||
|
||
commandPromise.resolve() | ||
}) | ||
|
||
return [io, commandPromise] | ||
}) | ||
|
||
spawnAsync.mockContext = (options) => { | ||
spawnAsync.contextOptions = options | ||
} | ||
|
||
spawnAsync.restoreContext = () => { | ||
spawnAsync.contextOptions = DEFAULT_SPAWN_CONTEXT | ||
} | ||
|
||
spawnAsync.restoreContext() |
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