2
2
// @ts -check
3
3
4
4
import fs from 'fs'
5
+ import path from 'path'
6
+
5
7
import minimist from 'minimist'
6
8
import prompts from 'prompts'
7
9
import { red , green , bold } from 'kolorist'
8
10
9
- import emptyDir from './emptyDir.js'
10
11
import renderTemplate from './renderTemplate.js'
11
- import path from 'path'
12
+ import {
13
+ postOrderDirectoryTraverse ,
14
+ preOrderDirectoryTraverse
15
+ } from './directoryTraverse.js'
12
16
13
17
function isValidPackageName ( projectName ) {
14
18
return / ^ (?: @ [ a - z 0 - 9 - * ~ ] [ a -z 0 -9 -* ._ ~ ] * \/ ) ? [ a - z 0 - 9 - ~ ] [ a - z 0 - 9 - ._ ~ ] * $ / . test (
@@ -29,6 +33,14 @@ function canSafelyOverwrite(dir) {
29
33
return ! fs . existsSync ( dir ) || fs . readdirSync ( dir ) . length === 0
30
34
}
31
35
36
+ function emptyDir ( dir ) {
37
+ postOrderDirectoryTraverse (
38
+ dir ,
39
+ ( dir ) => fs . rmdirSync ( dir ) ,
40
+ ( file ) => fs . unlinkSync ( file )
41
+ )
42
+ }
43
+
32
44
async function init ( ) {
33
45
const cwd = process . cwd ( )
34
46
const argv = minimist ( process . argv . slice ( 2 ) )
@@ -153,25 +165,17 @@ async function init() {
153
165
154
166
// rename all `.js` files to `.ts`
155
167
// rename jsconfig.json to tsconfig.json
156
- function traverseAndRename ( dir ) {
157
- for ( const filename of fs . readdirSync ( dir ) ) {
158
- const fullpath = path . resolve ( dir , filename )
159
- if ( fs . lstatSync ( fullpath ) . isDirectory ( ) ) {
160
- traverseAndRename ( fullpath )
161
- continue
162
- }
163
-
164
- if ( filename . endsWith ( '.js' ) ) {
165
- fs . renameSync ( fullpath , fullpath . replace ( / \. j s $ / , '.ts' ) )
166
- }
167
-
168
- if ( filename === 'jsconfig.json' ) {
169
- fs . renameSync ( fullpath , fullpath . replace ( / j s c o n f i g \. j s o n $ / , 'tsconfig.json' ) )
168
+ preOrderDirectoryTraverse (
169
+ root ,
170
+ ( ) => { } ,
171
+ ( filepath ) => {
172
+ if ( filepath . endsWith ( '.js' ) ) {
173
+ fs . renameSync ( filepath , filepath . replace ( / \. j s $ / , '.ts' ) )
174
+ } else if ( path . basename ( filepath ) === 'jsconfig.json' ) {
175
+ fs . renameSync ( filepath , filepath . replace ( / j s c o n f i g \. j s o n $ / , 'tsconfig.json' ) )
170
176
}
171
177
}
172
- }
173
-
174
- traverseAndRename ( root )
178
+ )
175
179
}
176
180
177
181
// Render code template.
@@ -188,24 +192,18 @@ async function init() {
188
192
// All templates assumes the need of tests.
189
193
// If the user doesn't need it:
190
194
// rm -rf cypress **/__tests__/
191
- function removeTestDirectories ( dir ) {
192
- for ( const filename of fs . readdirSync ( dir ) ) {
193
- const subdir = path . resolve ( dir , filename )
194
- const stats = fs . lstatSync ( subdir )
195
-
196
- if ( ! stats . isDirectory ( ) ) { continue }
197
-
198
- if ( filename === 'cypress' || filename === '__tests__' ) {
199
- emptyDir ( subdir )
200
- fs . rmdirSync ( subdir )
201
- continue
195
+ preOrderDirectoryTraverse (
196
+ root ,
197
+ ( dirpath ) => {
198
+ const dirname = path . basename ( dirpath )
199
+
200
+ if ( dirname === 'cypress' || dirname === '__tests__' ) {
201
+ emptyDir ( dirpath )
202
+ fs . rmdirSync ( dirpath )
202
203
}
203
-
204
- removeTestDirectories ( subdir )
205
- }
206
- }
207
-
208
- removeTestDirectories ( root )
204
+ } ,
205
+ ( ) => { }
206
+ )
209
207
}
210
208
211
209
// Instructions:
0 commit comments