|
1 | 1 | #!/usr/bin/env node |
| 2 | + |
2 | 3 | const publishYalcPackage = require('./publish_yalc_package'); |
| 4 | +const findSemverPackage = require('./findSemverPackage'); |
3 | 5 | const shelljs = require('shelljs'); |
4 | 6 | const nodeCleanup = require('node-cleanup'); |
5 | | -const tmp = require('tmp'); |
6 | 7 | const fs = require('fs'); |
7 | 8 | const path = require('path'); |
8 | 9 | const has = require('lodash').has; |
9 | 10 | const util = require('./util'); |
10 | 11 | const _exec = util._exec; |
11 | 12 |
|
12 | 13 | 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('-'); |
19 | | - |
20 | | -const PACKAGE_JSON = JSON.parse(fs.readFileSync('./package.json')); |
| 14 | +const TYPEDOC_CONFIG = getTypedocConfig(); |
21 | 15 | const TS_CONFIG = JSON.parse(fs.readFileSync('./tsconfig.json')); |
22 | | -const TYPEDOC_CONFIG_FILENAME = getTypedocConfigFilename(); |
23 | | -const TYPEDOC_CONFIG = JSON.parse(fs.readFileSync(TYPEDOC_CONFIG_FILENAME)); |
24 | 16 | const PACKAGE_DIR = process.cwd(); |
25 | | -const DOWNSTREAM_CACHE = path.join(PACKAGE_DIR, '.downstream_cache'); |
26 | | -const DOCGEN_DIR = tmp.dirSync().name; |
27 | | -const DOCGEN_PACKAGE_DIR = path.join(DOCGEN_DIR, kebob(PACKAGE_JSON.name)); |
28 | | - |
29 | | -function getTypedocConfigFilename() { |
30 | | - return ['./typedoc.json', 'tsconfig.typedoc.json'].find(filename => fs.existsSync(filename)); |
31 | | -} |
32 | | - |
33 | | -const requiredKeys = [ 'typedoc', 'typedoc.generateOptions' ]; |
34 | | -const missing = requiredKeys.find(key => !has(TYPEDOC_CONFIG, key)); |
35 | | -if (missing) { |
36 | | - console.error(`${TYPEDOC_CONFIG_FILENAME} does not contain configuration key: "${missing}"`); |
37 | | - process.exit(1); |
| 17 | +const PACKAGE_JSON = JSON.parse(fs.readFileSync('package.json')); |
| 18 | + |
| 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)); |
| 26 | + if (missing) { |
| 27 | + console.error(`${file} does not contain configuration key: '${missing}'`); |
| 28 | + process.exit(1); |
| 29 | + } |
| 30 | + return config; |
38 | 31 | } |
39 | 32 |
|
40 | | -// Create directory in temp dir for the current package |
41 | | -shelljs.mkdir('-p', DOCGEN_PACKAGE_DIR); |
42 | | -// Register hook to cleanup temp dir |
| 33 | +// Register hook to cleanup temp directories |
43 | 34 | nodeCleanup(() => { |
44 | | - const symlinks = fs.readdirSync(DOCGEN_DIR) |
45 | | - .filter(file => fs.lstatSync(file).isSymbolicLink()); |
46 | | - symlinks.forEach(file => fs.unlinkSync(file)); |
47 | | - shelljs.rm('-rf', DOCGEN_DIR); |
| 35 | + 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 | + }); |
48 | 43 | }); |
49 | 44 |
|
50 | | -// Fetch all included packages (i.e., core module) to .downstream_cache |
51 | | -// Symlink each package into the temp dir |
| 45 | +// Fetch all included packages (i.e., core module) |
52 | 46 | const includes = TYPEDOC_CONFIG.typedoc.include || []; |
53 | | -includes.forEach(include => { |
54 | | - const { branch, package, repo } = include; |
55 | | - const flags = { noBuild: true, noPublish: true, noInstall: true, branch: branch }; |
| 47 | +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 | + }; |
56 | 55 |
|
57 | 56 | if (!branch) { |
58 | | - const versionline = _exec(`yarn list --pattern ${package}`).stdout |
59 | | - .split(/[\r\n]+/).find(line => line.includes(package)); |
60 | | - const match = /.*\@(([^@]*?)(-[a-zA-Z0-9]{8})?$)/.exec(versionline); |
61 | | - const version = match[ 2 ]; |
62 | | - console.log({ versionline }); |
63 | | - console.log({ match }); |
64 | | - console.log({ version }); |
| 57 | + const semver = ['dependencies', 'peerDependencies', 'devDependencies'] |
| 58 | + .map((key) => (PACKAGE_JSON[key] || {})[pkg]) |
| 59 | + .find((x) => !!x); |
| 60 | + const version = findSemverPackage(pkg, semver); |
65 | 61 | flags.branch = version ? version : flags.branch; |
66 | 62 | } |
67 | 63 |
|
68 | | - publishYalcPackage(path.join(DOCGEN_DIR, kebob(package)), repo, flags); |
| 64 | + console.log(`fetching ${repo} to temporary directory ${pkg}`); |
| 65 | + _exec(`mkdir -p ${pkg}`); |
| 66 | + publishYalcPackage(pkg, repo, flags); |
69 | 67 | }); |
70 | 68 |
|
71 | | -// symlink node_modules, package.json, tsconfig.typedoc.json into temp dir |
72 | | -shelljs.ln('-s', path.join(PACKAGE_DIR, 'package.json'), path.join(DOCGEN_DIR, 'package.json')); |
73 | | -shelljs.ln('-s', path.join(PACKAGE_DIR, TYPEDOC_CONFIG_FILENAME), path.join(DOCGEN_DIR, TYPEDOC_CONFIG_FILENAME)); |
74 | | -shelljs.mkdir(path.join(DOCGEN_DIR, 'node_modules')); |
75 | | -fs.readdirSync(path.join(PACKAGE_DIR, 'node_modules')).forEach(module => { |
76 | | - const source = path.join(PACKAGE_DIR, 'node_modules', module); |
77 | | - const dest = path.join(DOCGEN_DIR, 'node_modules', module); |
78 | | - shelljs.ln('-s', source, dest); |
79 | | -}); |
80 | | -// re-hydrate current package using .git dir |
81 | | -shelljs.cp('-r', path.join(PACKAGE_DIR, '.git'), DOCGEN_PACKAGE_DIR); |
82 | | -process.chdir(DOCGEN_PACKAGE_DIR); |
83 | | -shelljs.exec("git checkout ."); |
84 | | - |
85 | 69 | // create command line |
86 | 70 | const typedocOptions = TYPEDOC_CONFIG.typedoc.generateOptions || {}; |
87 | | -typedocOptions.out = path.join(PACKAGE_DIR, typedocOptions.out || "_doc"); |
| 71 | +typedocOptions.out = path.join(PACKAGE_DIR, typedocOptions.out || '_doc'); |
88 | 72 |
|
89 | 73 | const cmdLineOpts = Object.keys(typedocOptions) |
90 | | - .map(key => `--${key} ${typedocOptions[ key ]}`) |
91 | | - .join(" "); |
92 | | - |
93 | | -process.chdir(DOCGEN_DIR); |
94 | | - |
95 | | -const possibleEs6LibPaths = [ |
96 | | - 'node_modules/typedoc/node_modules/typescript/lib/lib.es6.d.ts', |
97 | | - 'node_modules/typescript/lib/lib.es6.d.ts', |
98 | | -].map(libPath => path.join(PACKAGE_DIR, libPath)); |
99 | | - |
100 | | -const es6LibPath = possibleEs6LibPaths.find(libPath => fs.existsSync(libPath)); |
101 | | - |
102 | | -if (!es6LibPath) { |
103 | | - throw new Error(`Couldn't find lib.es6.d.ts at ${possibleEs6LibPaths.join(' nor ')}`); |
104 | | -} |
| 74 | + .map((key) => `--${key} ${typedocOptions[key]}`) |
| 75 | + .join(' '); |
105 | 76 |
|
106 | 77 | const files = [] |
107 | | - .concat(TYPEDOC_CONFIG.files || []) |
108 | | - .concat(TS_CONFIG.files.map(x => path.join(DOCGEN_PACKAGE_DIR, x))) |
109 | | - .concat(includes.map(x => path.join(DOCGEN_DIR, kebob(x.package), x.entry))) |
110 | | - .map(x => `${path.normalize(x)}`) |
111 | | - .map(x => `./${path.relative(DOCGEN_DIR, x)}`) |
112 | | - .concat(es6LibPath); |
| 78 | + .concat(TYPEDOC_CONFIG.files || []) |
| 79 | + .concat(TS_CONFIG.files) |
| 80 | + .concat(includes.map((x) => './' + path.join(x.package, x.entry))); |
113 | 81 |
|
114 | 82 | // run typedoc command |
115 | 83 | _exec(`npx typedoc ${cmdLineOpts} ${files.join(' ')}`); |
116 | | - |
0 commit comments