subshell 0.1.1

Substrate shell
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

mod auth_tokens;
mod cache;
mod cdp;
mod checksum;
mod compat;
mod config_file;
mod deno_dir;
mod diagnostics;
mod diff;
mod disk_cache;
mod display;
mod emit;
mod errors;
mod file_fetcher;
mod file_watcher;
mod flags;
mod flags_allow_net;
mod fmt_errors;
mod fs_util;
mod graph_util;
mod http_cache;
mod http_util;
mod lockfile;
mod logger;
mod lsp;
mod module_loader;
mod ops;
mod proc_state;
mod resolver;
mod standalone;
mod text_encoding;
mod tools;
mod tsc;
mod unix_util;
mod version;
mod windows_util;

use crate::file_fetcher::File;
use crate::file_watcher::ResolutionResult;
use crate::flags::BenchFlags;
use crate::flags::BundleFlags;
use crate::flags::CacheFlags;
use crate::flags::CheckFlags;
use crate::flags::CompileFlags;
use crate::flags::CompletionsFlags;
use crate::flags::CoverageFlags;
use crate::flags::DenoSubcommand;
use crate::flags::DocFlags;
use crate::flags::EvalFlags;
use crate::flags::Flags;
use crate::flags::FmtFlags;
use crate::flags::InfoFlags;
use crate::flags::InstallFlags;
use crate::flags::LintFlags;
use crate::flags::ReplFlags;
use crate::flags::RunFlags;
use crate::flags::TaskFlags;
use crate::flags::TestFlags;
use crate::flags::TypeCheckMode;
use crate::flags::UninstallFlags;
use crate::flags::UpgradeFlags;
use crate::flags::VendorFlags;
use crate::fmt_errors::format_js_error;
use crate::graph_util::graph_lock_or_exit;
use crate::graph_util::graph_valid;
use crate::module_loader::CliModuleLoader;
use crate::proc_state::ProcState;
use crate::resolver::ImportMapResolver;
use crate::resolver::JsxResolver;
use deno_ast::MediaType;
use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::error::JsError;
use deno_core::futures::future::FutureExt;
use deno_core::futures::future::LocalFutureObj;
use deno_core::futures::Future;
use deno_core::located_script_name;
use deno_core::parking_lot::RwLock;
use deno_core::resolve_url_or_path;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_core::v8_set_flags;
use deno_core::Extension;
use deno_core::ModuleSpecifier;
use deno_runtime::colors;
use deno_runtime::ops::worker_host::CreateWebWorkerCb;
use deno_runtime::ops::worker_host::PreloadModuleCb;
use deno_runtime::permissions::Permissions;
use deno_runtime::tokio_util::run_basic;
use deno_runtime::web_worker::WebWorker;
use deno_runtime::web_worker::WebWorkerOptions;
use deno_runtime::worker::MainWorker;
use deno_runtime::worker::WorkerOptions;
use deno_runtime::BootstrapOptions;
use log::debug;
use log::info;
use std::env;
use std::io::Read;
use std::io::Write;
use std::iter::once;
use std::path::PathBuf;
use std::pin::Pin;
use std::sync::Arc;

fn create_web_worker_preload_module_callback(
  ps: ProcState,
) -> Arc<PreloadModuleCb> {
  let compat = ps.flags.compat;

  Arc::new(move |mut worker| {
    let fut = async move {
      if compat {
        worker.execute_side_module(&compat::GLOBAL_URL).await?;
        worker.execute_side_module(&compat::MODULE_URL).await?;
      }

      Ok(worker)
    };
    LocalFutureObj::new(Box::new(fut))
  })
}

fn create_web_worker_callback(
  ps: ProcState,
  stdio: deno_runtime::ops::io::Stdio,
) -> Arc<CreateWebWorkerCb> {
  Arc::new(move |args| {
    let maybe_inspector_server = ps.maybe_inspector_server.clone();

    let module_loader = CliModuleLoader::new_for_worker(
      ps.clone(),
      args.parent_permissions.clone(),
    );
    let create_web_worker_cb =
      create_web_worker_callback(ps.clone(), stdio.clone());
    let preload_module_cb =
      create_web_worker_preload_module_callback(ps.clone());

    let extensions = ops::cli_exts(ps.clone());

    let options = WebWorkerOptions {
      bootstrap: BootstrapOptions {
        args: ps.flags.argv.clone(),
        cpu_count: std::thread::available_parallelism()
          .map(|p| p.get())
          .unwrap_or(1),
        debug_flag: ps
          .flags
          .log_level
          .map_or(false, |l| l == log::Level::Debug),
        enable_testing_features: ps.flags.enable_testing_features,
        location: Some(args.main_module.clone()),
        no_color: !colors::use_color(),
        is_tty: colors::is_tty(),
        runtime_version: version::deno(),
        ts_version: version::TYPESCRIPT.to_string(),
        unstable: ps.flags.unstable,
        user_agent: version::get_user_agent(),
      },
      extensions,
      unsafely_ignore_certificate_errors: ps
        .flags
        .unsafely_ignore_certificate_errors
        .clone(),
      root_cert_store: ps.root_cert_store.clone(),
      seed: ps.flags.seed,
      module_loader,
      create_web_worker_cb,
      preload_module_cb,
      format_js_error_fn: Some(Arc::new(format_js_error)),
      source_map_getter: Some(Box::new(ps.clone())),
      worker_type: args.worker_type,
      maybe_inspector_server,
      get_error_class_fn: Some(&crate::errors::get_error_class_name),
      blob_store: ps.blob_store.clone(),
      broadcast_channel: ps.broadcast_channel.clone(),
      shared_array_buffer_store: Some(ps.shared_array_buffer_store.clone()),
      compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()),
      stdio: stdio.clone(),
    };

    WebWorker::bootstrap_from_options(
      args.name,
      args.permissions,
      args.main_module,
      args.worker_id,
      options,
    )
  })
}

