flecks/packages/dox/build/commands.js

99 lines
2.9 KiB
JavaScript
Raw Normal View History

2024-01-26 06:42:43 -06:00
const {mkdir, writeFile} = require('fs/promises');
const {join, relative, resolve} = require('path');
2024-01-16 00:28:20 -06:00
const {
2024-01-26 06:42:43 -06:00
generateDocusaurus,
generateJson,
} = require('./generate');
2023-12-31 16:21:43 -06:00
const {
FLECKS_CORE_ROOT = process.cwd(),
} = process.env;
2024-01-16 00:28:20 -06:00
module.exports = (program, flecks) => {
2023-12-31 16:21:43 -06:00
const commands = {};
2024-01-26 06:42:43 -06:00
commands.dox = {
description: 'Generate documentation',
2024-01-26 09:08:57 -06:00
action: async (subcommand, outputPath, {rewriteFilenames = []}) => {
2024-01-26 06:42:43 -06:00
let actualOutputPath = outputPath;
if (!actualOutputPath) {
switch (subcommand) {
case 'docusaurus':
actualOutputPath = 'website/docs/flecks';
break;
case 'json':
actualOutputPath = 'dist/dox';
break;
default:
break;
2023-12-31 16:21:43 -06:00
}
}
2024-01-26 06:42:43 -06:00
actualOutputPath = resolve(FLECKS_CORE_ROOT, actualOutputPath);
await mkdir(actualOutputPath, {recursive: true});
let output;
const json = await generateJson(flecks);
2024-01-26 09:08:57 -06:00
const pairs = rewriteFilenames
.map((pair) => pair.split('='))
2024-01-27 09:15:44 -06:00
.map(([from, to]) => [new RegExp(from, 'g'), to]);
2024-01-26 09:08:57 -06:00
const rewrite = (array) => (
array.map(
(object) => ({
...object,
filename: pairs.reduce(
(filename, [from, to]) => filename.replace(from, to),
object.filename,
),
}),
)
);
json.hooks = Object.fromEntries(
Object.entries(json.hooks)
.map(([hook, {implementations, invocations, specification}]) => (
[
hook,
{
implementations: rewrite(implementations),
invocations: rewrite(invocations),
specification,
},
]
)),
);
json.todos = rewrite(json.todos);
2023-12-31 16:21:43 -06:00
switch (subcommand) {
2024-01-26 06:42:43 -06:00
case 'docusaurus':
output = Object.fromEntries(
Object.entries(generateDocusaurus(json))
.map(([type, page]) => [`${type}.mdx`, page]),
);
2023-12-31 16:21:43 -06:00
break;
2024-01-26 06:42:43 -06:00
case 'json':
output = Object.fromEntries(
Object.entries(json)
.map(([type, value]) => [`${type}.json`, JSON.stringify(value, null, 2)]),
);
2023-12-31 16:21:43 -06:00
break;
default:
break;
}
2024-01-26 06:42:43 -06:00
await Promise.all(
Object.entries(output)
.map(([filename, output]) => (
writeFile(join(actualOutputPath, filename), output)
)),
);
// eslint-disable-next-line no-console
console.log("Output documentation to '%s'", relative(FLECKS_CORE_ROOT, actualOutputPath));
2023-12-31 16:21:43 -06:00
},
args: [
2024-01-26 06:42:43 -06:00
program.createArgument('subcommand', 'Generation type')
.choices(['docusaurus', 'json']),
program.createArgument('[output path]', 'Where the files are output'),
2023-12-31 16:21:43 -06:00
],
2024-01-26 09:08:57 -06:00
options: [
program.createOption('-r, --rewrite-filenames [pairs...]', 'rewrite filenames'),
],
2023-12-31 16:21:43 -06:00
};
return commands;
};