2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 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;
|
2023-01-14 12:39:56 -05:00
|
|
|
use crate::args::DocSourceFileFlag;
|
2022-06-27 16:54:09 -04:00
|
|
|
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;
|
2023-05-01 14:35:23 -04:00
|
|
|
use crate::factory::CliFactory;
|
2021-02-25 10:24:05 -05:00
|
|
|
use crate::file_fetcher::File;
|
2023-03-13 17:04:00 -04:00
|
|
|
use crate::graph_util::graph_lock_or_exit;
|
2022-12-08 11:50:09 -05:00
|
|
|
use crate::tsc::get_types_declaration_file_text;
|
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;
|
2023-03-13 19:31:03 -04:00
|
|
|
use deno_core::resolve_path;
|
2021-02-25 10:24:05 -05:00
|
|
|
use deno_core::resolve_url_or_path;
|
|
|
|
use deno_doc as doc;
|
2023-06-06 17:07:46 -04:00
|
|
|
use deno_graph::GraphKind;
|
2021-09-02 11:38:19 -04:00
|
|
|
use deno_graph::ModuleSpecifier;
|
2021-02-25 10:24:05 -05:00
|
|
|
|
|
|
|
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> {
|
2023-05-01 14:35:23 -04:00
|
|
|
let factory = CliFactory::from_flags(flags).await?;
|
|
|
|
let cli_options = factory.cli_options();
|
2021-02-25 10:24:05 -05:00
|
|
|
|
2023-01-14 12:39:56 -05:00
|
|
|
let mut doc_nodes = match doc_flags.source_file {
|
|
|
|
DocSourceFileFlag::Builtin => {
|
|
|
|
let source_file_specifier =
|
2023-02-05 11:49:20 -05:00
|
|
|
ModuleSpecifier::parse("internal://lib.deno.d.ts").unwrap();
|
2023-05-01 14:35:23 -04:00
|
|
|
let content = get_types_declaration_file_text(cli_options.unstable());
|
2023-01-14 12:39:56 -05:00
|
|
|
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();
|
2023-06-06 17:07:46 -04:00
|
|
|
let mut graph = deno_graph::ModuleGraph::new(GraphKind::TypesOnly);
|
2023-02-09 22:00:23 -05:00
|
|
|
graph
|
|
|
|
.build(
|
|
|
|
vec![source_file_specifier.clone()],
|
|
|
|
&mut loader,
|
|
|
|
deno_graph::BuildOptions {
|
|
|
|
module_analyzer: Some(&analyzer),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
2023-01-14 12:39:56 -05:00
|
|
|
let doc_parser = doc::DocParser::new(
|
|
|
|
graph,
|
|
|
|
doc_flags.private,
|
|
|
|
analyzer.as_capturing_parser(),
|
|
|
|
);
|
|
|
|
doc_parser.parse_module(&source_file_specifier)?.definitions
|
|
|
|
}
|
|
|
|
DocSourceFileFlag::Path(source_file) => {
|
2023-05-01 14:35:23 -04:00
|
|
|
let file_fetcher = factory.file_fetcher()?;
|
|
|
|
let module_graph_builder = factory.module_graph_builder().await?;
|
|
|
|
let maybe_lockfile = factory.maybe_lockfile();
|
2023-10-25 18:13:22 -04:00
|
|
|
let parsed_source_cache = factory.parsed_source_cache();
|
2023-05-01 14:35:23 -04:00
|
|
|
|
2023-03-13 21:12:09 -04:00
|
|
|
let module_specifier =
|
2023-05-01 14:35:23 -04:00
|
|
|
resolve_url_or_path(&source_file, cli_options.initial_cwd())?;
|
2021-02-25 10:24:05 -05:00
|
|
|
|
2023-01-14 12:39:56 -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.
|
2023-03-13 21:12:09 -04:00
|
|
|
let root_specifier =
|
2023-05-01 14:35:23 -04:00
|
|
|
resolve_path("./$deno$doc.ts", cli_options.initial_cwd()).unwrap();
|
2023-01-14 12:39:56 -05:00
|
|
|
let root = File {
|
|
|
|
maybe_types: None,
|
|
|
|
media_type: MediaType::TypeScript,
|
2023-01-27 10:43:16 -05:00
|
|
|
source: format!("export * from \"{module_specifier}\";").into(),
|
2023-01-14 12:39:56 -05:00
|
|
|
specifier: root_specifier.clone(),
|
|
|
|
maybe_headers: None,
|
|
|
|
};
|
2021-02-25 10:24:05 -05:00
|
|
|
|
2023-01-14 12:39:56 -05:00
|
|
|
// Save our fake file into file fetcher cache.
|
2023-05-01 14:35:23 -04:00
|
|
|
file_fetcher.insert_cached(root);
|
2021-02-25 10:24:05 -05:00
|
|
|
|
2023-05-01 14:35:23 -04:00
|
|
|
let graph = module_graph_builder
|
2023-06-06 17:07:46 -04:00
|
|
|
.create_graph(GraphKind::TypesOnly, vec![root_specifier.clone()])
|
2023-04-14 16:22:33 -04:00
|
|
|
.await?;
|
2023-03-13 17:04:00 -04:00
|
|
|
|
2023-05-01 14:35:23 -04:00
|
|
|
if let Some(lockfile) = maybe_lockfile {
|
2023-03-13 17:04:00 -04:00
|
|
|
graph_lock_or_exit(&graph, &mut lockfile.lock());
|
|
|
|
}
|
|
|
|
|
2023-01-14 12:39:56 -05:00
|
|
|
let doc_parser = doc::DocParser::new(
|
|
|
|
graph,
|
|
|
|
doc_flags.private,
|
2023-05-01 14:35:23 -04:00
|
|
|
parsed_source_cache.as_capturing_parser(),
|
2023-01-14 12:39:56 -05: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)
|
|
|
|
}
|
|
|
|
}
|