2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-07-31 17:11:37 -04:00
|
|
|
use crate::fs as deno_fs;
|
2019-06-26 06:02:13 -04:00
|
|
|
use clap::App;
|
|
|
|
use clap::AppSettings;
|
|
|
|
use clap::Arg;
|
|
|
|
use clap::ArgMatches;
|
|
|
|
use clap::Shell;
|
|
|
|
use clap::SubCommand;
|
2019-07-08 17:04:07 -04:00
|
|
|
use deno::ModuleSpecifier;
|
2019-06-22 12:02:51 -04:00
|
|
|
use log::Level;
|
2019-06-26 06:02:13 -04:00
|
|
|
use std;
|
2019-06-29 18:35:56 -04:00
|
|
|
use std::str;
|
2019-06-26 06:02:13 -04:00
|
|
|
use std::str::FromStr;
|
2018-08-17 16:34:30 -04:00
|
|
|
|
|
|
|
// Creates vector of strings, Vec<String>
|
|
|
|
macro_rules! svec {
|
|
|
|
($($x:expr),*) => (vec![$($x.to_string()),*]);
|
|
|
|
}
|
|
|
|
|
2019-01-08 14:44:06 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Default)]
|
2018-08-17 16:34:30 -04:00
|
|
|
pub struct DenoFlags {
|
2019-06-22 12:02:51 -04:00
|
|
|
pub log_level: Option<Level>,
|
2018-08-17 16:34:30 -04:00
|
|
|
pub version: bool,
|
|
|
|
pub reload: bool,
|
2019-04-29 10:58:31 -04:00
|
|
|
/// When the `--config`/`-c` flag is used to pass the name, this will be set
|
|
|
|
/// the path passed on the command line, otherwise `None`.
|
|
|
|
pub config_path: Option<String>,
|
2019-06-09 09:08:20 -04:00
|
|
|
/// When the `--importmap` flag is used to pass the name, this will be set
|
|
|
|
/// the path passed on the command line, otherwise `None`.
|
|
|
|
pub import_map_path: Option<String>,
|
2019-02-08 15:59:38 -05:00
|
|
|
pub allow_read: bool,
|
2019-05-08 19:20:30 -04:00
|
|
|
pub read_whitelist: Vec<String>,
|
2018-08-17 16:34:30 -04:00
|
|
|
pub allow_write: bool,
|
2019-05-08 19:20:30 -04:00
|
|
|
pub write_whitelist: Vec<String>,
|
2018-08-17 16:34:30 -04:00
|
|
|
pub allow_net: bool,
|
2019-05-08 19:20:30 -04:00
|
|
|
pub net_whitelist: Vec<String>,
|
2018-08-31 07:51:12 -04:00
|
|
|
pub allow_env: bool,
|
2018-11-15 23:07:40 -05:00
|
|
|
pub allow_run: bool,
|
2019-05-23 12:28:29 -04:00
|
|
|
pub allow_hrtime: bool,
|
2019-03-13 12:43:47 -04:00
|
|
|
pub no_prompts: bool,
|
2019-05-03 11:09:51 -04:00
|
|
|
pub no_fetch: bool,
|
2019-06-11 10:34:39 -04:00
|
|
|
pub seed: Option<u64>,
|
2019-04-21 11:34:18 -04:00
|
|
|
pub v8_flags: Option<Vec<String>>,
|
2019-05-03 16:24:09 -04:00
|
|
|
pub xeval_replvar: Option<String>,
|
|
|
|
pub xeval_delim: Option<String>,
|
2019-07-31 11:02:20 -04:00
|
|
|
// Use tokio::runtime::current_thread
|
|
|
|
pub current_thread: bool,
|
2018-11-15 12:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-13 13:24:15 -04:00
|
|
|
static ENV_VARIABLES_HELP: &str = "ENVIRONMENT VARIABLES:
|
2019-04-06 18:13:06 -04:00
|
|
|
DENO_DIR Set deno's base directory
|
|
|
|
NO_COLOR Set to disable color";
|
2018-10-24 00:02:43 -04:00
|
|
|
|
2019-06-05 13:44:46 -04:00
|
|
|
fn add_run_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
|
|
|
|
app
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("allow-read")
|
|
|
|
.long("allow-read")
|
|
|
|
.min_values(0)
|
|
|
|
.takes_value(true)
|
|
|
|
.use_delimiter(true)
|
|
|
|
.require_equals(true)
|
|
|
|
.help("Allow file system read access"),
|
2019-07-31 17:11:37 -04:00
|
|
|
)
|
|
|
|
.arg(
|
2019-06-05 13:44:46 -04:00
|
|
|
Arg::with_name("allow-write")
|
|
|
|
.long("allow-write")
|
|
|
|
.min_values(0)
|
|
|
|
.takes_value(true)
|
|
|
|
.use_delimiter(true)
|
|
|
|
.require_equals(true)
|
|
|
|
.help("Allow file system write access"),
|
2019-07-31 17:11:37 -04:00
|
|
|
)
|
|
|
|
.arg(
|
2019-06-05 13:44:46 -04:00
|
|
|
Arg::with_name("allow-net")
|
|
|
|
.long("allow-net")
|
|
|
|
.min_values(0)
|
|
|
|
.takes_value(true)
|
|
|
|
.use_delimiter(true)
|
|
|
|
.require_equals(true)
|
|
|
|
.help("Allow network access"),
|
2019-07-31 17:11:37 -04:00
|
|
|
)
|
|
|
|
.arg(
|
2019-06-05 13:44:46 -04:00
|
|
|
Arg::with_name("allow-env")
|
|
|
|
.long("allow-env")
|
|
|
|
.help("Allow environment access"),
|
2019-07-31 17:11:37 -04:00
|
|
|
)
|
|
|
|
.arg(
|
2019-06-05 13:44:46 -04:00
|
|
|
Arg::with_name("allow-run")
|
|
|
|
.long("allow-run")
|
|
|
|
.help("Allow running subprocesses"),
|
2019-07-31 17:11:37 -04:00
|
|
|
)
|
|
|
|
.arg(
|
2019-06-05 13:44:46 -04:00
|
|
|
Arg::with_name("allow-hrtime")
|
|
|
|
.long("allow-hrtime")
|
|
|
|
.help("Allow high resolution time measurement"),
|
2019-07-31 17:11:37 -04:00
|
|
|
)
|
|
|
|
.arg(
|
2019-06-05 13:44:46 -04:00
|
|
|
Arg::with_name("allow-all")
|
|
|
|
.short("A")
|
|
|
|
.long("allow-all")
|
|
|
|
.help("Allow all permissions"),
|
2019-07-31 17:11:37 -04:00
|
|
|
)
|
|
|
|
.arg(
|
2019-06-05 13:44:46 -04:00
|
|
|
Arg::with_name("no-prompt")
|
|
|
|
.long("no-prompt")
|
|
|
|
.help("Do not use prompts"),
|
2019-07-31 17:11:37 -04:00
|
|
|
)
|
|
|
|
.arg(
|
2019-07-20 09:19:06 -04:00
|
|
|
Arg::with_name("no-fetch")
|
|
|
|
.long("no-fetch")
|
|
|
|
.help("Do not download remote modules"),
|
2019-06-05 13:44:46 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
pub fn create_cli_app<'a, 'b>() -> App<'a, 'b> {
|
2019-06-05 13:44:46 -04:00
|
|
|
add_run_args(App::new("deno"))
|
2019-04-13 13:24:15 -04:00
|
|
|
.bin_name("deno")
|
2019-07-27 05:20:40 -04:00
|
|
|
.global_settings(&[AppSettings::ColorNever, AppSettings::UnifiedHelpMessage, AppSettings::DisableVersion])
|
|
|
|
.settings(&[AppSettings::AllowExternalSubcommands])
|
2019-05-03 17:15:16 -04:00
|
|
|
.after_help(ENV_VARIABLES_HELP)
|
2019-05-23 14:57:44 -04:00
|
|
|
.long_about("A secure runtime for JavaScript and TypeScript built with V8, Rust, and Tokio.
|
2019-05-06 10:48:19 -04:00
|
|
|
|
2019-05-23 14:57:44 -04:00
|
|
|
Docs: https://deno.land/manual.html
|
|
|
|
Modules: https://github.com/denoland/deno_std
|
|
|
|
Bugs: https://github.com/denoland/deno/issues
|
|
|
|
|
|
|
|
To run the REPL:
|
|
|
|
|
|
|
|
deno
|
|
|
|
|
|
|
|
To execute a sandboxed script:
|
|
|
|
|
2019-06-05 13:44:46 -04:00
|
|
|
deno https://deno.land/welcome.ts
|
2019-05-23 14:57:44 -04:00
|
|
|
|
|
|
|
To evaluate code from the command line:
|
|
|
|
|
|
|
|
deno eval \"console.log(30933 + 404)\"
|
|
|
|
|
|
|
|
To get help on the another subcommands (run in this case):
|
|
|
|
|
|
|
|
deno help run")
|
2019-07-27 05:20:40 -04:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("version")
|
|
|
|
.short("v")
|
|
|
|
.long("version")
|
|
|
|
.help("Print the version"),
|
|
|
|
)
|
2019-04-06 18:13:06 -04:00
|
|
|
.arg(
|
2019-06-22 12:02:51 -04:00
|
|
|
Arg::with_name("log-level")
|
|
|
|
.short("L")
|
|
|
|
.long("log-level")
|
|
|
|
.help("Set log level")
|
|
|
|
.takes_value(true)
|
|
|
|
.possible_values(&["debug", "info"])
|
2019-05-03 17:15:16 -04:00
|
|
|
.global(true),
|
2019-04-06 18:13:06 -04:00
|
|
|
).arg(
|
|
|
|
Arg::with_name("reload")
|
|
|
|
.short("r")
|
|
|
|
.long("reload")
|
2019-05-03 17:15:16 -04:00
|
|
|
.help("Reload source code cache (recompile TypeScript)")
|
|
|
|
.global(true),
|
2019-04-29 10:58:31 -04:00
|
|
|
).arg(
|
|
|
|
Arg::with_name("config")
|
|
|
|
.short("c")
|
|
|
|
.long("config")
|
|
|
|
.value_name("FILE")
|
|
|
|
.help("Load compiler configuration file")
|
2019-05-03 17:15:16 -04:00
|
|
|
.takes_value(true)
|
|
|
|
.global(true),
|
2019-07-31 11:02:20 -04:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("current-thread")
|
|
|
|
.long("current-thread")
|
|
|
|
.global(true)
|
|
|
|
.help("Use tokio::runtime::current_thread"),
|
2019-07-27 10:37:03 -04:00
|
|
|
).arg(
|
|
|
|
Arg::with_name("importmap")
|
|
|
|
.long("importmap")
|
|
|
|
.value_name("FILE")
|
|
|
|
.help("Load import map file")
|
|
|
|
.long_help(
|
|
|
|
"Load import map file
|
|
|
|
Specification: https://wicg.github.io/import-maps/
|
|
|
|
Examples: https://github.com/WICG/import-maps#the-import-map",
|
|
|
|
)
|
|
|
|
.takes_value(true)
|
|
|
|
.global(true),
|
2019-06-11 10:34:39 -04:00
|
|
|
).arg(
|
|
|
|
Arg::with_name("seed")
|
|
|
|
.long("seed")
|
|
|
|
.value_name("NUMBER")
|
|
|
|
.help("Seed Math.random()")
|
|
|
|
.takes_value(true)
|
|
|
|
.validator(|val: String| {
|
|
|
|
match val.parse::<u64>() {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_) => Err("Seed should be a number".to_string())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.global(true),
|
2019-04-06 18:13:06 -04:00
|
|
|
).arg(
|
|
|
|
Arg::with_name("v8-options")
|
|
|
|
.long("v8-options")
|
2019-05-03 17:15:16 -04:00
|
|
|
.help("Print V8 command line options")
|
|
|
|
.global(true),
|
2019-04-06 18:13:06 -04:00
|
|
|
).arg(
|
|
|
|
Arg::with_name("v8-flags")
|
|
|
|
.long("v8-flags")
|
|
|
|
.takes_value(true)
|
2019-04-21 11:34:18 -04:00
|
|
|
.use_delimiter(true)
|
2019-04-06 18:13:06 -04:00
|
|
|
.require_equals(true)
|
2019-05-03 17:15:16 -04:00
|
|
|
.help("Set V8 command line options")
|
|
|
|
.global(true),
|
2019-04-27 14:19:50 -04:00
|
|
|
).subcommand(
|
|
|
|
SubCommand::with_name("version")
|
2019-05-01 19:15:37 -04:00
|
|
|
.about("Print the version")
|
2019-05-23 14:57:44 -04:00
|
|
|
.long_about("Print current version of Deno.
|
2019-05-01 19:15:37 -04:00
|
|
|
|
|
|
|
Includes versions of Deno, V8 JavaScript Engine, and the TypeScript
|
2019-05-23 14:57:44 -04:00
|
|
|
compiler.",
|
2019-05-01 19:15:37 -04:00
|
|
|
),
|
2019-06-08 14:42:28 -04:00
|
|
|
).subcommand(
|
|
|
|
SubCommand::with_name("bundle")
|
2019-06-10 09:20:19 -04:00
|
|
|
.about("Bundle module and dependencies into single file")
|
2019-06-08 14:42:28 -04:00
|
|
|
.long_about(
|
2019-06-11 14:38:19 -04:00
|
|
|
"Output a single JavaScript file with all dependencies
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
deno bundle https://deno.land/std/examples/colors.ts"
|
2019-06-08 14:42:28 -04:00
|
|
|
)
|
|
|
|
.arg(Arg::with_name("source_file").takes_value(true).required(true))
|
2019-06-11 14:38:19 -04:00
|
|
|
.arg(Arg::with_name("out_file").takes_value(true).required(false)),
|
2019-04-27 14:19:50 -04:00
|
|
|
).subcommand(
|
2019-04-25 13:47:33 -04:00
|
|
|
SubCommand::with_name("fetch")
|
|
|
|
.about("Fetch the dependencies")
|
2019-05-01 19:15:37 -04:00
|
|
|
.long_about(
|
2019-05-23 14:57:44 -04:00
|
|
|
"Fetch and compile remote dependencies recursively.
|
2019-05-01 19:15:37 -04:00
|
|
|
|
|
|
|
Downloads all statically imported scripts and save them in local
|
|
|
|
cache, without running the code. No future import network requests
|
|
|
|
would be made unless --reload is specified.
|
|
|
|
|
|
|
|
# Downloads all dependencies
|
|
|
|
deno fetch https://deno.land/std/http/file_server.ts
|
2019-05-23 14:57:44 -04:00
|
|
|
|
2019-05-01 19:15:37 -04:00
|
|
|
# Once cached, static imports no longer send network requests
|
2019-05-23 14:57:44 -04:00
|
|
|
deno run -A https://deno.land/std/http/file_server.ts",
|
2019-05-01 19:15:37 -04:00
|
|
|
).arg(Arg::with_name("file").takes_value(true).required(true)),
|
2019-04-21 11:34:18 -04:00
|
|
|
).subcommand(
|
|
|
|
SubCommand::with_name("types")
|
2019-05-01 19:15:37 -04:00
|
|
|
.about("Print runtime TypeScript declarations")
|
2019-05-23 14:57:44 -04:00
|
|
|
.long_about("Print runtime TypeScript declarations.
|
2019-05-01 19:15:37 -04:00
|
|
|
|
|
|
|
deno types > lib.deno_runtime.d.ts
|
2019-05-23 14:57:44 -04:00
|
|
|
|
|
|
|
The declaration file could be saved and used for typing information.",
|
2019-05-01 19:15:37 -04:00
|
|
|
),
|
2019-04-06 18:13:06 -04:00
|
|
|
).subcommand(
|
|
|
|
SubCommand::with_name("info")
|
|
|
|
.about("Show source file related info")
|
2019-05-23 14:57:44 -04:00
|
|
|
.long_about("Show source file related info.
|
2019-05-01 19:15:37 -04:00
|
|
|
|
2019-07-09 18:59:13 -04:00
|
|
|
deno info https://deno.land/std@v0.11/http/file_server.ts
|
2019-05-01 19:15:37 -04:00
|
|
|
|
2019-05-23 14:57:44 -04:00
|
|
|
The following information is shown:
|
2019-05-01 19:15:37 -04:00
|
|
|
|
2019-05-23 14:57:44 -04:00
|
|
|
local: Local path of the file.
|
|
|
|
type: JavaScript, TypeScript, or JSON.
|
|
|
|
compiled: TypeScript only. shown local path of compiled source code.
|
|
|
|
map: TypeScript only. shown local path of source map.
|
|
|
|
deps: Dependency tree of the source file.",
|
2019-05-01 19:15:37 -04:00
|
|
|
).arg(Arg::with_name("file").takes_value(true).required(true)),
|
2019-04-06 18:13:06 -04:00
|
|
|
).subcommand(
|
2019-04-13 13:24:15 -04:00
|
|
|
SubCommand::with_name("eval")
|
|
|
|
.about("Eval script")
|
2019-05-01 19:15:37 -04:00
|
|
|
.long_about(
|
2019-05-23 14:57:44 -04:00
|
|
|
"Evaluate provided script.
|
2019-05-01 19:15:37 -04:00
|
|
|
|
2019-05-06 10:48:19 -04:00
|
|
|
This command has implicit access to all permissions (equivalent to deno run --allow-all)
|
|
|
|
|
2019-05-23 14:57:44 -04:00
|
|
|
deno eval \"console.log('hello world')\"",
|
2019-05-01 19:15:37 -04:00
|
|
|
).arg(Arg::with_name("code").takes_value(true).required(true)),
|
2019-04-13 13:24:15 -04:00
|
|
|
).subcommand(
|
|
|
|
SubCommand::with_name("fmt")
|
|
|
|
.about("Format files")
|
2019-05-01 19:15:37 -04:00
|
|
|
.long_about(
|
2019-05-23 14:57:44 -04:00
|
|
|
"Auto-format JavaScript/TypeScript source code using Prettier
|
|
|
|
|
|
|
|
Automatically downloads Prettier dependencies on first run.
|
2019-05-01 19:15:37 -04:00
|
|
|
|
2019-06-01 08:54:32 -04:00
|
|
|
deno fmt myfile1.ts myfile2.ts",
|
|
|
|
).arg(
|
|
|
|
Arg::with_name("stdout")
|
|
|
|
.long("stdout")
|
|
|
|
.help("Output formated code to stdout")
|
|
|
|
.takes_value(false),
|
2019-05-01 19:15:37 -04:00
|
|
|
).arg(
|
2019-04-13 13:24:15 -04:00
|
|
|
Arg::with_name("files")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple(true)
|
|
|
|
.required(true),
|
|
|
|
),
|
2019-05-03 16:24:09 -04:00
|
|
|
).subcommand(
|
2019-06-05 13:44:46 -04:00
|
|
|
add_run_args(SubCommand::with_name("run"))
|
2019-05-03 17:15:16 -04:00
|
|
|
.settings(&[
|
|
|
|
AppSettings::AllowExternalSubcommands,
|
|
|
|
AppSettings::DisableHelpSubcommand,
|
|
|
|
AppSettings::SubcommandRequired,
|
|
|
|
]).about("Run a program given a filename or url to the source code")
|
|
|
|
.long_about(
|
2019-05-23 14:57:44 -04:00
|
|
|
"Run a program given a filename or url to the source code.
|
2019-05-03 17:15:16 -04:00
|
|
|
|
|
|
|
By default all programs are run in sandbox without access to disk, network or
|
|
|
|
ability to spawn subprocesses.
|
|
|
|
|
|
|
|
deno run https://deno.land/welcome.ts
|
|
|
|
|
|
|
|
# run program with permission to read from disk and listen to network
|
|
|
|
deno run --allow-net --allow-read https://deno.land/std/http/file_server.ts
|
|
|
|
|
2019-05-24 07:36:50 -04:00
|
|
|
# run program with permission to read whitelist files from disk and listen to network
|
2019-06-01 08:54:32 -04:00
|
|
|
deno run --allow-net --allow-read=$(pwd) https://deno.land/std/http/file_server.ts
|
2019-05-16 10:39:19 -04:00
|
|
|
|
2019-05-03 17:15:16 -04:00
|
|
|
# run program with all permissions
|
2019-05-23 14:57:44 -04:00
|
|
|
deno run -A https://deno.land/std/http/file_server.ts",
|
2019-05-03 17:15:16 -04:00
|
|
|
).subcommand(
|
|
|
|
// this is a fake subcommand - it's used in conjunction with
|
|
|
|
// AppSettings:AllowExternalSubcommand to treat it as an
|
|
|
|
// entry point script
|
2019-06-26 06:02:13 -04:00
|
|
|
SubCommand::with_name("[SCRIPT]").about("Script to run"),
|
2019-05-03 17:15:16 -04:00
|
|
|
),
|
|
|
|
).subcommand(
|
|
|
|
SubCommand::with_name("xeval")
|
2019-05-03 16:24:09 -04:00
|
|
|
.about("Eval a script on text segments from stdin")
|
|
|
|
.long_about(
|
2019-05-23 14:57:44 -04:00
|
|
|
"Eval a script on lines from stdin
|
2019-05-03 16:24:09 -04:00
|
|
|
|
|
|
|
Read from standard input and eval code on each whitespace-delimited
|
|
|
|
string chunks.
|
|
|
|
|
2019-05-06 10:48:19 -04:00
|
|
|
-I/--replvar optionally sets variable name for input to be used in eval.
|
2019-05-03 16:24:09 -04:00
|
|
|
Otherwise '$' will be used as default variable name.
|
|
|
|
|
2019-05-06 10:48:19 -04:00
|
|
|
This command has implicit access to all permissions (equivalent to deno run --allow-all)
|
|
|
|
|
2019-05-23 14:57:44 -04:00
|
|
|
Print all the usernames in /etc/passwd:
|
|
|
|
|
2019-05-03 16:24:09 -04:00
|
|
|
cat /etc/passwd | deno xeval \"a = $.split(':'); if (a) console.log(a[0])\"
|
2019-05-06 10:48:19 -04:00
|
|
|
|
2019-05-23 14:57:44 -04:00
|
|
|
A complicated way to print the current git branch:
|
|
|
|
|
2019-05-03 16:24:09 -04:00
|
|
|
git branch | deno xeval -I 'line' \"if (line.startsWith('*')) console.log(line.slice(2))\"
|
2019-05-06 10:48:19 -04:00
|
|
|
|
2019-05-23 14:57:44 -04:00
|
|
|
Demonstrates breaking the input up by space delimiter instead of by lines:
|
|
|
|
|
|
|
|
cat LICENSE | deno xeval -d \" \" \"if ($ === 'MIT') console.log('MIT licensed')\"",
|
2019-05-03 16:24:09 -04:00
|
|
|
).arg(
|
|
|
|
Arg::with_name("replvar")
|
|
|
|
.long("replvar")
|
|
|
|
.short("I")
|
|
|
|
.help("Set variable name to be used in eval, defaults to $")
|
|
|
|
.takes_value(true),
|
|
|
|
).arg(
|
|
|
|
Arg::with_name("delim")
|
|
|
|
.long("delim")
|
|
|
|
.short("d")
|
|
|
|
.help("Set delimiter, defaults to newline")
|
|
|
|
.takes_value(true),
|
|
|
|
).arg(Arg::with_name("code").takes_value(true).required(true)),
|
2019-06-14 13:05:06 -04:00
|
|
|
).subcommand(
|
|
|
|
SubCommand::with_name("install")
|
|
|
|
.settings(&[
|
|
|
|
AppSettings::DisableHelpSubcommand,
|
|
|
|
AppSettings::AllowExternalSubcommands,
|
|
|
|
AppSettings::SubcommandRequired,
|
|
|
|
])
|
|
|
|
.about("Install script as executable")
|
|
|
|
.long_about(
|
|
|
|
"Automatically downloads deno_installer dependencies on first run.
|
|
|
|
|
2019-06-20 14:25:13 -04:00
|
|
|
Default installation directory is $HOME/.deno/bin and it must be added to the path manually.
|
|
|
|
|
|
|
|
deno install file_server https://deno.land/std/http/file_server.ts --allow-net --allow-read
|
|
|
|
|
|
|
|
deno install colors https://deno.land/std/examples/colors.ts
|
|
|
|
|
|
|
|
To change installation directory use -d/--dir flag
|
|
|
|
|
|
|
|
deno install -d /usr/local/bin file_server https://deno.land/std/http/file_server.ts --allow-net --allow-read",
|
|
|
|
).arg(
|
|
|
|
Arg::with_name("dir")
|
|
|
|
.long("dir")
|
|
|
|
.short("d")
|
|
|
|
.help("Installation directory (defaults to $HOME/.deno/bin)")
|
|
|
|
.takes_value(true)
|
2019-06-14 13:05:06 -04:00
|
|
|
).arg(
|
|
|
|
Arg::with_name("exe_name")
|
|
|
|
.help("Executable name")
|
|
|
|
.required(true),
|
|
|
|
).subcommand(
|
|
|
|
// this is a fake subcommand - it's used in conjunction with
|
|
|
|
// AppSettings:AllowExternalSubcommand to treat it as an
|
|
|
|
// entry point script
|
2019-06-26 06:02:13 -04:00
|
|
|
SubCommand::with_name("[SCRIPT]").about("Script URL"),
|
2019-06-14 13:05:06 -04:00
|
|
|
),
|
2019-06-05 13:44:46 -04:00
|
|
|
).subcommand(
|
2019-06-26 06:02:13 -04:00
|
|
|
SubCommand::with_name("completions")
|
|
|
|
.settings(&[
|
|
|
|
AppSettings::DisableHelpSubcommand,
|
|
|
|
]).about("Generate shell completions")
|
|
|
|
.long_about(
|
|
|
|
"Output shell completion script to standard output.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
deno completions bash > /usr/local/etc/bash_completion.d/deno.bash
|
|
|
|
source /usr/local/etc/bash_completion.d/deno.bash")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("shell")
|
|
|
|
.possible_values(&Shell::variants())
|
|
|
|
.required(true),
|
|
|
|
),
|
|
|
|
).subcommand(
|
2019-06-05 13:44:46 -04:00
|
|
|
// this is a fake subcommand - it's used in conjunction with
|
|
|
|
// AppSettings:AllowExternalSubcommand to treat it as an
|
|
|
|
// entry point script
|
2019-06-26 06:02:13 -04:00
|
|
|
SubCommand::with_name("[SCRIPT]").about("Script to run"),
|
2019-04-17 09:25:51 -04:00
|
|
|
)
|
2019-04-13 13:24:15 -04:00
|
|
|
}
|
2019-05-09 12:20:34 -04:00
|
|
|
|
|
|
|
/// Convert paths supplied into full path.
|
|
|
|
/// If a path is invalid, we print out a warning
|
|
|
|
/// and ignore this path in the output.
|
|
|
|
fn resolve_paths(paths: Vec<String>) -> Vec<String> {
|
|
|
|
let mut out: Vec<String> = vec![];
|
|
|
|
for pathstr in paths.iter() {
|
2019-07-17 18:15:30 -04:00
|
|
|
let result = deno_fs::resolve_from_cwd(pathstr);
|
2019-05-09 12:20:34 -04:00
|
|
|
if result.is_err() {
|
|
|
|
eprintln!("Unrecognized path to whitelist: {}", pathstr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let mut full_path = result.unwrap().1;
|
|
|
|
// Remove trailing slash.
|
|
|
|
if full_path.len() > 1 && full_path.ends_with('/') {
|
|
|
|
full_path.pop();
|
|
|
|
}
|
|
|
|
out.push(full_path);
|
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
/// Parse ArgMatches into internal DenoFlags structure.
|
|
|
|
/// This method should not make any side effects.
|
2019-06-29 18:32:54 -04:00
|
|
|
pub fn parse_flags(
|
|
|
|
matches: &ArgMatches,
|
|
|
|
maybe_flags: Option<DenoFlags>,
|
|
|
|
) -> DenoFlags {
|
|
|
|
let mut flags = maybe_flags.unwrap_or_default();
|
2019-04-06 18:13:06 -04:00
|
|
|
|
2019-07-31 11:02:20 -04:00
|
|
|
if matches.is_present("current-thread") {
|
|
|
|
flags.current_thread = true;
|
|
|
|
}
|
2019-06-22 12:02:51 -04:00
|
|
|
if matches.is_present("log-level") {
|
|
|
|
flags.log_level = match matches.value_of("log-level").unwrap() {
|
|
|
|
"debug" => Some(Level::Debug),
|
|
|
|
"info" => Some(Level::Info),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2019-04-21 11:34:18 -04:00
|
|
|
}
|
|
|
|
if matches.is_present("version") {
|
|
|
|
flags.version = true;
|
|
|
|
}
|
|
|
|
if matches.is_present("reload") {
|
|
|
|
flags.reload = true;
|
|
|
|
}
|
2019-04-29 10:58:31 -04:00
|
|
|
flags.config_path = matches.value_of("config").map(ToOwned::to_owned);
|
2019-04-06 18:13:06 -04:00
|
|
|
if matches.is_present("v8-options") {
|
2019-05-03 08:59:52 -04:00
|
|
|
let v8_flags = svec!["deno", "--help"];
|
|
|
|
flags.v8_flags = Some(v8_flags);
|
2019-04-06 18:13:06 -04:00
|
|
|
}
|
|
|
|
if matches.is_present("v8-flags") {
|
2019-05-03 08:59:52 -04:00
|
|
|
let mut v8_flags: Vec<String> = matches
|
2019-04-06 18:13:06 -04:00
|
|
|
.values_of("v8-flags")
|
|
|
|
.unwrap()
|
|
|
|
.map(String::from)
|
|
|
|
.collect();
|
|
|
|
|
2019-05-03 08:59:52 -04:00
|
|
|
v8_flags.insert(0, "deno".to_string());
|
2019-04-21 11:34:18 -04:00
|
|
|
flags.v8_flags = Some(v8_flags);
|
2019-04-06 18:13:06 -04:00
|
|
|
}
|
2019-06-11 10:34:39 -04:00
|
|
|
if matches.is_present("seed") {
|
|
|
|
let seed_string = matches.value_of("seed").unwrap();
|
|
|
|
let seed = seed_string.parse::<u64>().unwrap();
|
|
|
|
flags.seed = Some(seed);
|
|
|
|
|
|
|
|
let v8_seed_flag = format!("--random-seed={}", seed);
|
|
|
|
|
|
|
|
match flags.v8_flags {
|
|
|
|
Some(ref mut v8_flags) => {
|
|
|
|
v8_flags.push(v8_seed_flag);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
flags.v8_flags = Some(svec!["deno", v8_seed_flag]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-06 18:13:06 -04:00
|
|
|
|
2019-06-09 09:08:20 -04:00
|
|
|
flags = parse_run_args(flags, matches);
|
2019-05-03 17:15:16 -04:00
|
|
|
// flags specific to "run" subcommand
|
|
|
|
if let Some(run_matches) = matches.subcommand_matches("run") {
|
2019-06-09 09:08:20 -04:00
|
|
|
flags = parse_run_args(flags.clone(), run_matches);
|
2019-06-05 13:44:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse permission specific matches Args and assign to DenoFlags.
|
|
|
|
/// This method is required because multiple subcommands use permission args.
|
2019-06-09 09:08:20 -04:00
|
|
|
fn parse_run_args(mut flags: DenoFlags, matches: &ArgMatches) -> DenoFlags {
|
2019-06-05 13:44:46 -04:00
|
|
|
if matches.is_present("allow-read") {
|
|
|
|
if matches.value_of("allow-read").is_some() {
|
|
|
|
let read_wl = matches.values_of("allow-read").unwrap();
|
|
|
|
let raw_read_whitelist: Vec<String> =
|
|
|
|
read_wl.map(std::string::ToString::to_string).collect();
|
|
|
|
flags.read_whitelist = resolve_paths(raw_read_whitelist);
|
|
|
|
debug!("read whitelist: {:#?}", &flags.read_whitelist);
|
|
|
|
} else {
|
2019-05-03 17:15:16 -04:00
|
|
|
flags.allow_read = true;
|
2019-06-05 13:44:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if matches.is_present("allow-write") {
|
|
|
|
if matches.value_of("allow-write").is_some() {
|
|
|
|
let write_wl = matches.values_of("allow-write").unwrap();
|
|
|
|
let raw_write_whitelist =
|
|
|
|
write_wl.map(std::string::ToString::to_string).collect();
|
|
|
|
flags.write_whitelist = resolve_paths(raw_write_whitelist);
|
|
|
|
debug!("write whitelist: {:#?}", &flags.write_whitelist);
|
|
|
|
} else {
|
2019-05-03 17:15:16 -04:00
|
|
|
flags.allow_write = true;
|
|
|
|
}
|
2019-06-05 13:44:46 -04:00
|
|
|
}
|
|
|
|
if matches.is_present("allow-net") {
|
|
|
|
if matches.value_of("allow-net").is_some() {
|
|
|
|
let net_wl = matches.values_of("allow-net").unwrap();
|
|
|
|
flags.net_whitelist =
|
|
|
|
net_wl.map(std::string::ToString::to_string).collect();
|
|
|
|
debug!("net whitelist: {:#?}", &flags.net_whitelist);
|
|
|
|
} else {
|
|
|
|
flags.allow_net = true;
|
2019-05-03 17:15:16 -04:00
|
|
|
}
|
|
|
|
}
|
2019-06-05 13:44:46 -04:00
|
|
|
if matches.is_present("allow-env") {
|
|
|
|
flags.allow_env = true;
|
|
|
|
}
|
|
|
|
if matches.is_present("allow-run") {
|
|
|
|
flags.allow_run = true;
|
|
|
|
}
|
|
|
|
if matches.is_present("allow-hrtime") {
|
|
|
|
flags.allow_hrtime = true;
|
|
|
|
}
|
|
|
|
if matches.is_present("allow-all") {
|
|
|
|
flags.allow_read = true;
|
|
|
|
flags.allow_env = true;
|
|
|
|
flags.allow_net = true;
|
|
|
|
flags.allow_run = true;
|
|
|
|
flags.allow_read = true;
|
|
|
|
flags.allow_write = true;
|
|
|
|
flags.allow_hrtime = true;
|
|
|
|
}
|
|
|
|
if matches.is_present("no-prompt") {
|
|
|
|
flags.no_prompts = true;
|
|
|
|
}
|
2019-07-20 09:19:06 -04:00
|
|
|
if matches.is_present("no-fetch") {
|
|
|
|
flags.no_fetch = true;
|
|
|
|
}
|
2019-06-09 09:08:20 -04:00
|
|
|
flags.import_map_path = matches.value_of("importmap").map(ToOwned::to_owned);
|
2019-05-03 17:15:16 -04:00
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
flags
|
2018-08-17 16:34:30 -04:00
|
|
|
}
|
|
|
|
|
2019-06-29 18:32:54 -04:00
|
|
|
/// Parse vector or arguments as DenoFlags.
|
|
|
|
///
|
|
|
|
/// This is very specialized utility that parses arguments passed after script URL.
|
|
|
|
///
|
|
|
|
/// Only dash (eg. `-r`) and double dash (eg. `--reload`) arguments are supported.
|
|
|
|
/// Arguments recognized as DenoFlags will be eaten.
|
|
|
|
/// Parsing stops after double dash `--` argument.
|
|
|
|
///
|
|
|
|
/// NOTE: this method ignores `-h/--help` and `-v/--version` flags.
|
|
|
|
fn parse_script_args(
|
|
|
|
args: Vec<String>,
|
|
|
|
mut flags: DenoFlags,
|
|
|
|
) -> (Vec<String>, DenoFlags) {
|
|
|
|
let mut argv = vec![];
|
|
|
|
let mut seen_double_dash = false;
|
|
|
|
|
|
|
|
// we have to iterate and parse argument one by one because clap returns error on any
|
|
|
|
// unrecognized argument
|
|
|
|
for arg in args.iter() {
|
|
|
|
if seen_double_dash {
|
|
|
|
argv.push(arg.to_string());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if arg == "--" {
|
|
|
|
seen_double_dash = true;
|
|
|
|
argv.push(arg.to_string());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-10 20:26:01 -04:00
|
|
|
if !arg.starts_with('-') || arg == "-" {
|
2019-06-29 18:32:54 -04:00
|
|
|
argv.push(arg.to_string());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let cli_app = create_cli_app();
|
|
|
|
// `get_matches_from_safe` returns error for `-h/-v` flags
|
|
|
|
let matches =
|
|
|
|
cli_app.get_matches_from_safe(vec!["deno".to_string(), arg.to_string()]);
|
|
|
|
|
|
|
|
if matches.is_ok() {
|
|
|
|
flags = parse_flags(&matches.unwrap(), Some(flags));
|
|
|
|
} else {
|
|
|
|
argv.push(arg.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(argv, flags)
|
|
|
|
}
|
|
|
|
|
2019-04-29 19:43:06 -04:00
|
|
|
/// Used for `deno fmt <files>...` subcommand
|
2019-07-09 18:59:13 -04:00
|
|
|
const PRETTIER_URL: &str = "https://deno.land/std@v0.11/prettier/main.ts";
|
2019-06-14 13:05:06 -04:00
|
|
|
/// Used for `deno install...` subcommand
|
2019-07-09 18:59:13 -04:00
|
|
|
const INSTALLER_URL: &str = "https://deno.land/std@v0.11/installer/mod.ts";
|
2019-04-29 19:43:06 -04:00
|
|
|
|
|
|
|
/// These are currently handled subcommands.
|
|
|
|
/// There is no "Help" subcommand because it's handled by `clap::App` itself.
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum DenoSubcommand {
|
2019-06-08 14:42:28 -04:00
|
|
|
Bundle,
|
2019-06-26 06:02:13 -04:00
|
|
|
Completions,
|
2019-04-29 19:43:06 -04:00
|
|
|
Eval,
|
|
|
|
Fetch,
|
|
|
|
Info,
|
2019-06-14 13:05:06 -04:00
|
|
|
Install,
|
2019-04-29 19:43:06 -04:00
|
|
|
Repl,
|
|
|
|
Run,
|
|
|
|
Types,
|
2019-05-03 17:15:16 -04:00
|
|
|
Version,
|
2019-05-03 16:24:09 -04:00
|
|
|
Xeval,
|
2019-04-29 19:43:06 -04:00
|
|
|
}
|
|
|
|
|
2019-06-11 14:38:19 -04:00
|
|
|
fn get_default_bundle_filename(source_file: &str) -> String {
|
2019-07-08 17:04:07 -04:00
|
|
|
let specifier = ModuleSpecifier::resolve_url_or_path(source_file).unwrap();
|
|
|
|
let path_segments = specifier.as_url().path_segments().unwrap();
|
2019-07-08 17:05:00 -04:00
|
|
|
let file_name = path_segments.filter(|s| !s.is_empty()).last().unwrap();
|
2019-07-08 17:04:07 -04:00
|
|
|
let file_stem = file_name.trim_end_matches(".ts").trim_end_matches(".js");
|
|
|
|
format!("{}.bundle.js", file_stem)
|
2019-06-11 14:38:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_default_bundle_filename() {
|
|
|
|
assert_eq!(get_default_bundle_filename("blah.ts"), "blah.bundle.js");
|
|
|
|
assert_eq!(
|
|
|
|
get_default_bundle_filename("http://example.com/blah.ts"),
|
|
|
|
"blah.bundle.js"
|
|
|
|
);
|
|
|
|
assert_eq!(get_default_bundle_filename("blah.js"), "blah.bundle.js");
|
|
|
|
assert_eq!(
|
|
|
|
get_default_bundle_filename("http://example.com/blah.js"),
|
|
|
|
"blah.bundle.js"
|
|
|
|
);
|
2019-07-08 17:05:00 -04:00
|
|
|
assert_eq!(
|
|
|
|
get_default_bundle_filename("http://zombo.com/stuff/"),
|
|
|
|
"stuff.bundle.js"
|
|
|
|
);
|
2019-06-11 14:38:19 -04:00
|
|
|
}
|
|
|
|
|
2019-04-29 19:43:06 -04:00
|
|
|
pub fn flags_from_vec(
|
|
|
|
args: Vec<String>,
|
|
|
|
) -> (DenoFlags, DenoSubcommand, Vec<String>) {
|
|
|
|
let cli_app = create_cli_app();
|
|
|
|
let matches = cli_app.get_matches_from(args);
|
|
|
|
let mut argv: Vec<String> = vec!["deno".to_string()];
|
2019-06-29 18:32:54 -04:00
|
|
|
let mut flags = parse_flags(&matches.clone(), None);
|
2019-04-29 19:43:06 -04:00
|
|
|
|
2019-07-27 05:20:40 -04:00
|
|
|
if flags.version {
|
|
|
|
return (flags, DenoSubcommand::Version, argv);
|
|
|
|
}
|
|
|
|
|
2019-04-29 19:43:06 -04:00
|
|
|
let subcommand = match matches.subcommand() {
|
2019-06-08 14:42:28 -04:00
|
|
|
("bundle", Some(bundle_match)) => {
|
|
|
|
flags.allow_write = true;
|
|
|
|
let source_file: &str = bundle_match.value_of("source_file").unwrap();
|
2019-06-11 14:38:19 -04:00
|
|
|
let out_file = bundle_match
|
|
|
|
.value_of("out_file")
|
|
|
|
.map(String::from)
|
|
|
|
.unwrap_or_else(|| get_default_bundle_filename(source_file));
|
2019-06-08 14:42:28 -04:00
|
|
|
argv.extend(vec![source_file.to_string(), out_file.to_string()]);
|
|
|
|
DenoSubcommand::Bundle
|
|
|
|
}
|
2019-06-26 06:02:13 -04:00
|
|
|
("completions", Some(completions_match)) => {
|
|
|
|
let shell: &str = completions_match.value_of("shell").unwrap();
|
2019-06-29 18:35:56 -04:00
|
|
|
let mut buf: Vec<u8> = vec![];
|
2019-06-26 06:02:13 -04:00
|
|
|
create_cli_app().gen_completions_to(
|
|
|
|
"deno",
|
|
|
|
Shell::from_str(shell).unwrap(),
|
2019-06-29 18:35:56 -04:00
|
|
|
&mut buf,
|
2019-06-26 06:02:13 -04:00
|
|
|
);
|
2019-06-29 18:35:56 -04:00
|
|
|
print!("{}", std::str::from_utf8(&buf).unwrap());
|
2019-06-26 06:02:13 -04:00
|
|
|
DenoSubcommand::Completions
|
|
|
|
}
|
2019-04-29 19:43:06 -04:00
|
|
|
("eval", Some(eval_match)) => {
|
2019-05-03 17:15:16 -04:00
|
|
|
flags.allow_net = true;
|
|
|
|
flags.allow_env = true;
|
|
|
|
flags.allow_run = true;
|
|
|
|
flags.allow_read = true;
|
|
|
|
flags.allow_write = true;
|
2019-05-23 12:28:29 -04:00
|
|
|
flags.allow_hrtime = true;
|
2019-04-29 19:43:06 -04:00
|
|
|
let code: &str = eval_match.value_of("code").unwrap();
|
|
|
|
argv.extend(vec![code.to_string()]);
|
|
|
|
DenoSubcommand::Eval
|
|
|
|
}
|
|
|
|
("fetch", Some(fetch_match)) => {
|
|
|
|
let file: &str = fetch_match.value_of("file").unwrap();
|
|
|
|
argv.extend(vec![file.to_string()]);
|
|
|
|
DenoSubcommand::Fetch
|
|
|
|
}
|
|
|
|
("fmt", Some(fmt_match)) => {
|
|
|
|
flags.allow_read = true;
|
|
|
|
flags.allow_write = true;
|
|
|
|
argv.push(PRETTIER_URL.to_string());
|
|
|
|
|
|
|
|
let files: Vec<String> = fmt_match
|
|
|
|
.values_of("files")
|
|
|
|
.unwrap()
|
|
|
|
.map(String::from)
|
|
|
|
.collect();
|
|
|
|
argv.extend(files);
|
|
|
|
|
2019-06-01 08:54:32 -04:00
|
|
|
if !fmt_match.is_present("stdout") {
|
|
|
|
// `deno fmt` writes to the files by default
|
|
|
|
argv.push("--write".to_string());
|
|
|
|
}
|
2019-05-16 10:11:35 -04:00
|
|
|
|
2019-04-29 19:43:06 -04:00
|
|
|
DenoSubcommand::Run
|
|
|
|
}
|
|
|
|
("info", Some(info_match)) => {
|
|
|
|
let file: &str = info_match.value_of("file").unwrap();
|
|
|
|
argv.extend(vec![file.to_string()]);
|
|
|
|
DenoSubcommand::Info
|
|
|
|
}
|
2019-06-14 13:05:06 -04:00
|
|
|
("install", Some(install_match)) => {
|
|
|
|
flags.allow_read = true;
|
|
|
|
flags.allow_write = true;
|
|
|
|
flags.allow_net = true;
|
|
|
|
flags.allow_env = true;
|
|
|
|
flags.allow_run = true;
|
|
|
|
argv.push(INSTALLER_URL.to_string());
|
|
|
|
|
2019-06-20 14:25:13 -04:00
|
|
|
if install_match.is_present("dir") {
|
|
|
|
let install_dir = install_match.value_of("dir").unwrap();
|
|
|
|
argv.push("--dir".to_string());
|
|
|
|
argv.push(install_dir.to_string());
|
|
|
|
}
|
|
|
|
|
2019-06-14 13:05:06 -04:00
|
|
|
let exe_name: &str = install_match.value_of("exe_name").unwrap();
|
2019-06-20 14:25:13 -04:00
|
|
|
argv.push(exe_name.to_string());
|
|
|
|
|
2019-06-14 13:05:06 -04:00
|
|
|
match install_match.subcommand() {
|
|
|
|
(script_url, Some(script_match)) => {
|
2019-06-20 14:25:13 -04:00
|
|
|
argv.push(script_url.to_string());
|
2019-06-15 10:08:11 -04:00
|
|
|
if script_match.is_present("") {
|
|
|
|
let flags: Vec<String> = script_match
|
|
|
|
.values_of("")
|
|
|
|
.unwrap()
|
|
|
|
.map(String::from)
|
|
|
|
.collect();
|
|
|
|
argv.extend(flags);
|
|
|
|
}
|
2019-06-14 13:05:06 -04:00
|
|
|
DenoSubcommand::Install
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2019-04-29 19:43:06 -04:00
|
|
|
("types", Some(_)) => DenoSubcommand::Types,
|
2019-05-03 17:15:16 -04:00
|
|
|
("run", Some(run_match)) => {
|
|
|
|
match run_match.subcommand() {
|
|
|
|
(script, Some(script_match)) => {
|
|
|
|
argv.extend(vec![script.to_string()]);
|
|
|
|
// check if there are any extra arguments that should
|
|
|
|
// be passed to script
|
|
|
|
if script_match.is_present("") {
|
|
|
|
let script_args: Vec<String> = script_match
|
|
|
|
.values_of("")
|
|
|
|
.unwrap()
|
|
|
|
.map(String::from)
|
|
|
|
.collect();
|
2019-06-29 18:32:54 -04:00
|
|
|
|
|
|
|
let (script_args, flags_) = parse_script_args(script_args, flags);
|
|
|
|
flags = flags_;
|
2019-05-03 17:15:16 -04:00
|
|
|
argv.extend(script_args);
|
|
|
|
}
|
|
|
|
DenoSubcommand::Run
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2019-05-03 16:24:09 -04:00
|
|
|
("xeval", Some(eval_match)) => {
|
2019-05-06 10:48:19 -04:00
|
|
|
flags.allow_net = true;
|
|
|
|
flags.allow_env = true;
|
|
|
|
flags.allow_run = true;
|
|
|
|
flags.allow_read = true;
|
|
|
|
flags.allow_write = true;
|
2019-05-23 12:28:29 -04:00
|
|
|
flags.allow_hrtime = true;
|
2019-05-03 16:24:09 -04:00
|
|
|
let code: &str = eval_match.value_of("code").unwrap();
|
|
|
|
flags.xeval_replvar =
|
|
|
|
Some(eval_match.value_of("replvar").unwrap_or("$").to_owned());
|
|
|
|
// Currently clap never escapes string,
|
|
|
|
// So -d "\n" won't expand to newline.
|
|
|
|
// Instead, do -d $'\n'
|
|
|
|
flags.xeval_delim = eval_match.value_of("delim").map(String::from);
|
|
|
|
argv.extend(vec![code.to_string()]);
|
|
|
|
DenoSubcommand::Xeval
|
|
|
|
}
|
2019-06-05 13:44:46 -04:00
|
|
|
(script, Some(script_match)) => {
|
|
|
|
argv.extend(vec![script.to_string()]);
|
|
|
|
// check if there are any extra arguments that should
|
|
|
|
// be passed to script
|
|
|
|
if script_match.is_present("") {
|
|
|
|
let script_args: Vec<String> = script_match
|
|
|
|
.values_of("")
|
|
|
|
.unwrap()
|
|
|
|
.map(String::from)
|
|
|
|
.collect();
|
2019-06-29 18:32:54 -04:00
|
|
|
|
|
|
|
let (script_args, flags_) = parse_script_args(script_args, flags);
|
|
|
|
flags = flags_;
|
2019-06-05 13:44:46 -04:00
|
|
|
argv.extend(script_args);
|
|
|
|
}
|
|
|
|
DenoSubcommand::Run
|
|
|
|
}
|
2019-05-03 17:15:16 -04:00
|
|
|
_ => {
|
|
|
|
flags.allow_net = true;
|
|
|
|
flags.allow_env = true;
|
|
|
|
flags.allow_run = true;
|
|
|
|
flags.allow_read = true;
|
|
|
|
flags.allow_write = true;
|
2019-05-23 12:28:29 -04:00
|
|
|
flags.allow_hrtime = true;
|
2019-05-03 17:15:16 -04:00
|
|
|
DenoSubcommand::Repl
|
2019-04-29 19:43:06 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
(flags, subcommand, argv)
|
|
|
|
}
|
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-08-17 16:34:30 -04:00
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
#[test]
|
2019-04-29 19:43:06 -04:00
|
|
|
fn test_flags_from_vec_1() {
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec!["deno", "version"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
version: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
2019-05-03 17:15:16 -04:00
|
|
|
assert_eq!(subcommand, DenoSubcommand::Version);
|
|
|
|
assert_eq!(argv, svec!["deno"]);
|
2019-07-27 05:20:40 -04:00
|
|
|
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec!["deno", "--version"]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
version: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Version);
|
|
|
|
assert_eq!(argv, svec!["deno"]);
|
|
|
|
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec!["deno", "-v"]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
version: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Version);
|
|
|
|
assert_eq!(argv, svec!["deno"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
}
|
2018-08-17 16:34:30 -04:00
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
#[test]
|
2019-04-29 19:43:06 -04:00
|
|
|
fn test_flags_from_vec_2() {
|
|
|
|
let (flags, subcommand, argv) =
|
2019-06-22 12:02:51 -04:00
|
|
|
flags_from_vec(svec!["deno", "-r", "run", "script.ts"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
reload: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
2019-04-29 19:43:06 -04:00
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
}
|
2018-08-24 15:26:40 -04:00
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
#[test]
|
2019-04-29 19:43:06 -04:00
|
|
|
fn test_flags_from_vec_3() {
|
2019-06-22 12:02:51 -04:00
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "run", "-r", "--allow-write", "script.ts"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
reload: true,
|
|
|
|
allow_write: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
2019-04-29 19:43:06 -04:00
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
}
|
2018-10-11 17:23:22 -04:00
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
#[test]
|
2019-04-29 19:43:06 -04:00
|
|
|
fn test_flags_from_vec_4() {
|
|
|
|
let (flags, subcommand, argv) =
|
2019-06-22 12:02:51 -04:00
|
|
|
flags_from_vec(svec!["deno", "-r", "run", "--allow-write", "script.ts"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
reload: true,
|
|
|
|
allow_write: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
2019-04-29 19:43:06 -04:00
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
}
|
2018-10-15 14:26:22 -04:00
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
#[test]
|
2019-04-29 19:43:06 -04:00
|
|
|
fn test_flags_from_vec_5() {
|
|
|
|
let (flags, subcommand, argv) =
|
2019-05-03 17:15:16 -04:00
|
|
|
flags_from_vec(svec!["deno", "--v8-options", "run", "script.ts"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
2019-05-03 08:59:52 -04:00
|
|
|
v8_flags: Some(svec!["deno", "--help"]),
|
2019-04-21 11:34:18 -04:00
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
2019-05-03 17:15:16 -04:00
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
2019-01-09 11:59:54 -05:00
|
|
|
|
2019-05-03 17:15:16 -04:00
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
|
|
|
"deno",
|
|
|
|
"--v8-flags=--expose-gc,--gc-stats=1",
|
|
|
|
"run",
|
|
|
|
"script.ts"
|
|
|
|
]);
|
2019-04-21 11:34:18 -04:00
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
2019-05-03 08:59:52 -04:00
|
|
|
v8_flags: Some(svec!["deno", "--expose-gc", "--gc-stats=1"]),
|
2019-04-21 11:34:18 -04:00
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
2019-05-03 17:15:16 -04:00
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-04-29 19:43:06 -04:00
|
|
|
fn test_flags_from_vec_6() {
|
2019-05-03 17:15:16 -04:00
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
|
|
|
"deno",
|
|
|
|
"run",
|
|
|
|
"--allow-net",
|
|
|
|
"gist.ts",
|
|
|
|
"--title",
|
|
|
|
"X"
|
|
|
|
]);
|
2019-04-21 11:34:18 -04:00
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_net: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
2019-04-29 19:43:06 -04:00
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "gist.ts", "--title", "X"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-04-29 19:43:06 -04:00
|
|
|
fn test_flags_from_vec_7() {
|
|
|
|
let (flags, subcommand, argv) =
|
2019-05-03 17:15:16 -04:00
|
|
|
flags_from_vec(svec!["deno", "run", "--allow-all", "gist.ts"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_net: true,
|
|
|
|
allow_env: true,
|
|
|
|
allow_run: true,
|
|
|
|
allow_read: true,
|
|
|
|
allow_write: true,
|
2019-05-23 12:28:29 -04:00
|
|
|
allow_hrtime: true,
|
2019-04-21 11:34:18 -04:00
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
2019-04-29 19:43:06 -04:00
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "gist.ts"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-04-29 19:43:06 -04:00
|
|
|
fn test_flags_from_vec_8() {
|
|
|
|
let (flags, subcommand, argv) =
|
2019-05-03 17:15:16 -04:00
|
|
|
flags_from_vec(svec!["deno", "run", "--allow-read", "gist.ts"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_read: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
2019-04-29 19:43:06 -04:00
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "gist.ts"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-04-29 19:43:06 -04:00
|
|
|
fn test_flags_from_vec_9() {
|
2019-05-23 12:28:29 -04:00
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "run", "--allow-hrtime", "script.ts"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
2019-05-23 12:28:29 -04:00
|
|
|
allow_hrtime: true,
|
2019-04-21 11:34:18 -04:00
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
2019-04-29 19:43:06 -04:00
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
}
|
2019-04-08 16:22:40 -04:00
|
|
|
|
2019-04-21 11:34:18 -04:00
|
|
|
#[test]
|
2019-04-29 19:43:06 -04:00
|
|
|
fn test_flags_from_vec_10() {
|
2019-06-29 18:32:54 -04:00
|
|
|
// notice that flags passed after double dash will not
|
2019-04-21 11:34:18 -04:00
|
|
|
// be parsed to DenoFlags but instead forwarded to
|
|
|
|
// script args as Deno.args
|
2019-04-29 19:43:06 -04:00
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
2019-04-21 11:34:18 -04:00
|
|
|
"deno",
|
2019-05-03 17:15:16 -04:00
|
|
|
"run",
|
2019-04-21 11:34:18 -04:00
|
|
|
"--allow-write",
|
|
|
|
"script.ts",
|
2019-06-29 18:32:54 -04:00
|
|
|
"--",
|
2019-04-21 11:34:18 -04:00
|
|
|
"-D",
|
|
|
|
"--allow-net"
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_write: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
2019-04-29 19:43:06 -04:00
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
2019-06-29 18:32:54 -04:00
|
|
|
assert_eq!(argv, svec!["deno", "script.ts", "--", "-D", "--allow-net"]);
|
2019-04-29 19:43:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_11() {
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "fmt", "script_1.ts", "script_2.ts"]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_write: true,
|
|
|
|
allow_read: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(
|
|
|
|
argv,
|
2019-05-16 10:11:35 -04:00
|
|
|
svec![
|
|
|
|
"deno",
|
|
|
|
PRETTIER_URL,
|
|
|
|
"script_1.ts",
|
|
|
|
"script_2.ts",
|
|
|
|
"--write"
|
|
|
|
]
|
2019-04-29 19:43:06 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_12() {
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec!["deno", "types"]);
|
|
|
|
assert_eq!(flags, DenoFlags::default());
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Types);
|
|
|
|
assert_eq!(argv, svec!["deno"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_13() {
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "fetch", "script.ts"]);
|
|
|
|
assert_eq!(flags, DenoFlags::default());
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Fetch);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_14() {
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "info", "script.ts"]);
|
|
|
|
assert_eq!(flags, DenoFlags::default());
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Info);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
2019-04-21 11:34:18 -04:00
|
|
|
}
|
2019-04-29 10:58:31 -04:00
|
|
|
|
2019-05-03 16:24:09 -04:00
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_15() {
|
2019-05-03 17:15:16 -04:00
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "run", "-c", "tsconfig.json", "script.ts"]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
config_path: Some("tsconfig.json".to_owned()),
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_16() {
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "eval", "'console.log(\"hello\")'"]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_net: true,
|
|
|
|
allow_env: true,
|
|
|
|
allow_run: true,
|
|
|
|
allow_read: true,
|
|
|
|
allow_write: true,
|
2019-05-23 12:28:29 -04:00
|
|
|
allow_hrtime: true,
|
2019-05-03 17:15:16 -04:00
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Eval);
|
|
|
|
assert_eq!(argv, svec!["deno", "'console.log(\"hello\")'"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_17() {
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec!["deno"]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_net: true,
|
|
|
|
allow_env: true,
|
|
|
|
allow_run: true,
|
|
|
|
allow_read: true,
|
|
|
|
allow_write: true,
|
2019-05-23 12:28:29 -04:00
|
|
|
allow_hrtime: true,
|
2019-05-03 17:15:16 -04:00
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Repl);
|
|
|
|
assert_eq!(argv, svec!["deno"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_18() {
|
2019-05-03 16:24:09 -04:00
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
|
|
|
"deno",
|
|
|
|
"xeval",
|
|
|
|
"-I",
|
|
|
|
"val",
|
|
|
|
"-d",
|
|
|
|
" ",
|
|
|
|
"console.log(val)"
|
|
|
|
]);
|
2019-05-06 10:48:19 -04:00
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_net: true,
|
|
|
|
allow_env: true,
|
|
|
|
allow_run: true,
|
|
|
|
allow_read: true,
|
|
|
|
allow_write: true,
|
2019-05-23 12:28:29 -04:00
|
|
|
allow_hrtime: true,
|
2019-05-06 10:48:19 -04:00
|
|
|
xeval_replvar: Some("val".to_owned()),
|
|
|
|
xeval_delim: Some(" ".to_owned()),
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
2019-05-03 16:24:09 -04:00
|
|
|
assert_eq!(subcommand, DenoSubcommand::Xeval);
|
|
|
|
assert_eq!(argv, svec!["deno", "console.log(val)"]);
|
|
|
|
}
|
2019-06-09 09:08:20 -04:00
|
|
|
|
2019-05-08 19:20:30 -04:00
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_19() {
|
2019-05-09 12:20:34 -04:00
|
|
|
use tempfile::TempDir;
|
|
|
|
let temp_dir = TempDir::new().expect("tempdir fail");
|
|
|
|
let (_, temp_dir_path) =
|
2019-07-17 18:15:30 -04:00
|
|
|
deno_fs::resolve_from_cwd(temp_dir.path().to_str().unwrap()).unwrap();
|
2019-05-09 12:20:34 -04:00
|
|
|
|
2019-05-08 19:20:30 -04:00
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
|
|
|
"deno",
|
|
|
|
"run",
|
2019-05-09 12:20:34 -04:00
|
|
|
format!("--allow-read={}", &temp_dir_path),
|
2019-05-08 19:20:30 -04:00
|
|
|
"script.ts"
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_read: false,
|
2019-05-09 12:20:34 -04:00
|
|
|
read_whitelist: svec![&temp_dir_path],
|
2019-05-08 19:20:30 -04:00
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
|
|
|
}
|
2019-06-09 09:08:20 -04:00
|
|
|
|
2019-05-08 19:20:30 -04:00
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_20() {
|
2019-05-09 12:20:34 -04:00
|
|
|
use tempfile::TempDir;
|
|
|
|
let temp_dir = TempDir::new().expect("tempdir fail");
|
|
|
|
let (_, temp_dir_path) =
|
2019-07-17 18:15:30 -04:00
|
|
|
deno_fs::resolve_from_cwd(temp_dir.path().to_str().unwrap()).unwrap();
|
2019-05-09 12:20:34 -04:00
|
|
|
|
2019-05-08 19:20:30 -04:00
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
|
|
|
"deno",
|
|
|
|
"run",
|
2019-05-09 12:20:34 -04:00
|
|
|
format!("--allow-write={}", &temp_dir_path),
|
2019-05-08 19:20:30 -04:00
|
|
|
"script.ts"
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_write: false,
|
2019-05-09 12:20:34 -04:00
|
|
|
write_whitelist: svec![&temp_dir_path],
|
2019-05-08 19:20:30 -04:00
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
|
|
|
}
|
2019-06-09 09:08:20 -04:00
|
|
|
|
2019-05-08 19:20:30 -04:00
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_21() {
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
|
|
|
"deno",
|
|
|
|
"run",
|
|
|
|
"--allow-net=127.0.0.1",
|
|
|
|
"script.ts"
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_net: false,
|
|
|
|
net_whitelist: svec!["127.0.0.1"],
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
|
|
|
}
|
2019-06-01 08:54:32 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_22() {
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
|
|
|
"deno",
|
|
|
|
"fmt",
|
|
|
|
"--stdout",
|
|
|
|
"script_1.ts",
|
|
|
|
"script_2.ts"
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_write: true,
|
|
|
|
allow_read: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(
|
|
|
|
argv,
|
|
|
|
svec!["deno", PRETTIER_URL, "script_1.ts", "script_2.ts"]
|
|
|
|
);
|
|
|
|
}
|
2019-06-05 13:44:46 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_23() {
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec!["deno", "script.ts"]);
|
|
|
|
assert_eq!(flags, DenoFlags::default());
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_24() {
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "--allow-net", "--allow-read", "script.ts"]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_net: true,
|
|
|
|
allow_read: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_25() {
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
|
|
|
"deno",
|
|
|
|
"-r",
|
|
|
|
"--allow-net",
|
|
|
|
"run",
|
|
|
|
"--allow-read",
|
|
|
|
"script.ts"
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
reload: true,
|
|
|
|
allow_net: true,
|
|
|
|
allow_read: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
|
|
|
}
|
2019-06-08 14:42:28 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_26() {
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "bundle", "source.ts", "bundle.js"]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_write: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Bundle);
|
|
|
|
assert_eq!(argv, svec!["deno", "source.ts", "bundle.js"])
|
|
|
|
}
|
2019-06-09 09:08:20 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_27() {
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
|
|
|
"deno",
|
|
|
|
"run",
|
|
|
|
"--importmap=importmap.json",
|
|
|
|
"script.ts"
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
import_map_path: Some("importmap.json".to_owned()),
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
|
|
|
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "--importmap=importmap.json", "script.ts"]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
import_map_path: Some("importmap.json".to_owned()),
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
2019-07-27 10:37:03 -04:00
|
|
|
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
|
|
|
"deno",
|
|
|
|
"fetch",
|
|
|
|
"--importmap=importmap.json",
|
|
|
|
"script.ts"
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
import_map_path: Some("importmap.json".to_owned()),
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Fetch);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
2019-06-09 09:08:20 -04:00
|
|
|
}
|
2019-06-11 10:34:39 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_28() {
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "--seed", "250", "run", "script.ts"]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
seed: Some(250 as u64),
|
|
|
|
v8_flags: Some(svec!["deno", "--random-seed=250"]),
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
2019-06-15 10:08:11 -04:00
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
2019-06-11 10:34:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_29() {
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
|
|
|
"deno",
|
|
|
|
"--seed",
|
|
|
|
"250",
|
|
|
|
"--v8-flags=--expose-gc",
|
|
|
|
"run",
|
|
|
|
"script.ts"
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
seed: Some(250 as u64),
|
|
|
|
v8_flags: Some(svec!["deno", "--expose-gc", "--random-seed=250"]),
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
2019-06-15 10:08:11 -04:00
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_30() {
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
|
|
|
"deno",
|
|
|
|
"install",
|
|
|
|
"deno_colors",
|
|
|
|
"https://deno.land/std/examples/colors.ts"
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_write: true,
|
|
|
|
allow_net: true,
|
|
|
|
allow_read: true,
|
|
|
|
allow_env: true,
|
|
|
|
allow_run: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Install);
|
|
|
|
assert_eq!(
|
|
|
|
argv,
|
|
|
|
svec![
|
|
|
|
"deno",
|
|
|
|
INSTALLER_URL,
|
|
|
|
"deno_colors",
|
|
|
|
"https://deno.land/std/examples/colors.ts"
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
|
|
|
"deno",
|
|
|
|
"install",
|
|
|
|
"file_server",
|
|
|
|
"https://deno.land/std/http/file_server.ts",
|
|
|
|
"--allow-net",
|
|
|
|
"--allow-read"
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_write: true,
|
|
|
|
allow_net: true,
|
|
|
|
allow_read: true,
|
|
|
|
allow_env: true,
|
|
|
|
allow_run: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Install);
|
|
|
|
assert_eq!(
|
|
|
|
argv,
|
|
|
|
svec![
|
|
|
|
"deno",
|
|
|
|
INSTALLER_URL,
|
|
|
|
"file_server",
|
|
|
|
"https://deno.land/std/http/file_server.ts",
|
|
|
|
"--allow-net",
|
|
|
|
"--allow-read"
|
|
|
|
]
|
|
|
|
);
|
2019-06-20 14:25:13 -04:00
|
|
|
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
|
|
|
"deno",
|
|
|
|
"install",
|
|
|
|
"-d",
|
|
|
|
"/usr/local/bin",
|
|
|
|
"file_server",
|
|
|
|
"https://deno.land/std/http/file_server.ts",
|
|
|
|
"--allow-net",
|
|
|
|
"--allow-read"
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_write: true,
|
|
|
|
allow_net: true,
|
|
|
|
allow_read: true,
|
|
|
|
allow_env: true,
|
|
|
|
allow_run: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Install);
|
|
|
|
assert_eq!(
|
|
|
|
argv,
|
|
|
|
svec![
|
|
|
|
"deno",
|
|
|
|
INSTALLER_URL,
|
|
|
|
"--dir",
|
|
|
|
"/usr/local/bin",
|
|
|
|
"file_server",
|
|
|
|
"https://deno.land/std/http/file_server.ts",
|
|
|
|
"--allow-net",
|
|
|
|
"--allow-read"
|
|
|
|
]
|
|
|
|
);
|
2019-06-11 10:34:39 -04:00
|
|
|
}
|
2019-06-22 12:02:51 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_31() {
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "--log-level=debug", "script.ts"]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
log_level: Some(Level::Debug),
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"])
|
|
|
|
}
|
2019-06-26 06:02:13 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_32() {
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "completions", "bash"]);
|
|
|
|
assert_eq!(flags, DenoFlags::default());
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Completions);
|
|
|
|
assert_eq!(argv, svec!["deno"])
|
|
|
|
}
|
2019-06-29 18:32:54 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_33() {
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "script.ts", "--allow-read", "--allow-net"]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_net: true,
|
|
|
|
allow_read: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"]);
|
|
|
|
|
|
|
|
let (flags, subcommand, argv) = flags_from_vec(svec![
|
|
|
|
"deno",
|
|
|
|
"--allow-read",
|
|
|
|
"run",
|
|
|
|
"script.ts",
|
|
|
|
"--allow-net",
|
|
|
|
"-r",
|
|
|
|
"--help",
|
|
|
|
"--foo",
|
|
|
|
"bar"
|
|
|
|
]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
allow_net: true,
|
|
|
|
allow_read: true,
|
|
|
|
reload: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts", "--help", "--foo", "bar"]);
|
2019-07-10 20:26:01 -04:00
|
|
|
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "script.ts", "foo", "bar"]);
|
|
|
|
assert_eq!(flags, DenoFlags::default());
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts", "foo", "bar"]);
|
|
|
|
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "script.ts", "-"]);
|
|
|
|
assert_eq!(flags, DenoFlags::default());
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts", "-"]);
|
|
|
|
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "script.ts", "-", "foo", "bar"]);
|
|
|
|
assert_eq!(flags, DenoFlags::default());
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts", "-", "foo", "bar"]);
|
2019-06-29 18:32:54 -04:00
|
|
|
}
|
2019-07-20 09:19:06 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_34() {
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "--no-fetch", "script.ts"]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
no_fetch: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"])
|
|
|
|
}
|
2019-07-31 11:02:20 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_flags_from_vec_35() {
|
|
|
|
let (flags, subcommand, argv) =
|
|
|
|
flags_from_vec(svec!["deno", "--current-thread", "script.ts"]);
|
|
|
|
assert_eq!(
|
|
|
|
flags,
|
|
|
|
DenoFlags {
|
|
|
|
current_thread: true,
|
|
|
|
..DenoFlags::default()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(subcommand, DenoSubcommand::Run);
|
|
|
|
assert_eq!(argv, svec!["deno", "script.ts"])
|
|
|
|
}
|
2019-04-08 16:22:40 -04:00
|
|
|
}
|