Skip to content

Commit

Permalink
extract into file
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgo committed Aug 17, 2020
1 parent 499604f commit 6c732eb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 35 deletions.
33 changes: 33 additions & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import stringArgv from "string-argv"
import { RunningProcess } from "./running-process"

/** The options that can be provided to Spawn */
export interface SpawnOptions {
cwd?: string
env?: NodeJS.ProcessEnv
}

/** starts a new ObservableProcess with the given options */
export function run(command: string | string[], args: SpawnOptions = {}) {
// determine args
if (!command) {
throw new Error("run: no command to execute given")
}
let argv: string[] = []
if (typeof command === "string") {
argv = stringArgv(command)
} else if (Array.isArray(command)) {
argv = command
} else {
throw new Error("run: you must provide the command to run as a string or string[]")
}
const [runnable, ...params] = argv

// start the process
return new RunningProcess({
cwd: args.cwd || process.cwd(),
env: args.env || process.env,
params,
runnable,
})
}
32 changes: 0 additions & 32 deletions src/observable-process.ts → src/running-process.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,10 @@
import * as childProcess from "child_process"
import mergeStream from "merge-stream"
import stringArgv from "string-argv"
import { createSearchableStream, SearchableStream } from "./searchable-stream"
import util from "util"
import { Result } from "./result"
const delay = util.promisify(setTimeout)

/** The options that can be provided to Spawn */
export interface SpawnOptions {
cwd?: string
env?: NodeJS.ProcessEnv
}

/** starts a new ObservableProcess with the given options */
export function run(command: string | string[], args: SpawnOptions = {}) {
// determine args
if (!command) {
throw new Error("run: no command to execute given")
}
let argv: string[] = []
if (typeof command === "string") {
argv = stringArgv(command)
} else if (Array.isArray(command)) {
argv = command
} else {
throw new Error("run: you must provide the command to run as a string or string[]")
}
const [runnable, ...params] = argv

// start the process
return new RunningProcess({
cwd: args.cwd || process.cwd(),
env: args.env || process.env,
params,
runnable,
})
}

/** a long-running process whose behavior can be observed at runtime */
export class RunningProcess {
/** the underlying ChildProcess instance */
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/start-node-process.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { run, RunningProcess } from "../../src/observable-process"
import { RunningProcess } from "../../src/running-process"
import { run } from "../../src/run"

export function startNodeProcess(code: string): RunningProcess {
return run(["node", "-e", code])
Expand Down
2 changes: 1 addition & 1 deletion test/start-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { strict as assert } from "assert"
import { run } from "../src/observable-process"
import { run } from "../src/run"

suite("ObservableProcess.spawn()")

Expand Down
2 changes: 1 addition & 1 deletion test/wait-for-end-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { strict as assert } from "assert"
import { run } from "../src/observable-process"
import { run } from "../src/run"
import util from "util"
const delay = util.promisify(setTimeout)

Expand Down

0 comments on commit 6c732eb

Please sign in to comment.