Skip to content

Commit 2686ad9

Browse files
committed
fix: add id to generate file name
BREAKING CHANGE: file are now named by its id (symbolType + symbolName)
1 parent 84d9702 commit 2686ad9

File tree

11 files changed

+86
-47
lines changed

11 files changed

+86
-47
lines changed

packages/ts-doc/components/code-highlight/code-highlight.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const {stripsComments} = require("../../src/utils/strips");
2-
const {highlight} = require("../../src/highlight");
2+
const {highlight} = require("./highlight");
33

44
module.exports = {
55
name: "codeHighlight",

packages/ts-doc/src/highlight/index.js renamed to packages/ts-doc/components/code-highlight/highlight.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
const {context} = require("../context");
3+
const {context} = require("../../src/context");
44
const KEYWORDS =
55
/(\benum\b|\bstatic\b|\bclass\b|\binterface\b|\bprivate\b|\bpublic\b|\bconst\b|\blet\b|\bprotected\b|\bimplements\b|\bconstructor\b|\breadonly\b|\babstract\b|\bimport\b|\bexport\b|\bas\b|\bfrom\b|\bextends\b)/g;
66
const TYPES = /(\bany\b|\bstring\b|\bboolean\b|\bnumber\b|\bDate\b|\bvoid\b)/g;

packages/ts-doc/src/highlight/index.spec.js renamed to packages/ts-doc/components/code-highlight/highlight.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const {expect} = require("chai");
2-
const {highlight} = require("./index");
2+
const {highlight} = require("./highlight");
33

44
describe("Highlight", () => {
55
it("should highlight content", () => {

packages/ts-doc/components/symbol-params/symbol-params.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {bindSymbols} = require("../../src/highlight");
1+
const {bindSymbols} = require("../code-highlight/highlight");
22
module.exports = {
33
name: "symbolParams",
44
trim: false,

packages/ts-doc/src/models/DocSymbol.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require("path");
22
const normalizePath = require("normalize-path");
33
const {context} = require("../context");
44
const {descriptionParser} = require("../parsers/description-parser.js");
5+
const {dashCase} = require("../utils/dashCase");
56

67
const _filterParams = (labels) => {
78
return labels
@@ -45,6 +46,10 @@ class DocSymbol {
4546
this.labels = [];
4647
}
4748

49+
get id() {
50+
return dashCase(this.symbolType + "-" + this.symbolName);
51+
}
52+
4853
get path() {
4954
return this.docFile.path;
5055
}
@@ -126,7 +131,7 @@ class DocSymbol {
126131
* @returns {*}
127132
*/
128133
get outputPath() {
129-
const file = normalizePath(path.join(context.outputDir, path.dirname(this.docFile.relativePackagePath), `${this.symbolName}.md`));
134+
const file = normalizePath(path.join(context.outputDir, path.dirname(this.docFile.relativePackagePath), `${this.id}.md`));
130135

131136
return context.outputResolver(file);
132137
}

packages/ts-doc/src/parsers/DocParser.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,29 @@ class DocParser {
3737
static async parse(docFile) {
3838
const {symbols} = new DocParser(docFile.contents).parse();
3939

40-
for (const [, symbol] of symbols) {
41-
try {
42-
await symbol.setDocFile(docFile);
43-
const newSymbol = context.symbols.push(symbol);
44-
docFile.symbols.set(newSymbol.symbolName, newSymbol);
45-
} catch (er) {
46-
logger.error("Fail to process symbol", {symbol, error: er});
40+
const promises = await Promise.all(
41+
symbols.values().map(async (symbol) => {
42+
try {
43+
await symbol.setDocFile(docFile);
44+
45+
return symbol;
46+
} catch (er) {
47+
logger.error("Fail to process symbol", {symbol, error: er});
48+
}
49+
})
50+
);
51+
52+
const parsedSymbols = await Promise.all(promises);
53+
54+
for (const symbol of parsedSymbols) {
55+
if (symbol) {
56+
docFile.symbols.set(symbol.symbolName, symbol);
4757
}
4858
}
4959

5060
return docFile.symbols;
5161
}
5262

53-
// /**
54-
// *
55-
// * @param str
56-
// * @returns {string}
57-
// */
58-
// overview (str = '') {
59-
// return stripsTags(stripsComments(str))
60-
// .split('\n')
61-
// .filter(o => !!o.trim())
62-
// .join('\n')
63-
// .trim()
64-
// }
65-
6663
/**
6764
*
6865
*/

packages/ts-doc/src/scan/scan.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,24 @@ module.exports = {
5858
let symbolsSize = 0;
5959
const files = await globby(patterns);
6060

61-
for (const file of files) {
62-
try {
63-
const symbols = await DocParser.parse(new DocFile(file));
61+
// paginate files by 5
62+
for (let i = 0; i < files.length; i += 5) {
63+
const filesChunk = files.slice(i, i + 5);
6464

65-
symbols.forEach((symbol) => {
66-
context.logger(`Scanned symbol '${chalk.cyan(symbol.symbolName)}'`);
67-
symbolsSize++;
68-
});
69-
} catch (er) {
70-
context.logger.error(chalk.red(er), er.stack);
71-
}
65+
await Promise.all(
66+
filesChunk.map(async (file) => {
67+
try {
68+
const symbols = await DocParser.parse(new DocFile(file));
69+
70+
symbols.forEach((symbol) => {
71+
context.logger(`Scanned symbol '${chalk.cyan(symbol.symbolName)}'`);
72+
symbolsSize++;
73+
});
74+
} catch (er) {
75+
context.logger.error(chalk.red(er), er.stack);
76+
}
77+
})
78+
);
7279
}
7380

7481
context.logger(`${chalk.green(symbolsSize)} scanned symbols`);

packages/ts-doc/src/tasks/build-api.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ module.exports = {
1515
.then(() => context.readPkg())
1616
.then(() => scanComponents(context.templatesDir))
1717
.then(() => scanFiles(context.scanPatterns))
18-
.then(() => {
18+
.then(async () => {
1919
let symbols = 0;
20-
context.symbols.forEach((symbol) => {
21-
const content = context.components.page(symbol);
22-
symbols++;
23-
return writeSymbol(symbol, content);
24-
});
20+
21+
await Promise.all(
22+
context.symbols.toArray().map((symbol) => {
23+
const content = context.components.page(symbol);
24+
symbols++;
25+
return writeSymbol(symbol, content);
26+
})
27+
);
28+
2529
logger(chalk.green(symbols) + " symbols write");
2630
})
2731
.then(() => writeJson())
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports.dashCase = function dashCase(str) {
2+
return str
3+
.replace(/([a-z])([A-Z])/g, "$1-$2")
4+
.replace(/[\s_]+/g, "-")
5+
.toLowerCase();
6+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const {expect} = require("chai");
2+
const {dashCase} = require("./dashCase");
3+
4+
describe("dashCase", () => {
5+
it("should convert camelCase to dash-case", () => {
6+
expect(dashCase("camelCase")).to.equal("camel-case");
7+
expect(dashCase("camelCase-test")).to.equal("camel-case-test");
8+
});
9+
});

0 commit comments

Comments
 (0)