2021-02-25 10:24:05 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use crate::colors;
|
|
|
|
use crate::file_fetcher::File;
|
|
|
|
use crate::flags::Flags;
|
|
|
|
use crate::get_types;
|
2021-09-24 11:10:42 -04:00
|
|
|
use crate::proc_state::ProcState;
|
2021-02-25 10:24:05 -05:00
|
|
|
use crate::write_json_to_stdout;
|
|
|
|
use crate::write_to_stdout_ignore_sigpipe;
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::MediaType;
|
2021-02-25 10:24:05 -05:00
|
|
|
use deno_core::error::AnyError;
|
2021-09-02 11:38:19 -04:00
|
|
|
use deno_core::futures::future;
|
2021-02-25 10:24:05 -05:00
|
|
|
use deno_core::futures::future::FutureExt;
|
|
|
|
use deno_core::resolve_url_or_path;
|
|
|
|
use deno_doc as doc;
|
2021-09-02 11:38:19 -04:00
|
|
|
use deno_graph::create_graph;
|
|
|
|
use deno_graph::source::LoadFuture;
|
|
|
|
use deno_graph::source::LoadResponse;
|
|
|
|
use deno_graph::source::Loader;
|
|
|
|
use deno_graph::source::Resolver;
|
|
|
|
use deno_graph::ModuleSpecifier;
|
2021-02-25 10:24:05 -05:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2021-09-10 21:38:24 -04:00
|
|
|
use import_map::ImportMap;
|
2021-02-25 10:24:05 -05:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::Arc;
|
2021-03-02 07:39:56 -05:00
|
|
|
|
2021-02-25 10:24:05 -05:00
|
|
|
struct StubDocLoader;
|
|
|
|
|
2021-09-02 11:38:19 -04:00
|
|
|
impl Loader for StubDocLoader {
|
|
|
|
fn load(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
_is_dynamic: bool,
|
|
|
|
) -> LoadFuture {
|
|
|
|
Box::pin(future::ready((specifier.clone(), Ok(None))))
|
2021-02-25 10:24:05 -05:00
|
|
|
}
|
2021-09-02 11:38:19 -04:00
|
|
|
}
|
2021-02-25 10:24:05 -05:00
|
|
|
|
2021-09-02 11:38:19 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct DocResolver {
|
2021-11-08 20:26:39 -05:00
|
|
|
import_map: Option<Arc<ImportMap>>,
|
2021-02-25 10:24:05 -05:00
|
|
|
}
|
|
|
|
|
2021-09-02 11:38:19 -04:00
|
|
|
impl Resolver for DocResolver {
|
2021-02-25 10:24:05 -05:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
2021-09-02 11:38:19 -04:00
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
|
|
|
if let Some(import_map) = &self.import_map {
|
|
|
|
return import_map
|
|
|
|
.resolve(specifier, referrer.as_str())
|
|
|
|
.map_err(AnyError::from);
|
2021-02-25 10:24:05 -05:00
|
|
|
}
|
2021-09-02 11:38:19 -04:00
|
|
|
|
|
|
|
let module_specifier =
|
|
|
|
deno_core::resolve_import(specifier, referrer.as_str())?;
|
|
|
|
|
|
|
|
Ok(module_specifier)
|
2021-02-25 10:24:05 -05:00
|
|
|
}
|
2021-09-02 11:38:19 -04:00
|
|
|
}
|
2021-02-25 10:24:05 -05:00
|
|
|
|
2021-09-02 11:38:19 -04:00
|
|
|
struct DocLoader {
|
2021-09-24 11:10:42 -04:00
|
|
|
ps: ProcState,
|
2021-09-02 11:38:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Loader for DocLoader {
|
|
|
|
fn load(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
_is_dynamic: bool,
|
|
|
|
) -> LoadFuture {
|
|
|
|
let specifier = specifier.clone();
|
2021-09-24 11:10:42 -04:00
|
|
|
let ps = self.ps.clone();
|
2021-09-02 11:38:19 -04:00
|
|
|
async move {
|
2021-09-24 11:10:42 -04:00
|
|
|
let result = ps
|
2021-09-02 11:38:19 -04:00
|
|
|
.file_fetcher
|
|
|
|
.fetch(&specifier, &mut Permissions::allow_all())
|
|
|
|
.await
|
|
|
|
.map(|file| {
|
|
|
|
Some(LoadResponse {
|
|
|
|
specifier: specifier.clone(),
|
2021-09-07 10:39:32 -04:00
|
|
|
content: file.source.clone(),
|
2021-09-02 11:38:19 -04:00
|
|
|
maybe_headers: file.maybe_headers,
|
|
|
|
})
|
|
|
|
});
|
|
|
|
(specifier.clone(), result)
|
|
|
|
}
|
|
|
|
.boxed_local()
|
2021-02-25 10:24:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn print_docs(
|
|
|
|
flags: Flags,
|
|
|
|
source_file: Option<String>,
|
|
|
|
json: bool,
|
|
|
|
maybe_filter: Option<String>,
|
|
|
|
private: bool,
|
|
|
|
) -> Result<(), AnyError> {
|
2021-09-24 11:10:42 -04:00
|
|
|
let ps = ProcState::build(flags.clone()).await?;
|
2021-02-25 10:24:05 -05:00
|
|
|
let source_file = source_file.unwrap_or_else(|| "--builtin".to_string());
|
2021-09-07 10:39:32 -04:00
|
|
|
let source_parser = deno_graph::DefaultSourceParser::new();
|
2021-02-25 10:24:05 -05:00
|
|
|
|
|
|
|
let parse_result = if source_file == "--builtin" {
|
2021-09-02 11:38:19 -04:00
|
|
|
let mut loader = StubDocLoader;
|
|
|
|
let source_file_specifier =
|
|
|
|
ModuleSpecifier::parse("deno://lib.deno.d.ts").unwrap();
|
|
|
|
let graph = create_graph(
|
2021-10-10 17:26:22 -04:00
|
|
|
vec![source_file_specifier.clone()],
|
|
|
|
false,
|
|
|
|
None,
|
2021-09-02 11:38:19 -04:00
|
|
|
&mut loader,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.await;
|
2021-09-07 10:39:32 -04:00
|
|
|
let doc_parser = doc::DocParser::new(graph, private, &source_parser);
|
2021-02-25 10:24:05 -05:00
|
|
|
doc_parser.parse_source(
|
2021-09-02 11:38:19 -04:00
|
|
|
&source_file_specifier,
|
2021-09-07 10:39:32 -04:00
|
|
|
MediaType::Dts,
|
|
|
|
Arc::new(get_types(flags.unstable)),
|
2021-02-25 10:24:05 -05:00
|
|
|
)
|
|
|
|
} else {
|
2021-07-29 07:07:25 -04:00
|
|
|
let module_specifier = resolve_url_or_path(&source_file)?;
|
2021-02-25 10:24:05 -05:00
|
|
|
|
|
|
|
// If the root module has external types, the module graph won't redirect it,
|
|
|
|
// so instead create a dummy file which exports everything from the actual file being documented.
|
|
|
|
let root_specifier = resolve_url_or_path("./$deno$doc.ts").unwrap();
|
|
|
|
let root = File {
|
|
|
|
local: PathBuf::from("./$deno$doc.ts"),
|
|
|
|
maybe_types: None,
|
|
|
|
media_type: MediaType::TypeScript,
|
2021-09-07 10:39:32 -04:00
|
|
|
source: Arc::new(format!("export * from \"{}\";", module_specifier)),
|
2021-02-25 10:24:05 -05:00
|
|
|
specifier: root_specifier.clone(),
|
2021-09-02 11:38:19 -04:00
|
|
|
maybe_headers: None,
|
2021-02-25 10:24:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Save our fake file into file fetcher cache.
|
2021-09-24 11:10:42 -04:00
|
|
|
ps.file_fetcher.insert_cached(root);
|
2021-02-25 10:24:05 -05:00
|
|
|
|
2021-09-24 11:10:42 -04:00
|
|
|
let mut loader = DocLoader { ps: ps.clone() };
|
2021-09-02 11:38:19 -04:00
|
|
|
let resolver = DocResolver {
|
2021-09-24 11:10:42 -04:00
|
|
|
import_map: ps.maybe_import_map.clone(),
|
2021-09-02 11:38:19 -04:00
|
|
|
};
|
|
|
|
let graph = create_graph(
|
2021-10-10 17:26:22 -04:00
|
|
|
vec![root_specifier.clone()],
|
|
|
|
false,
|
|
|
|
None,
|
2021-09-02 11:38:19 -04:00
|
|
|
&mut loader,
|
|
|
|
Some(&resolver),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.await;
|
2021-09-07 10:39:32 -04:00
|
|
|
let doc_parser = doc::DocParser::new(graph, private, &source_parser);
|
2021-09-02 11:38:19 -04:00
|
|
|
doc_parser.parse_with_reexports(&root_specifier)
|
2021-02-25 10:24:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut doc_nodes = match parse_result {
|
|
|
|
Ok(nodes) => nodes,
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("{}", e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if json {
|
|
|
|
write_json_to_stdout(&doc_nodes)
|
|
|
|
} else {
|
|
|
|
doc_nodes.retain(|doc_node| doc_node.kind != doc::DocNodeKind::Import);
|
|
|
|
let details = if let Some(filter) = maybe_filter {
|
|
|
|
let nodes =
|
|
|
|
doc::find_nodes_by_name_recursively(doc_nodes, filter.clone());
|
|
|
|
if nodes.is_empty() {
|
|
|
|
eprintln!("Node {} was not found!", filter);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
format!(
|
|
|
|
"{}",
|
|
|
|
doc::DocPrinter::new(&nodes, colors::use_color(), private)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"{}",
|
|
|
|
doc::DocPrinter::new(&doc_nodes, colors::use_color(), private)
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
write_to_stdout_ignore_sigpipe(details.as_bytes()).map_err(AnyError::from)
|
|
|
|
}
|
|
|
|
}
|