pub fn create_main_worker(
  ps: &ProcState,
  main_module: ModuleSpecifier,
  permissions: Permissions,
  mut custom_extensions: Vec<Extension>,
  stdio: deno_runtime::ops::io::Stdio,
) -> MainWorker {
  let module_loader = CliModuleLoader::new(ps.clone());

  let maybe_inspector_server = ps.maybe_inspector_server.clone();
  let should_break_on_first_statement = ps.flags.inspect_brk.is_some();

  let create_web_worker_cb =
    create_web_worker_callback(ps.clone(), stdio.clone());
  let web_worker_preload_module_cb =
    create_web_worker_preload_module_callback(ps.clone());

  let maybe_storage_key = if let Some(location) = &ps.flags.location {
    // if a location is set, then the ascii serialization of the location is
    // used, unless the origin is opaque, and then no storage origin is set, as
    // we can't expect the origin to be reproducible
    let storage_origin = location.origin().ascii_serialization();
    if storage_origin == "null" {
      None
    } else {
      Some(storage_origin)
    }
  } else if let Some(config_file) = &ps.maybe_config_file {
    // otherwise we will use the path to the config file
    Some(config_file.specifier.to_string())
  } else {
    // otherwise we will use the path to the main module
    Some(main_module.to_string())
  };

  let origin_storage_dir = maybe_storage_key.map(|key| {
    ps.dir
      .root
      // TODO(@crowlKats): change to origin_data for 2.0
      .join("location_data")
      .join(checksum::gen(&[key.as_bytes()]))
  });

  let mut extensions = ops::cli_exts(ps.clone());
  extensions.append(&mut custom_extensions);

  let options = WorkerOptions {
    bootstrap: BootstrapOptions {
      args: ps.flags.argv.clone(),
      cpu_count: std::thread::available_parallelism()
        .map(|p| p.get())
        .unwrap_or(1),
      debug_flag: ps.flags.log_level.map_or(false, |l| l == log::Level::Debug),
      enable_testing_features: ps.flags.enable_testing_features,
      location: ps.flags.location.clone(),
      no_color: !colors::use_color(),
      is_tty: colors::is_tty(),
      runtime_version: version::deno(),
      ts_version: version::TYPESCRIPT.to_string(),
      unstable: ps.flags.unstable,
      user_agent: version::get_user_agent(),
    },
    extensions,
    unsafely_ignore_certificate_errors: ps
      .flags
      .unsafely_ignore_certificate_errors
      .clone(),
    root_cert_store: ps.root_cert_store.clone(),
    seed: ps.flags.seed,
    source_map_getter: Some(Box::new(ps.clone())),
    format_js_error_fn: Some(Arc::new(format_js_error)),
    create_web_worker_cb,
    web_worker_preload_module_cb,
    maybe_inspector_server,
    should_break_on_first_statement,
    module_loader,
    get_error_class_fn: Some(&crate::errors::get_error_class_name),
    origin_storage_dir,
    blob_store: ps.blob_store.clone(),
    broadcast_channel: ps.broadcast_channel.clone(),
    shared_array_buffer_store: Some(ps.shared_array_buffer_store.clone()),
    compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()),
    stdio,
  };

  MainWorker::bootstrap_from_options(main_module, permissions, options)
}

pub fn write_to_stdout_ignore_sigpipe(
  bytes: &[u8],
) -> Result<(), std::io::Error> {
  use std::io::ErrorKind;

  match std::io::stdout().write_all(bytes) {
    Ok(()) => Ok(()),
    Err(e) => match e.kind() {
      ErrorKind::BrokenPipe => Ok(()),
      _ => Err(e),
    },
  }
}

pub fn write_json_to_stdout<T>(value: &T) -> Result<(), AnyError>
where
  T: ?Sized + serde::ser::Serialize,
{
  let mut writer = std::io::BufWriter::new(std::io::stdout());
  serde_json::to_writer_pretty(&mut writer, value)?;
  writeln!(&mut writer)?;
  Ok(())
}

fn print_cache_info(
  state: &ProcState,
  json: bool,
  location: Option<&deno_core::url::Url>,
) -> Result<(), AnyError> {
  let deno_dir = &state.dir.root;
  let modules_cache = &state.file_fetcher.get_http_cache_location();
  let typescript_cache = &state.dir.gen_cache.location;
  let registry_cache =
    &state.dir.root.join(lsp::language_server::REGISTRIES_PATH);
  let mut origin_dir = state.dir.root.join("location_data");

  if let Some(location) = &location {
    origin_dir =
      origin_dir.join(&checksum::gen(&[location.to_string().as_bytes()]));
  }

  let local_storage_dir = origin_dir.join("local_storage");

  if json {
    let mut output = json!({
      "denoDir": deno_dir,
      "modulesCache": modules_cache,
      "typescriptCache": typescript_cache,
      "registryCache": registry_cache,
      "originStorage": origin_dir,
    });

    if location.is_some() {
      output["localStorage"] = serde_json::to_value(local_storage_dir)?;
    }

    write_json_to_stdout(&output)
  } else {
    println!(
      "{} {}",
      colors::bold("DENO_DIR location:"),
      deno_dir.display()
    );
    println!(
      "{} {}",
      colors::bold("Remote modules cache:"),
      modules_cache.display()
    );
    println!(
      "{} {}",
      colors::bold("Emitted modules cache:"),
      typescript_cache.display()
    );
    println!(
      "{} {}",
      colors::bold("Language server registries cache:"),
      registry_cache.display(),
    );
    println!(
      "{} {}",
      colors::bold("Origin storage:"),
      origin_dir.display()
    );
    if location.is_some() {
      println!(
        "{} {}",
        colors::bold("Local Storage:"),
        local_storage_dir.display(),
      );
    }
    Ok(())
  }
}

