@@ -5,29 +5,35 @@ const path = require('path');
55 * Cross-platform script to delete temporary and cache paths. Run this if Platform.Bible is holding
66 * on to outdated resources, such as localization strings, project data, or WebView ids.
77 *
8+ * Warning: The `--build` flag deletes:
9+ *
10+ * - All core extension builds, which makes the next core:start last minutes longer
11+ *
812 * Warning: The `--core` flag deletes:
913 *
1014 * - The `Electron` cache folder, which affect any other Electron apps
1115 * - `paranext-core/dev-appdata/`, which includes `installed-extensions/`
1216 *
1317 * Warning: The `--yalc` flag deletion includes:
1418 *
15- * - `paranext-core/dev-packages/`, which makes the core re- install last minutes longer
19+ * - `paranext-core/dev-packages/`, which makes the next core: install last minutes longer
1620 */
1721
1822// Check command-line arguments
1923
20- const hasAllFlag = process . argv . includes ( '--all' ) ;
21- const hasCoreFlag = process . argv . includes ( '--core' ) ;
22- const hasExtFlag = process . argv . includes ( '--ext' ) ;
23- const hasNpmFlag = process . argv . includes ( '--npm' ) ;
24- const hasTestFlag = process . argv . includes ( '--test' ) ;
25- const hasYalcFlag = process . argv . includes ( '--yalc' ) ;
24+ const allFlag = process . argv . includes ( '--all' ) ;
25+ const buildFlag = process . argv . includes ( '--build' ) ;
26+ const coreFlag = process . argv . includes ( '--core' ) ;
27+ const extFlag = process . argv . includes ( '--ext' ) ;
28+ const npmFlag = process . argv . includes ( '--npm' ) ;
29+ const testFlag = process . argv . includes ( '--test' ) ;
30+ const yalcFlag = process . argv . includes ( '--yalc' ) ;
2631
2732function printUsageAndExit ( code = 0 ) {
2833 console . info (
29- 'Usage: node delete-temp-files.cjs [--all] [--core] [--ext] [--npm] [--test] [--yalc]' ,
34+ 'Usage: node delete-temp-files.cjs [--all] [--build] [-- core] [--ext] [--npm] [--test] [--yalc]' ,
3035 ) ;
36+ console . info ( ' --build Delete core build files' ) ;
3137 console . info ( ' --core Delete Electron and core caches (Electron, dev-appdata)' ) ;
3238 console . info ( ' --ext Delete extension builds (dist, src/temp-build)' ) ;
3339 console . info ( ' --npm Delete extension package-lock.json and all node_modules' ) ;
@@ -41,22 +47,33 @@ if (process.argv.length <= 2 || process.argv.includes('--help') || process.argv.
4147 printUsageAndExit ( ) ;
4248}
4349
44- if ( ! hasAllFlag && ! hasCoreFlag && ! hasExtFlag && ! hasNpmFlag && ! hasTestFlag && ! hasYalcFlag ) {
50+ if ( ! allFlag && ! buildFlag && ! coreFlag && ! extFlag && ! npmFlag && ! testFlag && ! yalcFlag ) {
4551 printUsageAndExit ( 1 ) ;
4652}
4753
48- const shouldDeleteCore = hasAllFlag || hasCoreFlag ;
49- const shouldDeleteExt = hasAllFlag || hasExtFlag ;
50- const shouldDeleteNpm = hasAllFlag || hasNpmFlag ;
51- const shouldDeleteTest = hasAllFlag || hasTestFlag ;
52- const shouldDeleteYalc = hasAllFlag || hasYalcFlag ;
54+ const shouldDeleteBuild = allFlag || buildFlag ;
55+ const shouldDeleteCore = allFlag || coreFlag ;
56+ const shouldDeleteExt = allFlag || extFlag ;
57+ const shouldDeleteNpm = allFlag || npmFlag ;
58+ const shouldDeleteTest = allFlag || testFlag ;
59+ const shouldDeleteYalc = allFlag || yalcFlag ;
5360
5461// Define directory lists
5562
56- const CORE_DIRS = [ path . join ( __dirname , '..' , '..' , 'paranext-core' , 'dev-appdata' ) ] ;
63+ const EXT_DIR_PATH = path . join ( __dirname , '..' ) ;
64+ const CORE_DIR_PATH = path . join ( EXT_DIR_PATH , '..' , 'paranext-core' ) ;
65+ const SKIP_DIRS = new Set ( [ '.yalc' , 'dev-appdata' , 'dev-packages' , 'lib' ] ) ;
5766
67+ const BUILD_PATHS = [ ] ;
68+ if ( shouldDeleteBuild ) {
69+ // Add core build output subdirectories to `BUILD_PATHS`
70+ BUILD_PATHS . push ( ...findDirsNamed ( CORE_DIR_PATH , 'dist' , SKIP_DIRS ) ) ;
71+ BUILD_PATHS . push ( ...findDirsNamed ( CORE_DIR_PATH , 'temp-build' , SKIP_DIRS ) ) ;
72+ }
73+
74+ const CORE_PATHS = [ path . join ( CORE_DIR_PATH , 'dev-appdata' ) ] ;
5875if ( shouldDeleteCore ) {
59- // Determine Electron cache directory based on platform and add to `CORE_DIRS `
76+ // Determine Electron cache directory based on platform and add to `CORE_PATHS `
6077 let electronParent = '' ;
6178
6279 /* eslint-disable no-nested-ternary */
@@ -77,56 +94,53 @@ if (shouldDeleteCore) {
7794 /* eslint-enable no-nested-ternary */
7895
7996 if ( electronParent ) {
80- CORE_DIRS . push ( path . join ( electronParent , 'Electron Cache' ) ) ;
97+ CORE_PATHS . push ( path . join ( electronParent , 'Electron Cache' ) ) ;
8198 }
8299}
83100
84- const EXT_DIRS = [
85- path . join ( __dirname , '..' , 'dist' ) ,
86- path . join ( __dirname , '..' , 'src' , 'temp-build' ) ,
87- ] ;
101+ const EXT_PATHS = [ path . join ( EXT_DIR_PATH , 'dist' ) , path . join ( EXT_DIR_PATH , 'src' , 'temp-build' ) ] ;
88102
89103const NPM_PATHS = [
90- path . join ( __dirname , '..' , 'node_modules' ) ,
91- path . join ( __dirname , '..' , 'package-lock.json' ) ,
104+ path . join ( EXT_DIR_PATH , 'node_modules' ) ,
105+ path . join ( EXT_DIR_PATH , 'package-lock.json' ) ,
92106] ;
93-
94107if ( shouldDeleteNpm ) {
95- // Recursively find `node_modules` folders in `paranext-core/` and add them to `NPM_PATHS`
96- const corePath = path . join ( __dirname , '..' , '..' , 'paranext-core' ) ;
97- const SKIP_DIRS = new Set ( [ 'dev-appdata' , 'dev-packages' ] ) ;
98- const findNodeModules = ( dir ) => {
99- let entries ;
100- try {
101- entries = fs . readdirSync ( dir , { withFileTypes : true } ) ;
102- } catch {
103- return ;
104- }
105- entries
106- . filter ( ( entry ) => entry . isDirectory ( ) && ! SKIP_DIRS . has ( entry . name ) )
107- . forEach ( ( entry ) => {
108- const fullPath = path . join ( dir , entry . name ) ;
109- if ( entry . name === 'node_modules' ) {
110- NPM_PATHS . push ( fullPath ) ;
111- } else {
112- findNodeModules ( fullPath ) ;
113- }
114- } ) ;
115- } ;
116- findNodeModules ( corePath ) ;
108+ // Find core `node_modules` subdirectories and add them to `NPM_PATHS`
109+ NPM_PATHS . push ( ...findDirsNamed ( CORE_DIR_PATH , 'node_modules' , SKIP_DIRS ) ) ;
117110}
118111
119- const TEST_PATHS = [
120- path . join ( __dirname , '..' , 'coverage' ) ,
121- path . join ( __dirname , '..' , '.eslintcache' ) ,
122- ] ;
112+ const TEST_PATHS = [ path . join ( EXT_DIR_PATH , 'coverage' ) , path . join ( EXT_DIR_PATH , '.eslintcache' ) ] ;
123113
124114const YALC_PATHS = [
125- path . join ( __dirname , '..' , '..' , 'paranext-core' , '.yalc' ) ,
126- path . join ( __dirname , '..' , '..' , 'paranext-core' , 'dev-packages' ) ,
127- path . join ( __dirname , '..' , '..' , 'paranext-core' , 'yalc.lock' ) ,
115+ path . join ( CORE_DIR_PATH , '.yalc' ) ,
116+ path . join ( CORE_DIR_PATH , 'dev-packages' ) ,
117+ path . join ( CORE_DIR_PATH , 'yalc.lock' ) ,
128118] ;
129119
120+ /**
121+ * Recursively find all subdirectories named `targetDir` inside `corePath`, skipping `skipDirs`.
122+ *
123+ * @param {string } corePath - Root directory to search
124+ * @param {string } targetDir - Directory name to collect
125+ * @param {Set<string> } skipDirs - Directory names to skip entirely
126+ * @returns {string[] } Absolute paths of every matching directory found
127+ */
128+ function findDirsNamed ( corePath , targetDir , skipDirs ) {
129+ let entries ;
130+ try {
131+ entries = fs . readdirSync ( corePath , { withFileTypes : true } ) ;
132+ } catch {
133+ return [ ] ;
134+ }
135+ return entries
136+ . filter ( ( entry ) => entry . isDirectory ( ) && ! skipDirs . has ( entry . name ) )
137+ . flatMap ( ( entry ) => {
138+ const fullPath = path . join ( corePath , entry . name ) ;
139+ if ( entry . name === targetDir ) return [ fullPath ] ;
140+ return findDirsNamed ( fullPath , targetDir , skipDirs ) ;
141+ } ) ;
142+ }
143+
130144/**
131145 * Delete a path if it exists
132146 *
@@ -153,14 +167,19 @@ function deletePath(pathToDelete) {
153167// Delete paths based on command-line flags
154168
155169try {
170+ if ( shouldDeleteBuild ) {
171+ console . log ( 'Deleting core build directories...' ) ;
172+ BUILD_PATHS . forEach ( deletePath ) ;
173+ }
174+
156175 if ( shouldDeleteCore ) {
157176 console . log ( 'Deleting core cache directories...' ) ;
158- CORE_DIRS . forEach ( deletePath ) ;
177+ CORE_PATHS . forEach ( deletePath ) ;
159178 }
160179
161180 if ( shouldDeleteExt ) {
162181 console . log ( 'Deleting extension directories...' ) ;
163- EXT_DIRS . forEach ( deletePath ) ;
182+ EXT_PATHS . forEach ( deletePath ) ;
164183 }
165184
166185 if ( shouldDeleteNpm ) {
0 commit comments