1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
#![deny(warnings)]
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate futures;
#[macro_use]
extern crate serde_json;
extern crate clap;
extern crate deno_core;
extern crate indexmap;
#[cfg(unix)]
extern crate nix;
extern crate rand;
extern crate serde;
extern crate serde_derive;
extern crate tokio;
extern crate url;
mod checksum;
pub mod colors;
pub mod compilers;
pub mod deno_dir;
pub mod deno_error;
pub mod diagnostics;
mod disk_cache;
mod file_fetcher;
pub mod flags;
mod fmt;
pub mod fmt_errors;
mod fs;
mod global_state;
mod global_timer;
mod http_util;
mod import_map;
pub mod installer;
mod js;
mod lockfile;
mod metrics;
pub mod msg;
pub mod ops;
pub mod permissions;
mod progress;
mod repl;
pub mod resolve_addr;
mod shell;
pub mod signal;
pub mod source_maps;
mod startup_data;
pub mod state;
pub mod test_util;
mod tokio_util;
pub mod version;
mod web_worker;
pub mod worker;
use crate::compilers::TargetLib;
use crate::deno_error::js_check;
use crate::deno_error::{print_err_and_exit, print_msg_and_exit};
use crate::global_state::GlobalState;
use crate::ops::io::get_stdio;
use crate::state::State;
use crate::worker::MainWorker;
use deno_core::v8_set_flags;
use deno_core::ErrBox;
use deno_core::ModuleSpecifier;
use flags::DenoFlags;
use flags::DenoSubcommand;
use log::Level;
use log::Metadata;
use log::Record;
use std::env;
static LOGGER: Logger = Logger;
struct Logger;
impl log::Log for Logger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= log::max_level()
}
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
let mut target = record.target().to_string();
if let Some(line_no) = record.line() {
target.push_str(":");
target.push_str(&line_no.to_string());
}
println!("{} RS - {} - {}", record.level(), target, record.args());
}
}
fn flush(&self) {}
}
fn create_global_state(flags: DenoFlags) -> GlobalState {
GlobalState::new(flags)
.map_err(deno_error::print_err_and_exit)
.unwrap()
}
fn create_main_worker(
global_state: GlobalState,
main_module: ModuleSpecifier,
) -> MainWorker {
let state = State::new(global_state, None, main_module)
.map_err(deno_error::print_err_and_exit)
.unwrap();
let state_ = state.clone();
{
let mut state = state_.borrow_mut();
let (stdin, stdout, stderr) = get_stdio();
state.resource_table.add("stdin", Box::new(stdin));
state.resource_table.add("stdout", Box::new(stdout));
state.resource_table.add("stderr", Box::new(stderr));
}
MainWorker::new("main".to_string(), startup_data::deno_isolate_init(), state)
}
fn types_command() {
let types = format!(
"{}\n{}\n{}",
crate::js::DENO_NS_LIB,
crate::js::SHARED_GLOBALS_LIB,
crate::js::WINDOW_LIB
);
use std::io::Write;
let _r = std::io::stdout().write_all(types.as_bytes());
// TODO(ry) Only ignore SIGPIPE. Currently ignoring all errors.
}
fn print_cache_info(state: &GlobalState) {
println!(
"{} {:?}",
colors::bold("DENO_DIR location:".to_string()),
state.dir.root
);
println!(
"{} {:?}",
colors::bold("Remote modules cache:".to_string()),
state.dir.deps_cache.location
);
println!(
"{} {:?}",
colors::bold("TypeScript compiler cache:".to_string()),
state.dir.gen_cache.location
);
}
async fn print_file_info(
worker: &MainWorker,
module_specifier: ModuleSpecifier,
) {
let global_state = worker.state.borrow().global_state.clone();
let maybe_source_file = global_state
.file_fetcher
.fetch_source_file_async(&module_specifier, None)
.await;
if let Err(err) = maybe_source_file {
println!("{}", err);
return;
}
let out = maybe_source_file.unwrap();
println!(
"{} {}",
colors::bold("local:".to_string()),
out.filename.to_str().unwrap()
);
println!(
"{} {}",
colors::bold("type:".to_string()),
msg::enum_name_media_type(out.media_type)
);
let module_specifier_ = module_specifier.clone();
let maybe_compiled = global_state
.clone()
.fetch_compiled_module(module_specifier_, None, TargetLib::Main)
.await;
if let Err(e) = maybe_compiled {
debug!("compiler error exiting!");
eprintln!("\n{}", e.to_string());
std::process::exit(1);
}
if out.media_type == msg::MediaType::TypeScript
|| (out.media_type == msg::MediaType::JavaScript
&& global_state.ts_compiler.compile_js)
{
let compiled_source_file = global_state
.ts_compiler
.get_compiled_source_file(&out.url)
.unwrap();
println!(
"{} {}",
colors::bold("compiled:".to_string()),
compiled_source_file.filename.to_str().unwrap(),
);
}
if let Ok(source_map) = global_state
.clone()
.ts_compiler
.get_source_map_file(&module_specifier)
{
println!(
"{} {}",
colors::bold("map:".to_string()),
source_map.filename.to_str().unwrap()
);
}
if let Some(deps) = worker.isolate.modules.deps(&module_specifier) {
println!("{}{}", colors::bold("deps:\n".to_string()), deps.name);
if let Some(ref depsdeps) = deps.deps {
for d in depsdeps {
println!("{}", d);
}
}
} else {
println!(
"{} cannot retrieve full dependency graph",
colors::bold("deps:".to_string()),
);
}
}
async fn info_command(flags: DenoFlags, file: Option<String>) {
let global_state = create_global_state(flags);
// If it was just "deno info" print location of caches and exit
if file.is_none() {
return print_cache_info(&global_state);
}
// Setup runtime.
let main_module = ModuleSpecifier::resolve_url_or_path(&file.unwrap())
.expect("Bad specifier");
let mut worker = create_main_worker(global_state, main_module.clone());
// TODO(bartlomieju): not needed?
js_check(worker.execute("bootstrapMainRuntime()"));
let main_result = worker.execute_mod_async(&main_module, None, true).await;
if let Err(e) = main_result {
print_err_and_exit(e);
}
print_file_info(&worker, main_module.clone()).await;
let result = (&mut *worker).await;
js_check(result);
}
async fn install_command(
flags: DenoFlags,
dir: Option<String>,
exe_name: String,
module_url: String,
args: Vec<String>,
force: bool,
) {
// Firstly fetch and compile module, this
// ensures the module exists.
let mut fetch_flags = flags.clone();
fetch_flags.reload = true;
fetch_command(fetch_flags, vec![module_url.to_string()]).await;
let install_result =
installer::install(flags, dir, &exe_name, &module_url, args, force);
if let Err(e) = install_result {
print_msg_and_exit(&e.to_string());
}
}
async fn fetch_command(flags: DenoFlags, files: Vec<String>) {
let main_module =
ModuleSpecifier::resolve_url_or_path("./__$deno$fetch.ts").unwrap();
let global_state = create_global_state(flags);
let mut worker =
create_main_worker(global_state.clone(), main_module.clone());
// TODO(bartlomieju): not needed?
js_check(worker.execute("bootstrapMainRuntime()"));
for file in files {
let specifier = ModuleSpecifier::resolve_url_or_path(&file).unwrap();
let result = worker.execute_mod_async(&specifier, None, true).await;
js_check(result);
}
if global_state.flags.lock_write {
if let Some(ref lockfile) = global_state.lockfile {
let g = lockfile.lock().unwrap();
if let Err(e) = g.write() {
print_err_and_exit(ErrBox::from(e));
}
} else {
eprintln!("--lock flag must be specified when using --lock-write");
std::process::exit(11);
}
}
}
async fn eval_command(flags: DenoFlags, code: String) {
// Force TypeScript compile.
let main_module =
ModuleSpecifier::resolve_url_or_path("./__$deno$eval.ts").unwrap();
let global_state = create_global_state(flags);
let mut worker = create_main_worker(global_state, main_module.clone());
js_check(worker.execute("bootstrapMainRuntime()"));
debug!("main_module {}", &main_module);
let exec_result = worker
.execute_mod_async(&main_module, Some(code), false)
.await;
if let Err(e) = exec_result {
print_err_and_exit(e);
}
js_check(worker.execute("window.dispatchEvent(new Event('load'))"));
let result = (&mut *worker).await;
js_check(result);
js_check(worker.execute("window.dispatchEvent(new Event('unload'))"));
}
async fn bundle_command(
flags: DenoFlags,
source_file: String,
out_file: Option<String>,
) {
debug!(">>>>> bundle_async START");
let source_file_specifier =
ModuleSpecifier::resolve_url_or_path(&source_file).expect("Bad specifier");
let global_state = create_global_state(flags);
let mut worker =
create_main_worker(global_state.clone(), source_file_specifier.clone());
// NOTE: we need to poll `worker` otherwise TS compiler worker won't run properly
let result = (&mut *worker).await;
js_check(result);
let bundle_result = global_state
.ts_compiler
.bundle_async(
global_state.clone(),
source_file_specifier.to_string(),
out_file,
)
.await;
if let Err(err) = bundle_result {
debug!("diagnostics returned, exiting!");
eprintln!("");
print_err_and_exit(err);
}
debug!(">>>>> bundle_async END");
}
async fn run_repl(flags: DenoFlags) {
let main_module =
ModuleSpecifier::resolve_url_or_path("./__$deno$repl.ts").unwrap();
let global_state = create_global_state(flags);
let mut worker = create_main_worker(global_state, main_module);
js_check(worker.execute("bootstrapMainRuntime()"));
loop {
let result = (&mut *worker).await;
if let Err(err) = result {
eprintln!("{}", err.to_string());
}
}
}
async fn run_script(flags: DenoFlags, script: String) {
let main_module = ModuleSpecifier::resolve_url_or_path(&script).unwrap();
let global_state = create_global_state(flags);
let mut worker =
create_main_worker(global_state.clone(), main_module.clone());
// Setup runtime.
js_check(worker.execute("bootstrapMainRuntime()"));
debug!("main_module {}", main_module);
let mod_result = worker.execute_mod_async(&main_module, None, false).await;
if let Err(err) = mod_result {
print_err_and_exit(err);
}
if global_state.flags.lock_write {
if let Some(ref lockfile) = global_state.lockfile {
let g = lockfile.lock().unwrap();
if let Err(e) = g.write() {
print_err_and_exit(ErrBox::from(e));
}
} else {
eprintln!("--lock flag must be specified when using --lock-write");
std::process::exit(11);
}
}
js_check(worker.execute("window.dispatchEvent(new Event('load'))"));
let result = (&mut *worker).await;
js_check(result);
js_check(worker.execute("window.dispatchEvent(new Event('unload'))"));
}
async fn fmt_command(files: Option<Vec<String>>, check: bool) {
fmt::format_files(files, check);
}
pub fn main() {
#[cfg(windows)]
ansi_term::enable_ansi_support().ok(); // For Windows 10
log::set_logger(&LOGGER).unwrap();
let args: Vec<String> = env::args().collect();
let flags = flags::flags_from_vec(args);
if let Some(ref v8_flags) = flags.v8_flags {
let mut v8_flags_ = v8_flags.clone();
v8_flags_.insert(0, "UNUSED_BUT_NECESSARY_ARG0".to_string());
v8_set_flags(v8_flags_);
}
let log_level = match flags.log_level {
Some(level) => level,
None => Level::Warn,
};
log::set_max_level(log_level.to_level_filter());
let fut = async move {
match flags.clone().subcommand {
DenoSubcommand::Bundle {
source_file,
out_file,
} => bundle_command(flags, source_file, out_file).await,
DenoSubcommand::Completions { buf } => {
print!("{}", std::str::from_utf8(&buf).unwrap());
}
DenoSubcommand::Eval { code } => eval_command(flags, code).await,
DenoSubcommand::Fetch { files } => fetch_command(flags, files).await,
DenoSubcommand::Format { check, files } => {
fmt_command(files, check).await
}
DenoSubcommand::Info { file } => info_command(flags, file).await,
DenoSubcommand::Install {
dir,
exe_name,
module_url,
args,
force,
} => install_command(flags, dir, exe_name, module_url, args, force).await,
DenoSubcommand::Repl => run_repl(flags).await,
DenoSubcommand::Run { script } => run_script(flags, script).await,
DenoSubcommand::Types => types_command(),
_ => panic!("bad subcommand"),
}
};
tokio_util::run_basic(fut);
}
|