forked from ui-router/publish-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_docs.js
executable file
·116 lines (97 loc) · 4.34 KB
/
generate_docs.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env node
const publishYalcPackage = require('./publish_yalc_package');
const shelljs = require('shelljs');
const nodeCleanup = require('node-cleanup');
const tmp = require('tmp');
const fs = require('fs');
const path = require('path');
const has = require('lodash').has;
const util = require('./util');
const _exec = util._exec;
util.packageDir();
const kebob = (string) => string
.split(/[@_/-]/)
.filter(x => !!x)
.map(x => x.toLowerCase() === 'uirouter' ? 'ui-router' : x)
.join('-');
const PACKAGE_JSON = JSON.parse(fs.readFileSync('./package.json'));
const TS_CONFIG = JSON.parse(fs.readFileSync('./tsconfig.json'));
const TYPEDOC_CONFIG_FILENAME = getTypedocConfigFilename();
const TYPEDOC_CONFIG = JSON.parse(fs.readFileSync(TYPEDOC_CONFIG_FILENAME));
const PACKAGE_DIR = process.cwd();
const DOWNSTREAM_CACHE = path.join(PACKAGE_DIR, '.downstream_cache');
const DOCGEN_DIR = tmp.dirSync().name;
const DOCGEN_PACKAGE_DIR = path.join(DOCGEN_DIR, kebob(PACKAGE_JSON.name));
function getTypedocConfigFilename() {
return ['./typedoc.json', 'tsconfig.typedoc.json'].find(filename => fs.existsSync(filename));
}
const requiredKeys = [ 'typedoc', 'typedoc.generateOptions' ];
const missing = requiredKeys.find(key => !has(TYPEDOC_CONFIG, key));
if (missing) {
console.error(`${TYPEDOC_CONFIG_FILENAME} does not contain configuration key: "${missing}"`);
process.exit(1);
}
// Create directory in temp dir for the current package
shelljs.mkdir('-p', DOCGEN_PACKAGE_DIR);
// Register hook to cleanup temp dir
nodeCleanup(() => {
const symlinks = fs.readdirSync(DOCGEN_DIR)
.filter(file => fs.lstatSync(file).isSymbolicLink());
symlinks.forEach(file => fs.unlinkSync(file));
shelljs.rm('-rf', DOCGEN_DIR);
});
// Fetch all included packages (i.e., core module) to .downstream_cache
// Symlink each package into the temp dir
const includes = TYPEDOC_CONFIG.typedoc.include || [];
includes.forEach(include => {
const { branch, package, repo } = include;
const flags = { noBuild: true, noPublish: true, noInstall: true, branch: branch };
if (!branch) {
const versionline = _exec(`yarn list --pattern ${package}`).stdout
.split(/[\r\n]+/).find(line => line.includes(package));
const match = /.*\@(([^@]*?)(-[a-zA-Z0-9]{8})?$)/.exec(versionline);
const version = match[ 2 ];
console.log({ versionline });
console.log({ match });
console.log({ version });
flags.branch = version ? version : flags.branch;
}
publishYalcPackage(path.join(DOCGEN_DIR, kebob(package)), repo, flags);
});
// symlink node_modules, package.json, tsconfig.typedoc.json into temp dir
shelljs.ln('-s', path.join(PACKAGE_DIR, 'package.json'), path.join(DOCGEN_DIR, 'package.json'));
shelljs.ln('-s', path.join(PACKAGE_DIR, TYPEDOC_CONFIG_FILENAME), path.join(DOCGEN_DIR, TYPEDOC_CONFIG_FILENAME));
shelljs.mkdir(path.join(DOCGEN_DIR, 'node_modules'));
fs.readdirSync(path.join(PACKAGE_DIR, 'node_modules')).forEach(module => {
const source = path.join(PACKAGE_DIR, 'node_modules', module);
const dest = path.join(DOCGEN_DIR, 'node_modules', module);
shelljs.ln('-s', source, dest);
});
// re-hydrate current package using .git dir
shelljs.cp('-r', path.join(PACKAGE_DIR, '.git'), DOCGEN_PACKAGE_DIR);
process.chdir(DOCGEN_PACKAGE_DIR);
shelljs.exec("git checkout .");
// create command line
const typedocOptions = TYPEDOC_CONFIG.typedoc.generateOptions || {};
typedocOptions.out = path.join(PACKAGE_DIR, typedocOptions.out || "_doc");
const cmdLineOpts = Object.keys(typedocOptions)
.map(key => `--${key} ${typedocOptions[ key ]}`)
.join(" ");
process.chdir(DOCGEN_DIR);
const possibleEs6LibPaths = [
'node_modules/typedoc/node_modules/typescript/lib/lib.es6.d.ts',
'node_modules/typescript/lib/lib.es6.d.ts',
].map(libPath => path.join(PACKAGE_DIR, libPath));
const es6LibPath = possibleEs6LibPaths.find(libPath => fs.existsSync(libPath));
if (!es6LibPath) {
throw new Error(`Couldn't find lib.es6.d.ts at ${possibleEs6LibPaths.join(' nor ')}`);
}
const files = []
.concat(TYPEDOC_CONFIG.files || [])
.concat(TS_CONFIG.files.map(x => path.join(DOCGEN_PACKAGE_DIR, x)))
.concat(includes.map(x => path.join(DOCGEN_DIR, kebob(x.package), x.entry)))
.map(x => `${path.normalize(x)}`)
.map(x => `./${path.relative(DOCGEN_DIR, x)}`)
.concat(es6LibPath);
// run typedoc command
_exec(`npx typedoc ${cmdLineOpts} ${files.join(' ')}`);