Skip to content

Commit

Permalink
run
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgo committed Aug 17, 2020
1 parent 894a37a commit 499604f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,40 @@ $ npm install observable-process
Load this library into your JavaScript code:

```js
const { createObservableProcess } = require("observable-process")
const { run } = require("observable-process")
```

– or –

```ts
import { createObservableProcess } from "observable-process"
import { run } from "observable-process"
```

## Starting processes

The best way to provide the command to run is in the form of an argv array:

```js
const observable = createObservableProcess(["node", "server.js"])
const observable = run(["node", "server.js"])
```

You can also provide the full command line to run as a string:

```js
const observable = createObservableProcess("node server.js")
const observable = run("node server.js")
```

By default, the process runs in the current directory. To set the different
working directory for the subprocess:

```js
const observable = createObservableProcess("node server.js", { cwd: "~/tmp" })
const observable = run("node server.js", { cwd: "~/tmp" })
```

You can provide custom environment variables for the process:

```js
const observable = createObservableProcess("node server.js", {
const observable = run("node server.js", {
env: {
foo: "bar",
PATH: process.env.PATH,
Expand Down
6 changes: 3 additions & 3 deletions src/observable-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ export interface SpawnOptions {
}

/** starts a new ObservableProcess with the given options */
export function createObservableProcess(command: string | string[], args: SpawnOptions = {}) {
export function run(command: string | string[], args: SpawnOptions = {}) {
// determine args
if (!command) {
throw new Error("createObservableProcess: no command to execute given")
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("createObservableProcess: you must provide the command to run as a string or string[]")
throw new Error("run: you must provide the command to run as a string or string[]")
}
const [runnable, ...params] = argv

Expand Down
4 changes: 2 additions & 2 deletions test/helpers/start-node-process.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createObservableProcess, RunningProcess } from "../../src/observable-process"
import { run, RunningProcess } from "../../src/observable-process"

export function startNodeProcess(code: string): RunningProcess {
return createObservableProcess(["node", "-e", code])
return run(["node", "-e", code])
}
18 changes: 9 additions & 9 deletions test/start-test.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import { strict as assert } from "assert"
import { createObservableProcess } from "../src/observable-process"
import { run } from "../src/observable-process"

suite("ObservableProcess.spawn()")

test("starting a process via an argv array", async function () {
const observable = createObservableProcess(["node", "-e", "console.log('hello')"])
const observable = run(["node", "-e", "console.log('hello')"])
const result = await observable.waitForEnd()
assert.equal(result.exitCode, 0)
})

test("starting a process via a string", async function () {
const observable = createObservableProcess("node -e console.log('hello')")
const observable = run("node -e console.log('hello')")
const result = await observable.waitForEnd()
assert.equal(result.exitCode, 0)
})

test("starting processes in the path", async function () {
const observable = createObservableProcess("node -h")
const observable = run("node -h")
const result = await observable.waitForEnd()
assert.equal(result.exitCode, 0)
})

test("no command to run", function () {
assert.throws(function () {
// @ts-ignore
createObservableProcess()
}, new Error("createObservableProcess: no command to execute given"))
run()
}, new Error("run: no command to execute given"))
})

test("wrong argument type", function () {
assert.throws(function () {
// @ts-ignore
createObservableProcess(1)
}, new Error("createObservableProcess: you must provide the command to run as a string or string[]"))
run(1)
}, new Error("run: you must provide the command to run as a string or string[]"))
})

test("providing environment variables", async function () {
const observable = createObservableProcess(["node", "-e", "console.log('foo:', process.env.foo)"], {
const observable = run(["node", "-e", "console.log('foo:', process.env.foo)"], {
env: { foo: "bar", PATH: process.env.PATH },
})
await observable.waitForEnd()
Expand Down
6 changes: 3 additions & 3 deletions test/wait-for-end-test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { strict as assert } from "assert"
import { createObservableProcess } from "../src/observable-process"
import { run } from "../src/observable-process"
import util from "util"
const delay = util.promisify(setTimeout)

suite("ObservableProcess.waitForEnd()")

test("process ends before calling it", async function () {
const observable = createObservableProcess(["node", "-e", "console.log('hello')"])
const observable = run(["node", "-e", "console.log('hello')"])
await delay(50)
const result = await observable.waitForEnd()
assert.equal(result.exitCode, 0)
})

test("process still running when calling it", async function () {
const observable = createObservableProcess(["node", "-e", "setTimeout(function() {}, 10)"])
const observable = run(["node", "-e", "setTimeout(function() {}, 10)"])
const result = await observable.waitForEnd()
assert.equal(result.exitCode, 0)
})

0 comments on commit 499604f

Please sign in to comment.