2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 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-11-01 11:25:05 -04:00
|
|
|
use crate::args::DocHtmlFlag;
|
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;
|
2024-09-05 04:51:40 -04:00
|
|
|
use crate::display;
|
2023-05-01 14:35:23 -04:00
|
|
|
use crate::factory::CliFactory;
|
2024-09-26 12:31:03 -04:00
|
|
|
use crate::graph_util::graph_exit_integrity_errors;
|
|
|
|
use crate::graph_util::graph_walk_errors;
|
|
|
|
use crate::graph_util::GraphWalkErrorsOptions;
|
2022-12-08 11:50:09 -05:00
|
|
|
use crate::tsc::get_types_declaration_file_text;
|
2024-01-08 12:18:42 -05:00
|
|
|
use crate::util::fs::collect_specifiers;
|
2024-02-08 20:40:26 -05:00
|
|
|
use deno_ast::diagnostics::Diagnostic;
|
2024-01-15 19:15:39 -05:00
|
|
|
use deno_config::glob::FilePatterns;
|
|
|
|
use deno_config::glob::PathOrPatternSet;
|
2022-07-01 11:50:16 -04:00
|
|
|
use deno_core::anyhow::bail;
|
2023-11-01 11:25:05 -04:00
|
|
|
use deno_core::anyhow::Context;
|
2021-02-25 10:24:05 -05:00
|
|
|
use deno_core::error::AnyError;
|
2024-09-05 04:51:40 -04:00
|
|
|
use deno_core::serde_json;
|
2021-02-25 10:24:05 -05:00
|
|
|
use deno_doc as doc;
|
2024-04-24 16:34:21 -04:00
|
|
|
use deno_doc::html::UrlResolveKind;
|
2024-11-19 11:56:04 -05:00
|
|
|
use deno_doc::html::UsageComposer;
|
|
|
|
use deno_doc::html::UsageComposerEntry;
|
2024-04-11 19:00:17 -04:00
|
|
|
use deno_graph::source::NullFileSystem;
|
2024-11-01 12:27:00 -04:00
|
|
|
use deno_graph::EsParser;
|
2023-06-06 17:07:46 -04:00
|
|
|
use deno_graph::GraphKind;
|
2023-11-01 11:25:05 -04:00
|
|
|
use deno_graph::ModuleAnalyzer;
|
2021-09-02 11:38:19 -04:00
|
|
|
use deno_graph::ModuleSpecifier;
|
2024-02-20 16:29:57 -05:00
|
|
|
use doc::html::ShortPath;
|
2023-10-31 18:19:42 -04:00
|
|
|
use doc::DocDiagnostic;
|
|
|
|
use indexmap::IndexMap;
|
2023-11-01 11:25:05 -04:00
|
|
|
use std::collections::BTreeMap;
|
2023-12-01 15:12:10 -05:00
|
|
|
use std::rc::Rc;
|
2024-07-23 19:00:48 -04:00
|
|
|
use std::sync::Arc;
|
2021-02-25 10:24:05 -05:00
|
|
|
|
2024-09-05 04:51:40 -04:00
|
|
|
const JSON_SCHEMA_VERSION: u8 = 1;
|
|
|
|
|
2024-11-19 11:56:04 -05:00
|
|
|
const PRISM_CSS: &str = include_str!("./doc/prism.css");
|
|
|
|
const PRISM_JS: &str = include_str!("./doc/prism.js");
|
|
|
|
|
2023-11-01 11:25:05 -04:00
|
|
|
async fn generate_doc_nodes_for_builtin_types(
|
2021-12-20 16:29:02 -05:00
|
|
|
doc_flags: DocFlags,
|
2024-11-01 12:27:00 -04:00
|
|
|
parser: &dyn EsParser,
|
2023-11-01 11:25:05 -04:00
|
|
|
analyzer: &dyn ModuleAnalyzer,
|
|
|
|
) -> Result<IndexMap<ModuleSpecifier, Vec<doc::DocNode>>, AnyError> {
|
|
|
|
let source_file_specifier =
|
2024-04-24 16:34:21 -04:00
|
|
|
ModuleSpecifier::parse("file:///lib.deno.d.ts").unwrap();
|
2024-01-14 12:29:17 -05:00
|
|
|
let content = get_types_declaration_file_text();
|
2024-04-18 20:51:16 -04:00
|
|
|
let loader = deno_graph::source::MemoryLoader::new(
|
2023-11-01 11:25:05 -04:00
|
|
|
vec![(
|
|
|
|
source_file_specifier.to_string(),
|
|
|
|
deno_graph::source::Source::Module {
|
|
|
|
specifier: source_file_specifier.to_string(),
|
|
|
|
content,
|
|
|
|
maybe_headers: None,
|
|
|
|
},
|
|
|
|
)],
|
|
|
|
Vec::new(),
|
|
|
|
);
|
|
|
|
let mut graph = deno_graph::ModuleGraph::new(GraphKind::TypesOnly);
|
|
|
|
graph
|
|
|
|
.build(
|
|
|
|
vec![source_file_specifier.clone()],
|
2024-04-18 20:51:16 -04:00
|
|
|
&loader,
|
2023-11-01 11:25:05 -04:00
|
|
|
deno_graph::BuildOptions {
|
2024-04-11 19:00:17 -04:00
|
|
|
imports: Vec::new(),
|
2024-04-18 21:43:28 -04:00
|
|
|
is_dynamic: false,
|
|
|
|
passthrough_jsr_specifiers: false,
|
2024-04-11 19:00:17 -04:00
|
|
|
executor: Default::default(),
|
2024-04-18 21:43:28 -04:00
|
|
|
file_system: &NullFileSystem,
|
2024-04-11 19:00:17 -04:00
|
|
|
jsr_url_provider: Default::default(),
|
2024-05-28 14:58:43 -04:00
|
|
|
locker: None,
|
2024-04-18 21:43:28 -04:00
|
|
|
module_analyzer: analyzer,
|
2024-04-11 19:00:17 -04:00
|
|
|
npm_resolver: None,
|
|
|
|
reporter: None,
|
|
|
|
resolver: None,
|
2023-11-01 11:25:05 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
let doc_parser = doc::DocParser::new(
|
|
|
|
&graph,
|
2024-01-09 18:20:52 -05:00
|
|
|
parser,
|
2023-11-01 11:25:05 -04:00
|
|
|
doc::DocParserOptions {
|
|
|
|
diagnostics: false,
|
|
|
|
private: doc_flags.private,
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
let nodes = doc_parser.parse_module(&source_file_specifier)?.definitions;
|
|
|
|
|
|
|
|
Ok(IndexMap::from([(source_file_specifier, nodes)]))
|
|
|
|
}
|
|
|
|
|
2024-07-23 19:00:48 -04:00
|
|
|
pub async fn doc(
|
|
|
|
flags: Arc<Flags>,
|
|
|
|
doc_flags: DocFlags,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let factory = CliFactory::from_flags(flags);
|
|
|
|
let cli_options = factory.cli_options()?;
|
2023-10-26 21:27:50 -04:00
|
|
|
let module_info_cache = factory.module_info_cache()?;
|
2024-01-09 18:20:52 -05:00
|
|
|
let parsed_source_cache = factory.parsed_source_cache();
|
|
|
|
let capturing_parser = parsed_source_cache.as_capturing_parser();
|
2024-11-01 12:27:00 -04:00
|
|
|
let analyzer = module_info_cache.as_module_analyzer();
|
2021-02-25 10:24:05 -05:00
|
|
|
|
2023-11-01 11:25:05 -04:00
|
|
|
let doc_nodes_by_url = match doc_flags.source_files {
|
2023-01-14 12:39:56 -05:00
|
|
|
DocSourceFileFlag::Builtin => {
|
2023-11-01 11:25:05 -04:00
|
|
|
generate_doc_nodes_for_builtin_types(
|
|
|
|
doc_flags.clone(),
|
2024-01-09 18:20:52 -05:00
|
|
|
&capturing_parser,
|
2023-11-01 11:25:05 -04:00
|
|
|
&analyzer,
|
|
|
|
)
|
|
|
|
.await?
|
2023-01-14 12:39:56 -05:00
|
|
|
}
|
2023-11-01 11:25:05 -04:00
|
|
|
DocSourceFileFlag::Paths(ref source_files) => {
|
2024-02-20 16:29:57 -05:00
|
|
|
let module_graph_creator = factory.module_graph_creator().await?;
|
2024-09-26 12:31:03 -04:00
|
|
|
let fs = factory.fs();
|
2023-05-01 14:35:23 -04:00
|
|
|
|
2024-01-08 12:18:42 -05:00
|
|
|
let module_specifiers = collect_specifiers(
|
|
|
|
FilePatterns {
|
2024-01-18 15:57:30 -05:00
|
|
|
base: cli_options.initial_cwd().to_path_buf(),
|
2024-03-07 20:16:32 -05:00
|
|
|
include: Some(
|
|
|
|
PathOrPatternSet::from_include_relative_path_or_patterns(
|
|
|
|
cli_options.initial_cwd(),
|
|
|
|
source_files,
|
|
|
|
)?,
|
|
|
|
),
|
2024-01-08 12:18:42 -05:00
|
|
|
exclude: Default::default(),
|
|
|
|
},
|
2024-03-27 14:25:39 -04:00
|
|
|
cli_options.vendor_dir_path().map(ToOwned::to_owned),
|
2024-03-07 20:16:32 -05:00
|
|
|
|_| true,
|
2024-01-08 12:18:42 -05:00
|
|
|
)?;
|
2024-02-20 16:29:57 -05:00
|
|
|
let graph = module_graph_creator
|
2024-01-09 18:20:52 -05:00
|
|
|
.create_graph(GraphKind::TypesOnly, module_specifiers.clone())
|
2023-04-14 16:22:33 -04:00
|
|
|
.await?;
|
2023-03-13 17:04:00 -04:00
|
|
|
|
2024-09-26 12:31:03 -04:00
|
|
|
graph_exit_integrity_errors(&graph);
|
|
|
|
let errors = graph_walk_errors(
|
|
|
|
&graph,
|
|
|
|
fs,
|
|
|
|
&module_specifiers,
|
|
|
|
GraphWalkErrorsOptions {
|
|
|
|
check_js: false,
|
|
|
|
kind: GraphKind::TypesOnly,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
for error in errors {
|
|
|
|
log::warn!("{} {}", colors::yellow("Warning"), error);
|
2023-03-13 17:04:00 -04:00
|
|
|
}
|
|
|
|
|
2023-10-30 23:49:43 -04:00
|
|
|
let doc_parser = doc::DocParser::new(
|
|
|
|
&graph,
|
2024-01-09 18:20:52 -05:00
|
|
|
&capturing_parser,
|
2023-10-30 23:49:43 -04:00
|
|
|
doc::DocParserOptions {
|
|
|
|
private: doc_flags.private,
|
2023-10-31 18:19:42 -04:00
|
|
|
diagnostics: doc_flags.lint,
|
2023-10-30 23:49:43 -04:00
|
|
|
},
|
|
|
|
)?;
|
2023-10-30 18:58:57 -04:00
|
|
|
|
2023-11-01 11:25:05 -04:00
|
|
|
let mut doc_nodes_by_url =
|
|
|
|
IndexMap::with_capacity(module_specifiers.len());
|
2023-10-30 18:58:57 -04:00
|
|
|
|
2023-11-04 00:43:54 -04:00
|
|
|
for module_specifier in module_specifiers {
|
|
|
|
let nodes = doc_parser.parse_with_reexports(&module_specifier)?;
|
|
|
|
doc_nodes_by_url.insert(module_specifier, nodes);
|
2023-10-30 18:58:57 -04:00
|
|
|
}
|
|
|
|
|
2023-10-31 18:19:42 -04:00
|
|
|
if doc_flags.lint {
|
|
|
|
let diagnostics = doc_parser.take_diagnostics();
|
2024-02-08 20:40:26 -05:00
|
|
|
check_diagnostics(&diagnostics)?;
|
2023-10-31 18:19:42 -04:00
|
|
|
}
|
|
|
|
|
2023-11-01 11:25:05 -04:00
|
|
|
doc_nodes_by_url
|
2023-01-14 12:39:56 -05:00
|
|
|
}
|
2021-02-25 10:24:05 -05:00
|
|
|
};
|
|
|
|
|
2023-12-29 19:04:50 -05:00
|
|
|
if let Some(html_options) = &doc_flags.html {
|
|
|
|
let deno_ns = if doc_flags.source_files != DocSourceFileFlag::Builtin {
|
|
|
|
let deno_ns = generate_doc_nodes_for_builtin_types(
|
|
|
|
doc_flags.clone(),
|
2024-01-09 18:20:52 -05:00
|
|
|
&capturing_parser,
|
2023-12-29 19:04:50 -05:00
|
|
|
&analyzer,
|
|
|
|
)
|
|
|
|
.await?;
|
2024-03-27 19:25:39 -04:00
|
|
|
let (_, deno_ns) = deno_ns.into_iter().next().unwrap();
|
|
|
|
|
2024-04-24 16:34:21 -04:00
|
|
|
let short_path = Rc::new(ShortPath::new(
|
|
|
|
ModuleSpecifier::parse("file:///lib.deno.d.ts").unwrap(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
));
|
|
|
|
|
2024-03-27 19:25:39 -04:00
|
|
|
deno_doc::html::compute_namespaced_symbols(
|
2024-04-30 20:43:24 -04:00
|
|
|
&deno_ns
|
2024-03-27 19:25:39 -04:00
|
|
|
.into_iter()
|
|
|
|
.map(|node| deno_doc::html::DocNodeWithContext {
|
2024-04-24 16:34:21 -04:00
|
|
|
origin: short_path.clone(),
|
2024-07-24 15:23:16 -04:00
|
|
|
ns_qualifiers: Rc::new([]),
|
2024-03-27 19:25:39 -04:00
|
|
|
kind_with_drilldown:
|
2024-07-24 15:23:16 -04:00
|
|
|
deno_doc::html::DocNodeKindWithDrilldown::Other(node.kind()),
|
|
|
|
inner: Rc::new(node),
|
2024-10-08 09:55:44 -04:00
|
|
|
drilldown_name: None,
|
2024-06-28 08:03:51 -04:00
|
|
|
parent: None,
|
2024-03-27 19:25:39 -04:00
|
|
|
})
|
2024-04-30 20:43:24 -04:00
|
|
|
.collect::<Vec<_>>(),
|
2024-03-27 19:25:39 -04:00
|
|
|
)
|
2023-12-29 19:04:50 -05:00
|
|
|
} else {
|
2024-01-23 05:25:44 -05:00
|
|
|
Default::default()
|
2023-12-29 19:04:50 -05:00
|
|
|
};
|
|
|
|
|
2024-07-19 15:56:07 -04:00
|
|
|
let rewrite_map =
|
|
|
|
if let Some(config_file) = cli_options.start_dir.maybe_deno_json() {
|
|
|
|
let config = config_file.to_exports_config()?;
|
|
|
|
|
|
|
|
let rewrite_map = config
|
|
|
|
.clone()
|
|
|
|
.into_map()
|
|
|
|
.into_keys()
|
|
|
|
.map(|key| {
|
|
|
|
Ok((
|
|
|
|
config.get_resolved(&key)?.unwrap(),
|
|
|
|
key
|
|
|
|
.strip_prefix('.')
|
|
|
|
.unwrap_or(&key)
|
|
|
|
.strip_prefix('/')
|
|
|
|
.unwrap_or(&key)
|
|
|
|
.to_owned(),
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.collect::<Result<IndexMap<_, _>, AnyError>>()?;
|
|
|
|
|
|
|
|
Some(rewrite_map)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2024-06-28 08:03:51 -04:00
|
|
|
|
|
|
|
generate_docs_directory(
|
|
|
|
doc_nodes_by_url,
|
|
|
|
html_options,
|
|
|
|
deno_ns,
|
|
|
|
rewrite_map,
|
|
|
|
)
|
2021-02-25 10:24:05 -05:00
|
|
|
} else {
|
2023-11-04 00:43:54 -04:00
|
|
|
let modules_len = doc_nodes_by_url.len();
|
|
|
|
let doc_nodes =
|
|
|
|
doc_nodes_by_url.into_values().flatten().collect::<Vec<_>>();
|
|
|
|
|
|
|
|
if doc_flags.json {
|
2024-09-05 04:51:40 -04:00
|
|
|
let json_output = serde_json::json!({
|
|
|
|
"version": JSON_SCHEMA_VERSION,
|
|
|
|
"nodes": &doc_nodes
|
|
|
|
});
|
|
|
|
display::write_json_to_stdout(&json_output)
|
2023-11-04 00:43:54 -04:00
|
|
|
} else if doc_flags.lint {
|
|
|
|
// don't output docs if running with only the --lint flag
|
|
|
|
log::info!(
|
|
|
|
"Checked {} file{}",
|
|
|
|
modules_len,
|
|
|
|
if modules_len == 1 { "" } else { "s" }
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
print_docs_to_stdout(doc_flags, doc_nodes)
|
|
|
|
}
|
2023-11-01 11:25:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 05:25:44 -05:00
|
|
|
struct DocResolver {
|
2024-10-08 09:55:44 -04:00
|
|
|
deno_ns: std::collections::HashMap<Vec<String>, Option<Rc<ShortPath>>>,
|
2024-06-28 08:03:51 -04:00
|
|
|
strip_trailing_html: bool,
|
2024-01-23 05:25:44 -05:00
|
|
|
}
|
2023-12-29 19:04:50 -05:00
|
|
|
|
|
|
|
impl deno_doc::html::HrefResolver for DocResolver {
|
2024-04-24 16:34:21 -04:00
|
|
|
fn resolve_path(
|
|
|
|
&self,
|
|
|
|
current: UrlResolveKind,
|
|
|
|
target: UrlResolveKind,
|
|
|
|
) -> String {
|
2024-06-28 08:03:51 -04:00
|
|
|
let path = deno_doc::html::href_path_resolve(current, target);
|
|
|
|
if self.strip_trailing_html {
|
|
|
|
if let Some(path) = path
|
|
|
|
.strip_suffix("index.html")
|
|
|
|
.or_else(|| path.strip_suffix(".html"))
|
|
|
|
{
|
|
|
|
return path.to_owned();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
path
|
2024-04-24 16:34:21 -04:00
|
|
|
}
|
|
|
|
|
2024-01-23 05:25:44 -05:00
|
|
|
fn resolve_global_symbol(&self, symbol: &[String]) -> Option<String> {
|
2024-10-08 09:55:44 -04:00
|
|
|
if self.deno_ns.contains_key(symbol) {
|
2024-01-23 05:25:44 -05:00
|
|
|
Some(format!(
|
2024-06-17 10:06:05 -04:00
|
|
|
"https://deno.land/api@v{}?s={}",
|
2024-01-23 05:25:44 -05:00
|
|
|
env!("CARGO_PKG_VERSION"),
|
|
|
|
symbol.join(".")
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2023-12-29 19:04:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_import_href(
|
|
|
|
&self,
|
|
|
|
symbol: &[String],
|
|
|
|
src: &str,
|
|
|
|
) -> Option<String> {
|
|
|
|
let mut url = ModuleSpecifier::parse(src).ok()?;
|
|
|
|
|
|
|
|
if url.domain() == Some("deno.land") {
|
|
|
|
url.set_query(Some(&format!("s={}", symbol.join("."))));
|
|
|
|
return Some(url.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2024-01-31 12:34:15 -05:00
|
|
|
fn resolve_source(&self, location: &deno_doc::Location) -> Option<String> {
|
2024-07-24 15:23:16 -04:00
|
|
|
Some(location.filename.to_string())
|
2023-12-29 19:04:50 -05:00
|
|
|
}
|
2024-08-12 15:57:50 -04:00
|
|
|
|
|
|
|
fn resolve_external_jsdoc_module(
|
|
|
|
&self,
|
|
|
|
module: &str,
|
|
|
|
_symbol: Option<&str>,
|
|
|
|
) -> Option<(String, String)> {
|
|
|
|
if let Ok(url) = deno_core::url::Url::parse(module) {
|
|
|
|
match url.scheme() {
|
|
|
|
"npm" => {
|
|
|
|
let res =
|
|
|
|
deno_semver::npm::NpmPackageReqReference::from_str(module).ok()?;
|
|
|
|
let name = &res.req().name;
|
|
|
|
Some((
|
|
|
|
format!("https://www.npmjs.com/package/{name}"),
|
|
|
|
name.to_owned(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
"jsr" => {
|
|
|
|
let res =
|
|
|
|
deno_semver::jsr::JsrPackageReqReference::from_str(module).ok()?;
|
|
|
|
let name = &res.req().name;
|
|
|
|
Some((format!("https://jsr.io/{name}"), name.to_owned()))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2023-12-29 19:04:50 -05:00
|
|
|
}
|
|
|
|
|
2024-11-19 11:56:04 -05:00
|
|
|
struct DocComposer;
|
2024-06-28 08:03:51 -04:00
|
|
|
|
2024-11-19 11:56:04 -05:00
|
|
|
impl UsageComposer for DocComposer {
|
|
|
|
fn is_single_mode(&self) -> bool {
|
|
|
|
true
|
2024-06-28 08:03:51 -04:00
|
|
|
}
|
|
|
|
|
2024-11-19 11:56:04 -05:00
|
|
|
fn compose(
|
2024-06-28 08:03:51 -04:00
|
|
|
&self,
|
2024-11-19 11:56:04 -05:00
|
|
|
current_resolve: UrlResolveKind,
|
|
|
|
usage_to_md: deno_doc::html::UsageToMd,
|
|
|
|
) -> IndexMap<UsageComposerEntry, String> {
|
2024-06-28 08:03:51 -04:00
|
|
|
current_resolve
|
|
|
|
.get_file()
|
2024-11-19 11:56:04 -05:00
|
|
|
.map(|current_file| {
|
|
|
|
IndexMap::from([(
|
|
|
|
UsageComposerEntry {
|
|
|
|
name: "".to_string(),
|
|
|
|
icon: None,
|
|
|
|
},
|
|
|
|
usage_to_md(current_file.path.as_str(), None),
|
|
|
|
)])
|
|
|
|
})
|
|
|
|
.unwrap_or_default()
|
2024-08-12 15:57:50 -04:00
|
|
|
}
|
2024-06-28 08:03:51 -04:00
|
|
|
}
|
|
|
|
|
2024-03-11 23:48:00 -04:00
|
|
|
fn generate_docs_directory(
|
2024-03-27 19:25:39 -04:00
|
|
|
doc_nodes_by_url: IndexMap<ModuleSpecifier, Vec<doc::DocNode>>,
|
2023-12-29 19:04:50 -05:00
|
|
|
html_options: &DocHtmlFlag,
|
2024-10-08 09:55:44 -04:00
|
|
|
deno_ns: std::collections::HashMap<Vec<String>, Option<Rc<ShortPath>>>,
|
2024-06-28 08:03:51 -04:00
|
|
|
rewrite_map: Option<IndexMap<ModuleSpecifier, String>>,
|
2023-11-01 11:25:05 -04:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let cwd = std::env::current_dir().context("Failed to get CWD")?;
|
|
|
|
let output_dir_resolved = cwd.join(&html_options.output);
|
|
|
|
|
2024-06-28 08:03:51 -04:00
|
|
|
let category_docs =
|
|
|
|
if let Some(category_docs_path) = &html_options.category_docs_path {
|
|
|
|
let content = std::fs::read(category_docs_path)?;
|
2024-11-19 11:56:04 -05:00
|
|
|
Some(serde_json::from_slice(&content)?)
|
2024-06-28 08:03:51 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let symbol_redirect_map = if let Some(symbol_redirect_map_path) =
|
|
|
|
&html_options.symbol_redirect_map_path
|
|
|
|
{
|
|
|
|
let content = std::fs::read(symbol_redirect_map_path)?;
|
2024-11-19 11:56:04 -05:00
|
|
|
Some(serde_json::from_slice(&content)?)
|
2024-06-28 08:03:51 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let default_symbol_map = if let Some(default_symbol_map_path) =
|
|
|
|
&html_options.default_symbol_map_path
|
|
|
|
{
|
|
|
|
let content = std::fs::read(default_symbol_map_path)?;
|
2024-11-19 11:56:04 -05:00
|
|
|
Some(serde_json::from_slice(&content)?)
|
2024-06-28 08:03:51 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2023-11-01 11:25:05 -04:00
|
|
|
let options = deno_doc::html::GenerateOptions {
|
2024-04-24 16:34:21 -04:00
|
|
|
package_name: html_options.name.clone(),
|
2023-12-01 15:12:10 -05:00
|
|
|
main_entrypoint: None,
|
2024-06-28 08:03:51 -04:00
|
|
|
rewrite_map,
|
2024-11-19 11:56:04 -05:00
|
|
|
href_resolver: Rc::new(DocResolver {
|
|
|
|
deno_ns,
|
|
|
|
strip_trailing_html: html_options.strip_trailing_html,
|
|
|
|
}),
|
|
|
|
usage_composer: Rc::new(DocComposer),
|
2024-06-28 08:03:51 -04:00
|
|
|
category_docs,
|
2024-11-19 11:56:04 -05:00
|
|
|
disable_search: false,
|
2024-06-28 08:03:51 -04:00
|
|
|
symbol_redirect_map,
|
|
|
|
default_symbol_map,
|
2024-11-19 11:56:04 -05:00
|
|
|
markdown_renderer: deno_doc::html::comrak::create_renderer(
|
|
|
|
None,
|
|
|
|
Some(Box::new(|ammonia| {
|
|
|
|
ammonia.add_allowed_classes(
|
|
|
|
"code",
|
|
|
|
&[
|
|
|
|
"language-ts",
|
|
|
|
"language-tsx",
|
|
|
|
"language-typescript",
|
|
|
|
"language-js",
|
|
|
|
"language-jsx",
|
|
|
|
"language-javascript",
|
|
|
|
"language-bash",
|
|
|
|
"language-shell",
|
|
|
|
"language-md",
|
|
|
|
"language-markdown",
|
|
|
|
"language-rs",
|
|
|
|
"language-rust",
|
|
|
|
"language-html",
|
|
|
|
"language-xml",
|
|
|
|
"language-css",
|
|
|
|
"language-json",
|
|
|
|
"language-regex",
|
|
|
|
"language-svg",
|
|
|
|
],
|
|
|
|
);
|
|
|
|
})),
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
markdown_stripper: Rc::new(deno_doc::html::comrak::strip),
|
|
|
|
head_inject: Some(Rc::new(|root| {
|
|
|
|
format!(
|
|
|
|
r#"<link href="{root}{}" rel="stylesheet" /><link href="{root}prism.css" rel="stylesheet" /><script src="{root}prism.js"></script>"#,
|
|
|
|
deno_doc::html::comrak::COMRAK_STYLESHEET_FILENAME
|
|
|
|
)
|
|
|
|
})),
|
2023-11-01 11:25:05 -04:00
|
|
|
};
|
2021-02-25 10:24:05 -05:00
|
|
|
|
2024-11-19 11:56:04 -05:00
|
|
|
let mut files = deno_doc::html::generate(options, doc_nodes_by_url)
|
2023-11-01 11:25:05 -04:00
|
|
|
.context("Failed to generate HTML documentation")?;
|
|
|
|
|
2024-11-19 11:56:04 -05:00
|
|
|
files.insert("prism.js".to_string(), PRISM_JS.to_string());
|
|
|
|
files.insert("prism.css".to_string(), PRISM_CSS.to_string());
|
|
|
|
|
2023-11-01 11:25:05 -04:00
|
|
|
let path = &output_dir_resolved;
|
|
|
|
let _ = std::fs::remove_dir_all(path);
|
|
|
|
std::fs::create_dir(path)
|
|
|
|
.with_context(|| format!("Failed to create directory {:?}", path))?;
|
|
|
|
|
|
|
|
let no_of_files = files.len();
|
|
|
|
for (name, content) in files {
|
|
|
|
let this_path = path.join(name);
|
|
|
|
let prefix = this_path.parent().with_context(|| {
|
|
|
|
format!("Failed to get parent path for {:?}", this_path)
|
|
|
|
})?;
|
|
|
|
std::fs::create_dir_all(prefix)
|
|
|
|
.with_context(|| format!("Failed to create directory {:?}", prefix))?;
|
|
|
|
std::fs::write(&this_path, content)
|
|
|
|
.with_context(|| format!("Failed to write file {:?}", this_path))?;
|
2021-02-25 10:24:05 -05:00
|
|
|
}
|
2023-11-01 11:25:05 -04:00
|
|
|
|
|
|
|
log::info!(
|
|
|
|
"{}",
|
|
|
|
colors::green(format!(
|
|
|
|
"Written {} files to {:?}",
|
|
|
|
no_of_files, html_options.output
|
|
|
|
))
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-11-04 00:43:54 -04:00
|
|
|
fn print_docs_to_stdout(
|
2023-11-01 11:25:05 -04:00
|
|
|
doc_flags: DocFlags,
|
|
|
|
mut doc_nodes: Vec<deno_doc::DocNode>,
|
|
|
|
) -> Result<(), AnyError> {
|
2024-07-24 15:23:16 -04:00
|
|
|
doc_nodes.retain(|doc_node| doc_node.kind() != doc::DocNodeKind::Import);
|
2023-11-01 11:25:05 -04:00
|
|
|
let details = if let Some(filter) = doc_flags.filter {
|
2024-07-24 15:23:16 -04:00
|
|
|
let nodes = doc::find_nodes_by_name_recursively(doc_nodes, &filter);
|
2023-11-01 11:25:05 -04:00
|
|
|
if nodes.is_empty() {
|
|
|
|
bail!("Node {} was not found!", filter);
|
|
|
|
}
|
|
|
|
format!(
|
|
|
|
"{}",
|
|
|
|
doc::DocPrinter::new(&nodes, colors::use_color(), doc_flags.private)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"{}",
|
|
|
|
doc::DocPrinter::new(&doc_nodes, colors::use_color(), doc_flags.private)
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2024-09-05 04:51:40 -04:00
|
|
|
display::write_to_stdout_ignore_sigpipe(details.as_bytes())
|
|
|
|
.map_err(AnyError::from)
|
2021-02-25 10:24:05 -05:00
|
|
|
}
|
2023-10-31 18:19:42 -04:00
|
|
|
|
2024-02-08 20:40:26 -05:00
|
|
|
fn check_diagnostics(diagnostics: &[DocDiagnostic]) -> Result<(), AnyError> {
|
2023-10-31 18:19:42 -04:00
|
|
|
if diagnostics.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
// group by location then by line (sorted) then column (sorted)
|
|
|
|
let mut diagnostic_groups = IndexMap::new();
|
|
|
|
for diagnostic in diagnostics {
|
|
|
|
diagnostic_groups
|
|
|
|
.entry(diagnostic.location.filename.clone())
|
|
|
|
.or_insert_with(BTreeMap::new)
|
|
|
|
.entry(diagnostic.location.line)
|
|
|
|
.or_insert_with(BTreeMap::new)
|
|
|
|
.entry(diagnostic.location.col)
|
|
|
|
.or_insert_with(Vec::new)
|
|
|
|
.push(diagnostic);
|
|
|
|
}
|
|
|
|
|
2024-01-23 10:37:43 -05:00
|
|
|
for (_, diagnostics_by_lc) in diagnostic_groups {
|
|
|
|
for (_, diagnostics_by_col) in diagnostics_by_lc {
|
|
|
|
for (_, diagnostics) in diagnostics_by_col {
|
2023-10-31 18:19:42 -04:00
|
|
|
for diagnostic in diagnostics {
|
2024-06-05 11:04:16 -04:00
|
|
|
log::error!("{}\n", diagnostic.display());
|
2023-10-31 18:19:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bail!(
|
2023-11-10 13:40:39 -05:00
|
|
|
"Found {} documentation lint error{}.",
|
2023-10-31 18:19:42 -04:00
|
|
|
colors::bold(diagnostics.len().to_string()),
|
|
|
|
if diagnostics.len() == 1 { "" } else { "s" }
|
|
|
|
);
|
|
|
|
}
|