summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBartek Iwańczuk <biwanczuk@gmail.com>2019-02-21 21:52:35 +0100
committerRyan Dahl <ry@tinyclouds.org>2019-02-21 15:52:35 -0500
commit9d025facaa9b97a27d751dbcd67d8c003c77f3c2 (patch)
tree00f36f77d786ad6f8901669fe250035dd21f8e04
parent8c6d6b2832d9bd39c4d6edee87a7489b780b8cde (diff)
manual: add Deno.run example (#1811)
-rw-r--r--js/process.ts14
-rw-r--r--website/manual.md74
2 files changed, 51 insertions, 37 deletions
diff --git a/js/process.ts b/js/process.ts
index c47a5b5dd..2f60e5c4a 100644
--- a/js/process.ts
+++ b/js/process.ts
@@ -8,7 +8,7 @@ import { ReadCloser, WriteCloser } from "./io";
import { readAll } from "./buffer";
import { assert, unreachable } from "./util";
-/** How to handle subsubprocess stdio.
+/** How to handle subprocess stdio.
*
* "inherit" The default if unspecified. The child inherits from the
* corresponding parent descriptor.
@@ -101,6 +101,18 @@ function stdioMap(s: ProcessStdio): msg.ProcessStdio {
}
}
+/**
+ * Spawns new subprocess.
+ *
+ * Subprocess uses same working directory as parent process unless `opt.cwd`
+ * is specified.
+ *
+ * Environmental variables for subprocess can be specified using `opt.env`
+ * mapping.
+ *
+ * By default subprocess inherits stdio of parent process. To change that
+ * `opt.stdout`, `opt.stderr` and `opt.stdin` can be specified independently.
+ */
export function run(opt: RunOptions): Process {
const builder = flatbuffers.createBuilder();
const argsOffset = msg.Run.createArgsVector(
diff --git a/website/manual.md b/website/manual.md
index ea6f21719..6bd4a1d3d 100644
--- a/website/manual.md
+++ b/website/manual.md
@@ -312,68 +312,70 @@ file_server --reload
### Run subprocess
-```
-const p = Deno.run({
- args: ["deno", "--allow-read", "https://deno.land/x/examples/cat.ts", "README.md"],
-});
+[API Reference](https://deno.land/typedoc/index.html#run)
-// start subprocess
-await p.status();
-```
+Example:
-When this program is started, the user is prompted for permission to run
-subprocess:
+```ts
+async function main() {
+ // create subprocess
+ const p = Deno.run({
+ args: ["echo", "hello"]
+ });
+
+ // await its completion
+ await p.status();
+}
-```
-> deno https://deno.land/x/examples/subprocess_simple.ts
-⚠️ Deno requests access to run a subprocess. Grant? [yN] y
+main();
```
-For security reasons, deno does not allow programs to run subprocess without
-explicit permission. To avoid the console prompt, use a command-line flag:
+Run it:
```
-> deno https://deno.land/x/examples/subprocess_simple.ts --allow-run
+> deno --allow-run ./subprocess_simple.ts
+hello
```
By default when you use `deno.run()` subprocess inherits `stdin`, `stdout` and
`stdout` of parent process. If you want to communicate with started subprocess
you can use `"piped"` option.
-```
-const decoder = new TextDecoder();
+```ts
+async function main() {
+ const decoder = new TextDecoder();
-const filesToCat = Deno.args.slice(1);
-const subprocessArgs = ["deno", "--allow-read", "https://deno.land/x/examples/cat.ts", ...filesToCat];
+ const fileNames = Deno.args.slice(1);
-const p = Deno.run({
- subprocessArgs,
- stdout: "piped",
- stderr: "piped",
-});
+ const p = Deno.run({
+ args: [
+ "deno",
+ "--allow-read",
+ "https://deno.land/x/examples/cat.ts",
+ ...fileNames
+ ],
+ stdout: "piped",
+ stderr: "piped"
+ });
+
+ const { code } = await p.status();
-const { code } = await p.status();
+ const rawOutput = await p.output();
+ Deno.stdout.write(rawOutput);
-if (code === 0) {
- const rawOutput = await Deno.readAll(p.stdout);
- const output = decoder.decode(rawOutput);
- console.log(output);
-} else {
- const rawErr = await Deno.readAll(p.stderr);
- const err = decoder.decode(rawErr);
- console.log(err);
+ Deno.exit(code);
}
-Deno.exit(code);
+main();
```
When you run it:
```
-> deno https://deno.land/x/examples/subprocess.ts --allow-run <somefile>
+> deno ./subprocess.ts --allow-run <somefile>
[file content]
-> deno https://deno.land/x/examples/subprocess.ts --allow-run non_existent_file.md
+> deno ./subprocess.ts --allow-run non_existent_file.md
Uncaught NotFound: No such file or directory (os error 2)
at DenoError (deno/js/errors.ts:19:5)