pub fn get_types(unstable: bool) -> String {
  let mut types = vec![
    crate::tsc::DENO_NS_LIB,
    crate::tsc::DENO_CONSOLE_LIB,
    crate::tsc::DENO_URL_LIB,
    crate::tsc::DENO_WEB_LIB,
    crate::tsc::DENO_FETCH_LIB,
    crate::tsc::DENO_WEBGPU_LIB,
    crate::tsc::DENO_WEBSOCKET_LIB,
    crate::tsc::DENO_WEBSTORAGE_LIB,
    crate::tsc::DENO_CRYPTO_LIB,
    crate::tsc::DENO_BROADCAST_CHANNEL_LIB,
    crate::tsc::DENO_NET_LIB,
    crate::tsc::SHARED_GLOBALS_LIB,
    crate::tsc::WINDOW_LIB,
  ];

  if unstable {
    types.push(crate::tsc::UNSTABLE_NS_LIB);
  }

  types.join("\n")
}

async fn compile_command(
  flags: Flags,
  compile_flags: CompileFlags,
) -> Result<i32, AnyError> {
  let debug = flags.log_level == Some(log::Level::Debug);

  let run_flags = tools::standalone::compile_to_runtime_flags(
    &flags,
    compile_flags.args.clone(),
  )?;

  let module_specifier = resolve_url_or_path(&compile_flags.source_file)?;
  let ps = ProcState::build(Arc::new(flags)).await?;
  let deno_dir = &ps.dir;

  let output_path =
    tools::standalone::resolve_compile_executable_output_path(&compile_flags)?;

  let graph = Arc::try_unwrap(
    create_graph_and_maybe_check(module_specifier.clone(), &ps, debug).await?,
  )
  .map_err(|_| {
    generic_error("There should only be one reference to ModuleGraph")
  })?;

  graph.valid().unwrap();

  let eszip = eszip::EszipV2::from_graph(graph, Default::default())?;

  info!(
    "{} {}",
    colors::green("Compile"),
    module_specifier.to_string()
  );

  // Select base binary based on target
  let original_binary =
    tools::standalone::get_base_binary(deno_dir, compile_flags.target.clone())
      .await?;

  let final_bin = tools::standalone::create_standalone_binary(
    original_binary,
    eszip,
    module_specifier.clone(),
    run_flags,
    ps,
  )
  .await?;

  info!("{} {}", colors::green("Emit"), output_path.display());

  tools::standalone::write_standalone_binary(output_path, final_bin).await?;

  Ok(0)
}

async fn info_command(
  flags: Flags,
  info_flags: InfoFlags,
) -> Result<i32, AnyError> {
  let ps = ProcState::build(Arc::new(flags)).await?;
  if let Some(specifier) = info_flags.file {
    let specifier = resolve_url_or_path(&specifier)?;
    let mut cache = cache::FetchCacher::new(
      ps.dir.gen_cache.clone(),
      ps.file_fetcher.clone(),
      Permissions::allow_all(),
      Permissions::allow_all(),
    );
    let maybe_locker = lockfile::as_maybe_locker(ps.lockfile.clone());
    let maybe_import_map_resolver =
      ps.maybe_import_map.clone().map(ImportMapResolver::new);
    let maybe_jsx_resolver = ps.maybe_config_file.as_ref().and_then(|cf| {
      cf.to_maybe_jsx_import_source_module()
        .map(|im| JsxResolver::new(im, maybe_import_map_resolver.clone()))
    });
    let maybe_resolver = if maybe_jsx_resolver.is_some() {
      maybe_jsx_resolver.as_ref().map(|jr| jr.as_resolver())
    } else {
      maybe_import_map_resolver
        .as_ref()
        .map(|im| im.as_resolver())
    };
    let graph = deno_graph::create_graph(
      vec![(specifier, deno_graph::ModuleKind::Esm)],
      false,
      None,
      &mut cache,
      maybe_resolver,
      maybe_locker,
      None,
      None,
    )
    .await;

    if info_flags.json {
      write_json_to_stdout(&json!(graph))?;
    } else {
      write_to_stdout_ignore_sigpipe(graph.to_string().as_bytes())?;
    }
  } else {
    // If it was just "deno info" print location of caches and exit
    print_cache_info(&ps, info_flags.json, ps.flags.location.as_ref())?;
  }
  Ok(0)
}

async fn install_command(
  flags: Flags,
  install_flags: InstallFlags,
) -> Result<i32, AnyError> {
  let mut preload_flags = flags.clone();
  preload_flags.inspect = None;
  preload_flags.inspect_brk = None;
  let permissions =
    Permissions::from_options(&preload_flags.permissions_options());
  let ps = ProcState::build(Arc::new(preload_flags)).await?;
  let main_module = resolve_url_or_path(&install_flags.module_url)?;
  let mut worker = create_main_worker(
    &ps,
    main_module.clone(),
    permissions,
    vec![],
    Default::default(),
  );
  // First, fetch and compile the module; this step ensures that the module exists.
  worker.preload_module(&main_module, true).await?;
  tools::installer::install(flags, install_flags)?;
  Ok(0)
}

async fn uninstall_command(
  uninstall_flags: UninstallFlags,
) -> Result<i32, AnyError> {
  tools::installer::uninstall(uninstall_flags.name, uninstall_flags.root)?;
  Ok(0)
}

async fn lsp_command() -> Result<i32, AnyError> {
  lsp::start().await?;
  Ok(0)
}

