1
1
/**
2
2
* Utility for generating TypeDoc API documentation and placing it in the docs/en/packages folder so it can be hosted by
3
- * Github pages and viewed through Docusaurus.
4
- * Contains a dry run command to investigate how the script will operate without actually writing to anything.
3
+ * Github pages and viewed through Docusaurus.
4
+ * Contains a dry run command to investigate how the script will operate without actually writing to anything.
5
5
* Contains a verbose option to print detailed build output generated by the typedoc processes that create the
6
6
* api documentation.
7
- *
7
+ *
8
8
* Usage :run node build/documentation/generate-typedocs.js OR
9
9
* Usage (dry-run): run node build/documentation/generate-typedocs.js --dry-run
10
- * Usage (verbose): run node build/documentation/generate-typedocs.js --verbose
10
+ * Usage (verbose): run node build/documentation/generate-typedocs.js --verbose
11
11
*/
12
12
13
13
const path = require ( "path" ) ;
14
14
const fs = require ( "fs" ) ;
15
15
const glob = require ( "glob" ) ;
16
- const os = require ( 'os' ) ;
17
- const chalk = require ( ' chalk' ) ;
18
- const yargs = require ( ' yargs' ) ;
16
+ const os = require ( "os" ) ;
17
+ const chalk = require ( " chalk" ) ;
18
+ const yargs = require ( " yargs" ) ;
19
19
20
- const { exec } = require ( ' child_process' ) ;
20
+ const { exec } = require ( " child_process" ) ;
21
21
const { spawn } = require ( "child_process" ) ;
22
22
23
23
const rootDir = path . resolve ( process . cwd ( ) ) ;
24
24
const srcDir = "packages/*" ;
25
25
const destDir = path . join ( "docs" , "en" , "packages" ) ;
26
26
27
- var copyReadmeScript = ' copy-package-readme.js' ;
28
- var copyReadmeScriptPath = path . join ( ' build' , ' documentation' , copyReadmeScript ) ;
27
+ var copyReadmeScript = " copy-package-readme.js" ;
28
+ var copyReadmeScriptPath = path . join ( " build" , " documentation" , copyReadmeScript ) ;
29
29
var dryRun = false ;
30
30
var verbose = false ;
31
31
32
32
const excludedPackages = [
33
33
"fast-browser-extensions" ,
34
+ "fast-color-explorer" ,
34
35
"fast-development-site-react" ,
35
36
"fast-permutator" ,
36
- "fast-tslint-rules"
37
+ "fast-tslint-rules" ,
37
38
] ;
38
39
39
40
/**
@@ -46,22 +47,24 @@ verbose = argv.verbose;
46
47
47
48
/**
48
49
* Run the copy readme script, then this one if successful
49
- * To prevent new links constantly being appended to the readme every
50
- * time the script is run, the copy readme script is run to regenerate
50
+ * To prevent new links constantly being appended to the readme every
51
+ * time the script is run, the copy readme script is run to regenerate
51
52
* files to their original form with the needed header
52
53
*/
53
54
54
- var proc = dryRun ? exec ( `node ${ copyReadmeScriptPath } --dry-run` ) : exec ( `node ${ copyReadmeScriptPath } ` ) ;
55
+ var proc = dryRun
56
+ ? exec ( `node ${ copyReadmeScriptPath } --dry-run` )
57
+ : exec ( `node ${ copyReadmeScriptPath } ` ) ;
55
58
56
- proc . stdout . on ( ' data' , function ( data ) {
59
+ proc . stdout . on ( " data" , function ( data ) {
57
60
process . stdout . write ( data ) ;
58
61
} ) ;
59
62
60
- proc . on ( ' error' , ( err ) => {
63
+ proc . on ( " error" , err => {
61
64
console . log ( chalk . red ( err ) ) ;
62
65
} ) ;
63
66
64
- proc . on ( ' close' , function ( code ) {
67
+ proc . on ( " close" , function ( code ) {
65
68
console . log ( chalk . green ( `${ copyReadmeScript } ran successfully.` ) ) ;
66
69
execute ( ) ;
67
70
} ) ;
@@ -70,7 +73,6 @@ proc.on('close', function(code) {
70
73
* Generate TypeDocs for each package
71
74
*/
72
75
function execute ( ) {
73
-
74
76
if ( dryRun ) {
75
77
console . log ( `In ${ destDir } , this script would...` ) ;
76
78
} else {
@@ -79,58 +81,70 @@ function execute() {
79
81
80
82
const packages = path . resolve ( rootDir , srcDir ) ;
81
83
82
- glob ( packages , { realpath :true } , function ( error , srcFiles ) {
83
-
84
- srcFiles . forEach ( ( srcPath ) => {
85
-
84
+ glob ( packages , { realpath : true } , function ( error , srcFiles ) {
85
+ srcFiles . forEach ( srcPath => {
86
86
var valid = true ;
87
87
88
- var packageName = srcPath
89
- . split ( path . sep )
90
- . pop ( ) ;
88
+ var packageName = srcPath . split ( path . sep ) . pop ( ) ;
91
89
92
- excludedPackages . forEach ( ( excludedPackageName ) => {
90
+ excludedPackages . forEach ( excludedPackageName => {
93
91
if ( packageName === excludedPackageName ) {
94
- console . log ( chalk . yellow ( `...skip generating TypeDoc API files for ${ packageName } ` ) ) ;
92
+ console . log (
93
+ chalk . yellow (
94
+ `...skip generating TypeDoc API files for ${ packageName } `
95
+ )
96
+ ) ;
95
97
valid = false ;
96
98
}
97
99
} ) ;
98
100
99
- if ( valid ) {
100
- dryRun ? console . log ( `...generate TypeDoc API files for ${ packageName } ` ) : createAPIFiles ( packageName , srcPath ) ;
101
+ if ( valid ) {
102
+ dryRun
103
+ ? console . log ( `...generate TypeDoc API files for ${ packageName } ` )
104
+ : createAPIFiles ( packageName , srcPath ) ;
101
105
}
102
-
103
106
} ) ;
104
-
105
107
} ) ;
106
108
}
107
109
108
110
/**
109
111
* Uses TypeDoc process to generate API docs for a given package
110
- * Stores them in the docs/en/packages folder
112
+ * Stores them in the docs/en/packages folder
111
113
*/
112
114
function createAPIFiles ( packageName , srcPath ) {
113
-
114
- var config = path . join ( srcPath , '/tsconfig.json' ) ;
115
- var output = path . join ( destDir , packageName , 'api' ) ;
116
- var file = ( packageName === "fast-animation" ) ? path . join ( srcPath , '/lib/index.ts' ) : path . join ( srcPath , '/src/index.ts' ) ;
117
-
118
- var typedocCmd = os . platform ( ) == 'win32' ? 'typedoc.cmd' : 'typedoc' ;
119
- var typedocPath = path . join ( 'node_modules' , '.bin' , typedocCmd ) ;
120
-
121
- const typedoc = spawn ( typedocPath , // the local TypeDoc installation is needed for the markdown theme to work
115
+ var config = path . join ( srcPath , "/build.tsconfig.json" ) ;
116
+ var output = path . join ( destDir , packageName , "api" ) ;
117
+ var file =
118
+ packageName === "fast-animation"
119
+ ? path . join ( srcPath , "/lib/index.ts" )
120
+ : path . join ( srcPath , "/src/index.ts" ) ;
121
+
122
+ var typedocCmd = os . platform ( ) == "win32" ? "typedoc.cmd" : "typedoc" ;
123
+ var typedocPath = path . join ( "node_modules" , ".bin" , typedocCmd ) ;
124
+
125
+ const typedoc = spawn (
126
+ typedocPath , // the local TypeDoc installation is needed for the markdown theme to work
122
127
[
123
- '--tsconfig' , config ,
124
- '--excludeNotExported' ,
125
- '--out' , output , file ,
126
- '--theme' , "markdown"
127
- ] ) ;
128
-
129
- typedoc . on ( 'close' , code => {
128
+ "--tsconfig" ,
129
+ config ,
130
+ "--excludeNotExported" ,
131
+ "--out" ,
132
+ output ,
133
+ file ,
134
+ "--theme" ,
135
+ "markdown" ,
136
+ ]
137
+ ) ;
138
+
139
+ typedoc . on ( "close" , code => {
130
140
if ( code ) {
131
- console . log ( chalk . red ( `${ packageName } - TypeDoc API docs not generated, error code: ${ code } ` ) ) ;
141
+ console . log (
142
+ chalk . red (
143
+ `${ packageName } - TypeDoc API docs not generated, error code: ${ code } `
144
+ )
145
+ ) ;
132
146
} else {
133
- console . log ( `${ packageName } - TypeDoc API docs generated` )
147
+ console . log ( `${ packageName } - TypeDoc API docs generated` ) ;
134
148
addHeaderToReadme ( packageName ) ;
135
149
addAPILinkToReadme ( packageName ) ;
136
150
}
@@ -139,58 +153,47 @@ function createAPIFiles(packageName, srcPath) {
139
153
// if set to true, will print detailed build output for typedoc process
140
154
// useful for debugging
141
155
if ( verbose ) {
142
- typedoc . stdout . on ( ' data' , ( data ) => {
156
+ typedoc . stdout . on ( " data" , data => {
143
157
console . log ( `${ data } ` ) ;
144
158
} ) ;
145
159
}
146
160
147
- typedoc . on ( ' error' , ( err ) => {
161
+ typedoc . on ( " error" , err => {
148
162
console . log ( chalk . red ( err ) ) ;
149
163
} ) ;
150
-
151
164
}
152
165
153
166
/**
154
167
* If the TypeDoc readme doesn't have this header
155
168
* It won't be accessible in docusaurus
156
169
*/
157
170
function addHeaderToReadme ( packageName ) {
158
-
159
- const readmePath = path . join ( destDir , packageName , 'api' , 'README.md' ) ;
171
+ const readmePath = path . join ( destDir , packageName , "api" , "README.md" ) ;
160
172
const readmeText = fs . readFileSync ( readmePath ) . toString ( ) ;
161
173
162
- var docusaurusHeader =
163
- `---\n` +
164
- `id: index\n` +
165
- `---\n\n` ;
174
+ var docusaurusHeader = `---\n` + `id: index\n` + `---\n\n` ;
166
175
167
176
try {
168
177
fs . writeFileSync ( readmePath , docusaurusHeader ) ;
169
178
fs . appendFileSync ( readmePath , readmeText ) ;
170
179
} catch ( err ) {
171
180
console . log ( chalk . red ( err ) ) ;
172
181
}
173
-
174
182
}
175
183
176
184
/**
177
185
* Creates link in package readme.md docs used by Docusaurus
178
186
* Said link routes to the TypeDoc API docs generated by this script
179
187
*/
180
188
function addAPILinkToReadme ( packageName ) {
181
-
182
- var readmePath = path . join ( destDir , packageName , 'README.md' ) ;
189
+ var readmePath = path . join ( destDir , packageName , "README.md" ) ;
183
190
var apiLink = "api" ;
184
191
185
- var usageText =
186
- "\n" +
187
- `[API Reference](${ apiLink } )` ;
192
+ var usageText = "\n" + `[API Reference](${ apiLink } )` ;
188
193
189
- fs . appendFile ( readmePath , usageText , function ( err ) {
194
+ fs . appendFile ( readmePath , usageText , function ( err ) {
190
195
if ( err ) {
191
196
console . log ( chalk . red ( err ) ) ;
192
- }
193
- } )
194
-
197
+ }
198
+ } ) ;
195
199
}
196
-
0 commit comments