A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(editor): a new project can take an npm install straight away
yhhd007144ddfa9
diff --git a/desktop/electron/launcher.ts b/desktop/electron/launcher.tsindex 5d9cb060d..cd79bf0dc 100644--- a/desktop/electron/launcher.ts+++ b/desktop/electron/launcher.ts@@ -17,6 +17,7 @@ import {type RecentEntry,type TemplateEntry,} from '../src/project/format';+import { scaffoldProjectFiles } from './projectScaffold';type StoredRecent = Pick<RecentEntry, 'name' | 'root' | 'openedAt'>;@@ -158,5 +159,6 @@ export async function createFromTemplate(templateDir: string, location: string,const m = parseManifest(JSON.parse(await readFile(manifestPath, 'utf8')));m.name = name;await writeFile(manifestPath, JSON.stringify(m, null, 2) + '\n');+ await scaffoldProjectFiles(dest, name);return dest;}diff --git a/desktop/electron/projectScaffold.ts b/desktop/electron/projectScaffold.tsnew file mode 100644index 000000000..ff93c7a3b--- /dev/null+++ b/desktop/electron/projectScaffold.ts@@ -0,0 +1,80 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file The files every new project gets, whatever template it came from.+ *+ * Generated at creation rather than kept in each template, because there+ * are forty-odd templates — the blank starters plus every example — and a+ * copy in each is a copy that drifts. The blank one's `.gitignore` proved+ * it: it listed `dist/` while every export has long written+ * `dist-<platform>/`, so a packaged game landed in git.+ *+ * `package.json` is here because a project's scripts are bundled with+ * esbuild, which resolves npm dependencies out of the project's own+ * `node_modules` — so `npm install <a-library>` works and the library+ * ships with the game, on every target. Without a package.json that+ * starts with an `npm init` nobody would guess is a prerequisite.+ *+ * Pure Node (fs), no electron — so it can be tested without one.+ */+import { writeFile } from 'node:fs/promises';+import { existsSync } from 'node:fs';+import path from 'node:path';++/**+ * An npm-legal package name from a project's display name.+ *+ * "My Game" is a fine project name and an illegal package name — npm allows+ * lowercase, digits and `-._`, and refuses a leading `.` or `_`. A name with+ * nothing legal in it still yields something installable, rather than a+ * package.json npm refuses to read.+ */+export function npmPackageName(displayName: string): string {+ const slug = displayName+ .toLowerCase()+ .replace(/[^a-z0-9._-]+/g, '-')+ .replace(/^[._-]+/, '')+ .replace(/[._-]+$/, '')+ .slice(0, 214);+ return slug || 'estella-game';+}++export const PROJECT_GITIGNORE = `node_modules/++# Export output — the Package dialog writes dist-<platform>/ per target.+dist/+dist-*/++# Editor-managed staging: the SDK types mirror, the play-realm build and the+# workspace are regenerated every time the editor opens the project.+.esengine/++.vscode/+.idea/+.DS_Store+Thumbs.db+`;++/**+ * Write the scaffold into an already-created project directory.+ *+ * Existing files are left alone: a template that ships its own on purpose keeps+ * it, and running this over a project twice changes nothing.+ */+export async function scaffoldProjectFiles(dest: string, name: string): Promise<void> {+ const packageJson = path.join(dest, 'package.json');+ if (!existsSync(packageJson)) {+ // `private` so an accidental `npm publish` cannot reach the registry, and+ // `type: module` to match the ESM the tsconfig template already targets.+ // No dependencies and no scripts: the editor builds the project, and npm+ // is here only so installing a library works without `npm init` first.+ await writeFile(packageJson, `${JSON.stringify({+ name: npmPackageName(name),+ version: '0.0.0',+ private: true,+ type: 'module',+ }, null, 2)}\n`);+ }+ const gitignore = path.join(dest, '.gitignore');+ if (!existsSync(gitignore)) await writeFile(gitignore, PROJECT_GITIGNORE);+}diff --git a/desktop/templates/blank/.gitignore b/desktop/templates/blank/.gitignoredeleted file mode 100644index b4fb62542..000000000--- a/desktop/templates/blank/.gitignore+++ /dev/null@@ -1,10 +0,0 @@-node_modules/-dist/-.vscode/-.idea/-.DS_Store-Thumbs.db--# Editor-managed staging: the sdk types mirror and play-realm build are-# regenerated every time the editor opens the project.-.esengine/diff --git a/desktop/tests/templates.test.ts b/desktop/tests/templates.test.tsindex 2d65388c0..9cf74156c 100644--- a/desktop/tests/templates.test.ts+++ b/desktop/tests/templates.test.ts@@ -1,9 +1,12 @@// SPDX-License-Identifier: Apache-2.0// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team-import { describe, it, expect } from 'vitest';-import { existsSync, readFileSync, readdirSync } from 'node:fs';+import { describe, it, expect, beforeEach, afterEach } from 'vitest';+import { existsSync, readFileSync, readdirSync, writeFileSync } from 'node:fs';+import { mkdtemp, rm } from 'node:fs/promises';+import { tmpdir } from 'node:os';import path from 'node:path';import { parseManifest, resolveLayout, resolveScripts, isTransientProjectPath, PROJECT_MANIFEST_FILE } from '@/project/format';+import { scaffoldProjectFiles, npmPackageName } from '../electron/projectScaffold';// The bundled starter templates (shipped to resources/templates by// electron-builder) — every one must stay a complete, openable project:@@ -68,3 +71,46 @@ describe('isTransientProjectPath (template-copy filter)', () => {expect(isTransientProjectPath('.gitignore')).toBe(false);});});++// A project's scripts are bundled with esbuild, which resolves dependencies out+// of the project's own node_modules — so `npm install <a-library>` works and the+// library ships with the game. Without a package.json that starts with an+// `npm init` nobody would guess is a prerequisite.+describe('new-project scaffold', () => {+ let dest: string;+ beforeEach(async () => { dest = await mkdtemp(path.join(tmpdir(), 'es-scaffold-')); });+ afterEach(async () => { await rm(dest, { recursive: true, force: true }); });++ it('writes a package.json npm will accept', async () => {+ await scaffoldProjectFiles(dest, 'My Game');+ const pkg = JSON.parse(readFileSync(path.join(dest, 'package.json'), 'utf8'));+ expect(pkg).toEqual({ name: 'my-game', version: '0.0.0', private: true, type: 'module' });+ });++ it('makes an npm-legal name out of one npm would refuse', () => {+ expect(npmPackageName('My Game')).toBe('my-game');+ expect(npmPackageName('拾星 Starfall')).toBe('starfall'); // non-ASCII collapses+ expect(npmPackageName('_leading')).toBe('leading'); // npm bans a leading _+ expect(npmPackageName('.hidden.')).toBe('hidden');+ expect(npmPackageName('已经全是中文')).toBe('estella-game'); // nothing legal left+ expect(npmPackageName('a'.repeat(300)).length).toBe(214); // npm's cap+ });++ it('gitignores what the editor and the exporters actually write', async () => {+ await scaffoldProjectFiles(dest, 'x');+ const ignored = readFileSync(path.join(dest, '.gitignore'), 'utf8');+ expect(ignored).toContain('node_modules/');+ // The template's own copy listed `dist/` only, while every export writes+ // dist-<platform>/ — which is how a packaged game ended up in git.+ expect(ignored).toContain('dist-*/');+ expect(ignored).toContain('.esengine/');+ });++ it('leaves a template\'s own files alone, and is a no-op run twice', async () => {+ writeFileSync(path.join(dest, 'package.json'), '{"name":"chosen-by-the-template"}');+ await scaffoldProjectFiles(dest, 'My Game');+ await scaffoldProjectFiles(dest, 'My Game');+ expect(JSON.parse(readFileSync(path.join(dest, 'package.json'), 'utf8')).name)+ .toBe('chosen-by-the-template');+ });+});