@@ -4,6 +4,7 @@ Error.stackTraceLimit = Infinity;
4
4
5
5
const acquit = require ( 'acquit' ) ;
6
6
const fs = require ( 'fs' ) ;
7
+ const fsextra = require ( 'fs-extra' ) ;
7
8
const path = require ( 'path' ) ;
8
9
const pug = require ( 'pug' ) ;
9
10
const pkg = require ( '../package.json' ) ;
@@ -31,7 +32,7 @@ require('acquit-ignore')();
31
32
const { marked : markdown } = require ( 'marked' ) ;
32
33
const highlight = require ( 'highlight.js' ) ;
33
34
const { promisify } = require ( "util" ) ;
34
- const renderer = {
35
+ markdown . use ( {
35
36
heading : function ( text , level , raw , slugger ) {
36
37
const slug = slugger . slug ( raw ) ;
37
38
return `<h${ level } id="${ slug } ">
@@ -40,7 +41,7 @@ const renderer = {
40
41
</a>
41
42
</h${ level } >\n` ;
42
43
}
43
- } ;
44
+ } ) ;
44
45
markdown . setOptions ( {
45
46
highlight : function ( code , language ) {
46
47
if ( ! language ) {
@@ -52,30 +53,100 @@ markdown.setOptions({
52
53
return highlight . highlight ( code , { language } ) . value ;
53
54
}
54
55
} ) ;
55
- markdown . use ( { renderer } ) ;
56
-
57
- const testPath = path . resolve ( cwd , 'test' )
58
-
59
- const tests = [
60
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'geojson.test.js' ) ) . toString ( ) ) ,
61
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'docs/transactions.test.js' ) ) . toString ( ) ) ,
62
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'schema.alias.test.js' ) ) . toString ( ) ) ,
63
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'model.middleware.test.js' ) ) . toString ( ) ) ,
64
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'docs/date.test.js' ) ) . toString ( ) ) ,
65
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'docs/lean.test.js' ) ) . toString ( ) ) ,
66
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'docs/cast.test.js' ) ) . toString ( ) ) ,
67
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'docs/findoneandupdate.test.js' ) ) . toString ( ) ) ,
68
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'docs/custom-casting.test.js' ) ) . toString ( ) ) ,
69
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'docs/getters-setters.test.js' ) ) . toString ( ) ) ,
70
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'docs/virtuals.test.js' ) ) . toString ( ) ) ,
71
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'docs/defaults.test.js' ) ) . toString ( ) ) ,
72
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'docs/discriminators.test.js' ) ) . toString ( ) ) ,
73
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'docs/promises.test.js' ) ) . toString ( ) ) ,
74
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'docs/schematypes.test.js' ) ) . toString ( ) ) ,
75
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'docs/validation.test.js' ) ) . toString ( ) ) ,
76
- ...acquit . parse ( fs . readFileSync ( path . join ( testPath , 'docs/schemas.test.js' ) ) . toString ( ) )
56
+
57
+ const testPath = path . resolve ( cwd , 'test' ) ;
58
+
59
+ /** additional test files to scan, relative to `test/` */
60
+ const additionalTestFiles = [
61
+ 'geojson.test.js' ,
62
+ 'schema.alias.test.js'
63
+ ] ;
64
+ /** ignored files from `test/docs/` */
65
+ const ignoredTestFiles = [
66
+ // ignored because acquit does not like "for await"
67
+ 'asyncIterator.test.js'
77
68
] ;
78
69
70
+ /**
71
+ * Load all test file contents with acquit
72
+ * @returns {Object[] } acquit ast array
73
+ */
74
+ function getTests ( ) {
75
+ const testDocs = path . resolve ( testPath , 'docs' ) ;
76
+ const filesToScan = [
77
+ ...additionalTestFiles . map ( v => path . join ( testPath , v ) ) ,
78
+ ...fs . readdirSync ( testDocs ) . filter ( v => ! ignoredTestFiles . includes ( v ) ) . map ( v => path . join ( testDocs , v ) )
79
+ ] ;
80
+
81
+ const retArray = [ ] ;
82
+
83
+ for ( const file of filesToScan ) {
84
+ try {
85
+ retArray . push ( acquit . parse ( fs . readFileSync ( file ) . toString ( ) ) ) ;
86
+ } catch ( err ) {
87
+ // add a file path to a acquit error, for better debugging
88
+ err . filePath = file ;
89
+ throw err ;
90
+ }
91
+ }
92
+
93
+ return retArray . flat ( ) ;
94
+ }
95
+
96
+ function deleteAllHtmlFiles ( ) {
97
+ try {
98
+ console . log ( 'Delete' , path . join ( versionObj . versionedPath , 'index.html' ) ) ;
99
+ fs . unlinkSync ( path . join ( versionObj . versionedPath , 'index.html' ) ) ;
100
+ } catch ( err ) {
101
+ if ( err . code !== 'ENOENT' ) {
102
+ throw err ;
103
+ }
104
+ }
105
+ const foldersToClean = [
106
+ path . join ( '.' , versionObj . versionedPath , 'docs' ) ,
107
+ path . join ( '.' , versionObj . versionedPath , 'docs' , 'tutorials' ) ,
108
+ path . join ( '.' , versionObj . versionedPath , 'docs' , 'typescript' ) ,
109
+ path . join ( '.' , versionObj . versionedPath , 'docs' , 'api' ) ,
110
+ path . join ( '.' , versionObj . versionedPath , 'docs' , 'source' , '_docs' ) ,
111
+ './tmp'
112
+ ] ;
113
+ for ( const folder of foldersToClean ) {
114
+ let files = [ ] ;
115
+
116
+ try {
117
+ files = fs . readdirSync ( folder ) ;
118
+ } catch ( err ) {
119
+ if ( err . code === 'ENOENT' ) {
120
+ continue ;
121
+ }
122
+ }
123
+ for ( const file of files ) {
124
+ if ( file . endsWith ( '.html' ) ) {
125
+ console . log ( 'Delete' , path . join ( folder , file ) ) ;
126
+ fs . unlinkSync ( path . join ( folder , file ) ) ;
127
+ }
128
+ }
129
+ }
130
+ }
131
+
132
+ function moveDocsToTemp ( ) {
133
+ if ( ! versionObj . versionedPath ) {
134
+ throw new Error ( 'Cannot move unversioned deploy to /tmp' ) ;
135
+ }
136
+ try {
137
+ fs . rmSync ( './tmp' , { recursive : true } ) ;
138
+ } catch ( err ) {
139
+ if ( err . code !== 'ENOENT' ) {
140
+ throw err ;
141
+ }
142
+ }
143
+ const folder = versionObj . versionedPath . replace ( / ^ \/ / , '' ) ;
144
+ const directory = fs . readdirSync ( folder ) ;
145
+ for ( const file of directory ) {
146
+ fsextra . moveSync ( `${ folder } /${ file } ` , `./tmp/${ file } ` ) ;
147
+ }
148
+ }
149
+
79
150
/**
80
151
* Array of array of semver numbers, sorted with highest number first
81
152
* @example
@@ -227,7 +298,7 @@ const versionObj = (() => {
227
298
getLatestVersionOf ( 5 ) ,
228
299
]
229
300
} ;
230
- const versionedDeploy = process . env . DOCS_DEPLOY === "true" ? ! ( base . currentVersion . listed === base . latestVersion . listed ) : false ;
301
+ const versionedDeploy = ! ! process . env . DOCS_DEPLOY ? ! ( base . currentVersion . listed === base . latestVersion . listed ) : false ;
231
302
232
303
const versionedPath = versionedDeploy ? `/docs/${ base . currentVersion . path } ` : '' ;
233
304
@@ -364,7 +435,7 @@ async function pugify(filename, options, isReload = false) {
364
435
let contents = fs . readFileSync ( path . resolve ( cwd , inputFile ) ) . toString ( ) ;
365
436
366
437
if ( options . acquit ) {
367
- contents = transform ( contents , tests ) ;
438
+ contents = transform ( contents , getTests ( ) ) ;
368
439
369
440
contents = contents . replaceAll ( / ^ ` ` ` a c q u i t $ / gmi, "```javascript" ) ;
370
441
}
@@ -423,7 +494,7 @@ async function pugify(filename, options, isReload = false) {
423
494
} ) ;
424
495
}
425
496
426
- // extra function to start watching for file-changes, without having to call this file directly with "watch"
497
+ /** extra function to start watching for file-changes, without having to call this file directly with "watch" */
427
498
function startWatch ( ) {
428
499
Object . entries ( docsFilemap . fileMap ) . forEach ( ( [ file , fileValue ] ) => {
429
500
let watchPath = path . resolve ( cwd , file ) ;
@@ -491,7 +562,7 @@ const pathsToCopy = [
491
562
'docs/js' ,
492
563
'docs/css' ,
493
564
'docs/images'
494
- ]
565
+ ] ;
495
566
496
567
/** Copy all static files when versionedDeploy is used */
497
568
async function copyAllRequiredFiles ( ) {
@@ -500,7 +571,6 @@ async function copyAllRequiredFiles() {
500
571
return ;
501
572
}
502
573
503
- const fsextra = require ( 'fs-extra' ) ;
504
574
await Promise . all ( pathsToCopy . map ( async v => {
505
575
const resultPath = path . resolve ( cwd , path . join ( '.' , versionObj . versionedPath , v ) ) ;
506
576
await fsextra . copy ( v , resultPath ) ;
@@ -517,8 +587,16 @@ exports.cwd = cwd;
517
587
518
588
// only run the following code if this file is the main module / entry file
519
589
if ( isMain ) {
520
- console . log ( `Processing ~${ files . length } files` ) ;
521
- Promise . all ( [ pugifyAllFiles ( ) , copyAllRequiredFiles ( ) ] ) . then ( ( ) => {
522
- console . log ( "Done Processing" ) ;
523
- } )
590
+ ( async function main ( ) {
591
+ console . log ( `Processing ~${ files . length } files` ) ;
592
+
593
+ await deleteAllHtmlFiles ( ) ;
594
+ await pugifyAllFiles ( ) ;
595
+ await copyAllRequiredFiles ( ) ;
596
+ if ( ! ! process . env . DOCS_DEPLOY && ! ! versionObj . versionedPath ) {
597
+ await moveDocsToTemp ( ) ;
598
+ }
599
+
600
+ console . log ( 'Done Processing' ) ;
601
+ } ) ( ) ;
524
602
}
0 commit comments