1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

Command line flags

This commit is contained in:
Ryan Dahl 2018-05-19 05:38:51 -04:00
parent aeb85efdad
commit 83f436e175
8 changed files with 102 additions and 10 deletions

24
main.go
View file

@ -3,6 +3,8 @@ package main
import (
"crypto/md5"
"encoding/hex"
"flag"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/ry/v8worker2"
"io"
@ -17,6 +19,10 @@ import (
"time"
)
var flagReload = flag.Bool("reload", false, "Reload cached remote source code.")
var flagV8Options = flag.Bool("v8-options", false, "Print V8 command line options.")
var flagDebug = flag.Bool("debug", false, "Enable debug output.")
var DenoDir string
var CompileDir string
var SrcDir string
@ -44,12 +50,14 @@ func IsRemote(filename string) bool {
// Fetches a remoteUrl but also caches it to the localFilename.
func FetchRemoteSource(remoteUrl string, localFilename string) ([]byte, error) {
//println("FetchRemoteSource", remoteUrl)
Assert(strings.HasPrefix(localFilename, SrcDir), localFilename)
var sourceReader io.Reader
file, err := os.Open(localFilename)
if os.IsNotExist(err) {
if *flagReload || os.IsNotExist(err) {
// Fetch from HTTP.
println("Downloading", remoteUrl)
res, err := http.Get(remoteUrl)
if err != nil {
return nil, err
@ -100,6 +108,7 @@ func ResolveModule(moduleSpecifier string, containingFile string) (
const assetPrefix string = "/$asset$/"
func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out []byte) {
Assert(moduleSpecifier != "", "moduleSpecifier shouldn't be empty")
res := &Msg{}
var sourceCodeBuf []byte
var err error
@ -117,6 +126,9 @@ func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out [
return
}
//println("HandleSourceCodeFetch", "moduleSpecifier", moduleSpecifier,
// "containingFile", containingFile, "filename", filename)
if IsRemote(moduleName) {
sourceCodeBuf, err = FetchRemoteSource(moduleName, filename)
} else if strings.HasPrefix(moduleName, assetPrefix) {
@ -249,7 +261,14 @@ func recv(buf []byte) []byte {
}
func main() {
args := v8worker2.SetFlags(os.Args)
flag.Parse()
args := flag.Args()
if *flagV8Options {
args = append(args, "--help")
fmt.Println(args)
}
args = v8worker2.SetFlags(args)
createDirs()
worker := v8worker2.New(recv)
loadAsset(worker, "dist/main.js")
@ -264,6 +283,7 @@ func main() {
Start: &StartMsg{
Cwd: cwd,
Argv: args,
DebugFlag: *flagDebug,
},
},
})

14
main.ts
View file

@ -2,10 +2,16 @@ import { main as pb } from "./msg.pb";
import "./util";
import * as runtime from "./runtime";
import * as timers from "./timers";
import * as util from "./util";
function start(cwd: string, argv: string[]): void {
// TODO parse arguments.
const inputFn = argv[1];
// To control internal logging output
// Set with the -debug command-line flag.
export let debug = false;
function start(cwd: string, argv: string[], debugFlag: boolean): void {
debug = debugFlag;
util.log("start", { cwd, argv, debugFlag });
const inputFn = argv[0];
const mod = runtime.resolveModule(inputFn, cwd + "/");
mod.compileAndRun();
}
@ -14,7 +20,7 @@ V8Worker2.recv((ab: ArrayBuffer) => {
const msg = pb.Msg.decode(new Uint8Array(ab));
switch (msg.payload) {
case "start":
start(msg.start.cwd, msg.start.argv);
start(msg.start.cwd, msg.start.argv, msg.start.debugFlag);
break;
case "timerReady":
timers.timerReady(msg.timerReady.id, msg.timerReady.done);

49
modules_test.go Normal file
View file

@ -0,0 +1,49 @@
package main
import (
"path"
"testing"
)
func AssertEqual(t *testing.T, actual string, expected string) {
if actual != expected {
t.Fatalf("not equal <<%s>> <<%s>>", actual, expected)
}
}
func TestResolveModule(t *testing.T) {
moduleName, filename, err := ResolveModule(
"http://localhost:4545/testdata/subdir/print_hello.ts",
"/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts")
if err != nil {
t.Fatalf(err.Error())
}
AssertEqual(t, moduleName,
"http://localhost:4545/testdata/subdir/print_hello.ts")
AssertEqual(t, filename,
path.Join(SrcDir, "localhost:4545/testdata/subdir/print_hello.ts"))
moduleName, filename, err = ResolveModule(
"./subdir/print_hello.ts",
"/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts")
if err != nil {
t.Fatalf(err.Error())
}
AssertEqual(t, moduleName,
"/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts")
AssertEqual(t, filename,
"/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts")
// In the case where the containingFile is a directory (indicated with a
// trailing slash)
moduleName, filename, err = ResolveModule(
"testdata/001_hello.js",
"/Users/rld/go/src/github.com/ry/deno/")
if err != nil {
t.Fatalf(err.Error())
}
AssertEqual(t, moduleName,
"/Users/rld/go/src/github.com/ry/deno/testdata/001_hello.js")
AssertEqual(t, filename,
"/Users/rld/go/src/github.com/ry/deno/testdata/001_hello.js")
}

View file

@ -17,6 +17,7 @@ message Msg {
message StartMsg {
string cwd = 1;
repeated string argv = 2;
bool debug_flag = 3;
}
message SourceCodeFetchMsg {

View file

@ -99,8 +99,10 @@ export function resolveModule(
moduleSpecifier: string,
containingFile: string
): FileModule {
util.assert(moduleSpecifier != null && moduleSpecifier.length > 0);
// We ask golang to sourceCodeFetch. It will load the sourceCode and if
// there is any outputCode cached, it will return that as well.
util.log("resolveModule", { moduleSpecifier, containingFile });
const { filename, sourceCode, outputCode } = os.sourceCodeFetch(
moduleSpecifier,
containingFile

8
types.ts Normal file
View file

@ -0,0 +1,8 @@
export type TypedArray = Uint8Array | Float32Array | Int32Array;
export interface ModuleInfo {
moduleName?: string;
filename?: string;
sourceCode?: string;
outputCode?: string;
}

7
util.go Normal file
View file

@ -0,0 +1,7 @@
package main
func Assert(cond bool, msg string) {
if !cond {
panic(msg)
}
}

View file

@ -13,8 +13,7 @@ _global["window"] = _global; // Create a window object.
const print = V8Worker2.print;
// To control internal logging output
const debug = false;
import { debug } from "./main";
// Internal logging for deno. Use the "debug" variable above to control
// output.