1818* limitations under the License.
1919*/
2020
21- /* eslint-disable node/no-sync, no-console, max-statements */
21+ /* eslint-disable no-console, max-statements */
2222
2323'use strict' ;
2424
2525// MAIN //
2626
2727var path = require ( 'path' ) ;
28- var fs = require ( 'fs' ) ;
28+ var writeFileSync = require ( '@stdlib/fs/write-file' ) . sync ;
29+ var readFileSync = require ( '@stdlib/fs/read-file' ) . sync ;
30+ var existsSync = require ( '@stdlib/fs/exists' ) . sync ;
2931var contains = require ( '@stdlib/assert/contains' ) ;
3032var stdin = require ( '@stdlib/process/read-stdin' ) ;
3133var stdinStream = require ( '@stdlib/streams/node/stdin' ) ;
@@ -221,18 +223,19 @@ function create( fullPath ) {
221223 var properties ;
222224 var indexFile ;
223225 var tsDefPath ;
226+ var safeName ;
224227 var testFile ;
225228 var typesDir ;
226- var safeName ;
227229 var defFile ;
228230 var pkgPath ;
229231 var match ;
230232 var tsDef ;
231233 var tsDoc ;
232- var name ;
233234 var last ;
235+ var name ;
234236 var prop ;
235237 var stmt ;
238+ var err ;
236239 var str ;
237240 var ns ;
238241 var RE ;
@@ -251,15 +254,18 @@ function create( fullPath ) {
251254 }
252255 fullPath = path . join ( entryPoint , '..' , '..' ) ;
253256 typesDir = path . join ( fullPath , 'docs' , 'types' ) ;
254-
255- if ( ! fs . existsSync ( typesDir ) ) {
257+ if ( ! existsSync ( typesDir ) ) {
256258 return ;
257259 }
258260 defFilePath = path . join ( typesDir , 'index.d.ts' ) ;
259261
260- if ( fs . existsSync ( defFilePath ) ) {
262+ if ( existsSync ( defFilePath ) ) {
261263 // Use existing copyright year from definition file if available...
262- defFile = fs . readFileSync ( defFilePath , 'utf8' ) ;
264+ defFile = readFileSync ( defFilePath , 'utf8' ) ;
265+ if ( defFile instanceof Error ) {
266+ console . error ( 'Error reading definition file: ' + defFile . message ) ;
267+ return ;
268+ }
263269 match = defFile . match ( RE_COPYRIGHT_YEAR ) ;
264270 copyrightYear = ( match ) ? match [ 1 ] : currentYear ( ) ;
265271 } else {
@@ -269,8 +275,11 @@ function create( fullPath ) {
269275
270276 // Extract the copyright year from the existing definition file...
271277 RE_DOC_MAIN_DECLARE = / @ d e c l a r e \s + ( \w + ) \s + \( ( \w + ) \) / ;
272-
273- indexFile = fs . readFileSync ( entryPoint , 'utf-8' ) ;
278+ indexFile = readFileSync ( entryPoint , 'utf-8' ) ;
279+ if ( indexFile instanceof Error ) {
280+ console . error ( 'Error reading entry point file: ' + indexFile . message ) ;
281+ return ;
282+ }
274283 console . log ( 'Loading ' + entryPoint + '...' ) ;
275284 if ( ! RE_NAMESPACE . test ( indexFile ) ) {
276285 return ;
@@ -294,12 +303,16 @@ function create( fullPath ) {
294303 pkgPath = match [ 2 ] ;
295304
296305 tsDefPath = replace ( require . resolve ( pkgPath ) , 'lib/index.js' , 'docs/types/index.d.ts' ) ;
297- if ( fs . existsSync ( tsDefPath ) ) {
306+ if ( existsSync ( tsDefPath ) ) {
298307 stmt = replace ( IMPORT_STATEMENT , '<name>' , safeName ) ;
299308 stmt = replace ( stmt , '<path>' , pkgPath ) ;
300309 importStmts . push ( stmt ) ;
301-
302- tsDef = fs . readFileSync ( tsDefPath , 'utf-8' ) ;
310+ tsDef = readFileSync ( tsDefPath , 'utf-8' ) ;
311+ if ( tsDef instanceof Error ) {
312+ console . error ( 'Error reading TypeScript definition file: ' + tsDef . message ) ;
313+ match = RE . exec ( indexFile ) ;
314+ continue ;
315+ }
303316 mainExport = tsDef . match ( RE_TS_EXPORT ) ;
304317 RE_DOC_MAIN_DECLARE = new RegExp ( '(\\/\\*\\*\\n[\\s\\S]+?\\*\\/)\ndeclare (function|var|const) ' + mainExport [ 1 ] + '([\\s\\S]*): ([\\s\\S]+?);' ) ;
305318 last = tsDef . lastIndexOf ( '/**' ) ;
@@ -337,12 +350,19 @@ function create( fullPath ) {
337350 console . log ( 'Writing definition file...' ) ;
338351 defFile = createDefinitionFile ( ns , importStmts , properties , description ) ;
339352 defFile = replace ( defFile , '{{year}}' , String ( copyrightYear ) ) ;
340- fs . writeFileSync ( defFilePath , defFile ) ;
353+ err = writeFileSync ( defFilePath , defFile ) ;
354+ if ( err instanceof Error ) {
355+ console . error ( 'Error writing definition file: ' + err . message ) ;
356+ return ;
357+ }
341358
342359 console . log ( 'Writing test file...' ) ;
343360 testFile = createTestFile ( ns ) ;
344361 testFile = replace ( testFile , '{{year}}' , String ( copyrightYear ) ) ;
345- fs . writeFileSync ( path . join ( typesDir , 'test.ts' ) , testFile ) ;
362+ err = writeFileSync ( path . join ( typesDir , 'test.ts' ) , testFile ) ;
363+ if ( err instanceof Error ) {
364+ console . error ( 'Error writing test file: ' + err . message ) ;
365+ }
346366
347367 /**
348368 * Replaces variable names inside of example code to use exported namespace methods.
0 commit comments