-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass on running child processes
- Loading branch information
1 parent
bd88e56
commit 044c148
Showing
5 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2018 the Deno authors. All rights reserved. MIT license. | ||
import * as msg from "gen/msg_generated"; | ||
import * as flatbuffers from "./flatbuffers"; | ||
import * as dispatch from "./dispatch"; | ||
import { assert, unreachable } from "./util"; | ||
|
||
export interface CommandOptions { | ||
argv: string[]; | ||
dir?: string; | ||
shell?: boolean; | ||
} | ||
|
||
interface ExitStatusCode { | ||
success: boolean; | ||
code: number; | ||
} | ||
|
||
interface ExitStatusSignal { | ||
success: false; | ||
signal: number /* TODO: Make this a string. */; | ||
} | ||
|
||
export type ExitStatus = ExitStatusCode | ExitStatusSignal; | ||
|
||
// This is a convenience function a la Python's check_output. | ||
// TODO: | ||
// * Stdio and a lot more options. | ||
// * Throw if not succesful. | ||
// * Maybe a shorter name. run()? spawn()? | ||
// * really want it to have this interface, but I don't know how to express | ||
// that in typescript: | ||
// run("python", "tools/build.py") | ||
// run("magick", "convert", file, "-", { stdout: UInt8Array }) | ||
// run("curl http://deno.land > lol.hihi" { shell: true }) | ||
export async function runCommand(options: CommandOptions): Promise<ExitStatus> { | ||
return res(await dispatch.sendAsync(...req(options))); | ||
} | ||
|
||
function req({ | ||
argv, | ||
dir = undefined, | ||
shell = false | ||
}: CommandOptions): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] { | ||
const builder = flatbuffers.createBuilder(); | ||
const argvOffset = msg.RunCommand.createArgvVector( | ||
builder, | ||
argv.map(a => builder.createString(a)) | ||
); | ||
const dirOffset = dir === undefined ? -1 : builder.createString(dir); | ||
msg.RunCommand.startRunCommand(builder); | ||
msg.RunCommand.addArgv(builder, argvOffset); | ||
if (dir !== undefined) { | ||
msg.RunCommand.addDir(builder, dirOffset); | ||
} | ||
msg.RunCommand.addShell(builder, shell); | ||
const inner = msg.RunCommand.endRunCommand(builder); | ||
return [builder, msg.Any.RunCommand, inner]; | ||
} | ||
|
||
function res(baseRes: null | msg.Base): ExitStatus { | ||
assert(baseRes != null); | ||
assert(msg.Any.RunCommandRes === baseRes!.innerType()); | ||
const res = new msg.RunCommandRes(); | ||
assert(baseRes!.inner(res) != null); | ||
switch (res.status()) { | ||
case msg.CommandExitStatus.ExitedWithCode: | ||
const code = res.exitCode(); | ||
return { code, success: code === 0 }; | ||
case msg.CommandExitStatus.ExitedWithSignal: | ||
const signal = res.exitSignal(); | ||
return { signal, success: false }; | ||
default: | ||
return unreachable(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters