2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-25 10:24:05 -05:00
|
|
|
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::DocFlags;
|
|
|
|
use crate::args::Flags;
|
2021-02-25 10:24:05 -05:00
|
|
|
use crate::colors;
|
2022-10-28 11:03:33 -04:00
|
|
|
use crate::display::write_json_to_stdout;
|
|
|
|
use crate::display::write_to_stdout_ignore_sigpipe;
|
2021-02-25 10:24:05 -05:00
|
|
|
use crate::file_fetcher::File;
|
|
|
|
use crate::get_types;
|
2021-09-24 11:10:42 -04:00
|
|
|
use crate::proc_state::ProcState;
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::MediaType;
|
2022-07-01 11:50:16 -04:00
|
|
|
use deno_core::anyhow::bail;
|
2021-02-25 10:24:05 -05:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::resolve_url_or_path;
|
|
|
|
use deno_doc as doc;
|
2022-01-31 17:33:57 -05:00
|
|
|
use deno_graph::ModuleKind;
|
2021-09-02 11:38:19 -04:00
|
|
|
use deno_graph::ModuleSpecifier;
|
2021-02-25 10:24:05 -05:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
pub async fn print_docs(
|
|
|
|
flags: Flags,
|
2021-12-20 16:29:02 -05:00
|
|
|
doc_flags: DocFlags,
|
2021-02-25 10:24:05 -05:00
|
|
|
) -> Result<(), AnyError> {
|
2022-06-28 16:45:55 -04:00
|
|
|
let ps = ProcState::build(flags).await?;
|
2021-12-20 16:29:02 -05:00
|
|
|
let source_file = doc_flags
|
|
|
|
.source_file
|
|
|
|
.unwrap_or_else(|| "--builtin".to_string());
|
2021-02-25 10:24:05 -05:00
|
|
|
|
2022-07-01 11:50:16 -04:00
|
|
|
let mut doc_nodes = if source_file == "--builtin" {
|
2021-09-02 11:38:19 -04:00
|
|
|
let source_file_specifier =
|
2022-09-07 15:06:18 -04:00
|
|
|
ModuleSpecifier::parse("deno://lib.deno.d.ts").unwrap();
|
2022-08-22 12:14:59 -04:00
|
|
|
let content = get_types(ps.options.unstable());
|
|
|
|
let mut loader = deno_graph::source::MemoryLoader::new(
|
|
|
|
vec![(
|
|
|
|
source_file_specifier.to_string(),
|
|
|
|
deno_graph::source::Source::Module {
|
|
|
|
specifier: source_file_specifier.to_string(),
|
|
|
|
content,
|
|
|
|
maybe_headers: None,
|
|
|
|
},
|
|
|
|
)],
|
|
|
|
Vec::new(),
|
|
|
|
);
|
|
|
|
let analyzer = deno_graph::CapturingModuleAnalyzer::default();
|
|
|
|
let graph = deno_graph::create_graph(
|
|
|
|
vec![(source_file_specifier.clone(), ModuleKind::Esm)],
|
|
|
|
&mut loader,
|
2022-10-25 11:55:57 -04:00
|
|
|
deno_graph::GraphOptions {
|
|
|
|
is_dynamic: false,
|
|
|
|
imports: None,
|
|
|
|
resolver: None,
|
|
|
|
locker: None,
|
|
|
|
module_analyzer: Some(&analyzer),
|
|
|
|
reporter: None,
|
|
|
|
},
|
2022-08-22 12:14:59 -04:00
|
|
|
)
|
|
|
|
.await;
|
2022-09-18 13:59:33 -04:00
|
|
|
let doc_parser = doc::DocParser::new(
|
|
|
|
graph,
|
|
|
|
doc_flags.private,
|
|
|
|
analyzer.as_capturing_parser(),
|
|
|
|
);
|
2022-08-22 12:14:59 -04:00
|
|
|
doc_parser.parse_module(&source_file_specifier)?.definitions
|
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,
|
2022-05-20 16:40:55 -04:00
|
|
|
source: format!("export * from \"{}\";", module_specifier).into(),
|
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
|
|
|
|
2022-07-01 11:50:16 -04:00
|
|
|
let graph = ps
|
|
|
|
.create_graph(vec![(root_specifier.clone(), ModuleKind::Esm)])
|
|
|
|
.await?;
|
2022-09-18 13:59:33 -04:00
|
|
|
let doc_parser = doc::DocParser::new(
|
|
|
|
graph,
|
|
|
|
doc_flags.private,
|
|
|
|
ps.parsed_source_cache.as_capturing_parser(),
|
|
|
|
);
|
2022-07-01 11:50:16 -04:00
|
|
|
doc_parser.parse_with_reexports(&root_specifier)?
|
2021-02-25 10:24:05 -05:00
|
|
|
};
|
|
|
|
|
2021-12-20 16:29:02 -05:00
|
|
|
if doc_flags.json {
|
2021-02-25 10:24:05 -05:00
|
|
|
write_json_to_stdout(&doc_nodes)
|
|
|
|
} else {
|
|
|
|
doc_nodes.retain(|doc_node| doc_node.kind != doc::DocNodeKind::Import);
|
2021-12-20 16:29:02 -05:00
|
|
|
let details = if let Some(filter) = doc_flags.filter {
|
2021-02-25 10:24:05 -05:00
|
|
|
let nodes =
|
|
|
|
doc::find_nodes_by_name_recursively(doc_nodes, filter.clone());
|
|
|
|
if nodes.is_empty() {
|
2022-07-01 11:50:16 -04:00
|
|
|
bail!("Node {} was not found!", filter);
|
2021-02-25 10:24:05 -05:00
|
|
|
}
|
|
|
|
format!(
|
|
|
|
"{}",
|
2021-12-20 16:29:02 -05:00
|
|
|
doc::DocPrinter::new(&nodes, colors::use_color(), doc_flags.private)
|
2021-02-25 10:24:05 -05:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"{}",
|
2021-12-20 16:29:02 -05:00
|
|
|
doc::DocPrinter::new(
|
|
|
|
&doc_nodes,
|
|
|
|
colors::use_color(),
|
|
|
|
doc_flags.private
|
|
|
|
)
|
2021-02-25 10:24:05 -05:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
write_to_stdout_ignore_sigpipe(details.as_bytes()).map_err(AnyError::from)
|
|
|
|
}
|
|
|
|
}
|