mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
Command line flags
This commit is contained in:
parent
aeb85efdad
commit
83f436e175
8 changed files with 102 additions and 10 deletions
28
main.go
28
main.go
|
@ -3,6 +3,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
"github.com/ry/v8worker2"
|
"github.com/ry/v8worker2"
|
||||||
"io"
|
"io"
|
||||||
|
@ -17,6 +19,10 @@ import (
|
||||||
"time"
|
"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 DenoDir string
|
||||||
var CompileDir string
|
var CompileDir string
|
||||||
var SrcDir string
|
var SrcDir string
|
||||||
|
@ -44,12 +50,14 @@ func IsRemote(filename string) bool {
|
||||||
|
|
||||||
// Fetches a remoteUrl but also caches it to the localFilename.
|
// Fetches a remoteUrl but also caches it to the localFilename.
|
||||||
func FetchRemoteSource(remoteUrl string, localFilename string) ([]byte, error) {
|
func FetchRemoteSource(remoteUrl string, localFilename string) ([]byte, error) {
|
||||||
|
//println("FetchRemoteSource", remoteUrl)
|
||||||
Assert(strings.HasPrefix(localFilename, SrcDir), localFilename)
|
Assert(strings.HasPrefix(localFilename, SrcDir), localFilename)
|
||||||
var sourceReader io.Reader
|
var sourceReader io.Reader
|
||||||
|
|
||||||
file, err := os.Open(localFilename)
|
file, err := os.Open(localFilename)
|
||||||
if os.IsNotExist(err) {
|
if *flagReload || os.IsNotExist(err) {
|
||||||
// Fetch from HTTP.
|
// Fetch from HTTP.
|
||||||
|
println("Downloading", remoteUrl)
|
||||||
res, err := http.Get(remoteUrl)
|
res, err := http.Get(remoteUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -100,6 +108,7 @@ func ResolveModule(moduleSpecifier string, containingFile string) (
|
||||||
const assetPrefix string = "/$asset$/"
|
const assetPrefix string = "/$asset$/"
|
||||||
|
|
||||||
func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out []byte) {
|
func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out []byte) {
|
||||||
|
Assert(moduleSpecifier != "", "moduleSpecifier shouldn't be empty")
|
||||||
res := &Msg{}
|
res := &Msg{}
|
||||||
var sourceCodeBuf []byte
|
var sourceCodeBuf []byte
|
||||||
var err error
|
var err error
|
||||||
|
@ -117,6 +126,9 @@ func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out [
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//println("HandleSourceCodeFetch", "moduleSpecifier", moduleSpecifier,
|
||||||
|
// "containingFile", containingFile, "filename", filename)
|
||||||
|
|
||||||
if IsRemote(moduleName) {
|
if IsRemote(moduleName) {
|
||||||
sourceCodeBuf, err = FetchRemoteSource(moduleName, filename)
|
sourceCodeBuf, err = FetchRemoteSource(moduleName, filename)
|
||||||
} else if strings.HasPrefix(moduleName, assetPrefix) {
|
} else if strings.HasPrefix(moduleName, assetPrefix) {
|
||||||
|
@ -249,7 +261,14 @@ func recv(buf []byte) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
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()
|
createDirs()
|
||||||
worker := v8worker2.New(recv)
|
worker := v8worker2.New(recv)
|
||||||
loadAsset(worker, "dist/main.js")
|
loadAsset(worker, "dist/main.js")
|
||||||
|
@ -262,8 +281,9 @@ func main() {
|
||||||
out, err := proto.Marshal(&Msg{
|
out, err := proto.Marshal(&Msg{
|
||||||
Payload: &Msg_Start{
|
Payload: &Msg_Start{
|
||||||
Start: &StartMsg{
|
Start: &StartMsg{
|
||||||
Cwd: cwd,
|
Cwd: cwd,
|
||||||
Argv: args,
|
Argv: args,
|
||||||
|
DebugFlag: *flagDebug,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
14
main.ts
14
main.ts
|
@ -2,10 +2,16 @@ import { main as pb } from "./msg.pb";
|
||||||
import "./util";
|
import "./util";
|
||||||
import * as runtime from "./runtime";
|
import * as runtime from "./runtime";
|
||||||
import * as timers from "./timers";
|
import * as timers from "./timers";
|
||||||
|
import * as util from "./util";
|
||||||
|
|
||||||
function start(cwd: string, argv: string[]): void {
|
// To control internal logging output
|
||||||
// TODO parse arguments.
|
// Set with the -debug command-line flag.
|
||||||
const inputFn = argv[1];
|
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 + "/");
|
const mod = runtime.resolveModule(inputFn, cwd + "/");
|
||||||
mod.compileAndRun();
|
mod.compileAndRun();
|
||||||
}
|
}
|
||||||
|
@ -14,7 +20,7 @@ V8Worker2.recv((ab: ArrayBuffer) => {
|
||||||
const msg = pb.Msg.decode(new Uint8Array(ab));
|
const msg = pb.Msg.decode(new Uint8Array(ab));
|
||||||
switch (msg.payload) {
|
switch (msg.payload) {
|
||||||
case "start":
|
case "start":
|
||||||
start(msg.start.cwd, msg.start.argv);
|
start(msg.start.cwd, msg.start.argv, msg.start.debugFlag);
|
||||||
break;
|
break;
|
||||||
case "timerReady":
|
case "timerReady":
|
||||||
timers.timerReady(msg.timerReady.id, msg.timerReady.done);
|
timers.timerReady(msg.timerReady.id, msg.timerReady.done);
|
||||||
|
|
49
modules_test.go
Normal file
49
modules_test.go
Normal 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")
|
||||||
|
}
|
|
@ -17,6 +17,7 @@ message Msg {
|
||||||
message StartMsg {
|
message StartMsg {
|
||||||
string cwd = 1;
|
string cwd = 1;
|
||||||
repeated string argv = 2;
|
repeated string argv = 2;
|
||||||
|
bool debug_flag = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
message SourceCodeFetchMsg {
|
message SourceCodeFetchMsg {
|
||||||
|
|
|
@ -99,8 +99,10 @@ export function resolveModule(
|
||||||
moduleSpecifier: string,
|
moduleSpecifier: string,
|
||||||
containingFile: string
|
containingFile: string
|
||||||
): FileModule {
|
): FileModule {
|
||||||
|
util.assert(moduleSpecifier != null && moduleSpecifier.length > 0);
|
||||||
// We ask golang to sourceCodeFetch. It will load the sourceCode and if
|
// We ask golang to sourceCodeFetch. It will load the sourceCode and if
|
||||||
// there is any outputCode cached, it will return that as well.
|
// there is any outputCode cached, it will return that as well.
|
||||||
|
util.log("resolveModule", { moduleSpecifier, containingFile });
|
||||||
const { filename, sourceCode, outputCode } = os.sourceCodeFetch(
|
const { filename, sourceCode, outputCode } = os.sourceCodeFetch(
|
||||||
moduleSpecifier,
|
moduleSpecifier,
|
||||||
containingFile
|
containingFile
|
||||||
|
|
8
types.ts
Normal file
8
types.ts
Normal 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
7
util.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func Assert(cond bool, msg string) {
|
||||||
|
if !cond {
|
||||||
|
panic(msg)
|
||||||
|
}
|
||||||
|
}
|
3
util.ts
3
util.ts
|
@ -13,8 +13,7 @@ _global["window"] = _global; // Create a window object.
|
||||||
|
|
||||||
const print = V8Worker2.print;
|
const print = V8Worker2.print;
|
||||||
|
|
||||||
// To control internal logging output
|
import { debug } from "./main";
|
||||||
const debug = false;
|
|
||||||
|
|
||||||
// Internal logging for deno. Use the "debug" variable above to control
|
// Internal logging for deno. Use the "debug" variable above to control
|
||||||
// output.
|
// output.
|
||||||
|
|
Loading…
Add table
Reference in a new issue