Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add templatesDir configuration option #31

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/ts-doc/bin/tsdoc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node
const fs = require("fs");
const {buildApi} = require("../src/tasks/build-api");
const path = require("path");

let config = {
rootDir: process.cwd(),
Expand All @@ -9,6 +10,7 @@ let config = {
outputDir: "<rootDir>/docs/api",
baseUrl: "/api",
jsonOutputDir: "<rootDir>/docs/.vuepress/public",
templatesDir: path.join(__dirname, "..", "..", "components"),
modules: {}
};

Expand Down
9 changes: 8 additions & 1 deletion packages/ts-doc/src/context/context.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const readPkgUp = require("read-pkg-up");
const logger = require("fancy-log");
const normalizePath = require("normalize-path");
const path = require("path");

const SYMBOL_TYPES = {
"@": {value: "decorator", label: "Decorator"},
Expand Down Expand Up @@ -31,7 +32,9 @@ const reverseKeys = (obj) => {
};

module.exports = {
settings: {},
settings: {
templatesDir: path.join(__dirname, "..", "..", "components")
},
symbolTypes: reverseKeys(SYMBOL_TYPES),
symbolStatus: SYMBOL_STATUS,
status: SYMBOL_STATUS,
Expand Down Expand Up @@ -64,6 +67,10 @@ module.exports = {
return normalizePath(this.settings.jsonOutputDir.replace("<rootDir>", this.rootDir));
},

get templatesDir() {
return normalizePath(this.settings.templatesDir.replace("<rootDir>", this.rootDir));
},

get baseUrl() {
return this.settings.baseUrl;
},
Expand Down
35 changes: 19 additions & 16 deletions packages/ts-doc/src/scan/scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,33 @@ const {DocParser} = require("../parsers/DocParser");
const {DocFile} = require("../models/DocFile");

module.exports = {
/**
*
* @param directory
*/
scanComponents(directory) {
async scanComponents(directory) {
context.logger("Scan components '" + chalk.cyan(directory) + "'");

const files = globby.sync(path.join(directory, "**/*.ejs"));
const files = await globby(path.join(directory, "**/*.ejs"));

files.forEach((file) => {
const component = require(file.replace(".ejs", ".js"));
const promises = files.map(async (file) => {
try {
const mod = await import(file.replace(".ejs", ".js"));
const component = mod.default || mod;

context.components[component.name] = (...args) => {
const content = render(file, component.method(...args));
context.components[component.name] = (...args) => {
const content = render(file, component.method(...args));

if (component.trim) {
return trim(content);
}
return "\n" + content + "\n";
};
if (component.trim) {
return trim(content);
}
return "\n" + content + "\n";
};

context.logger("Import component '" + chalk.cyan(path.basename(file)) + "'");
context.logger("Import component '" + chalk.cyan(path.basename(file)) + "'");
} catch (er) {
context.logger.error("Fail to load template", chalk.red(er), er.stack);
}
});

await Promise.all(promises);

return files;
},
/**
Expand Down
42 changes: 17 additions & 25 deletions packages/ts-doc/src/tasks/build-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,30 @@
const logger = require("fancy-log");
const chalk = require("chalk");
const {context} = require("../context");
const {writeSymbol, writeTemplate, writeJson} = require("../write/write");
const {writeSymbol, writeJson} = require("../write/write");
const {scanFiles, scanComponents} = require("../scan/scan");
const path = require("path");

const options = {
components: path.join(__dirname, "..", "..", "components")
};

module.exports = {
/**
*
*/
buildApi(config) {
context.set(config);
return (
Promise.resolve()
.then(() => context.readPkg())
.then(() => scanComponents(options.components))
.then(() => scanFiles(context.scanPatterns))
.then(() => {
let symbols = 0;
context.symbols.forEach((symbol) => {
const content = context.components.page(symbol);
symbols++;
return writeSymbol(symbol, content);
});
logger(chalk.green(symbols) + " symbols write");
})
// .then(() => writeTemplate(context.docsDir + '/**/*.{ejs,emd}'))
.then(() => writeJson())
.then(() => logger("done"))
.catch((err) => console.error(err))
);
return Promise.resolve()
.then(() => context.readPkg())
.then(() => scanComponents(config.templatesDir))
.then(() => scanFiles(context.scanPatterns))
.then(() => {
let symbols = 0;
context.symbols.forEach((symbol) => {
const content = context.components.page(symbol);
symbols++;
return writeSymbol(symbol, content);
});
logger(chalk.green(symbols) + " symbols write");
})
.then(() => writeJson())
.then(() => logger("done"))
.catch((err) => console.error(err));
}
};
21 changes: 0 additions & 21 deletions packages/ts-doc/src/write/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,8 @@ const fsExtra = require("fs-extra");
const logger = require("fancy-log");
const chalk = require("chalk");
const {context, symbolTypes, symbolStatus} = require("../context");
const {render} = require("../render/render");
const {scanTemplate} = require("../scan/scan");

module.exports = {
/**
*
* @param templatePattern
*/
writeTemplate(templatePattern) {
scanTemplate(templatePattern).forEach((file) => {
const outfile = file.replace(/.ejs|.emd/, ".md");
logger(`Write '${chalk.cyan(outfile)}'`);
const content = render(file);

try {
fsExtra.mkdirsSync(outfile);
} catch (er) {}
fsExtra.writeFileSync(outfile, content.trim(), {
encoding: "utf8",
flag: "w+"
});
});
},
/**
*
* @param symbol
Expand Down
4 changes: 3 additions & 1 deletion packages/ts-doc/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {existsSync, writeFileSync, readFileSync} = require("fs");
const {resolve, join} = require("path");
const {scanComponents, scanFiles} = require("../src/scan/scan");
const {context} = require("../src/context");
const path = require("path");

const ROOT_DIR = __dirname;
const SNAPSHOTS_DIR = resolve(join(ROOT_DIR, "snapshots"));
Expand All @@ -18,13 +19,14 @@ async function compilePage() {
baseUrl: "/api",
scope: "@tsed",
scanPatterns: ["<rootDir>/packages/**/lib/**/*.d.ts"],
templatesDir: COMPONENTS_DIR,
modules: {}
};

context.logger = () => {};
context.logger.error = console.error;

await scanComponents(COMPONENTS_DIR);
await scanComponents(context.settings.templatesDir);
await scanFiles(context.scanPatterns);

return context.symbols.toArray().reduce((map, symbol) => {
Expand Down