Skip to content

Commit 3abfe07

Browse files
feat(generate_docs): update to typedoc 0.11.x and move sources to temp dir to get proper git urls
1 parent 05b7f6e commit 3abfe07

File tree

1 file changed

+67
-16
lines changed

1 file changed

+67
-16
lines changed

generate_docs.js

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,96 @@
11
#!/usr/bin/env node
22
const publishYalcPackage = require('./publish_yalc_package');
3-
const util = require('./util');
4-
util.packageDir();
5-
3+
const shelljs = require('shelljs');
4+
const nodeCleanup = require('node-cleanup');
5+
const tmp = require('tmp');
66
const fs = require('fs');
77
const path = require('path');
8+
const has = require('lodash').has;
9+
const util = require('./util');
810
const _exec = util._exec;
911

10-
const pkg = JSON.parse(fs.readFileSync('./package.json'));
11-
const CONFIG = JSON.parse(fs.readFileSync('./typedoc.json'));
12+
util.packageDir();
13+
14+
const kebob = (string) => string
15+
.split(/[@_/-]/)
16+
.filter(x => !!x)
17+
.map(x => x.toLowerCase() === 'uirouter' ? 'ui-router' : x)
18+
.join('-');
1219

13-
const has = require('lodash').has;
20+
const PACKAGE_JSON = JSON.parse(fs.readFileSync('./package.json'));
21+
const TS_CONFIG = JSON.parse(fs.readFileSync('./tsconfig.json'));
22+
const TYPEDOC_CONFIG = JSON.parse(fs.readFileSync('./typedoc.json'));
23+
const PACKAGE_DIR = process.cwd();
24+
const DOWNSTREAM_CACHE = path.join(PACKAGE_DIR, '.downstream_cache');
25+
const TEMP = tmp.dirSync();
26+
const DOCGEN_DIR = TEMP.name;
27+
const DOCGEN_PACKAGE_DIR = path.join(DOCGEN_DIR, kebob(PACKAGE_JSON.name));
1428

15-
const requiredKeys = ['typedoc', 'typedoc.generateOptions', 'files'];
16-
const missing = requiredKeys.find(key => !has(CONFIG, key));
29+
const requiredKeys = [ 'typedoc', 'typedoc.generateOptions' ];
30+
const missing = requiredKeys.find(key => !has(TYPEDOC_CONFIG, key));
1731
if (missing) {
1832
console.error(`typedoc.json does not contain configuration key: "${missing}"`);
1933
process.exit(1);
2034
}
2135

22-
const includes = CONFIG.typedoc.include || [];
36+
// Create directory in temp dir for the current package
37+
shelljs.mkdir('-p', DOCGEN_PACKAGE_DIR);
38+
// Register hook to cleanup temp dir
39+
nodeCleanup(() => {
40+
// const symlinks = fs.readdirSync(DOCGEN_DIR)
41+
// .filter(file => fs.lstatSync(file).isSymbolicLink());
42+
// symlinks.forEach(file => fs.unlinkSync(file));
43+
// shelljs.rm('-rf', DOCGEN_DIR);
44+
});
45+
46+
// Fetch all included packages (i.e., core module) to .downstream_cache
47+
// Symlink each package into the temp dir
48+
const includes = TYPEDOC_CONFIG.typedoc.include || [];
2349
includes.forEach(include => {
2450
const { branch, package, repo } = include;
2551
const flags = { noBuild: true, noPublish: true, noInstall: true, branch: branch };
2652

2753
if (!branch) {
2854
const versionline = _exec(`yarn list --pattern ${package}`).stdout.split(/[\r\n]+/).find(line => line.includes(package));
29-
const version = /.*\@([^@]*)/.exec(versionline)[1];
55+
const version = /.*\@([^@]*)/.exec(versionline)[ 1 ];
3056
flags.branch = version ? version : flags.branch;
3157
}
3258

33-
publishYalcPackage(path.join('.downstream_cache', package), repo, flags);
59+
publishYalcPackage(path.join(DOWNSTREAM_CACHE, package), repo, flags);
60+
shelljs.ln(path.join(DOWNSTREAM_CACHE, package), path.join(DOCGEN_DIR, kebob(package)));
61+
});
62+
63+
// symlink node_modules, package.json, typedoc.json into temp dir
64+
shelljs.ln('-s', path.join(PACKAGE_DIR, 'package.json'), path.join(DOCGEN_DIR, 'package.json'));
65+
shelljs.ln('-s', path.join(PACKAGE_DIR, 'typedoc.json'), path.join(DOCGEN_DIR, 'typedoc.json'));
66+
shelljs.mkdir(path.join(DOCGEN_DIR, 'node_modules'));
67+
fs.readdirSync(path.join(PACKAGE_DIR, 'node_modules')).forEach(module => {
68+
const source = path.join(PACKAGE_DIR, 'node_modules', module);
69+
const dest = path.join(DOCGEN_DIR, 'node_modules', module);
70+
shelljs.ln('-s', source, dest);
3471
});
72+
// re-hydrate current package using .git dir
73+
shelljs.cp('-r', path.join(PACKAGE_DIR, '.git'), DOCGEN_PACKAGE_DIR);
74+
process.chdir(DOCGEN_PACKAGE_DIR);
75+
shelljs.exec("git co .");
76+
77+
// create command line
78+
const typedocOptions = TYPEDOC_CONFIG.typedoc.generateOptions || {};
79+
typedocOptions.out = path.join(PACKAGE_DIR, typedocOptions.out || "_doc");
3580

36-
const typedocOptions = CONFIG.typedoc.generateOptions || {};
3781
const cmdLineOpts = Object.keys(typedocOptions)
38-
.map(key => `--${key} ${typedocOptions[key]}`)
82+
.map(key => `--${key} ${typedocOptions[ key ]}`)
3983
.join(" ");
4084

41-
const files = CONFIG.files.concat(includes.map(x => path.join('.downstream_cache', x.package, x.entry))).join(" ");
85+
process.chdir(DOCGEN_DIR);
86+
const files = []
87+
.concat(TYPEDOC_CONFIG.files || [])
88+
.concat(TS_CONFIG.files.map(x => path.join(DOCGEN_PACKAGE_DIR, x)))
89+
.concat(includes.map(x => path.join(DOCGEN_DIR, kebob(x.package), x.entry)))
90+
.map(x => `${path.normalize(x)}`)
91+
.map(x => `./${path.relative(DOCGEN_DIR, x)}`)
92+
.concat(path.join(PACKAGE_DIR, 'node_modules/typedoc/node_modules/typescript/lib/lib.es6.d.ts'));
4293

43-
util.packageDir();
44-
_exec(`./node_modules/.bin/typedoc ${cmdLineOpts} ${files}`);
94+
// run typedoc command
95+
_exec(`npx typedoc ${cmdLineOpts} ${files.join(' ')}`);
4596

0 commit comments

Comments
 (0)