1
1
#!/usr/bin/env node
2
2
3
- const publishYalcPackage = require ( './publish_yalc_package' ) ;
4
3
const findSemverPackage = require ( './findSemverPackage' ) ;
5
4
const shelljs = require ( 'shelljs' ) ;
6
5
const nodeCleanup = require ( 'node-cleanup' ) ;
7
6
const fs = require ( 'fs' ) ;
8
7
const path = require ( 'path' ) ;
9
- const has = require ( 'lodash' ) . has ;
8
+ const _ = require ( 'lodash' ) ;
10
9
const util = require ( './util' ) ;
11
10
const _exec = util . _exec ;
11
+ const options = require ( 'yargs' ) . option ( 'noclean' , { boolean : true , default : false } ) . argv ;
12
12
13
- util . packageDir ( ) ;
14
- const TYPEDOC_CONFIG = getTypedocConfig ( ) ;
15
- const TS_CONFIG = JSON . parse ( fs . readFileSync ( './tsconfig.json' ) ) ;
16
13
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 = [ ] ;
18
30
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 ) ) ;
26
41
if ( missing ) {
27
- console . error ( `${ file } does not contain configuration key: '${ missing } '` ) ;
42
+ console . error ( `${ DOCGENCONFIG_PATH } does not contain configuration key: '${ missing } '` ) ;
28
43
process . exit ( 1 ) ;
29
44
}
30
45
return config ;
@@ -33,51 +48,31 @@ function getTypedocConfig() {
33
48
// Register hook to cleanup temp directories
34
49
nodeCleanup ( ( ) => {
35
50
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
+ }
43
54
} ) ;
44
55
45
56
// Fetch all included packages (i.e., core module)
46
- const includes = TYPEDOC_CONFIG . typedoc . include || [ ] ;
57
+ const includes = DOCGENCONFIG . include || [ ] ;
47
58
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 ) ;
55
63
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 ) ) ;
62
69
}
63
70
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 ( )
67
75
} ) ;
68
76
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
-
82
77
// run typedoc command
83
- _exec ( `npx typedoc ${ cmdLineOpts } ${ files . join ( ' ' ) } ` ) ;
78
+ _exec ( `npx typedoc` ) ;
0 commit comments