From 2a9e69bcca701130cd580dd46a9226b608d25c3e Mon Sep 17 00:00:00 2001 From: ch1ny Date: Thu, 26 Oct 2023 21:22:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=BB=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B=E7=BC=96=E8=AF=91=E5=87=BD=E6=95=B0&=E4=B8=BB?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/creta/src/cli/scripts/dev.ts | 14 +++++----- src/creta/src/cli/utils/build.ts | 19 ++----------- src/creta/src/cli/utils/getChildrenScripts.ts | 27 +++++++++++++++++++ src/creta/src/cli/utils/getMainScripts.ts | 26 ++++++++++++++++++ src/creta/src/cli/utils/tsc.ts | 1 - 5 files changed, 62 insertions(+), 25 deletions(-) create mode 100644 src/creta/src/cli/utils/getChildrenScripts.ts create mode 100644 src/creta/src/cli/utils/getMainScripts.ts diff --git a/src/creta/src/cli/scripts/dev.ts b/src/creta/src/cli/scripts/dev.ts index 8cf42ad..fa51d35 100644 --- a/src/creta/src/cli/scripts/dev.ts +++ b/src/creta/src/cli/scripts/dev.ts @@ -1,8 +1,8 @@ -import fs from 'fs'; import path from 'path'; import { createServer } from 'vite'; import constants from '../constants'; import { buildMain, buildPreload, getCretaConfigs, runElectron, tscWatch } from '../utils'; +import getChildrenScripts from '../utils/getChildrenScripts'; const { defaultViteConfig, scriptsCwd } = constants; @@ -59,11 +59,10 @@ const main = async () => { await Promise.all([ // 2.2.1 tsc watch 主进程代码 new Promise(async (resolve) => { + const filesToBuild = await getChildrenScripts(path.resolve(scriptsCwd, 'src', 'main')); const mainConfigPath = path.resolve(scriptsCwd, 'src', 'main', 'tsconfig.json'); tscWatchPrograms.main = tscWatch( - (await fs.promises.readdir(path.resolve(scriptsCwd, 'src', 'main'))) - .filter((file) => file.endsWith('.js') || file.endsWith('.ts')) - .map((file) => path.resolve(scriptsCwd, 'src', 'main', file)), + filesToBuild, mainConfigPath, { onAfterFirstCompile(program, defaultCallback?) { @@ -127,10 +126,11 @@ const main = async () => { // 2.2.2 tsc watch 预加载脚本代码 new Promise(async (resolve) => { const preloadConfigPath = path.resolve(scriptsCwd, 'src', 'preload', 'tsconfig.json'); + + const filesToBuild = await getChildrenScripts(path.resolve(scriptsCwd, 'src', 'preload')); + tscWatchPrograms.preload = tscWatch( - (await fs.promises.readdir(path.resolve(scriptsCwd, 'src', 'preload'))) - .filter((file) => file.endsWith('.js') || file.endsWith('.ts')) - .map((file) => path.resolve(scriptsCwd, 'src', 'preload', file)), + filesToBuild, preloadConfigPath, { onAfterFirstCompile(program, defaultCallback?) { diff --git a/src/creta/src/cli/utils/build.ts b/src/creta/src/cli/utils/build.ts index a8b7c07..e1e79b3 100644 --- a/src/creta/src/cli/utils/build.ts +++ b/src/creta/src/cli/utils/build.ts @@ -4,6 +4,7 @@ import { build } from 'vite'; import constants from '../constants'; import { getCretaConfigs } from './getCretaConfigs'; import { tscBuild } from './tsc'; +import getChildrenScripts from './getChildrenScripts'; const { defaultViteConfig, scriptsCwd } = constants; @@ -29,23 +30,7 @@ export const buildPreload = async () => ); export const buildMain = async () => { - const mainRootDir = path.resolve(scriptsCwd, 'src', 'main'); - const nextDirList = [mainRootDir]; - const filesToBuild: string[] = []; - - let nextDir: string | undefined; - while ((nextDir = nextDirList.shift())) { - const files = await fs.promises.readdir(path.resolve(nextDir)); - files.map(async (fileName) => { - const filePath = path.resolve(nextDir!, fileName); - const fsStatus = await fs.promises.stat(filePath); - if (fsStatus.isDirectory()) { - nextDirList.push(filePath); - } else if (filePath.endsWith('.js') || filePath.endsWith('.ts')) { - filesToBuild.push(filePath); - } - }); - } + const filesToBuild = await getChildrenScripts(path.resolve(scriptsCwd, 'src', 'main')); await tscBuild(filesToBuild, path.resolve(scriptsCwd, 'src', 'main', 'tsconfig.json')); }; diff --git a/src/creta/src/cli/utils/getChildrenScripts.ts b/src/creta/src/cli/utils/getChildrenScripts.ts new file mode 100644 index 0000000..4496ef6 --- /dev/null +++ b/src/creta/src/cli/utils/getChildrenScripts.ts @@ -0,0 +1,27 @@ +import fs from 'fs'; +import path from 'path'; + +export default async function (dir: string) { + const mainRootDir = path.resolve(dir); + + const nextDirList = [mainRootDir]; + const filesToBuild: string[] = []; + + let nextDir: string | undefined; + while ((nextDir = nextDirList.shift())) { + const files = await fs.promises.readdir(path.resolve(nextDir)); + await Promise.all( + files.map(async (fileName) => { + const filePath = path.resolve(nextDir!, fileName); + const fsStatus = await fs.promises.stat(filePath); + if (fsStatus.isDirectory()) { + nextDirList.push(filePath); + } else if (filePath.endsWith('.js') || filePath.endsWith('.ts')) { + filesToBuild.push(filePath); + } + }) + ); + } + + return filesToBuild; +} diff --git a/src/creta/src/cli/utils/getMainScripts.ts b/src/creta/src/cli/utils/getMainScripts.ts new file mode 100644 index 0000000..6d351d2 --- /dev/null +++ b/src/creta/src/cli/utils/getMainScripts.ts @@ -0,0 +1,26 @@ +import fs from 'fs'; +import path from 'path'; +import constants from '../constants'; + +export default async function () { + const mainRootDir = path.resolve(constants.scriptsCwd, 'src', 'main'); + + const nextDirList = [mainRootDir]; + const filesToBuild: string[] = []; + + let nextDir: string | undefined; + while ((nextDir = nextDirList.shift())) { + const files = await fs.promises.readdir(path.resolve(nextDir)); + files.map(async (fileName) => { + const filePath = path.resolve(nextDir!, fileName); + const fsStatus = await fs.promises.stat(filePath); + if (fsStatus.isDirectory()) { + nextDirList.push(filePath); + } else if (filePath.endsWith('.js') || filePath.endsWith('.ts')) { + filesToBuild.push(filePath); + } + }); + } + + return filesToBuild; +} diff --git a/src/creta/src/cli/utils/tsc.ts b/src/creta/src/cli/utils/tsc.ts index c861609..e23f731 100644 --- a/src/creta/src/cli/utils/tsc.ts +++ b/src/creta/src/cli/utils/tsc.ts @@ -366,7 +366,6 @@ export const tscWatch = async ( createProgram, reportDiagnostic, reportWatchStatusChanged, - undefined, undefined );