Skip to content

Commit

Permalink
Add 'path' to the experimental json output #143
Browse files Browse the repository at this point in the history
  • Loading branch information
runem committed Feb 10, 2020
1 parent fd57636 commit 226683b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/cli/analyze/analyze-cli-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() }));
}
}
}
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/cli/analyzer-cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export interface AnalyzerCliConfig {
};

ts?: typeof tsModule;
cwd?: string;
}
6 changes: 6 additions & 0 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/transformers/json/custom-elements-json-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface HtmlDataTag {
name: string;
description?: string;
attributes?: HtmlDataAttribute[];
path?: string;

// Suggested fields:
properties?: HtmlDataProperty[];
Expand Down
8 changes: 8 additions & 0 deletions src/transformers/json/json-transformer.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 {
Expand Down Expand Up @@ -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)));
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/transformers/transformer-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { VisibilityKind } from "../analyze/types/visibility-kind";

export interface TransformerConfig {
cwd?: string;
visibility: VisibilityKind;
markdown?: {
titleLevel?: number; // deprecated
Expand Down
7 changes: 7 additions & 0 deletions src/util/set-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Returns the first element in the set
* @param set
*/
export function getFirst<T>(set: Set<T>): T | undefined {
return set.values().next().value;
}

0 comments on commit 226683b

Please sign in to comment.