Skip to content

Commit

Permalink
Merge pull request #34 from ch1ny/fix/build-main
Browse files Browse the repository at this point in the history
fix: 修复主进程编译函数&主进程监听
  • Loading branch information
ch1ny authored Oct 26, 2023
2 parents 6729d6d + 2a9e69b commit 8d00740
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 deletions.
14 changes: 7 additions & 7 deletions src/creta/src/cli/scripts/dev.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -59,11 +59,10 @@ const main = async () => {
await Promise.all([
// 2.2.1 tsc watch 主进程代码
new Promise<void>(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?) {
Expand Down Expand Up @@ -127,10 +126,11 @@ const main = async () => {
// 2.2.2 tsc watch 预加载脚本代码
new Promise<void>(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?) {
Expand Down
19 changes: 2 additions & 17 deletions src/creta/src/cli/utils/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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'));
};
27 changes: 27 additions & 0 deletions src/creta/src/cli/utils/getChildrenScripts.ts
Original file line number Diff line number Diff line change
@@ -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;
}
26 changes: 26 additions & 0 deletions src/creta/src/cli/utils/getMainScripts.ts
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 0 additions & 1 deletion src/creta/src/cli/utils/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@ export const tscWatch = async (
createProgram,
reportDiagnostic,
reportWatchStatusChanged,
undefined,
undefined
);

Expand Down

0 comments on commit 8d00740

Please sign in to comment.