Merge pull request #8272 from timja/add-options

Add options to spawn in runCommand
This commit is contained in:
Patrik Oldsberg
2021-11-30 23:38:25 +01:00
committed by GitHub
3 changed files with 19 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': patch
---
Add options to spawn in runCommand helper
+3 -1
View File
@@ -24,6 +24,7 @@ import { PluginDatabaseManager } from '@backstage/backend-common';
import { Schema } from 'jsonschema';
import { ScmIntegrationRegistry } from '@backstage/integration';
import { ScmIntegrations } from '@backstage/integration';
import { SpawnOptionsWithoutStdio } from 'child_process';
import { TemplateEntityV1beta2 } from '@backstage/catalog-model';
import { UrlReader } from '@backstage/backend-common';
import { Writable } from 'stream';
@@ -304,11 +305,12 @@ export interface RouterOptions {
// Warning: (ae-forgotten-export) The symbol "RunCommandOptions" needs to be exported by the entry point index.d.ts
// Warning: (ae-missing-release-tag) "runCommand" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
// @public
export const runCommand: ({
command,
args,
logStream,
options,
}: RunCommandOptions) => Promise<void>;
// @public (undocumented)
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { spawn } from 'child_process';
import { SpawnOptionsWithoutStdio, spawn } from 'child_process';
import { PassThrough, Writable } from 'stream';
import { Logger } from 'winston';
import { Git } from '@backstage/backend-common';
@@ -22,18 +22,27 @@ import { Octokit } from '@octokit/rest';
import { assertError } from '@backstage/errors';
export type RunCommandOptions = {
/** command to run */
command: string;
/** arguments to pass the command */
args: string[];
/** options to pass to spawn */
options?: SpawnOptionsWithoutStdio;
/** stream to capture stdout and stderr output */
logStream?: Writable;
};
/**
* Run a command in a sub-process, normally a shell command.
*/
export const runCommand = async ({
command,
args,
logStream = new PassThrough(),
options,
}: RunCommandOptions) => {
await new Promise<void>((resolve, reject) => {
const process = spawn(command, args);
const process = spawn(command, args, options);
process.stdout.on('data', stream => {
logStream.write(stream);