-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-builder.js
217 lines (199 loc) · 7.28 KB
/
post-builder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* eslint no-var: 0 */
var path = require( 'path' );
var fs = require( 'fs' );
var BASE_DIR = process.cwd();
var CHARSET = 'utf8';
var PACKAGE_FILE = path.join( BASE_DIR, 'package.json' );
var COMMENT_BLOCK_END_PATTERN = /\*\/$/;
var COMMENT_BLOCK_START_PATTERN = /^\s*\/\*(\*)/;
var EXPORT_LINE_PATTERN = /^exports\..+=\s*(.+)(?<!void\s*0);\s*$/;
var EXT_PATTERN = /\.(j|t)s$/;
var JSDOC_BASEFILE_PATTERN = /[\/\\]types\.js$/;
var PATH_SEP_PATTERN = /[\/\\]{1,2}/g;
var readFile = promisify( fs.readFile.bind( fs ) );
var unlink = promisify( fs.unlink.bind( fs ) );
var writeFile = promisify( fs.writeFile.bind( fs ) );
/** @type {{[x:string]:*}} */
var packageJson;
var PKG_TYPE_DEF, PKG_TYPE_DEF_FILEPATH, PKG_TYPE_DEF_RELEASE_TASK_INDEX;
var PKG_TYPE_SOURCE, PKG_TYPE_SOURCE_FILEPATH, PKG_TYPE_SOURCE_RELEASE_TASK_INDEX;
/** @type {Array<ReleaseInfo>} */
var releaseInfoList = [];
/** @type {Array<Promise<ReleaseInfo>>} */
var trimResults = [];
var asyncTask = readFile( PACKAGE_FILE, CHARSET )
.then( function ( str ) {
packageJson = JSON.parse( str );
PKG_TYPE_DEF_FILEPATH = path.join( BASE_DIR, packageJson.types );
PKG_TYPE_SOURCE_FILEPATH = PKG_TYPE_DEF_FILEPATH.replace( /\.d\.ts$/, '.js' );
} )
.catch( function ( e ) {
console.log( 'FATAL: Error obtaining package.json data' )
throw e;
} );
asyncTask.then( function () { traverse( path.join( BASE_DIR, 'dist' ) ) } );
asyncTask = new Promise( function ( resolve, reject ) {
asyncTask.then( function () {
Promise.all( trimResults ).then(
function ( list ) {
releaseInfoList = list;
resolve();
}
).catch( reject )
} );
} );
asyncTask
.then( function () {
attachPublicTsDocs();
return Promise.all( [
[ PKG_TYPE_DEF, PKG_TYPE_DEF_FILEPATH, PKG_TYPE_DEF_RELEASE_TASK_INDEX ],
[ PKG_TYPE_SOURCE, PKG_TYPE_SOURCE_FILEPATH, PKG_TYPE_SOURCE_RELEASE_TASK_INDEX ]
].map( function ( item ) {
item[ 0 ] = item[ 1 ] === PKG_TYPE_DEF_FILEPATH
? item[ 0 ].replace( /^\n/, '' ).replace( /T_[0-9]+/gm, 'T' )
: trimComments( item[ 0 ] );
return writeFile( item[ 1 ], item[ 0 ], CHARSET )
.then( function () {
releaseInfoList[ item[ 2 ] ] = {
fname: item[ 1 ], result: true, t: 'write'
}
} )
.catch( function ( e ) {
releaseInfoList[ item[ 2 ] ] = {
fname: item[ 1 ], result: e, t: 'write'
}
} )
.finally( function () { item[ 0 ] = null } )
} ) );
} );
asyncTask
.then( updateReleaseManifest )
.then( function () {
var errorResults = releaseInfoList.filter( function ( r ) { return r.result !== true } );
if( errorResults.length ) {
console.log( '\nFollowing errors were encountered while trimming comments from dist source files: ' );
for( const e of errorResults ) {
console.log( e.fname + ': ' );
console.dir( e.result );
console.log( '.'.repeat( 16 ) );
}
}
console.log( '\nPost build completed @ ', new Date().toString() );
} )
.catch( e => {
console.dir( e );
console.log( '.'.repeat( 16 ) );
console.log( '\nPost build failure occurring @ ', new Date().toString() );
} );
function attachPublicTsDocs() {
for( var sourceLines = PKG_TYPE_SOURCE.split( /[\n\r]+/ ), l = sourceLines.length; l--; ) {
var exportLineMatches = sourceLines[ l ].match( EXPORT_LINE_PATTERN );
if( !exportLineMatches ) { continue }
var exportName = exportLineMatches[ 1 ];
var jsDeclarationLinePattern = new RegExp( `^((?:var\\s+${ exportName }\\s*=)|(?:function\\s+${ exportName }\\s*\\())` );
L2: {
for( ; l--; ) {
if( !jsDeclarationLinePattern.test( sourceLines[ l ] ) ) { continue }
if( !COMMENT_BLOCK_END_PATTERN.test( sourceLines[ l - 1 ] ) ) { break }
l--;
var commentBlockLines = [ sourceLines[ l ] ];
var oneLinerBlockMatches = sourceLines[ l ].match( COMMENT_BLOCK_START_PATTERN );
if( oneLinerBlockMatches ) {
if( !oneLinerBlockMatches[ 1 ] ) { break L2 } // not a jsDoc block
} else {
for( ; l--; ) {
commentBlockLines.push( sourceLines[ l ] );
var startBlockMatches = sourceLines[ l ].match( COMMENT_BLOCK_START_PATTERN );
if( !startBlockMatches ) { continue }
if( !startBlockMatches[ 1 ] ) { break L2 } // not a jsDoc block
break;
}
}
var tsDeclarationLinePattern = new RegExp( `(export\\s+(?:(?:function\\s+${ exportName }[<(])|(?:(const|let)\\s+${ exportName }:)|(?:class\\s+${ exportName }(?:(\\s+.)|(\\s*\\{)))))`, 'm' );
commentBlockLines = commentBlockLines.reverse().join( '\n' );
PKG_TYPE_DEF = PKG_TYPE_DEF.replace( tsDeclarationLinePattern, `\n${ commentBlockLines }\n$1` );
break;
}
}
}
}
function promisify( fn ) {
return function ( ...args ) {
return new Promise( function ( resolve, reject ) {
fn( ...args, function ( err, data ) {
err ? reject( err ) : resolve( data );
} );
} );
};
};
/** @param {string} directory */
function traverse( directory ) {
fs.readdirSync( directory, CHARSET ).forEach( function ( entry ) {
var fPath = path.join( directory, entry );
if( fs.statSync( fPath ).isDirectory() ) {
return traverse( fPath );
}
EXT_PATTERN.test( fPath ) && trimResults.push(
JSDOC_BASEFILE_PATTERN.test( fPath )
? unlink( fPath )
.then( function () { return { fname: fPath, result: true, t: 'unlink' } } )
.catch( function ( e ) { return { fname: fPath, result: e, t: 'unlink' } } )
: readFile( fPath, CHARSET ).then( function ( f ) {
let interrupt = false;
if( fPath === PKG_TYPE_DEF_FILEPATH ) {
PKG_TYPE_DEF = trimComments( f );
PKG_TYPE_DEF_RELEASE_TASK_INDEX = trimResults.length;
interrupt = true;
} else if( fPath === PKG_TYPE_SOURCE_FILEPATH ) {
/** next line addresses the babel jsdoc misplacement for this file */
PKG_TYPE_SOURCE = f.replace( /(\/\*(?:[^*]|\*(?!\/))*\*\/)\n(exports\.[^=]+=[^;]+;)/gm, '$2\n$1' )
PKG_TYPE_SOURCE_RELEASE_TASK_INDEX = trimResults.length;
interrupt = true;
}
if( interrupt ) {
return { fname: fPath, result: true, t: 'read' };
}
return writeFile( fPath, trimComments( f ), CHARSET )
.then( function () { return { fname: fPath, result: true, t: 'write' } } )
.catch( function ( e ) { return { fname: fPath, result: e, t: 'write' } } )
} ).catch( function ( e ) { return { fname: fPath, result: e, t: 'read' } } )
)
} );
}
/** @param {string} code */
function trimComments( code ) {
return code
.replace( /\/\*#__PURE__\*\//gm, '' )
.replace( /\s*\/\/.*$/gm, '' )
.replace( /\s*\/\*\*?\s+(?:[^*]|\*(?!\/))*(?:\*\/|$)/gm, '' )
.replace( /(?:^\s*\n)*/gm, '' );
}
/** @returns {Promise<void>} */
function updateReleaseManifest() {
if( !releaseInfoList.length ) { return Promise.resolve() }
try {
var files = [ 'package.json', 'index.js' ];
const distStart = releaseInfoList[ 0 ].fname.indexOf( 'dist' );
for( var i = releaseInfoList.length; i--; ) {
releaseInfoList[ i ].t !== 'unlink' &&
files.push(
releaseInfoList[ i ].fname
.slice( distStart )
.replace( PATH_SEP_PATTERN, '/' )
);
}
packageJson.files = files;
return writeFile( PACKAGE_FILE, JSON.stringify( packageJson, null, 2 ), CHARSET )
.catch( e => {
console.log( 'FATAL: Error while writing package.json files property' )
throw e;
} );
} catch( e ) { return Promise.reject( e ) }
}
/**
* @typedef {{
* fname: string,
* result: true|Error,
* t: 'read'|'unlink'|'write'
* }} ReleaseInfo
*/