|
1 | 1 | #!/usr/bin/env node
|
2 | 2 | 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'); |
6 | 6 | const fs = require('fs');
|
7 | 7 | const path = require('path');
|
| 8 | +const has = require('lodash').has; |
| 9 | +const util = require('./util'); |
8 | 10 | const _exec = util._exec;
|
9 | 11 |
|
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('-'); |
12 | 19 |
|
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)); |
14 | 28 |
|
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)); |
17 | 31 | if (missing) {
|
18 | 32 | console.error(`typedoc.json does not contain configuration key: "${missing}"`);
|
19 | 33 | process.exit(1);
|
20 | 34 | }
|
21 | 35 |
|
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 || []; |
23 | 49 | includes.forEach(include => {
|
24 | 50 | const { branch, package, repo } = include;
|
25 | 51 | const flags = { noBuild: true, noPublish: true, noInstall: true, branch: branch };
|
26 | 52 |
|
27 | 53 | if (!branch) {
|
28 | 54 | 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 ]; |
30 | 56 | flags.branch = version ? version : flags.branch;
|
31 | 57 | }
|
32 | 58 |
|
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); |
34 | 71 | });
|
| 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"); |
35 | 80 |
|
36 |
| -const typedocOptions = CONFIG.typedoc.generateOptions || {}; |
37 | 81 | const cmdLineOpts = Object.keys(typedocOptions)
|
38 |
| - .map(key => `--${key} ${typedocOptions[key]}`) |
| 82 | + .map(key => `--${key} ${typedocOptions[ key ]}`) |
39 | 83 | .join(" ");
|
40 | 84 |
|
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')); |
42 | 93 |
|
43 |
| -util.packageDir(); |
44 |
| -_exec(`./node_modules/.bin/typedoc ${cmdLineOpts} ${files}`); |
| 94 | +// run typedoc command |
| 95 | +_exec(`npx typedoc ${cmdLineOpts} ${files.join(' ')}`); |
45 | 96 |
|
0 commit comments