Skip to content

Commit d0b08db

Browse files
committed
refactored for loop in test function to be more parallel
1 parent fc50a5c commit d0b08db

1 file changed

Lines changed: 27 additions & 20 deletions

File tree

index.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { readdir as readDir, writeFile, mkdir as mkDir, readFile, copyFile, stat
22
import { watch as watchDir } from "chokidar"
33
import { minify } from "terser"
44
import { resolve as resolvePath, basename, extname } from "path"
5-
import { transpileModule, ScriptTarget, CompilerOptions, createProgram, createCompilerHost } from "typescript"
5+
import { transpileModule, ScriptTarget } from "typescript"
66
import { format } from "prettier"
77

88
interface Info {
@@ -316,13 +316,22 @@ export async function test(srcPath: string) {
316316
}[] = []
317317

318318
for (const dirent of await readDir(srcPath, { withFileTypes: true }))
319-
if (dirent.isDirectory()) {
320-
for (const file of await readDir(resolvePath(srcPath, dirent.name), { withFileTypes: true }))
321-
if (file.isFile() && supportedExtensions.includes(extname(file.name)))
322-
promises.push(processScript(await readFile(resolvePath(srcPath, dirent.name, file.name), { encoding: "utf-8" }))
323-
.catch(error => errors.push({ error, file: `${dirent.name}/${file.name}` })))
324-
} else if (dirent.isFile() && supportedExtensions.includes(extname(dirent.name)))
325-
promises.push(processScript(await readFile(resolvePath(srcPath, dirent.name), { encoding: "utf-8" })).catch(error => errors.push({ error, file: dirent.name })))
319+
if (dirent.isDirectory())
320+
promises.push(readDir(resolvePath(srcPath, dirent.name), { withFileTypes: true }).then(files => {
321+
const promises: Promise<any>[] = []
322+
323+
for (const file of files)
324+
if (file.isFile() && supportedExtensions.includes(extname(file.name)))
325+
promises.push(readFile(resolvePath(srcPath, dirent.name, file.name), { encoding: "utf-8" }).then(code =>
326+
processScript(code).catch(error => errors.push({ error, file: `${dirent.name}/${file.name}` }))
327+
))
328+
329+
return Promise.all(promises)
330+
}))
331+
else if (dirent.isFile() && supportedExtensions.includes(extname(dirent.name)))
332+
promises.push(readFile(resolvePath(srcPath, dirent.name), { encoding: "utf-8" }).then(code =>
333+
processScript(code).catch(error => errors.push({ error, file: dirent.name }))
334+
))
326335

327336
await Promise.all(promises)
328337

@@ -443,18 +452,16 @@ export async function processScript(script: string) {
443452
}).outputText
444453

445454
// minification
446-
script = (await minify(script, {
447-
compress: {
448-
keep_fargs: false,
449-
negate_iife: false,
450-
// booleans_as_integers: true,
451-
unsafe_undefined: true,
452-
unsafe_comps: true,
453-
unsafe_proto: true,
454-
passes: 2,
455-
ecma: 2017
456-
}
457-
})).code || ""
455+
script = (await minify(script, { compress: {
456+
keep_fargs: false,
457+
negate_iife: false,
458+
// booleans_as_integers: true,
459+
unsafe_undefined: true,
460+
unsafe_comps: true,
461+
unsafe_proto: true,
462+
passes: 2,
463+
ecma: 2017
464+
} })).code || ""
458465

459466
// extra formatting to get the non whitespace character count lower
460467
script = format(script, {

0 commit comments

Comments
 (0)