async fn lint_command(
  flags: Flags,
  lint_flags: LintFlags,
) -> Result<i32, AnyError> {
  if lint_flags.rules {
    tools::lint::print_rules_list(lint_flags.json);
    return Ok(0);
  }

  tools::lint::lint(flags, lint_flags).await?;
  Ok(0)
}

async fn cache_command(
  flags: Flags,
  cache_flags: CacheFlags,
) -> Result<i32, AnyError> {
  let lib = if flags.unstable {
    emit::TypeLib::UnstableDenoWindow
  } else {
    emit::TypeLib::DenoWindow
  };
  let ps = ProcState::build(Arc::new(flags)).await?;

  for file in cache_flags.files {
    let specifier = resolve_url_or_path(&file)?;
    ps.prepare_module_load(
      vec![specifier],
      false,
      lib.clone(),
      Permissions::allow_all(),
      Permissions::allow_all(),
      false,
    )
    .await?;
  }

  Ok(0)
}

async fn check_command(
  flags: Flags,
  check_flags: CheckFlags,
) -> Result<i32, AnyError> {
  cache_command(
    flags,
    CacheFlags {
      files: check_flags.files,
    },
  )
  .await
}

async fn eval_command(
  flags: Flags,
  eval_flags: EvalFlags,
) -> Result<i32, AnyError> {
  // deno_graph works off of extensions for local files to determine the media
  // type, and so our "fake" specifier needs to have the proper extension.
  let main_module =
    resolve_url_or_path(&format!("./$deno$eval.{}", eval_flags.ext)).unwrap();
  let permissions = Permissions::from_options(&flags.permissions_options());
  let ps = ProcState::build(Arc::new(flags)).await?;
  let mut worker = create_main_worker(
    &ps,
    main_module.clone(),
    permissions,
    vec![],
    Default::default(),
  );
  // Create a dummy source file.
  let source_code = if eval_flags.print {
    format!("console.log({})", eval_flags.code)
  } else {
    eval_flags.code
  }
  .into_bytes();

  let file = File {
    local: main_module.clone().to_file_path().unwrap(),
    maybe_types: None,
    media_type: MediaType::Unknown,
    source: String::from_utf8(source_code)?.into(),
    specifier: main_module.clone(),
    maybe_headers: None,
  };

  // Save our fake file into file fetcher cache
  // to allow module access by TS compiler.
  ps.file_fetcher.insert_cached(file);
  debug!("main_module {}", &main_module);
  if ps.flags.compat {
    worker.execute_side_module(&compat::GLOBAL_URL).await?;
  }
  worker.execute_main_module(&main_module).await?;
  worker.dispatch_load_event(&located_script_name!())?;
  worker.run_event_loop(false).await?;
  worker.dispatch_unload_event(&located_script_name!())?;
  Ok(0)
}

async fn create_graph_and_maybe_check(
  root: ModuleSpecifier,
  ps: &ProcState,
  debug: bool,
) -> Result<Arc<deno_graph::ModuleGraph>, AnyError> {
  let mut cache = cache::FetchCacher::new(
    ps.dir.gen_cache.clone(),
    ps.file_fetcher.clone(),
    Permissions::allow_all(),
    Permissions::allow_all(),
  );
  let maybe_locker = lockfile::as_maybe_locker(ps.lockfile.clone());
  let maybe_imports = if let Some(config_file) = &ps.maybe_config_file {
    config_file.to_maybe_imports()?
  } else {
    None
  };
  let maybe_import_map_resolver =
    ps.maybe_import_map.clone().map(ImportMapResolver::new);
  let maybe_jsx_resolver = ps.maybe_config_file.as_ref().and_then(|cf| {
    cf.to_maybe_jsx_import_source_module()
      .map(|im| JsxResolver::new(im, maybe_import_map_resolver.clone()))
  });
  let maybe_resolver = if maybe_jsx_resolver.is_some() {
    maybe_jsx_resolver.as_ref().map(|jr| jr.as_resolver())
  } else {
    maybe_import_map_resolver
      .as_ref()
      .map(|im| im.as_resolver())
  };
  let graph = Arc::new(
    deno_graph::create_graph(
      vec![(root, deno_graph::ModuleKind::Esm)],
      false,
      maybe_imports,
      &mut cache,
      maybe_resolver,
      maybe_locker,
      None,
      None,
    )
    .await,
  );

  let check_js = ps
    .maybe_config_file
    .as_ref()
    .map(|cf| cf.get_check_js())
    .unwrap_or(false);
  graph_valid(
    &graph,
    ps.flags.type_check_mode != TypeCheckMode::None,
    check_js,
  )?;
  graph_lock_or_exit(&graph);

  if ps.flags.type_check_mode != TypeCheckMode::None {
    let lib = if ps.flags.unstable {
      emit::TypeLib::UnstableDenoWindow
    } else {
      emit::TypeLib::DenoWindow
    };
    let (ts_config, maybe_ignored_options) = emit::get_ts_config(
      emit::ConfigType::Check {
        tsc_emit: false,
        lib,
      },
      ps.maybe_config_file.as_ref(),
      None,
    )?;
    if let Some(ignored_options) = maybe_ignored_options {
      eprintln!("{}", ignored_options);
    }
    let maybe_config_specifier =
      ps.maybe_config_file.as_ref().map(|cf| cf.specifier.clone());
    let check_result = emit::check_and_maybe_emit(
      &graph.roots,
      Arc::new(RwLock::new(graph.as_ref().into())),
      &mut cache,
      emit::CheckOptions {
        type_check_mode: ps.flags.type_check_mode.clone(),
        debug,
        emit_with_diagnostics: false,
        maybe_config_specifier,
        ts_config,
        log_checks: true,
        reload: ps.flags.reload,
        reload_exclusions: Default::default(),
      },
    )?;
    debug!("{}", check_result.stats);
    if !check_result.diagnostics.is_empty() {
      return Err(check_result.diagnostics.into());
    }
  }

  Ok(graph)
}

