Skip to content

Commit e41798b

Browse files
feat: Improve generate_docs
- Migrate to use docgen.json - Copy included libs (core) sources into src, i.e., src/@uirouter/core - Use updated typedoc-plugin-ui-router
1 parent e17c9e1 commit e41798b

File tree

4 files changed

+58
-63
lines changed

4 files changed

+58
-63
lines changed

findSemverPackage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = function findSemverPackage(packageName, semver) {
99
} else if (lines.length === 1) {
1010
return lines[ 0 ];
1111
} else {
12-
const line = stdout.split(/\n/).pop().trim();
12+
const line = stdout.trim().split(/\n/).pop().trim();
1313
const [, version] = /.* '(.*)'$/.exec(line) || [];
1414
if (!version) {
1515
console.log({ stdout, line });

generate_docs.js

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,45 @@
11
#!/usr/bin/env node
22

3-
const publishYalcPackage = require('./publish_yalc_package');
43
const findSemverPackage = require('./findSemverPackage');
54
const shelljs = require('shelljs');
65
const nodeCleanup = require('node-cleanup');
76
const fs = require('fs');
87
const path = require('path');
9-
const has = require('lodash').has;
8+
const _ = require('lodash');
109
const util = require('./util');
1110
const _exec = util._exec;
11+
const options = require('yargs').option('noclean', { boolean: true, default: false }).argv;
1212

13-
util.packageDir();
14-
const TYPEDOC_CONFIG = getTypedocConfig();
15-
const TS_CONFIG = JSON.parse(fs.readFileSync('./tsconfig.json'));
1613
const PACKAGE_DIR = process.cwd();
17-
const PACKAGE_JSON = JSON.parse(fs.readFileSync('package.json'));
14+
const SRC_DIR = `${PACKAGE_DIR}/src`;
15+
const DOCGENCONFIG_PATH = `${PACKAGE_DIR}/docgen.json`;
16+
const TSCONFIG_PATH = `${PACKAGE_DIR}/tsconfig.json`;
17+
const PACKAGEJSON_PATH = `${PACKAGE_DIR}/package.json`;
18+
19+
util.packageDir();
20+
21+
if (!fs.existsSync(SRC_DIR)) { throw new Error(`${SRC_DIR} does not exist`) }
22+
if (!fs.existsSync(DOCGENCONFIG_PATH)) { throw new Error(`${DOCGENCONFIG_PATH} does not exist`); }
23+
24+
const PACKAGEJSON = JSON.parse(fs.readFileSync(PACKAGEJSON_PATH));
25+
const DOCGENCONFIG = getDocgenConfig();
26+
const TSCONFIG_ORIG = JSON.parse(fs.readFileSync(TSCONFIG_PATH));
27+
const TSCONFIG_COPY = _.cloneDeep(TSCONFIG_ORIG);
28+
29+
const cleanupFns = [];
1830

19-
function getTypedocConfig() {
20-
const file = ['./typedoc.json', 'tsconfig.typedoc.json'].find((filename) =>
21-
fs.existsSync(filename)
22-
);
23-
const config = JSON.parse(fs.readFileSync(file));
24-
const requiredKeys = ['typedoc', 'typedoc.generateOptions'];
25-
const missing = requiredKeys.find((key) => !has(config, key));
31+
// Merge tsconfig block from docgen.json into tsconfig.json
32+
_.defaultsDeep(TSCONFIG_COPY, DOCGENCONFIG.tsconfig);
33+
fs.writeFileSync(TSCONFIG_PATH, JSON.stringify(TSCONFIG_COPY, null, 2));
34+
cleanupFns.push(() => fs.writeFileSync(TSCONFIG_PATH, JSON.stringify(TSCONFIG_ORIG, null, 2)));
35+
shelljs.cat(TSCONFIG_PATH);
36+
37+
function getDocgenConfig() {
38+
const config = JSON.parse(fs.readFileSync(DOCGENCONFIG_PATH));
39+
const requiredKeys = ['navigation', 'tsconfig'];
40+
const missing = requiredKeys.find((key) => !_.has(config, key));
2641
if (missing) {
27-
console.error(`${file} does not contain configuration key: '${missing}'`);
42+
console.error(`${DOCGENCONFIG_PATH} does not contain configuration key: '${missing}'`);
2843
process.exit(1);
2944
}
3045
return config;
@@ -33,51 +48,31 @@ function getTypedocConfig() {
3348
// Register hook to cleanup temp directories
3449
nodeCleanup(() => {
3550
util.packageDir();
36-
process
37-
.exit(0)(TYPEDOC_CONFIG.typedoc.include || [])
38-
.forEach((include) => {
39-
const pkg = include.package.split('/').shift();
40-
console.log(`(not) removing temporary directory ./${pkg}...`);
41-
shelljs.rm('-rf', package);
42-
});
51+
if (!options.noclean) {
52+
cleanupFns.forEach(fn => fn());
53+
}
4354
});
4455

4556
// Fetch all included packages (i.e., core module)
46-
const includes = TYPEDOC_CONFIG.typedoc.include || [];
57+
const includes = DOCGENCONFIG.include || [];
4758
includes.forEach((include) => {
48-
const { branch, package: pkg, repo } = include;
49-
const flags = {
50-
noBuild: true,
51-
noPublish: true,
52-
noInstall: true,
53-
branch: branch,
54-
};
59+
const { pkg, repo } = include;
60+
const semver = ['peerDependencies', 'dependencies', 'devDependencies']
61+
.map((key) => (PACKAGEJSON[key] || {})[pkg])
62+
.find((x) => !!x);
5563

56-
if (!branch) {
57-
const semver = ['dependencies', 'peerDependencies', 'devDependencies']
58-
.map((key) => (PACKAGE_JSON[key] || {})[pkg])
59-
.find((x) => !!x);
60-
const version = findSemverPackage(pkg, semver);
61-
flags.branch = version ? version : flags.branch;
64+
const INSTALLDIR = `${SRC_DIR}/${pkg}`;
65+
shelljs.mkdir('-p', INSTALLDIR);
66+
if (!fs.existsSync(path.join(INSTALLDIR, '.git'))) {
67+
_exec(`git clone ${repo} ${INSTALLDIR}`);
68+
cleanupFns.push(() => shelljs.rm('-rf', INSTALLDIR));
6269
}
6370

64-
console.log(`fetching ${repo} to temporary directory ${pkg}`);
65-
_exec(`mkdir -p ${pkg}`);
66-
publishYalcPackage(pkg, repo, flags);
71+
const version = findSemverPackage(pkg, semver);
72+
shelljs.pushd(INSTALLDIR)
73+
_exec(`git checkout ${version}`);
74+
shelljs.popd()
6775
});
6876

69-
// create command line
70-
const typedocOptions = TYPEDOC_CONFIG.typedoc.generateOptions || {};
71-
typedocOptions.out = path.join(PACKAGE_DIR, typedocOptions.out || '_doc');
72-
73-
const cmdLineOpts = Object.keys(typedocOptions)
74-
.map((key) => `--${key} ${typedocOptions[key]}`)
75-
.join(' ');
76-
77-
const files = []
78-
.concat(TYPEDOC_CONFIG.files || [])
79-
.concat(TS_CONFIG.files)
80-
.concat(includes.map((x) => './' + path.join(x.package, x.entry)));
81-
8277
// run typedoc command
83-
_exec(`npx typedoc ${cmdLineOpts} ${files.join(' ')}`);
78+
_exec(`npx typedoc`);

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@
2828
"test_downstream_projects": "./test_downstream_projects.js",
2929
"util": "./util.js"
3030
},
31+
"peerDependencies": {
32+
"typedoc": "~0.17.7",
33+
"typedoc-plugin-ui-router": "^3.0.1"
34+
},
3135
"dependencies": {
32-
"check-peer-dependencies": "2.0.2",
36+
"check-peer-dependencies": "^2.0.2",
3337
"conventional-changelog": "^3.1.21",
3438
"conventional-changelog-ui-router-core": "^1.4.2",
3539
"find-parent-dir": "^0.3.0",
@@ -43,11 +47,6 @@
4347
"shx": "^0.3.1",
4448
"tmp": "^0.2.1",
4549
"tweak-sourcemap-paths": "0.0.4",
46-
"typedoc": "^0.17.7",
47-
"typedoc-plugin-external-module-name": "3.1.0",
48-
"typedoc-plugin-internal-external": "2.1.1",
49-
"typedoc-plugin-ui-router": "2.0.2",
50-
"ui-router-typedoc-themes": "1.0.2",
5150
"yalc": "^1.0.0-pre.35",
5251
"yargs": "^15.3.1"
5352
},

publish_docs.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,24 @@ const _exec = util._exec;
1010
const sh = require('shelljs');
1111
const readlineSync = require('readline-sync');
1212

13-
const CONFIG = JSON.parse(fs.readFileSync('./typedoc.json'));
13+
const CONFIG = JSON.parse(fs.readFileSync('./docgen.json'));
14+
const TYPEDOC_CONFIG = JSON.parse(fs.readFileSync('./typedoc.json'));
1415
const pkg = JSON.parse(fs.readFileSync('./package.json'));
1516
const version = pkg.version;
1617

1718
const GIT_URL = "[email protected]:ui-router/ui-router.github.io.git";
1819
const installTargetDir = path.join(".downstream_cache", "ui-router.gihub.io");
19-
const PAGES_DIR = path.join(installTargetDir, CONFIG.typedoc.publishDir);
20+
const PAGES_DIR = path.join(installTargetDir, CONFIG.publishDir);
2021

2122
publishYalcPackage(installTargetDir, GIT_URL, { noInstall: true, noBuild: true, noPublish: true });
2223

2324
util.packageDir();
2425

2526
sh.rm('-rf', path.join(PAGES_DIR, 'latest'));
26-
sh.cp('-r', CONFIG.typedoc.generateOptions.out, path.join(PAGES_DIR, 'latest'));
27+
sh.cp('-r', TYPEDOC_CONFIG.out, path.join(PAGES_DIR, 'latest'));
2728

2829
sh.rm('-rf', path.join(PAGES_DIR, version));
29-
sh.cp('-r', CONFIG.typedoc.generateOptions.out, path.join(PAGES_DIR, version));
30+
sh.cp('-r', TYPEDOC_CONFIG.out, path.join(PAGES_DIR, version));
3031

3132
sh.cd(PAGES_DIR);
3233
_exec("./process_docs.sh");

0 commit comments

Comments
 (0)