Skip to content

Commit 2813655

Browse files
committed
refactor: extract common logic for directory traversal
1 parent 04bd773 commit 2813655

File tree

3 files changed

+63
-54
lines changed

3 files changed

+63
-54
lines changed

directoryTraverse.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
export function preOrderDirectoryTraverse(dir, dirCallback, fileCallback) {
5+
for (const filename of fs.readdirSync(dir)) {
6+
const fullpath = path.resolve(dir, filename);
7+
if (fs.lstatSync(fullpath).isDirectory()) {
8+
dirCallback(fullpath)
9+
// in case the dirCallback removes the directory entirely
10+
if (fs.existsSync(fullpath)) {
11+
preOrderDirectoryTraverse(fullpath, dirCallback, fileCallback)
12+
}
13+
continue
14+
}
15+
fileCallback(fullpath);
16+
}
17+
}
18+
19+
export function postOrderDirectoryTraverse(dir, dirCallback, fileCallback) {
20+
for (const filename of fs.readdirSync(dir)) {
21+
const fullpath = path.resolve(dir, filename)
22+
if (fs.lstatSync(fullpath).isDirectory()) {
23+
postOrderDirectoryTraverse(fullpath, dirCallback, fileCallback)
24+
dirCallback(fullpath)
25+
continue;
26+
}
27+
fileCallback(fullpath)
28+
}
29+
}

emptyDir.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

index.js

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
// @ts-check
33

44
import fs from 'fs'
5+
import path from 'path'
6+
57
import minimist from 'minimist'
68
import prompts from 'prompts'
79
import { red, green, bold } from 'kolorist'
810

9-
import emptyDir from './emptyDir.js'
1011
import renderTemplate from './renderTemplate.js'
11-
import path from 'path'
12+
import {
13+
postOrderDirectoryTraverse,
14+
preOrderDirectoryTraverse
15+
} from './directoryTraverse.js'
1216

1317
function isValidPackageName(projectName) {
1418
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(
@@ -29,6 +33,14 @@ function canSafelyOverwrite(dir) {
2933
return !fs.existsSync(dir) || fs.readdirSync(dir).length === 0
3034
}
3135

36+
function emptyDir (dir) {
37+
postOrderDirectoryTraverse(
38+
dir,
39+
(dir) => fs.rmdirSync(dir),
40+
(file) => fs.unlinkSync(file)
41+
)
42+
}
43+
3244
async function init() {
3345
const cwd = process.cwd()
3446
const argv = minimist(process.argv.slice(2))
@@ -153,25 +165,17 @@ async function init() {
153165

154166
// rename all `.js` files to `.ts`
155167
// 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(/\.js$/, '.ts'))
166-
}
167-
168-
if (filename === 'jsconfig.json') {
169-
fs.renameSync(fullpath, fullpath.replace(/jsconfig\.json$/, 'tsconfig.json'))
168+
preOrderDirectoryTraverse(
169+
root,
170+
() => {},
171+
(filepath) => {
172+
if (filepath.endsWith('.js')) {
173+
fs.renameSync(filepath, filepath.replace(/\.js$/, '.ts'))
174+
} else if (path.basename(filepath) === 'jsconfig.json') {
175+
fs.renameSync(filepath, filepath.replace(/jsconfig\.json$/, 'tsconfig.json'))
170176
}
171177
}
172-
}
173-
174-
traverseAndRename(root)
178+
)
175179
}
176180

177181
// Render code template.
@@ -188,24 +192,18 @@ async function init() {
188192
// All templates assumes the need of tests.
189193
// If the user doesn't need it:
190194
// 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)
202203
}
203-
204-
removeTestDirectories(subdir)
205-
}
206-
}
207-
208-
removeTestDirectories(root)
204+
},
205+
() => {}
206+
)
209207
}
210208

211209
// Instructions:

0 commit comments

Comments
 (0)