@@ -76,19 +76,19 @@ let PACKAGE = [];
7676 * @param {boolean } top True if this is the initial list of files
7777 */
7878function processList ( base , dir , list , top = true ) {
79- for ( const item of list ) {
80- const file = path . join ( dir , item ) ;
81- if ( ! EXCLUDE . has ( file ) ) {
82- const stat = fs . statSync ( path . resolve ( base , file ) ) ;
83- if ( stat . isDirectory ( ) ) {
84- if ( top || ! EXCLUDESUBDIRS ) {
85- processDir ( base , file ) ;
86- }
87- } else if ( file . match ( / \. t s $ / ) ) {
88- processFile ( base , file ) ;
89- }
79+ for ( const item of list ) {
80+ const file = path . join ( dir , item ) ;
81+ if ( ! EXCLUDE . has ( file ) ) {
82+ const stat = fs . statSync ( path . resolve ( base , file ) ) ;
83+ if ( stat . isDirectory ( ) ) {
84+ if ( top || ! EXCLUDESUBDIRS ) {
85+ processDir ( base , file ) ;
9086 }
87+ } else if ( file . match ( / \. t s $ / ) ) {
88+ processFile ( base , file ) ;
89+ }
9190 }
91+ }
9292}
9393
9494/**
@@ -98,8 +98,8 @@ function processList(base, dir, list, top = true) {
9898 * @param {string } dir The relative path within the root for the directory to process
9999 */
100100function processDir ( base , dir ) {
101- const root = path . resolve ( base , dir ) ;
102- processList ( base , dir , fs . readdirSync ( root ) , false ) ;
101+ const root = path . resolve ( base , dir ) ;
102+ processList ( base , dir , fs . readdirSync ( root ) , false ) ;
103103}
104104
105105/**
@@ -113,15 +113,15 @@ function processDir(base, dir) {
113113 * @param {string } name The path of the file relative to the base
114114 */
115115function processFile ( base , name ) {
116- console . info ( ' ' + name ) ;
117- const file = fs . readFileSync ( path . resolve ( base , name ) ) . toString ( ) ;
118- const parts = file . split ( EXPORTPATTERN ) ;
119- const objects = processParts ( parts ) ;
120- const lines = processLines ( name , objects ) ;
121- makeFile ( name , lines ) ;
122- if ( objects . length ) {
123- PACKAGE . push ( name ) ;
124- }
116+ console . info ( ' ' + name ) ;
117+ const file = fs . readFileSync ( path . resolve ( base , name ) ) . toString ( ) ;
118+ const parts = file . split ( EXPORTPATTERN ) ;
119+ const objects = processParts ( parts ) ;
120+ const lines = processLines ( name , objects ) ;
121+ makeFile ( name , lines ) ;
122+ if ( objects . length ) {
123+ PACKAGE . push ( name ) ;
124+ }
125125}
126126
127127/**
@@ -132,21 +132,21 @@ function processFile(base, name) {
132132 * @return {string[] } An array of names of exported objects
133133 */
134134function processParts ( parts ) {
135- const objects = [ ] ;
136- for ( let i = 1 ; i < parts . length ; i += 2 ) {
137- const words = parts [ i ] . split ( / \s + / ) ;
138- const type = words [ words . length - 2 ] ;
139- const name = words [ words . length - 1 ] ;
135+ const objects = [ ] ;
136+ for ( let i = 1 ; i < parts . length ; i += 2 ) {
137+ const words = parts [ i ] . split ( / \s + / ) ;
138+ const type = words [ words . length - 2 ] ;
139+ const name = words [ words . length - 1 ] ;
140140
141- if ( words [ 1 ] === 'default' || type === 'default' ) {
142- objects . push ( 'default' ) ;
143- } else if ( EXPORT_PROCESS . indexOf ( type ) >= 0 ) {
144- objects . push ( name ) ;
145- } else if ( EXPORT_IGNORE . indexOf ( type ) < 0 ) {
146- console . info ( " Can't process '" + parts [ i ] + "'" ) ;
147- }
141+ if ( words [ 1 ] === 'default' || type === 'default' ) {
142+ objects . push ( 'default' ) ;
143+ } else if ( EXPORT_PROCESS . indexOf ( type ) >= 0 ) {
144+ objects . push ( name ) ;
145+ } else if ( EXPORT_IGNORE . indexOf ( type ) < 0 ) {
146+ console . info ( " Can't process '" + parts [ i ] + "'" ) ;
148147 }
149- return objects ;
148+ }
149+ return objects ;
150150}
151151
152152/**
@@ -157,38 +157,38 @@ function processParts(parts) {
157157 * @return {string[] } Array of lines for the contents of the library file
158158 */
159159function processLines ( file , objects ) {
160- if ( objects . length === 0 ) return [ ] ;
161- const dir = path . dirname ( file ) . replace ( / ^ \. $ / , '' ) ;
162- const dots = dir . replace ( / [ ^ \/ ] + / g, '..' ) || '.' ;
163- const relative = path . join ( dots , '..' , MATHJAX , dir , path . basename ( file ) ) . replace ( / \. t s $ / , '.js' ) ;
164- const name = path . parse ( file ) . name ;
165- const lines = [
166- '"use strict";' ,
167- `Object.defineProperty(exports, '__esModule', {value: true});`
168- ] ;
169- let source = ( dir . replace ( / \/ / g, '.' ) + '.' + name ) . replace ( / ^ \. / , '' )
170- . replace ( / \. [ ^ . ] * / g, ( x ) => ( x . substr ( 1 ) . match ( / [ ^ a - z A - Z _ ] / ) ? '[\'' + x . substr ( 1 ) + '\']' : x ) ) ;
171- if ( exists ( path . resolve ( MATHJAX , file . replace ( / \. t s $ / , '' ) ) ) ) {
172- source += '_ts' ;
173- }
174- for ( const id of objects ) {
175- lines . push ( `exports.${ id } = MathJax._.${ source } .${ id } ;` ) ;
176- }
177- return lines ;
160+ if ( objects . length === 0 ) return [ ] ;
161+ const dir = path . dirname ( file ) . replace ( / ^ \. $ / , '' ) ;
162+ const dots = dir . replace ( / [ ^ \/ ] + / g, '..' ) || '.' ;
163+ const relative = path . join ( dots , '..' , MATHJAX , dir , path . basename ( file ) ) . replace ( / \. t s $ / , '.js' ) ;
164+ const name = path . parse ( file ) . name ;
165+ const lines = [
166+ '"use strict";' ,
167+ `Object.defineProperty(exports, '__esModule', {value: true});`
168+ ] ;
169+ let source = ( dir . replace ( / \/ / g, '.' ) + '.' + name ) . replace ( / ^ \. / , '' )
170+ . replace ( / \. [ ^ . ] * / g, ( x ) => ( x . substr ( 1 ) . match ( / [ ^ a - z A - Z _ ] / ) ? '[\'' + x . substr ( 1 ) + '\']' : x ) ) ;
171+ if ( exists ( path . resolve ( MATHJAX , file . replace ( / \. t s $ / , '' ) ) ) ) {
172+ source += '_ts' ;
173+ }
174+ for ( const id of objects ) {
175+ lines . push ( `exports.${ id } = MathJax._.${ source } .${ id } ;` ) ;
176+ }
177+ return lines ;
178178}
179179
180180/**
181181 * @param {string } file Path to a file
182182 * @return {boolean } True if the file exists, false if not
183183 */
184184function exists ( file ) {
185- if ( ! fs . existsSync ( file ) ) return false ;
186- //
187- // For case-insensitive file systems (like Mac OS),
188- // check that the file names match exactly
189- //
190- const [ dir , name ] = [ path . dirname ( file ) , path . basename ( file ) ] ;
191- return fs . readdirSync ( dir ) . indexOf ( name ) >= 0 ;
185+ if ( ! fs . existsSync ( file ) ) return false ;
186+ //
187+ // For case-insensitive file systems (like Mac OS),
188+ // check that the file names match exactly
189+ //
190+ const [ dir , name ] = [ path . dirname ( file ) , path . basename ( file ) ] ;
191+ return fs . readdirSync ( dir ) . indexOf ( name ) >= 0 ;
192192}
193193
194194/**
@@ -197,10 +197,10 @@ function exists(file) {
197197 * @param {string } dir The directory relative to the library directory
198198 */
199199function makeDir ( dir ) {
200- const fulldir = path . resolve ( LIB , dir ) ;
201- if ( fs . existsSync ( fulldir ) ) return ;
202- makeDir ( path . dirname ( fulldir ) ) ;
203- fs . mkdirSync ( fulldir ) ;
200+ const fulldir = path . resolve ( LIB , dir ) ;
201+ if ( fs . existsSync ( fulldir ) ) return ;
202+ makeDir ( path . dirname ( fulldir ) ) ;
203+ fs . mkdirSync ( fulldir ) ;
204204}
205205
206206/**
@@ -210,41 +210,41 @@ function makeDir(dir) {
210210 * @param {string[] } lines The contents of the file to create
211211 */
212212function makeFile ( file , lines ) {
213- if ( ! lines . length ) return ;
214- const [ dir , name ] = [ path . dirname ( file ) , path . basename ( file ) ] ;
215- makeDir ( dir ) ;
216- fs . writeFileSync ( path . resolve ( LIB , dir , name . replace ( / \. t s $ / , '.js' ) ) , lines . join ( '\n' ) + '\n' ) ;
213+ if ( ! lines . length ) return ;
214+ const [ dir , name ] = [ path . dirname ( file ) , path . basename ( file ) ] ;
215+ makeDir ( dir ) ;
216+ fs . writeFileSync ( path . resolve ( LIB , dir , name . replace ( / \. t s $ / , '.js' ) ) , lines . join ( '\n' ) + '\n' ) ;
217217}
218218
219219/**
220220 * Make the library file that adds all the exported objects into the MathJax global object
221221 */
222222function processGlobal ( ) {
223- console . info ( ' ' + COMPONENT + '.ts' ) ;
224- const lines = [
225- `import {combineWithMathJax} from '${ GLOBAL } ';` ,
226- '' ,
227- ] ;
228- const packages = [ ] ;
229- PACKAGE = PACKAGE . sort ( sortDir ) ;
230- while ( PACKAGE . length ) {
231- const dir = path . dirname ( PACKAGE [ 0 ] ) . split ( path . sep ) [ 0 ] ;
232- packages . push ( processPackage ( lines , INDENT , dir ) ) ;
233- }
234- lines . push ( '' , `combineWithMathJax({_: {` ) ;
235- lines . push ( INDENT + packages . join ( ',\n' + INDENT ) ) ;
236- lines . push ( '}});' ) ;
237- fs . writeFileSync ( path . join ( LIB , COMPONENT + '.js' ) , lines . join ( '\n' ) + '\n' ) ;
223+ console . info ( ' ' + COMPONENT + '.ts' ) ;
224+ const lines = [
225+ `import {combineWithMathJax} from '${ GLOBAL } ';` ,
226+ '' ,
227+ ] ;
228+ const packages = [ ] ;
229+ PACKAGE = PACKAGE . sort ( sortDir ) ;
230+ while ( PACKAGE . length ) {
231+ const dir = path . dirname ( PACKAGE [ 0 ] ) . split ( path . sep ) [ 0 ] ;
232+ packages . push ( processPackage ( lines , INDENT , dir ) ) ;
233+ }
234+ lines . push ( '' , `combineWithMathJax({_: {` ) ;
235+ lines . push ( INDENT + packages . join ( ',\n' + INDENT ) ) ;
236+ lines . push ( '}});' ) ;
237+ fs . writeFileSync ( path . join ( LIB , COMPONENT + '.js' ) , lines . join ( '\n' ) + '\n' ) ;
238238}
239239
240240/**
241241 * Sort file paths alphabetically
242242 */
243243function sortDir ( a , b ) {
244- const A = a . replace ( / \/ / g, '|' ) ; // Replace directory separator by one that is after the alphnumerics
245- const B = b . replace ( / \/ / g, '|' ) ;
246- return ( A === B ? 0 : A < B ? - 1 : 1 ) ;
247- }
244+ const A = a . replace ( / \/ / g, '|' ) ; // Replace directory separator by one that is after the alphnumerics
245+ const B = b . replace ( / \/ / g, '|' ) ;
246+ return ( A === B ? 0 : A < B ? - 1 : 1 ) ;
247+ }
248248
249249let importCount = 0 ;
250250/**
@@ -256,65 +256,65 @@ let importCount = 0;
256256 * @return {string } The string to use to load all the objects from the given directory
257257 */
258258function processPackage ( lines , space , dir ) {
259- const packages = [ ] ;
259+ const packages = [ ] ;
260+ //
261+ // Loop through the lines that are in the current directory
262+ //
263+ while ( PACKAGE . length && ( PACKAGE [ 0 ] . substr ( 0 , dir . length ) === dir || dir === '.' ) ) {
260264 //
261- // Loop through the lines that are in the current directory
265+ // If the current package is in this directory (not a subdirectory)
266+ // Get the location of transpiled mathjax file
267+ // Get the name to use for the data from that file
268+ // Create an entry for the file in the MathJax global that loads the reuired component
269+ // Otherwise (its in a subdirectory)
270+ // Get the subdirectory name
271+ // Process the subdirectory using an additional indentation
262272 //
263- while ( PACKAGE . length && ( PACKAGE [ 0 ] . substr ( 0 , dir . length ) === dir || dir === '.' ) ) {
264- //
265- // If the current package is in this directory (not a subdirectory)
266- // Get the location of transpiled mathjax file
267- // Get the name to use for the data from that file
268- // Create an entry for the file in the MathJax global that loads the reuired component
269- // Otherwise (its in a subdirectory)
270- // Get the subdirectory name
271- // Process the subdirectory using an additional indentation
272- //
273- if ( path . dirname ( PACKAGE [ 0 ] ) === dir ) {
274- const file = PACKAGE . shift ( ) ;
275- const name = path . basename ( file ) ;
276- const relativefile = path . join ( '..' , MATHJAX , dir , name ) . replace ( / \. t s $ / , '.js' ) ;
277- const component = 'module' + ( ++ importCount ) ;
278- lines . push ( `import * as ${ component } from '${ relativefile } ';` ) ;
279- let property = name . replace ( / \. t s $ / , '' ) ;
280- if ( property !== name && exists ( path . resolve ( MATHJAX , file . replace ( / \. t s $ / , '' ) ) ) ) {
281- property += '_ts' ;
282- }
283- if ( property . match ( / [ ^ a - z A - Z 0 - 9 _ ] / ) ) {
284- property = `"${ property } "` ;
285- }
286- packages . push ( `${ property } : ${ component } ` ) ;
287- } else {
288- let subdir = path . dirname ( PACKAGE [ 0 ] ) ;
289- while ( path . dirname ( subdir ) !== dir && subdir !== '.' ) {
290- subdir = path . dirname ( subdir ) ;
291- }
292- packages . push ( processPackage ( lines , space + ( dir === '.' ? '' : INDENT ) , subdir ) ) ;
293- }
273+ if ( path . dirname ( PACKAGE [ 0 ] ) === dir ) {
274+ const file = PACKAGE . shift ( ) ;
275+ const name = path . basename ( file ) ;
276+ const relativefile = path . join ( '..' , MATHJAX , dir , name ) . replace ( / \. t s $ / , '.js' ) ;
277+ const component = 'module' + ( ++ importCount ) ;
278+ lines . push ( `import * as ${ component } from '${ relativefile } ';` ) ;
279+ let property = name . replace ( / \. t s $ / , '' ) ;
280+ if ( property !== name && exists ( path . resolve ( MATHJAX , file . replace ( / \. t s $ / , '' ) ) ) ) {
281+ property += '_ts' ;
282+ }
283+ if ( property . match ( / [ ^ a - z A - Z 0 - 9 _ ] / ) ) {
284+ property = `"${ property } "` ;
285+ }
286+ packages . push ( `${ property } : ${ component } ` ) ;
287+ } else {
288+ let subdir = path . dirname ( PACKAGE [ 0 ] ) ;
289+ while ( path . dirname ( subdir ) !== dir && subdir !== '.' ) {
290+ subdir = path . dirname ( subdir ) ;
291+ }
292+ packages . push ( processPackage ( lines , space + ( dir === '.' ? '' : INDENT ) , subdir ) ) ;
294293 }
295- //
296- // Create the string defining the object that loads all the needed files into the proper places
297- //
298- if ( dir === '.' ) return packages . join ( ',\n ' ) ;
299- return path . basename ( dir ) + ': {\n' + INDENT + space + packages . join ( ',\n' + INDENT + space ) + '\n' + space + '}' ;
294+ }
295+ //
296+ // Create the string defining the object that loads all the needed files into the proper places
297+ //
298+ if ( dir === '.' ) return packages . join ( ',\n ' ) ;
299+ return path . basename ( dir ) + ': {\n' + INDENT + space + packages . join ( ',\n' + INDENT + space ) + '\n' + space + '}' ;
300300}
301301
302302/**
303303 * @param {string } dir The path to the directory tree to be removed (recursively)
304304 */
305305function rmDir ( dir ) {
306- if ( ! dir ) return ;
307- if ( fs . existsSync ( dir ) ) {
308- for ( const name of fs . readdirSync ( dir ) ) {
309- const file = path . join ( dir , name ) ;
310- if ( fs . lstatSync ( file ) . isDirectory ( ) ) {
311- rmDir ( file ) ;
312- } else {
313- fs . unlinkSync ( file ) ;
314- }
315- }
316- fs . rmdirSync ( dir ) ;
306+ if ( ! dir ) return ;
307+ if ( fs . existsSync ( dir ) ) {
308+ for ( const name of fs . readdirSync ( dir ) ) {
309+ const file = path . join ( dir , name ) ;
310+ if ( fs . lstatSync ( file ) . isDirectory ( ) ) {
311+ rmDir ( file ) ;
312+ } else {
313+ fs . unlinkSync ( file ) ;
314+ }
317315 }
316+ fs . rmdirSync ( dir ) ;
317+ }
318318}
319319
320320//
0 commit comments