Skip to content

Commit

Permalink
start
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgo committed Aug 18, 2020
1 parent dcb5cd7 commit b877cc1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
14 changes: 7 additions & 7 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 { run } = require("observable-process")
const { start } = require("observable-process")
```

– or –

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

## Starting processes

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

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

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

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

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

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

You can provide custom environment variables for the process:

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

/** starts a new ObservableProcess with the given options */
export function run(command: string | string[], args: SpawnOptions = {}): ObservableProcess {
export function start(command: string | string[], args: SpawnOptions = {}): ObservableProcess {
// determine args
if (!command) {
throw new Error("run: no command to execute given")
throw new Error("start: 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[]")
throw new Error("start: 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,6 +1,6 @@
import { ObservableProcess } from "../../src/observable-process"
import { run } from "../../src/run"
import { start } from "../../src/start"

export function startNodeProcess(code: string): ObservableProcess {
return run(["node", "-e", code])
return start(["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 { run } from "../src/run"
import { start } from "../src/start"

suite("ObservableProcess.spawn()")

test("starting a process via an argv array", async function () {
const observable = run(["node", "-e", "console.log('hello')"])
const observable = start(["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 = run("node -e console.log('hello')")
const observable = start("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 = run("node -h")
const observable = start("node -h")
const result = await observable.waitForEnd()
assert.equal(result.exitCode, 0)
})

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

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

test("providing environment variables", async function () {
const observable = run(["node", "-e", "console.log('foo:', process.env.foo)"], {
const observable = start(["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,12 +1,12 @@
import { strict as assert } from "assert"
import { run } from "../src/run"
import { start } from "../src/start"
import util from "util"
const delay = util.promisify(setTimeout)

suite("ObservableProcess.waitForEnd()")

test("process ends before calling it", async function () {
const observable = run(["node", "-e", "console.log('hello')"])
const observable = start(["node", "-e", "console.log('hello')"])
await delay(50)
const result = await observable.waitForEnd()
assert.deepEqual(result, {
Expand All @@ -19,7 +19,7 @@ test("process ends before calling it", async function () {
})

test("process still running when calling it", async function () {
const observable = run(["node", "-e", "setTimeout(function() { console.log('finally')}, 10)"])
const observable = start(["node", "-e", "setTimeout(function() { console.log('finally')}, 10)"])
const result = await observable.waitForEnd()
assert.equal(result.exitCode, 0)
assert.equal(result.killed, false)
Expand Down

0 comments on commit b877cc1

Please sign in to comment.