fn bundle_module_graph(
  graph: &deno_graph::ModuleGraph,
  ps: &ProcState,
  flags: &Flags,
) -> Result<deno_emit::BundleEmit, AnyError> {
  info!("{} {}", colors::green("Bundle"), graph.roots[0].0);

  let (ts_config, maybe_ignored_options) = emit::get_ts_config(
    emit::ConfigType::Bundle,
    ps.maybe_config_file.as_ref(),
    None,
  )?;
  if flags.type_check_mode == TypeCheckMode::None {
    if let Some(ignored_options) = maybe_ignored_options {
      eprintln!("{}", ignored_options);
    }
  }

  deno_emit::bundle_graph(
    graph,
    deno_emit::BundleOptions {
      bundle_type: deno_emit::BundleType::Module,
      emit_options: ts_config.into(),
      emit_ignore_directives: true,
    },
  )
}

async fn bundle_command(
  flags: Flags,
  bundle_flags: BundleFlags,
) -> Result<i32, AnyError> {
  let debug = flags.log_level == Some(log::Level::Debug);
  let flags = Arc::new(flags);
  let resolver = |_| {
    let flags = flags.clone();
    let source_file1 = bundle_flags.source_file.clone();
    let source_file2 = bundle_flags.source_file.clone();
    async move {
      let module_specifier = resolve_url_or_path(&source_file1)?;

      debug!(">>>>> bundle START");
      let ps = ProcState::build(flags).await?;

      let graph =
        create_graph_and_maybe_check(module_specifier, &ps, debug).await?;

      let mut paths_to_watch: Vec<PathBuf> = graph
        .specifiers()
        .iter()
        .filter_map(|(_, r)| {
          r.as_ref().ok().and_then(|(s, _, _)| s.to_file_path().ok())
        })
        .collect();

      if let Ok(Some(import_map_path)) =
        config_file::resolve_import_map_specifier(
          ps.flags.import_map_path.as_deref(),
          ps.maybe_config_file.as_ref(),
        )
        .map(|ms| ms.and_then(|ref s| s.to_file_path().ok()))
      {
        paths_to_watch.push(import_map_path);
      }

      Ok((paths_to_watch, graph, ps))
    }
    .map(move |result| match result {
      Ok((paths_to_watch, graph, ps)) => ResolutionResult::Restart {
        paths_to_watch,
        result: Ok((ps, graph)),
      },
      Err(e) => ResolutionResult::Restart {
        paths_to_watch: vec![PathBuf::from(source_file2)],
        result: Err(e),
      },
    })
  };

  let operation = |(ps, graph): (ProcState, Arc<deno_graph::ModuleGraph>)| {
    let out_file = bundle_flags.out_file.clone();
    async move {
      let bundle_output = bundle_module_graph(graph.as_ref(), &ps, &ps.flags)?;
      debug!(">>>>> bundle END");

      if let Some(out_file) = out_file.as_ref() {
        let output_bytes = bundle_output.code.as_bytes();
        let output_len = output_bytes.len();
        fs_util::write_file(out_file, output_bytes, 0o644)?;
        info!(
          "{} {:?} ({})",
          colors::green("Emit"),
          out_file,
          colors::gray(display::human_size(output_len as f64))
        );
        if let Some(bundle_map) = bundle_output.maybe_map {
          let map_bytes = bundle_map.as_bytes();
          let map_len = map_bytes.len();
          let ext = if let Some(curr_ext) = out_file.extension() {
            format!("{}.map", curr_ext.to_string_lossy())
          } else {
            "map".to_string()
          };
          let map_out_file = out_file.with_extension(ext);
          fs_util::write_file(&map_out_file, map_bytes, 0o644)?;
          info!(
            "{} {:?} ({})",
            colors::green("Emit"),
            map_out_file,
            colors::gray(display::human_size(map_len as f64))
          );
        }
      } else {
        println!("{}", bundle_output.code);
      }

      Ok(())
    }
  };

  if flags.watch.is_some() {
    file_watcher::watch_func(
      resolver,
      operation,
      file_watcher::PrintConfig {
        job_name: "Bundle".to_string(),
        clear_screen: !flags.no_clear_screen,
      },
    )
    .await?;
  } else {
    let module_graph =
      if let ResolutionResult::Restart { result, .. } = resolver(None).await {
        result?
      } else {
        unreachable!();
      };
    operation(module_graph).await?;
  }

  Ok(0)
}

async fn doc_command(
  flags: Flags,
  doc_flags: DocFlags,
) -> Result<i32, AnyError> {
  tools::doc::print_docs(flags, doc_flags).await?;
  Ok(0)
}

async fn format_command(
  flags: Flags,
  fmt_flags: FmtFlags,
) -> Result<i32, AnyError> {
  let ps = ProcState::build(Arc::new(flags)).await?;
  let maybe_fmt_config = if let Some(config_file) = &ps.maybe_config_file {
    config_file.to_fmt_config()?
  } else {
    None
  };

  if fmt_flags.files.len() == 1 && fmt_flags.files[0].to_string_lossy() == "-" {
    tools::fmt::format_stdin(
      fmt_flags,
      maybe_fmt_config.map(|c| c.options).unwrap_or_default(),
    )?;
    return Ok(0);
  }

  tools::fmt::format(ps.flags.as_ref(), fmt_flags, maybe_fmt_config, &ps.dir)
    .await?;
  Ok(0)
}

