22
33const fs = require ( 'fs-extra' ) ;
44const path = require ( 'path' ) ;
5- const TypeDoc = require ( path . join ( __dirname , '..' ) ) ;
5+ const TypeDoc = require ( '..' ) ;
66
77const app = new TypeDoc . Application ( {
88 mode : 'Modules' ,
@@ -18,75 +18,111 @@ const app = new TypeDoc.Application({
1818 ] ,
1919} ) ;
2020
21- const base = path . join ( __dirname , '../src/test/converter' ) ;
21+ // Note that this uses the test files in dist, not in src, this is important since
22+ // when running the tests we copy the tests to dist and then convert them.
23+ const base = path . join ( __dirname , '../dist/test/converter' ) ;
2224
23- fs . remove ( path . join ( __dirname , '../src/test/renderer/specs' ) )
24- . then ( ( ) => fs . readdir ( base ) )
25- . then ( dirs => {
26- // Get converter directories
27- return Promise . all ( dirs . map ( dir => {
28- const dirPath = path . join ( base , dir ) ;
29- return Promise . all ( [ dirPath , fs . stat ( dirPath ) ] ) ;
30- } ) ) ;
31- } ) . then ( dirs => {
32- // Rebuild converter specs
33- return dirs . map ( ( [ fullPath , isDir ] ) => {
34- if ( ! isDir ) return ;
25+ /** @type { [string, () => void, () => void][] } */
26+ const conversions = [
27+ [ 'specs' , ( ) => { } , ( ) => { } ] ,
28+ [ 'specs-without-exported' ,
29+ ( ) => app . options . setValue ( 'excludeNotExported' , true ) ,
30+ ( ) => app . options . setValue ( 'excludeNotExported' , false )
31+ ] ,
32+ [ 'specs-with-lump-categories' ,
33+ ( ) => app . options . setValue ( 'categorizeByGroup' , false ) ,
34+ ( ) => app . options . setValue ( 'categorizeByGroup' , true )
35+ ] ,
36+ ] ;
3537
36- console . log ( fullPath ) ;
37- TypeDoc . resetReflectionID ( ) ;
38- const src = app . expandInputFiles ( [ fullPath ] ) ;
39- const out = path . join ( fullPath , 'specs.json' ) ;
40- const result = app . convert ( src ) ;
41- const data = JSON . stringify ( result . toObject ( ) , null , ' ' )
42- . split ( TypeDoc . normalizePath ( base ) )
43- . join ( '%BASE%' ) ;
38+ /**
39+ * Rebuilds the converter specs for the provided dirs.
40+ * @param {string[] } dirs
41+ */
42+ function rebuildConverterTests ( dirs ) {
43+ return Promise . all ( dirs . map ( fullPath => {
44+ console . log ( fullPath ) ;
45+ const src = app . expandInputFiles ( [ fullPath ] ) ;
46+ return Promise . all ( conversions . map ( ( [ file , before , after ] ) => {
47+ const out = path . join ( fullPath , `${ file } .json` ) ;
48+ if ( fs . existsSync ( out ) ) {
49+ TypeDoc . resetReflectionID ( ) ;
50+ before ( ) ;
51+ const result = app . convert ( src ) ;
52+ const data = JSON . stringify ( result . toObject ( ) , null , ' ' )
53+ . split ( TypeDoc . normalizePath ( base ) )
54+ . join ( '%BASE%' ) ;
55+ after ( ) ;
56+ return fs . writeFile ( out . replace ( 'dist' , 'src' ) , data ) ;
57+ }
58+ } ) ) ;
59+ } ) ) ;
60+ }
4461
45- return fs . writeFile ( out , data ) ;
46- } )
47- } ) . then ( ( ) => {
48- // Rebuild renderer example
49- const src = path . join ( __dirname , '../examples/basic/src' ) ;
50- const out = path . join ( __dirname , '../src/test/renderer/specs' ) ;
62+ async function rebuildRendererTest ( ) {
63+ await fs . remove ( path . join ( __dirname , '../src/test/renderer/specs' ) ) ;
64+ const src = path . join ( __dirname , '../examples/basic/src' ) ;
65+ const out = path . join ( __dirname , '../src/test/renderer/specs' ) ;
5166
52- return fs . remove ( out )
53- . then ( ( ) => app . generateDocs ( app . expandInputFiles ( [ src ] ) , out ) )
54- . then ( ( ) => fs . remove ( path . join ( out , 'assets' ) ) )
55- . then ( ( ) => out ) ;
56- } ) . then ( out => {
57- // Rewrite GitHub urls
67+ await fs . remove ( out )
68+ app . generateDocs ( app . expandInputFiles ( [ src ] ) , out )
69+ await fs . remove ( path . join ( out , 'assets' ) )
5870
59- /**
60- * Avoiding sync methods here is... difficult.
61- * @param {string } base
62- * @param {string } dir
63- * @param {string[] } results
64- * @returns {string[] }
65- */
66- function getFiles ( base , dir = '' , results = [ ] ) {
67- const files = fs . readdirSync ( path . join ( base , dir ) ) ;
68- for ( const file of files ) {
69- const relativeToBase = path . join ( dir , file ) ;
70- if ( fs . statSync ( path . join ( base , relativeToBase ) ) . isDirectory ( ) ) {
71- getFiles ( base , relativeToBase , results ) ;
72- } else {
73- results . push ( relativeToBase ) ;
74- }
71+ /**
72+ * Avoiding sync methods here is... difficult.
73+ * @param {string } base
74+ * @param {string } dir
75+ * @param {string[] } results
76+ * @returns {string[] }
77+ */
78+ function getFiles ( base , dir = '' , results = [ ] ) {
79+ const files = fs . readdirSync ( path . join ( base , dir ) ) ;
80+ for ( const file of files ) {
81+ const relativeToBase = path . join ( dir , file ) ;
82+ if ( fs . statSync ( path . join ( base , relativeToBase ) ) . isDirectory ( ) ) {
83+ getFiles ( base , relativeToBase , results ) ;
84+ } else {
85+ results . push ( relativeToBase ) ;
7586 }
76- return results ;
7787 }
88+ return results ;
89+ }
7890
79- const gitHubRegExp = / h t t p s : \/ \/ g i t h u b .c o m \/ [ A - Z a - z 0 - 9 \- ] + \/ t y p e d o c \/ b l o b \/ [ ^ \/ ] * \/ e x a m p l e s / g;
80- return getFiles ( out ) . map ( file => {
81- const full = path . join ( out , file ) ;
82- return fs . readFile ( full , { encoding : 'utf-8' } )
83- . then ( text => fs . writeFile (
84- full ,
85- text . replace ( gitHubRegExp , 'https://github.com/sebastian-lenz/typedoc/blob/master/examples' )
86- ) ) ;
87- } ) ;
88- } )
89- . catch ( reason => {
90- console . error ( reason ) ;
91- process . exit ( 1 ) ;
91+ const gitHubRegExp = / h t t p s : \/ \/ g i t h u b .c o m \/ [ A - Z a - z 0 - 9 \- ] + \/ t y p e d o c \/ b l o b \/ [ ^ \/ ] * \/ e x a m p l e s / g;
92+ return getFiles ( out ) . map ( file => {
93+ const full = path . join ( out , file ) ;
94+ return fs . readFile ( full , { encoding : 'utf-8' } )
95+ . then ( text => fs . writeFile (
96+ full ,
97+ text . replace ( gitHubRegExp , 'https://github.com/sebastian-lenz/typedoc/blob/master/examples' )
98+ ) ) ;
9299 } ) ;
100+ }
101+
102+ async function main ( command = 'all' , filter = '' ) {
103+ if ( ! [ 'all' , 'converter' , 'renderer' ] . includes ( command ) ) {
104+ console . error ( 'Invalid command. Usage: node scripts/rebuild_specs.js <all|converter|renderer> [filter]' ) ;
105+ throw new Error ( ) ;
106+ }
107+
108+ if ( [ 'all' , 'converter' ] . includes ( command ) ) {
109+ const dirs = await Promise . all ( ( await fs . readdir ( base ) ) . map ( dir => {
110+ const dirPath = path . join ( base , dir ) ;
111+ return Promise . all ( [ dirPath , fs . stat ( dirPath ) ] ) ;
112+ } ) ) ;
113+
114+ await rebuildConverterTests ( dirs . filter ( ( [ fullPath , stat ] ) => {
115+ if ( ! stat . isDirectory ( ) ) return false ;
116+ return fullPath . endsWith ( filter ) ;
117+ } ) . map ( ( [ path ] ) => path ) ) ;
118+ }
119+
120+ if ( [ 'all' , 'renderer' ] . includes ( command ) ) {
121+ await rebuildRendererTest ( ) ;
122+ }
123+ }
124+
125+ main ( process . argv [ 2 ] , process . argv [ 3 ] ) . catch ( reason => {
126+ console . error ( reason ) ;
127+ process . exit ( 1 ) ;
128+ } ) ;
0 commit comments