@@ -4,28 +4,37 @@ import { execSync } from 'node:child_process';
44
55const PKG = JSON . parse ( fs . readFileSync ( './package.json' ) . toString ( ) ) ;
66const RELEASE_BRANCH = 'master' ;
7+
78// Paths for ESLint to check. Converted to string for convenience.
8- const ESLINT_PATHS = [ 'eslint.config.mjs' , 'src' , 'npm-scripts.mjs' ] . join ( ' ' ) ;
9+ const ESLINT_PATHS = [
10+ 'eslint.config.mjs' ,
11+ 'jest.config.mjs' ,
12+ 'npm-scripts.mjs' ,
13+ 'src' ,
14+ ] . join ( ' ' ) ;
915// Paths for ESLint to ignore. Converted to string argument for convenience.
16+
1017const ESLINT_IGNORE_PATTERN_ARGS = [ ]
1118 . map ( entry => `--ignore-pattern ${ entry } ` )
1219 . join ( ' ' ) ;
20+
1321// Paths for Prettier to check/write. Converted to string for convenience.
1422// NOTE: Prettier ignores paths in .gitignore so we don't need to care about
1523// node/src/fbs.
1624const PRETTIER_PATHS = [
1725 'README.md' ,
1826 'eslint.config.mjs' ,
19- 'src ' ,
27+ 'jest.config.mjs ' ,
2028 'npm-scripts.mjs' ,
2129 'package.json' ,
2230 'tsconfig.json' ,
31+ 'src' ,
2332] . join ( ' ' ) ;
2433
2534const task = process . argv [ 2 ] ;
2635const args = process . argv . slice ( 3 ) . join ( ' ' ) ;
2736
28- run ( ) ;
37+ void run ( ) ;
2938
3039async function run ( ) {
3140 logInfo ( args ? `[args:"${ args } "]` : '' ) ;
@@ -34,11 +43,13 @@ async function run() {
3443 // As per NPM documentation (https://docs.npmjs.com/cli/v9/using-npm/scripts)
3544 // `prepare` script:
3645 //
37- // - Runs BEFORE the package is packed, i.e. during `npm publish` and `npm pack`.
46+ // - Runs BEFORE the package is packed, i.e. during `npm publish` and
47+ // `npm pack`.
3848 // - Runs on local `npm install` without any arguments.
39- // - NOTE: If a package being installed through git contains a `prepare` script,
40- // its dependencies and devDependencies will be installed, and the `prepare`
41- // script will be run, before the package is packaged and installed.
49+ // - NOTE: If a package being installed through git contains a `prepare`
50+ // script, its dependencies and devDependencies will be installed, and
51+ // the `prepare` script will be run, before the package is packaged and
52+ // installed.
4253 //
4354 // So here we compile TypeScript to JavaScript.
4455 case 'prepare' : {
@@ -48,15 +59,13 @@ async function run() {
4859 }
4960
5061 case 'typescript:build' : {
51- installDeps ( ) ;
5262 buildTypescript ( { force : true } ) ;
5363
5464 break ;
5565 }
5666
5767 case 'typescript:watch' : {
58- deleteLib ( ) ;
59- executeCmd ( `tsc --watch ${ args } ` ) ;
68+ watchTypescript ( ) ;
6069
6170 break ;
6271 }
@@ -74,14 +83,12 @@ async function run() {
7483 }
7584
7685 case 'test' : {
77- buildTypescript ( { force : false } ) ;
7886 test ( ) ;
7987
8088 break ;
8189 }
8290
8391 case 'coverage' : {
84- buildTypescript ( { force : false } ) ;
8592 executeCmd ( `jest --coverage ${ args } ` ) ;
8693 executeCmd ( 'open-cli coverage/lcov-report/index.html' ) ;
8794
@@ -123,15 +130,25 @@ function deleteLib() {
123130 fs . rmSync ( 'lib' , { recursive : true , force : true } ) ;
124131}
125132
126- function buildTypescript ( { force = false } = { force : false } ) {
133+ function buildTypescript ( { force } ) {
127134 if ( ! force && fs . existsSync ( 'lib' ) ) {
128135 return ;
129136 }
130137
131138 logInfo ( 'buildTypescript()' ) ;
132139
133140 deleteLib ( ) ;
134- executeCmd ( 'tsc' ) ;
141+
142+ // Generate .js CommonJS code and .d.ts TypeScript declaration files in lib/.
143+ executeCmd ( `tsc ${ args } ` ) ;
144+ }
145+
146+ function watchTypescript ( ) {
147+ logInfo ( 'watchTypescript()' ) ;
148+
149+ deleteLib ( ) ;
150+
151+ executeCmd ( `tsc --watch ${ args } ` ) ;
135152}
136153
137154function lint ( ) {
@@ -165,6 +182,7 @@ function installDeps() {
165182
166183 // Install/update deps.
167184 executeCmd ( 'npm ci --ignore-scripts' ) ;
185+
168186 // Update package-lock.json.
169187 executeCmd ( 'npm install --package-lock-only --ignore-scripts' ) ;
170188}
@@ -178,19 +196,15 @@ function checkRelease() {
178196 test ( ) ;
179197}
180198
181- function executeCmd ( command , exitOnError = true ) {
199+ function executeCmd ( command ) {
182200 logInfo ( `executeCmd(): ${ command } ` ) ;
183201
184202 try {
185203 execSync ( command , { stdio : [ 'ignore' , process . stdout , process . stderr ] } ) ;
186204 } catch ( error ) {
187- if ( exitOnError ) {
188- logError ( `executeCmd() failed, exiting: ${ error } ` ) ;
205+ logError ( `executeCmd() failed, exiting: ${ error } ` ) ;
189206
190- exitWithError ( ) ;
191- } else {
192- logInfo ( `executeCmd() failed, ignoring: ${ error } ` ) ;
193- }
207+ exitWithError ( ) ;
194208 }
195209}
196210
0 commit comments