schlechtenburg/packages/docgen/ts-to-json.mjs

70 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-03-15 19:58:11 +00:00
#!/usr/bin/env node
2022-03-13 22:12:18 +00:00
import { parse } from 'vue-docgen-api'
import { join } from 'path'
import glob from 'glob-promise'
2022-03-15 19:58:11 +00:00
import TypeDoc from 'typedoc'
2022-03-15 23:03:45 +00:00
import { writeFile, readFile } from 'fs/promises'
2022-03-13 22:12:18 +00:00
2022-03-15 19:58:11 +00:00
const DOCS_PATH = join(process.cwd(), process.argv[2] || './docs');
const LIB_PATH = join(process.cwd(), process.argv[3] || './lib');
2022-03-15 23:03:45 +00:00
const transformTSDocs = (docs) => {
return {
...docs,
children: docs.children.map(child => ({
...child,
typeParameters: child.typeParameter,
})),
};
};
const getTSDocs = async (outputFile) => {
2022-03-15 19:58:11 +00:00
const app = new TypeDoc.Application();
app.options.addReader(new TypeDoc.TSConfigReader());
2022-03-22 20:07:05 +00:00
app.bootstrap({
entryPoints: ['lib/index.ts'],
json: 'docs',
excludeExternals: true,
});
2022-03-15 19:58:11 +00:00
const project = app.convert();
2022-03-15 23:03:45 +00:00
await app.generateJson(project, outputFile);
const data = JSON.parse(await readFile(outputFile));
await writeFile(outputFile, JSON.stringify(transformTSDocs(data), null, 2))
2022-03-15 19:58:11 +00:00
};
const getVueComponentDocs = async (dir) => {
const found = await glob(join(dir, '/**/*'));
const files = found
.filter(found_path => found_path.match(/\.(tsx|jsx|vue)$/));
2022-03-13 22:12:18 +00:00
const vueParsed = await Promise.all(files
.map(file => parse(file)
.catch((error) => {
// For now, ignore any errors
// console.log(error, file);
return null;
})
)
);
const vueFiltered = vueParsed.filter(p => p);
2022-03-15 19:58:11 +00:00
return vueFiltered;
};
(async () => {
2022-03-15 23:03:45 +00:00
await Promise.all([
(() => {
const tsDocsOutput = join(DOCS_PATH, 'lib.json');
return getTSDocs(tsDocsOutput);
})(),
2022-03-15 19:58:11 +00:00
2022-03-15 23:03:45 +00:00
(async () => {
const vueComponents = await getVueComponentDocs(LIB_PATH);
const componentJsonPath = join(DOCS_PATH, 'components.json');
console.log(`Info: JSON written to ${componentJsonPath}`);
return writeFile(componentJsonPath, JSON.stringify(vueComponents, null, 2));
})(),
])
2022-03-13 22:12:18 +00:00
})();