DNDGamePlayer: Electron редактор сцен, презентация, упаковка electron-builder
Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { build } from 'esbuild';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const root = path.resolve(__dirname, '..');
|
||||
|
||||
function runViteBuild() {
|
||||
execFileSync('npx vite build', {
|
||||
cwd: root,
|
||||
stdio: 'inherit',
|
||||
shell: true,
|
||||
});
|
||||
}
|
||||
|
||||
async function buildNodeTargets() {
|
||||
await build({
|
||||
entryPoints: [path.join(root, 'app/main/index.ts')],
|
||||
outfile: path.join(root, 'dist/main/index.cjs'),
|
||||
platform: 'node',
|
||||
target: 'node22',
|
||||
format: 'cjs',
|
||||
bundle: true,
|
||||
sourcemap: true,
|
||||
external: ['electron'],
|
||||
});
|
||||
|
||||
await build({
|
||||
entryPoints: [path.join(root, 'app/preload/index.ts')],
|
||||
outfile: path.join(root, 'dist/preload/index.cjs'),
|
||||
platform: 'node',
|
||||
target: 'node22',
|
||||
format: 'cjs',
|
||||
bundle: true,
|
||||
sourcemap: true,
|
||||
external: ['electron'],
|
||||
});
|
||||
}
|
||||
|
||||
await buildNodeTargets();
|
||||
execFileSync('node', [path.join(root, 'scripts', 'gen-window-icon.mjs')], {
|
||||
cwd: root,
|
||||
stdio: 'inherit',
|
||||
});
|
||||
runViteBuild();
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
import { context } from 'esbuild';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { spawn } from 'node:child_process';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const root = path.resolve(__dirname, '..');
|
||||
|
||||
function spawnShell(command, opts = {}) {
|
||||
const child = spawn(command, {
|
||||
cwd: root,
|
||||
stdio: 'inherit',
|
||||
shell: true,
|
||||
...opts,
|
||||
});
|
||||
child.on('exit', (code) => {
|
||||
if (code !== 0 && code !== null) {
|
||||
process.exitCode = code;
|
||||
}
|
||||
});
|
||||
return child;
|
||||
}
|
||||
|
||||
/** Убивает дерево процессов (на Windows `child.kill()` часто не гасит Vite на 5173). */
|
||||
function killTree(child) {
|
||||
if (!child || child.killed) return;
|
||||
if (typeof child.exitCode === 'number' && child.exitCode !== null) return;
|
||||
if (process.platform === 'win32' && child.pid) {
|
||||
spawn('taskkill', ['/PID', String(child.pid), '/T', '/F'], {
|
||||
stdio: 'ignore',
|
||||
windowsHide: true,
|
||||
detached: true,
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
child.kill('SIGTERM');
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function watchMainAndPreload() {
|
||||
const main = await context({
|
||||
entryPoints: [path.join(root, 'app/main/index.ts')],
|
||||
outfile: path.join(root, 'dist/main/index.cjs'),
|
||||
platform: 'node',
|
||||
target: 'node22',
|
||||
format: 'cjs',
|
||||
bundle: true,
|
||||
sourcemap: true,
|
||||
external: ['electron'],
|
||||
define: { 'process.env.NODE_ENV': JSON.stringify('development') },
|
||||
});
|
||||
await main.rebuild();
|
||||
await main.watch();
|
||||
|
||||
const preload = await context({
|
||||
entryPoints: [path.join(root, 'app/preload/index.ts')],
|
||||
outfile: path.join(root, 'dist/preload/index.cjs'),
|
||||
platform: 'node',
|
||||
target: 'node22',
|
||||
format: 'cjs',
|
||||
bundle: true,
|
||||
sourcemap: true,
|
||||
external: ['electron'],
|
||||
define: { 'process.env.NODE_ENV': JSON.stringify('development') },
|
||||
});
|
||||
await preload.rebuild();
|
||||
await preload.watch();
|
||||
|
||||
return async () => {
|
||||
await Promise.all([main.dispose(), preload.dispose()]);
|
||||
};
|
||||
}
|
||||
|
||||
const dispose = await watchMainAndPreload();
|
||||
const vite = spawnShell('npx vite dev --strictPort', {
|
||||
env: { ...process.env, NODE_ENV: 'development', VITE_DEV_SERVER_URL: 'http://localhost:5173/' },
|
||||
});
|
||||
const electron = spawnShell('npx electron .', {
|
||||
env: { ...process.env, NODE_ENV: 'development', VITE_DEV_SERVER_URL: 'http://localhost:5173/' },
|
||||
});
|
||||
|
||||
let shuttingDown = false;
|
||||
|
||||
const shutdown = async () => {
|
||||
if (shuttingDown) return;
|
||||
shuttingDown = true;
|
||||
killTree(vite);
|
||||
killTree(electron);
|
||||
await dispose();
|
||||
process.exit(0);
|
||||
};
|
||||
|
||||
process.on('SIGINT', () => void shutdown());
|
||||
process.on('SIGTERM', () => void shutdown());
|
||||
|
||||
electron.once('exit', () => {
|
||||
void shutdown();
|
||||
});
|
||||
|
||||
vite.once('exit', (code) => {
|
||||
if (code !== 0 && code !== null && !shuttingDown) {
|
||||
void shutdown();
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Растеризует app-logo.svg в app-window-icon.png для nativeImage (Windows).
|
||||
* Запуск: node scripts/gen-window-icon.mjs
|
||||
*/
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
import { Resvg } from '@resvg/resvg-js';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const root = path.join(__dirname, '..');
|
||||
const svgPath = path.join(root, 'app', 'renderer', 'public', 'app-logo.svg');
|
||||
const outPath = path.join(root, 'app', 'renderer', 'public', 'app-window-icon.png');
|
||||
|
||||
const svg = fs.readFileSync(svgPath);
|
||||
const resvg = new Resvg(svg, {
|
||||
fitTo: { mode: 'width', value: 256 },
|
||||
});
|
||||
const out = resvg.render();
|
||||
fs.writeFileSync(outPath, out.asPng());
|
||||
console.log('wrote', outPath, out.width, 'x', out.height);
|
||||
|
||||
const buildDir = path.join(root, 'build');
|
||||
fs.mkdirSync(buildDir, { recursive: true });
|
||||
const packIconPath = path.join(buildDir, 'icon.png');
|
||||
const resvg512 = new Resvg(svg, {
|
||||
fitTo: { mode: 'width', value: 512 },
|
||||
});
|
||||
const packOut = resvg512.render();
|
||||
fs.writeFileSync(packIconPath, packOut.asPng());
|
||||
console.log('wrote', packIconPath, packOut.width, 'x', packOut.height, '(electron-builder)');
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Печатает версию из package.json и подсказку по git-тегу для релиза.
|
||||
* Запуск: node scripts/print-release-info.mjs
|
||||
*/
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'));
|
||||
const v = pkg.version;
|
||||
console.log(`Версия (package.json): ${v}`);
|
||||
console.log(`Пример git-тега: v${v}`);
|
||||
Reference in New Issue
Block a user