-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Expand file tree
/
Copy pathcombine-compodoc.ts
More file actions
executable file
·82 lines (73 loc) · 2.65 KB
/
combine-compodoc.ts
File metadata and controls
executable file
·82 lines (73 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Compodoc does not follow symlinks (it ignores them and their contents entirely)
// So, we need to run a separate compodoc process on every symlink inside the project,
// then combine the results into one large documentation.json
// eslint-disable-next-line depend/ban-dependencies
import { execaCommand } from 'execa';
// eslint-disable-next-line depend/ban-dependencies
import { lstat, readFile, realpath, writeFile } from 'fs-extra';
// eslint-disable-next-line depend/ban-dependencies
import { globSync } from 'glob';
import { join, resolve } from 'path';
import { temporaryDirectory } from '../code/core/src/common/utils/cli';
import { esMain } from './utils/esmain';
const logger = console;
// Find all symlinks in a directory. There may be more efficient ways to do this, but this works.
async function findSymlinks(dir: string) {
const potentialDirs = await globSync(`${dir}/**/*/`);
return (
await Promise.all(
potentialDirs.map(
async (p) => [p, (await lstat(p.replace(/\/$/, ''))).isSymbolicLink()] as [string, boolean]
)
)
)
.filter(([, s]) => s)
.map(([p]) => p);
}
async function run(cwd: string) {
const dirs = [
cwd,
...(await findSymlinks(resolve(cwd, './src'))),
...(await findSymlinks(resolve(cwd, './stories'))),
...(await findSymlinks(resolve(cwd, './template-stories'))),
];
const docsArray: Record<string, any>[] = await Promise.all(
dirs.map(async (dir) => {
const outputDir = await temporaryDirectory();
const resolvedDir = await realpath(dir);
await execaCommand(
`yarn --cwd ${cwd} compodoc ${resolvedDir} -p ./tsconfig.json -e json -d ${outputDir}`,
{ cwd }
);
const contents = await readFile(join(outputDir, 'documentation.json'), 'utf8');
try {
return JSON.parse(contents);
} catch (err) {
logger.error(`Error parsing JSON at ${outputDir}\n\n`);
logger.error(contents);
throw err;
}
})
);
// Compose together any array entries, discard anything else (we happen to only read the array fields)
const documentation = docsArray.slice(1).reduce((acc, entry) => {
return Object.fromEntries(
Object.entries(acc).map(([key, accValue]) => {
if (Array.isArray(accValue)) {
return [key, [...accValue, ...entry[key]]];
}
return [key, accValue];
})
);
}, docsArray[0]);
await writeFile(join(cwd, 'documentation.json'), JSON.stringify(documentation));
}
if (esMain(import.meta.url)) {
run(resolve(process.argv[2]))
.then(() => process.exit(0))
.catch((err) => {
logger.error();
logger.error(err);
process.exit(1);
});
}