1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-11 09:11:50 +00:00
tobspr_shapez.io/gulp/standalone.js
Даниїл Григор'єв c836589d9b
Rewrite the Electron wrapper (#47)
* Migrate Electron wrapper to ESM

Use ESM syntax for main process, move some fs usages to fs.promises,
switch to import.meta.url/import.meta.dirname to handle file paths;
clean up redundant code.

* Add TypeScript support to Electron wrapper

The support is very basic, tsc is used to transpile code. Build scripts
are modified to not copy any Electron code other than preload.cjs and
use an extremely cursed setup to call the TypeScript compiler.

* [TS] Rename platform/storage

* Rewrite Electron wrapper

MVP, missing some features from the old wrapper and most planned
features. Some of the functionality hasn't been verified.
2025-03-21 16:30:13 +02:00

125 lines
4.8 KiB
JavaScript

import packager from "electron-packager";
import fs from "fs/promises";
import gulp from "gulp";
import path from "path/posix";
import electronPackageJson from "../electron/package.json" with { type: "json" };
import { BUILD_VARIANTS } from "./build_variants.js";
import { getVersion } from "./buildutils.js";
import { buildProject } from "./typescript.js";
import gulpClean from "gulp-clean";
const platforms = /** @type {const} */ (["win32", "linux", "darwin"]);
const architectures = /** @type {const} */ (["x64", "arm64"]);
export default Object.fromEntries(
Object.entries(BUILD_VARIANTS)
.filter(([variant, variantData]) => variantData.standalone)
.map(([variant, variantData]) => {
const tempDestDir = path.join("..", "build_output", variant);
const electronBaseDir = path.join("..", "electron");
const tempDestBuildDir = path.join(tempDestDir, "built");
function cleanup() {
return gulp
.src(tempDestDir, { read: false, allowEmpty: true })
.pipe(gulpClean({ force: true }));
}
function copyPrefab() {
const requiredFiles = [
path.join(electronBaseDir, "preload.cjs"),
path.join(electronBaseDir, "node_modules", "**", "*.*"),
path.join(electronBaseDir, "node_modules", "**", ".*"),
path.join(electronBaseDir, "favicon*"),
];
return gulp.src(requiredFiles, { base: electronBaseDir }).pipe(gulp.dest(tempDestBuildDir));
}
async function transpileTypeScript() {
const tsconfigPath = path.join(electronBaseDir, "tsconfig.json");
const outDir = path.join(tempDestBuildDir, "dist");
buildProject(tsconfigPath, undefined, outDir);
return Promise.resolve();
}
async function writePackageJson() {
const pkgJson = structuredClone(electronPackageJson);
pkgJson.version = getVersion();
delete pkgJson.scripts;
const packageJsonString = JSON.stringify(pkgJson);
await fs.writeFile(path.join(tempDestBuildDir, "package.json"), packageJsonString);
}
function copyGamefiles() {
return gulp.src("../build/**/*.*", { base: "../build" }).pipe(gulp.dest(tempDestBuildDir));
}
const prepare = {
cleanup,
copyPrefab,
transpileTypeScript,
writePackageJson,
copyGamefiles,
all: gulp.series(cleanup, copyPrefab, transpileTypeScript, writePackageJson, copyGamefiles),
};
/**
*
* @param {typeof platforms[number] | (typeof platforms[number])[]} platform
* @param {typeof architectures[number] | (typeof architectures[number])[]} arch
*/
async function packageStandalone(platform, arch) {
const appPaths = await packager({
dir: tempDestBuildDir,
appCopyright: "tobspr Games",
appVersion: getVersion(),
buildVersion: "1.0.0",
arch,
platform,
asar: true,
executableName: "shapezio",
icon: path.join(electronBaseDir, "favicon"),
name: "shapez",
out: tempDestDir,
overwrite: true,
appBundleId: "tobspr.shapezio." + variant,
appCategoryType: "public.app-category.games",
});
console.log("Packages created:", appPaths);
await Promise.all(
appPaths.map(async appPath => {
await fs.writeFile(
path.join(appPath, "LICENSE"),
await fs.readFile(path.join("..", "LICENSE"))
);
})
);
}
const pack = {
...Object.fromEntries(
platforms.flatMap(platform =>
architectures.map(arch => [
`${platform}-${arch}`,
() => packageStandalone(platform, arch),
])
)
),
// TODO: Review this hack forced by readonly types
all: () => packageStandalone([...platforms], [...architectures]),
};
return [
variant,
{
prepare,
package: pack,
},
];
})
);