flecks/packages/dox/build/generate.js

306 lines
9.0 KiB
JavaScript
Raw Normal View History

2024-01-26 06:42:43 -06:00
const {
basename,
dirname,
extname,
join,
} = require('path');
const {parseFlecks} = require('./parser');
2022-03-07 02:44:48 -06:00
2024-01-26 06:42:43 -06:00
exports.generateDocusaurus = function generate({
'build-files': buildFiles,
config,
hooks,
todos,
}) {
return {
'build-files': exports.generateDocusaurusBuildFilesPage(buildFiles),
config: exports.generateDocusaurusConfigPage(config),
hooks: exports.generateDocusaurusHookPage(hooks),
todos: exports.generateDocusaurusTodoPage(todos),
};
};
exports.generateDocusaurusBuildFilesPage = (buildFiles) => {
2022-03-09 08:51:10 -06:00
const source = [];
2024-01-23 09:06:00 -06:00
source.push('---');
source.push('title: Build files');
source.push('description: All the build files in this project.');
source.push('---');
2022-03-09 08:51:10 -06:00
source.push('');
source.push('This page documents all the build configuration files in this project.');
source.push('');
2024-01-23 09:06:00 -06:00
if (buildFiles.length > 0) {
buildFiles
.sort(({filename: l}, {filename: r}) => (l < r ? -1 : 1))
.forEach(({filename, description}) => {
source.push(`## \`${filename}\``);
2022-03-09 08:51:10 -06:00
source.push('');
2024-01-23 09:06:00 -06:00
source.push(description);
2022-03-09 08:51:10 -06:00
source.push('');
});
}
return source.join('\n');
};
2024-01-26 06:42:43 -06:00
exports.generateDocusaurusConfigPage = (configs) => {
2022-03-09 14:43:54 -06:00
const source = [];
2024-01-23 09:06:00 -06:00
source.push('---');
source.push('title: Fleck configuration');
source.push('description: All the configurable flecks in this project.');
source.push('---');
2022-03-09 14:43:54 -06:00
source.push('');
2024-01-23 09:06:00 -06:00
source.push("import CodeBlock from '@theme/CodeBlock';");
2023-12-31 16:21:43 -06:00
source.push('');
source.push('<style>td > .theme-code-block \\{ margin: 0; \\}</style>');
source.push('');
source.push('This page documents all configurable flecks in this project.');
2022-03-09 14:43:54 -06:00
source.push('');
Object.entries(configs)
.sort(([l], [r]) => (l < r ? -1 : 1))
.forEach(([fleck, configs]) => {
2023-12-31 16:21:43 -06:00
source.push(`## \`${fleck}\``);
source.push('|Name|Default|Description|');
source.push('|-|-|-|');
2024-01-23 09:06:00 -06:00
configs.forEach(({defaultValue, description, key}) => {
2023-12-31 16:21:43 -06:00
// Leading and trailing empty cell to make table rendering easier.
2024-01-23 09:06:00 -06:00
const row = ['', key];
2023-12-31 16:21:43 -06:00
let code = defaultValue.replace(/`/g, '\\`');
// Multiline code. Fix indentation.
if (defaultValue.includes('\n')) {
const defaultValueLines = code.split('\n');
const [first, ...rest] = defaultValueLines;
const indent = (rest[0].length - rest[0].trimStart().length) - 2;
code = [first, ...rest.map((line) => line.substring(indent))].join('\\n');
}
row.push(`<CodeBlock language="javascript">{\`${code}\`}</CodeBlock>`);
2024-01-23 09:06:00 -06:00
row.push(description, '');
2023-12-31 16:21:43 -06:00
source.push(row.join('|'));
2022-03-09 14:43:54 -06:00
});
2023-12-31 16:21:43 -06:00
source.push('');
2022-03-09 14:43:54 -06:00
});
return source.join('\n');
};
2024-01-26 06:42:43 -06:00
exports.generateDocusaurusHookPage = (hooks) => {
2022-03-07 00:21:16 -06:00
const source = [];
2024-01-23 09:06:00 -06:00
source.push('---');
source.push('title: Hooks');
source.push('description: All the hooks in this project.');
source.push('---');
2022-03-07 00:21:16 -06:00
source.push('');
source.push('This page documents all the hooks in this project.');
source.push('');
Object.entries(hooks)
.sort(([lhook], [rhook]) => (lhook < rhook ? -1 : 1))
.forEach(([hook, {implementations = [], invocations = [], specification}]) => {
const {description, example, params} = specification || {
params: [],
};
2023-12-31 16:21:43 -06:00
source.push(`## \`${hook}\``);
source.push('');
2022-03-07 00:21:16 -06:00
if (description) {
2023-12-31 16:21:43 -06:00
source.push(...description.split('\n'));
2022-03-07 00:21:16 -06:00
source.push('');
}
2024-01-23 09:06:00 -06:00
if (example) {
source.push('### Example usage');
source.push('');
source.push('```javascript');
source.push('export const hooks = {');
source.push(` '${hook}': ${example}`);
source.push('};');
source.push('```');
source.push('');
}
2022-03-07 00:21:16 -06:00
if (params.length > 0) {
params.forEach(({description, name, type}) => {
2023-12-31 16:21:43 -06:00
source.push(`### <code>${name}: ${type}</code>`);
source.push('');
source.push(`<p>${description.trim()}</p>`);
source.push('');
2022-03-07 00:21:16 -06:00
});
source.push('');
}
if (implementations.length > 0) {
source.push('<details>');
source.push('<summary>Implementations</summary>');
source.push('<ul>');
2024-01-23 09:06:00 -06:00
implementations.forEach(({filename, column, line}) => {
2024-01-26 06:42:43 -06:00
source.push(`<li>${[filename, line, column].join(':')}</li>`);
2022-03-07 00:21:16 -06:00
});
source.push('</ul>');
source.push('</details>');
source.push('');
}
if (invocations.length > 0) {
source.push('<details>');
source.push('<summary>Invocations</summary>');
source.push('<ul>');
2024-01-23 09:06:00 -06:00
invocations.forEach(({filename, column, line}) => {
2024-01-26 06:42:43 -06:00
source.push(`<li>${[filename, line, column].join(':')}</li>`);
2022-03-07 00:21:16 -06:00
});
source.push('</ul>');
source.push('</details>');
source.push('');
}
});
return source.join('\n');
};
2024-01-26 06:42:43 -06:00
exports.generateDocusaurusTodoPage = (todos) => {
2022-03-07 00:21:16 -06:00
const source = [];
2024-01-23 09:06:00 -06:00
source.push('---');
source.push('title: TODO list');
source.push('description: All the TODO items in this project.');
source.push('---');
source.push('');
source.push("import CodeBlock from '@theme/CodeBlock';");
2022-03-07 00:21:16 -06:00
source.push('');
source.push('This page documents all the TODO items in this project.');
source.push('');
if (todos.length > 0) {
2024-01-23 09:06:00 -06:00
todos.forEach(({
filename,
context,
description,
}) => {
2024-01-26 06:42:43 -06:00
source.push(filename);
2024-01-23 09:06:00 -06:00
source.push(`> ## ${description}`);
source.push(`> <CodeBlock>${context}</CodeBlock>`);
source.push('');
2022-03-07 00:21:16 -06:00
});
source.push('');
}
return source.join('\n');
};
2024-01-26 06:42:43 -06:00
exports.generateJson = async function generate(flecks) {
const parsed = await parseFlecks(flecks);
const {
buildFiles,
config,
hooks,
todos,
} = parsed
.reduce(
(
r,
[
root,
sources,
],
) => {
const ensureHook = (hook) => {
if (!r.hooks[hook]) {
r.hooks[hook] = {
implementations: [],
invocations: [],
specification: undefined,
};
}
};
sources.forEach(
(
[
path,
{
buildFiles = [],
config = [],
hookImplementations = [],
hookInvocations = [],
hookSpecifications = [],
todos = [],
},
],
) => {
r.buildFiles.push(...buildFiles);
r.todos.push(...todos.map((todo) => ({
...todo,
filename: join(`**${root}**`, path),
})));
if (config.length > 0) {
let fleck = root;
if ('build/flecks.bootstrap.js' !== path) {
fleck = join(fleck, path.startsWith('src') ? path.slice(4) : path);
fleck = join(dirname(fleck), basename(fleck, extname(fleck)));
fleck = fleck.endsWith('/index') ? fleck.slice(0, -6) : fleck;
}
r.config[fleck] = config;
}
hookImplementations.forEach(({column, hook, line}) => {
ensureHook(hook);
r.hooks[hook].implementations.push({
column,
filename: join(`**${root}**`, path),
line,
});
});
hookInvocations.forEach(({
column,
hook,
line,
type,
}) => {
ensureHook(hook);
r.hooks[hook].invocations.push({
column,
filename: join(`**${root}**`, path),
line,
type,
});
});
hookSpecifications.forEach(({
hook,
description,
example,
params,
}) => {
ensureHook(hook);
r.hooks[hook].specification = {
description,
example,
params,
};
});
},
);
return r;
},
{
buildFiles: [],
config: {},
hooks: {},
todos: [],
},
);
const sortedHooks = Object.fromEntries(
Object.entries(hooks)
.map(([hook, {implementations, invocations, specification}]) => (
[
hook,
{
implementations: implementations
.sort(({filename: l}, {filename: r}) => (l < r ? -1 : 1)),
invocations: invocations
.sort(({filename: l}, {filename: r}) => (l < r ? -1 : 1)),
specification,
},
]
))
.sort(([l], [r]) => (l < r ? -1 : 1)),
);
Object.entries(sortedHooks)
.forEach(([hook, {specification}]) => {
if (!specification) {
// eslint-disable-next-line no-console
console.warn(`Warning: no specification for hook: '${hook}'`);
}
});
return {
'build-files': buildFiles,
config,
hooks: sortedHooks,
todos,
};
};