async fn repl_command(
  flags: Flags,
  repl_flags: ReplFlags,
) -> Result<i32, AnyError> {
  let main_module = resolve_url_or_path("./$deno$repl.ts").unwrap();
  let permissions = Permissions::from_options(&flags.permissions_options());
  let ps = ProcState::build(Arc::new(flags)).await?;
  let mut worker = create_main_worker(
    &ps,
    main_module.clone(),
    permissions,
    vec![],
    Default::default(),
  );
  if ps.flags.compat {
    worker.execute_side_module(&compat::GLOBAL_URL).await?;
    compat::add_global_require(&mut worker.js_runtime, main_module.as_str())?;
    worker.run_event_loop(false).await?;
    compat::setup_builtin_modules(&mut worker.js_runtime)?;
  }
  worker.run_event_loop(false).await?;

  tools::repl::run(&ps, worker, repl_flags.eval_files, repl_flags.eval).await
}

async fn run_from_stdin(flags: Flags) -> Result<i32, AnyError> {
  let ps = ProcState::build(Arc::new(flags)).await?;
  let permissions = Permissions::from_options(&ps.flags.permissions_options());
  let main_module = resolve_url_or_path("./$deno$stdin.ts").unwrap();
  let mut worker = create_main_worker(
    &ps.clone(),
    main_module.clone(),
    permissions,
    vec![],
    Default::default(),
  );

  let mut source = Vec::new();
  std::io::stdin().read_to_end(&mut source)?;
  // Create a dummy source file.
  let source_file = File {
    local: main_module.clone().to_file_path().unwrap(),
    maybe_types: None,
    media_type: MediaType::TypeScript,
    source: String::from_utf8(source)?.into(),
    specifier: main_module.clone(),
    maybe_headers: None,
  };
  // Save our fake file into file fetcher cache
  // to allow module access by TS compiler
  ps.file_fetcher.insert_cached(source_file);

  debug!("main_module {}", main_module);
  if ps.flags.compat {
    worker.execute_side_module(&compat::GLOBAL_URL).await?;
  }
  worker.execute_main_module(&main_module).await?;
  worker.dispatch_load_event(&located_script_name!())?;
  worker.run_event_loop(false).await?;
  worker.dispatch_unload_event(&located_script_name!())?;
  Ok(worker.get_exit_code())
}

// TODO(bartlomieju): this function is not handling `exit_code` set by the runtime
// code properly.
async fn run_with_watch(flags: Flags, script: String) -> Result<i32, AnyError> {
  /// The FileWatcherModuleExecutor provides module execution with safe dispatching of life-cycle events by tracking the
  /// state of any pending events and emitting accordingly on drop in the case of a future
  /// cancellation.
  struct FileWatcherModuleExecutor {
    worker: MainWorker,
    pending_unload: bool,
    compat: bool,
  }

  impl FileWatcherModuleExecutor {
    pub fn new(worker: MainWorker, compat: bool) -> FileWatcherModuleExecutor {
      FileWatcherModuleExecutor {
        worker,
        pending_unload: false,
        compat,
      }
    }

    /// Execute the given main module emitting load and unload events before and after execution
    /// respectively.
    pub async fn execute(
      &mut self,
      main_module: &ModuleSpecifier,
    ) -> Result<(), AnyError> {
      if self.compat {
        self.worker.execute_side_module(&compat::GLOBAL_URL).await?;
      }
      self.worker.execute_main_module(main_module).await?;
      self.worker.dispatch_load_event(&located_script_name!())?;
      self.pending_unload = true;

      let result = self.worker.run_event_loop(false).await;
      self.pending_unload = false;

      if let Err(err) = result {
        return Err(err);
      }

      self.worker.dispatch_unload_event(&located_script_name!())?;

      Ok(())
    }
  }

  impl Drop for FileWatcherModuleExecutor {
    fn drop(&mut self) {
      if self.pending_unload {
        self
          .worker
          .dispatch_unload_event(&located_script_name!())
          .unwrap();
      }
    }
  }

  let flags = Arc::new(flags);
  let main_module = resolve_url_or_path(&script)?;
  let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();

  let operation = |(sender, main_module): (
    tokio::sync::mpsc::UnboundedSender<Vec<PathBuf>>,
    ModuleSpecifier,
  )| {
    let flags = flags.clone();
    let permissions = Permissions::from_options(&flags.permissions_options());
    async move {
      let ps = ProcState::build_for_file_watcher(flags.clone(), sender.clone())
        .await?;
      // We make use an module executor guard to ensure that unload is always fired when an
      // operation is called.
      let mut executor = FileWatcherModuleExecutor::new(
        create_main_worker(
          &ps,
          main_module.clone(),
          permissions,
          vec![],
          Default::default(),
        ),
        flags.compat,
      );

      executor.execute(&main_module).await?;

      Ok(())
    }
  };

  file_watcher::watch_func2(
    receiver,
    operation,
    (sender, main_module),
    file_watcher::PrintConfig {
      job_name: "Process".to_string(),
      clear_screen: !flags.no_clear_screen,
    },
  )
  .await?;

  Ok(0)
}

