diff --git a/src/cli/analyze/analyze-cli-command.ts b/src/cli/analyze/analyze-cli-command.ts index 46bf6eca..b5eecb42 100644 --- a/src/cli/analyze/analyze-cli-command.ts +++ b/src/cli/analyze/analyze-cli-command.ts @@ -54,7 +54,7 @@ Please follow and contribute to the discussion at: if (result.componentDefinitions.length > 0) { // Always use "console.log" when outputting the results /* eslint-disable-next-line no-console */ - console.log(transformResults(result, program, config)); + console.log(transformResults(result, program, { ...config, cwd: config.cwd || process.cwd() })); } } } @@ -77,7 +77,7 @@ Please follow and contribute to the discussion at: const tagNames = arrayFlat(results.map(result => result.componentDefinitions.map(d => d.tagName))); log(`[dry] Intending to write ${tagNames} to ./${relative(process.cwd(), outputPath)}`, config); } else { - const content = transformResults(results, program, config); + const content = transformResults(results, program, { ...config, cwd: config.cwd || dirname(outputPath) }); ensureDirSync(dirname(outputPath)); writeFileSync(outputPath, content); } @@ -100,7 +100,8 @@ function transformResults(results: AnalyzerResult[] | AnalyzerResult, program: P const transformerConfig: TransformerConfig = { visibility: config.visibility ?? "public", - markdown: config.markdown + markdown: config.markdown, + cwd: config.cwd }; return transformAnalyzerResult(format, results, program, transformerConfig); diff --git a/src/cli/analyzer-cli-config.ts b/src/cli/analyzer-cli-config.ts index eeed46c2..88a989ed 100644 --- a/src/cli/analyzer-cli-config.ts +++ b/src/cli/analyzer-cli-config.ts @@ -25,4 +25,5 @@ export interface AnalyzerCliConfig { }; ts?: typeof tsModule; + cwd?: string; } diff --git a/src/cli/cli.ts b/src/cli/cli.ts index 7daccc2d..fbf3240d 100644 --- a/src/cli/cli.ts +++ b/src/cli/cli.ts @@ -86,6 +86,12 @@ o {tagname}: The element's tag name`, hidden: true }) + // This option makes it possible to specify a base cwd to use when emitting paths + .option("cwd", { + string: true, + hidden: true + }) + .alias("v", "version") .help("h") .wrap(110) diff --git a/src/transformers/json/custom-elements-json-data.ts b/src/transformers/json/custom-elements-json-data.ts index 7e34512b..e4b5ab90 100644 --- a/src/transformers/json/custom-elements-json-data.ts +++ b/src/transformers/json/custom-elements-json-data.ts @@ -46,6 +46,7 @@ export interface HtmlDataTag { name: string; description?: string; attributes?: HtmlDataAttribute[]; + path?: string; // Suggested fields: properties?: HtmlDataProperty[]; diff --git a/src/transformers/json/json-transformer.ts b/src/transformers/json/json-transformer.ts index f187a37a..cf7fcd50 100644 --- a/src/transformers/json/json-transformer.ts +++ b/src/transformers/json/json-transformer.ts @@ -1,3 +1,4 @@ +import { relative } from "path"; import { Program, TypeChecker } from "typescript"; import { AnalyzerResult } from "../../analyze/types/analyzer-result"; import { ComponentDefinition } from "../../analyze/types/component-definition"; @@ -10,6 +11,7 @@ import { JsDoc } from "../../analyze/types/js-doc"; import { arrayDefined, arrayFlat } from "../../util/array-util"; import { getTypeHintFromType } from "../../util/get-type-hint-from-type"; import { filterVisibility } from "../../util/model-util"; +import { getFirst } from "../../util/set-util"; import { TransformerConfig } from "../transformer-config"; import { TransformerFunction } from "../transformer-function"; import { @@ -49,6 +51,11 @@ export const jsonTransformer: TransformerFunction = (results: AnalyzerResult[], function definitionToHtmlDataTag(definition: ComponentDefinition, checker: TypeChecker, config: TransformerConfig): HtmlDataTag { const declaration = definition.declaration(); + // Grab path to the definition file if possible + const node = getFirst(definition.tagNameNodes) || getFirst(definition.identifierNodes); + const fileName = node?.getSourceFile().fileName; + const path = fileName != null && config.cwd != null ? `./${relative(config.cwd, fileName)}` : undefined; + const attributes = arrayDefined(filterVisibility(config.visibility, declaration.members).map(d => componentMemberToHtmlDataAttribute(d, checker))); const properties = arrayDefined(filterVisibility(config.visibility, declaration.members).map(d => componentMemberToHtmlDataProperty(d, checker))); @@ -63,6 +70,7 @@ function definitionToHtmlDataTag(definition: ComponentDefinition, checker: TypeC return { name: definition.tagName, + path, description: getDescriptionFromJsDoc(declaration.jsDoc), attributes: attributes.length === 0 ? undefined : attributes, properties: properties.length === 0 ? undefined : properties, diff --git a/src/transformers/transformer-config.ts b/src/transformers/transformer-config.ts index 9bc41835..fdd90f64 100644 --- a/src/transformers/transformer-config.ts +++ b/src/transformers/transformer-config.ts @@ -1,6 +1,7 @@ import { VisibilityKind } from "../analyze/types/visibility-kind"; export interface TransformerConfig { + cwd?: string; visibility: VisibilityKind; markdown?: { titleLevel?: number; // deprecated diff --git a/src/util/set-util.ts b/src/util/set-util.ts new file mode 100644 index 00000000..1c7796de --- /dev/null +++ b/src/util/set-util.ts @@ -0,0 +1,7 @@ +/** + * Returns the first element in the set + * @param set + */ +export function getFirst(set: Set): T | undefined { + return set.values().next().value; +}