11#!/usr/bin/env node
22
3- const publishYalcPackage = require ( './publish_yalc_package' ) ;
43const findSemverPackage = require ( './findSemverPackage' ) ;
54const shelljs = require ( 'shelljs' ) ;
65const nodeCleanup = require ( 'node-cleanup' ) ;
76const fs = require ( 'fs' ) ;
87const path = require ( 'path' ) ;
9- const has = require ( 'lodash' ) . has ;
8+ const _ = require ( 'lodash' ) ;
109const util = require ( './util' ) ;
1110const _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' ) ) ;
1613const 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
3449nodeCleanup ( ( ) => {
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 || [ ] ;
4758includes . 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` ) ;
0 commit comments