async fn run_command(
  flags: Flags,
  run_flags: RunFlags,
) -> Result<i32, AnyError> {
  // Read script content from stdin
  if run_flags.script == "-" {
    return run_from_stdin(flags).await;
  }

  if flags.watch.is_some() {
    return run_with_watch(flags, run_flags.script).await;
  }

  // TODO(bartlomieju): it should not be resolved here if we're in compat mode
  // because it might be a bare specifier
  // TODO(bartlomieju): actually I think it will also fail if there's an import
  // map specified and bare specifier is used on the command line - this should
  // probably call `ProcState::resolve` instead
  let main_module = resolve_url_or_path(&run_flags.script)?;
  let ps = ProcState::build(Arc::new(flags)).await?;
  let permissions = Permissions::from_options(&ps.flags.permissions_options());
  let mut worker = create_main_worker(
    &ps,
    main_module.clone(),
    permissions,
    vec![],
    Default::default(),
  );

  let mut maybe_coverage_collector =
    if let Some(ref coverage_dir) = ps.coverage_dir {
      let session = worker.create_inspector_session().await;

      let coverage_dir = PathBuf::from(coverage_dir);
      let mut coverage_collector =
        tools::coverage::CoverageCollector::new(coverage_dir, session);
      worker
        .with_event_loop(coverage_collector.start_collecting().boxed_local())
        .await?;
      Some(coverage_collector)
    } else {
      None
    };

  debug!("main_module {}", main_module);

  if ps.flags.compat {
    // TODO(bartlomieju): fix me
    assert_eq!(main_module.scheme(), "file");

    // Set up Node globals
    worker.execute_side_module(&compat::GLOBAL_URL).await?;
    // And `module` module that we'll use for checking which
    // loader to use and potentially load CJS module with.
    // This allows to skip permission check for `--allow-net`
    // which would otherwise be requested by dynamically importing
    // this file.
    worker.execute_side_module(&compat::MODULE_URL).await?;

    let use_esm_loader = compat::check_if_should_use_esm_loader(&main_module)?;

    if use_esm_loader {
      // ES module execution in Node compatiblity mode
      worker.execute_main_module(&main_module).await?;
    } else {
      // CJS module execution in Node compatiblity mode
      compat::load_cjs_module(
        &mut worker.js_runtime,
        &main_module.to_file_path().unwrap().display().to_string(),
        true,
      )?;
    }
  } else {
    // Regular ES module execution
    worker.execute_main_module(&main_module).await?;
  }

  worker.dispatch_load_event(&located_script_name!())?;
  worker
    .run_event_loop(maybe_coverage_collector.is_none())
    .await?;
  worker.dispatch_unload_event(&located_script_name!())?;

  if let Some(coverage_collector) = maybe_coverage_collector.as_mut() {
    worker
      .with_event_loop(coverage_collector.stop_collecting().boxed_local())
      .await?;
  }
  Ok(worker.get_exit_code())
}

async fn task_command(
  flags: Flags,
  task_flags: TaskFlags,
) -> Result<i32, AnyError> {
  tools::task::execute_script(flags, task_flags).await
}

async fn coverage_command(
  flags: Flags,
  coverage_flags: CoverageFlags,
) -> Result<i32, AnyError> {
  if coverage_flags.files.is_empty() {
    return Err(generic_error("No matching coverage profiles found"));
  }

  tools::coverage::cover_files(flags, coverage_flags).await?;
  Ok(0)
}

async fn bench_command(
  flags: Flags,
  bench_flags: BenchFlags,
) -> Result<i32, AnyError> {
  if flags.watch.is_some() {
    tools::bench::run_benchmarks_with_watch(flags, bench_flags).await?;
  } else {
    tools::bench::run_benchmarks(flags, bench_flags).await?;
  }

  Ok(0)
}

async fn test_command(
  flags: Flags,
  test_flags: TestFlags,
) -> Result<i32, AnyError> {
  if let Some(ref coverage_dir) = flags.coverage_dir {
    std::fs::create_dir_all(&coverage_dir)?;
    env::set_var(
      "DENO_UNSTABLE_COVERAGE_DIR",
      PathBuf::from(coverage_dir).canonicalize()?,
    );
  }

  if flags.watch.is_some() {
    tools::test::run_tests_with_watch(flags, test_flags).await?;
  } else {
    tools::test::run_tests(flags, test_flags).await?;
  }

  Ok(0)
}

async fn completions_command(
  _flags: Flags,
  completions_flags: CompletionsFlags,
) -> Result<i32, AnyError> {
  write_to_stdout_ignore_sigpipe(&completions_flags.buf)?;
  Ok(0)
}

async fn types_command(flags: Flags) -> Result<i32, AnyError> {
  let types = get_types(flags.unstable);
  write_to_stdout_ignore_sigpipe(types.as_bytes())?;
  Ok(0)
}

async fn upgrade_command(
  _flags: Flags,
  upgrade_flags: UpgradeFlags,
) -> Result<i32, AnyError> {
  tools::upgrade::upgrade(upgrade_flags).await?;
  Ok(0)
}

async fn vendor_command(
  flags: Flags,
  vendor_flags: VendorFlags,
) -> Result<i32, AnyError> {
  let ps = ProcState::build(Arc::new(flags)).await?;
  tools::vendor::vendor(ps, vendor_flags).await?;
  Ok(0)
}

fn init_v8_flags(v8_flags: &[String]) {
  let v8_flags_includes_help = v8_flags
    .iter()
    .any(|flag| flag == "-help" || flag == "--help");
  // Keep in sync with `standalone.rs`.
  let v8_flags = once("UNUSED_BUT_NECESSARY_ARG0".to_owned())
    .chain(v8_flags.iter().cloned())
    .collect::<Vec<_>>();
  let unrecognized_v8_flags = v8_set_flags(v8_flags)
    .into_iter()
    .skip(1)
    .collect::<Vec<_>>();
  if !unrecognized_v8_flags.is_empty() {
    for f in unrecognized_v8_flags {
      eprintln!("error: V8 did not recognize flag '{}'", f);
    }
    eprintln!("\nFor a list of V8 flags, use '--v8-flags=--help'");
    std::process::exit(1);
  }
  if v8_flags_includes_help {
    std::process::exit(0);
  }
}

fn get_subcommand(
  flags: Flags,
) -> Pin<Box<dyn Future<Output = Result<i32, AnyError>>>> {
  match flags.subcommand.clone() {
    DenoSubcommand::Bench(bench_flags) => {
      bench_command(flags, bench_flags).boxed_local()
    }
    DenoSubcommand::Bundle(bundle_flags) => {
      bundle_command(flags, bundle_flags).boxed_local()
    }
    DenoSubcommand::Doc(doc_flags) => {
      doc_command(flags, doc_flags).boxed_local()
    }
    DenoSubcommand::Eval(eval_flags) => {
      eval_command(flags, eval_flags).boxed_local()
    }
    DenoSubcommand::Cache(cache_flags) => {
      cache_command(flags, cache_flags).boxed_local()
    }
    DenoSubcommand::Check(check_flags) => {
      check_command(flags, check_flags).boxed_local()
    }
    DenoSubcommand::Compile(compile_flags) => {
      compile_command(flags, compile_flags).boxed_local()
    }
    DenoSubcommand::Coverage(coverage_flags) => {
      coverage_command(flags, coverage_flags).boxed_local()
    }
    DenoSubcommand::Fmt(fmt_flags) => {
      format_command(flags, fmt_flags).boxed_local()
    }
    DenoSubcommand::Info(info_flags) => {
      info_command(flags, info_flags).boxed_local()
    }
    DenoSubcommand::Install(install_flags) => {
      install_command(flags, install_flags).boxed_local()
    }
    DenoSubcommand::Uninstall(uninstall_flags) => {
      uninstall_command(uninstall_flags).boxed_local()
    }
    DenoSubcommand::Lsp => lsp_command().boxed_local(),
    DenoSubcommand::Lint(lint_flags) => {
      lint_command(flags, lint_flags).boxed_local()
    }
    DenoSubcommand::Repl(repl_flags) => {
      repl_command(flags, repl_flags).boxed_local()
    }
    DenoSubcommand::Run(run_flags) => {
      run_command(flags, run_flags).boxed_local()
    }
    DenoSubcommand::Task(task_flags) => {
      task_command(flags, task_flags).boxed_local()
    }
    DenoSubcommand::Test(test_flags) => {
      test_command(flags, test_flags).boxed_local()
    }
    DenoSubcommand::Completions(completions_flags) => {
      completions_command(flags, completions_flags).boxed_local()
    }
    DenoSubcommand::Types => types_command(flags).boxed_local(),
    DenoSubcommand::Upgrade(upgrade_flags) => {
      upgrade_command(flags, upgrade_flags).boxed_local()
    }
    DenoSubcommand::Vendor(vendor_flags) => {
      vendor_command(flags, vendor_flags).boxed_local()
    }
  }
}

fn setup_panic_hook() {
  // This function does two things inside of the panic hook:
  // - Tokio does not exit the process when a task panics, so we define a custom
  //   panic hook to implement this behaviour.
  // - We print a message to stderr to indicate that this is a bug in Deno, and
  //   should be reported to us.
  let orig_hook = std::panic::take_hook();
  std::panic::set_hook(Box::new(move |panic_info| {
    eprintln!("\n============================================================");
    eprintln!("Deno has panicked. This is a bug in Deno. Please report this");
    eprintln!("at https://github.com/denoland/deno/issues/new.");
    eprintln!("If you can reliably reproduce this panic, include the");
    eprintln!("reproduction steps and re-run with the RUST_BACKTRACE=1 env");
    eprintln!("var set and include the backtrace in your report.");
    eprintln!();
    eprintln!(
      "Platform: {} {}",
      std::env::consts::OS,
      std::env::consts::ARCH
    );
    eprintln!("Version: {}", version::deno());
    eprintln!("Args: {:?}", std::env::args().collect::<Vec<_>>());
    eprintln!();
    orig_hook(panic_info);
    std::process::exit(1);
  }));
}

fn unwrap_or_exit<T>(result: Result<T, AnyError>) -> T {
  match result {
    Ok(value) => value,
    Err(error) => {
      let error_string = match error.downcast_ref::<JsError>() {
        Some(e) => format_js_error(e),
        None => format!("{:?}", error),
      };
      eprintln!(
        "{}: {}",
        colors::red_bold("error"),
        error_string.trim_start_matches("error: ")
      );
      std::process::exit(1);
    }
  }
}

pub fn main() {
  setup_panic_hook();

  unix_util::raise_fd_limit();
  windows_util::ensure_stdio_open();
  #[cfg(windows)]
  colors::enable_ansi(); // For Windows 10

  let args: Vec<String> = env::args().collect();

  let exit_code = async move {
    let standalone_res =
      match standalone::extract_standalone(args.clone()).await {
        Ok(Some((metadata, eszip))) => standalone::run(eszip, metadata).await,
        Ok(None) => Ok(()),
        Err(err) => Err(err),
      };
    // TODO(bartlomieju): doesn't handle exit code set by the runtime properly
    unwrap_or_exit(standalone_res);

    let flags = match flags::flags_from_vec(args) {
      Ok(flags) => flags,
      Err(err @ clap::Error { .. })
        if err.kind() == clap::ErrorKind::DisplayHelp
          || err.kind() == clap::ErrorKind::DisplayVersion =>
      {
        err.print().unwrap();
        std::process::exit(0);
      }
      Err(err) => unwrap_or_exit(Err(AnyError::from(err))),
    };
    if !flags.v8_flags.is_empty() {
      init_v8_flags(&*flags.v8_flags);
    }

    logger::init(flags.log_level);

    let exit_code = get_subcommand(flags).await;

    exit_code
  };

  let exit_code = unwrap_or_exit(run_basic(exit_code));

  std::process::exit(exit_code);
}