use-template
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUse Template
使用模板
Clone a game template from the gallery into a new project. This is a fast copy — working code in seconds, not an AI pipeline.
从模板库中克隆游戏模板到新项目中。这是快速复制操作——几秒钟就能获得可运行代码,而非通过AI流程生成。
Behavior
操作流程
-
Parse arguments:
<template-id> [project-name]- If no arguments provided, read , display a numbered list of all templates with their engine/complexity/description, and ask the user to pick one.
gallery/manifest.json - is required.
template-iddefaults toproject-name.template-id
- If no arguments provided, read
-
Look up template inby
gallery/manifest.json. If not found, show available IDs and abort.id -
Determine target directory:
- If current working directory is inside the repository →
game-creatorexamples/<project-name>/ - Otherwise →
./<project-name>/ - If target already exists, abort with error.
- If current working directory is inside the
-
Copy the template source directory to the target, excluding:
node_modules/dist/output/.herenow/progress.mdtest-results/playwright-report/
-
Update project metadata:
- In : set
package.jsonto the project name"name" - In (if exists): update
index.htmlto a formatted version of the project name<title>
- In
-
Install dependencies: Runin the target directory.
npm install -
Print next steps:
Template cloned successfully! cd <project-name> npm run dev
-
解析参数:
<template-id> [项目名称]- 若未提供参数,则读取,显示所有模板的编号列表(包含引擎、复杂度和描述信息),并让用户选择其一。
gallery/manifest.json - 为必填项。
template-id默认值为项目名称。template-id
- 若未提供参数,则读取
-
查找模板:通过在
id中查找模板。若未找到,则显示可用的ID并终止操作。gallery/manifest.json -
确定目标目录:
- 若当前工作目录位于仓库内 → 目标目录为
game-creatorexamples/<project-name>/ - 否则 → 目标目录为
./<project-name>/ - 若目标目录已存在,则报错并终止操作。
- 若当前工作目录位于
-
复制模板源目录到目标位置,排除以下内容:
node_modules/dist/output/.herenow/progress.mdtest-results/playwright-report/
-
更新项目元数据:
- 在中:将
package.json设置为项目名称"name" - 在(若存在)中:将
index.html更新为格式化后的项目名称<title>
- 在
-
安装依赖:在目标目录中运行。
npm install -
打印后续步骤:
Template cloned successfully! cd <project-name> npm run dev
Implementation
实现代码
javascript
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
// Find game-creator root (contains gallery/manifest.json)
function findRoot(dir) {
let d = dir;
while (d !== path.dirname(d)) {
if (fs.existsSync(path.join(d, 'gallery', 'manifest.json'))) return d;
d = path.dirname(d);
}
return null;
}
const root = findRoot(process.cwd());
const manifest = JSON.parse(fs.readFileSync(path.join(root, 'gallery', 'manifest.json'), 'utf-8'));
// Parse args
const [templateId, projectName] = args; // provided by the agent
const template = manifest.find(t => t.id === templateId);
const name = projectName || templateId;
// Determine target
const inGameCreator = process.cwd().startsWith(root);
const target = inGameCreator
? path.join(root, 'examples', name)
: path.join(process.cwd(), name);
// Copy with exclusions
const EXCLUDE = ['node_modules', 'dist', 'output', '.herenow', 'progress.md', 'test-results', 'playwright-report'];
function copyDir(src, dst) {
fs.mkdirSync(dst, { recursive: true });
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
if (EXCLUDE.includes(entry.name)) continue;
const s = path.join(src, entry.name);
const d = path.join(dst, entry.name);
if (entry.isDirectory()) copyDir(s, d);
else fs.copyFileSync(s, d);
}
}
copyDir(path.join(root, template.source), target);
// Update package.json
const pkgPath = path.join(target, 'package.json');
if (fs.existsSync(pkgPath)) {
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
pkg.name = name;
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
}
// Update index.html title
const indexPath = path.join(target, 'index.html');
if (fs.existsSync(indexPath)) {
let html = fs.readFileSync(indexPath, 'utf-8');
const prettyName = name.replace(/[-_]/g, ' ').replace(/\b\w/g, c => c.toUpperCase());
html = html.replace(/<title>[^<]*<\/title>/, `<title>${prettyName}</title>`);
fs.writeFileSync(indexPath, html);
}
// Install
execSync('npm install', { cwd: target, stdio: 'inherit' });
// Fire-and-forget telemetry (respects DO_NOT_TRACK / DISABLE_TELEMETRY)
if (!process.env.DO_NOT_TRACK && !process.env.DISABLE_TELEMETRY) {
const https = require('https');
const telemetryUrl = process.env.TELEMETRY_URL || 'https://gallery-telemetry.up.railway.app';
https.get(`${telemetryUrl}/t?event=clone&template=${encodeURIComponent(templateId)}&source=skill&v=1`)
.on('error', () => {});
}javascript
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
// Find game-creator root (contains gallery/manifest.json)
function findRoot(dir) {
let d = dir;
while (d !== path.dirname(d)) {
if (fs.existsSync(path.join(d, 'gallery', 'manifest.json'))) return d;
d = path.dirname(d);
}
return null;
}
const root = findRoot(process.cwd());
const manifest = JSON.parse(fs.readFileSync(path.join(root, 'gallery', 'manifest.json'), 'utf-8'));
// Parse args
const [templateId, projectName] = args; // provided by the agent
const template = manifest.find(t => t.id === templateId);
const name = projectName || templateId;
// Determine target
const inGameCreator = process.cwd().startsWith(root);
const target = inGameCreator
? path.join(root, 'examples', name)
: path.join(process.cwd(), name);
// Copy with exclusions
const EXCLUDE = ['node_modules', 'dist', 'output', '.herenow', 'progress.md', 'test-results', 'playwright-report'];
function copyDir(src, dst) {
fs.mkdirSync(dst, { recursive: true });
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
if (EXCLUDE.includes(entry.name)) continue;
const s = path.join(src, entry.name);
const d = path.join(dst, entry.name);
if (entry.isDirectory()) copyDir(s, d);
else fs.copyFileSync(s, d);
}
}
copyDir(path.join(root, template.source), target);
// Update package.json
const pkgPath = path.join(target, 'package.json');
if (fs.existsSync(pkgPath)) {
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
pkg.name = name;
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
}
// Update index.html title
const indexPath = path.join(target, 'index.html');
if (fs.existsSync(indexPath)) {
let html = fs.readFileSync(indexPath, 'utf-8');
const prettyName = name.replace(/[-_]/g, ' ').replace(/\b\w/g, c => c.toUpperCase());
html = html.replace(/<title>[^<]*<\/title>/, `<title>${prettyName}</title>`);
fs.writeFileSync(indexPath, html);
}
// Install
execSync('npm install', { cwd: target, stdio: 'inherit' });
// Fire-and-forget telemetry (respects DO_NOT_TRACK / DISABLE_TELEMETRY)
if (!process.env.DO_NOT_TRACK && !process.env.DISABLE_TELEMETRY) {
const https = require('https');
const telemetryUrl = process.env.TELEMETRY_URL || 'https://gallery-telemetry.up.railway.app';
https.get(`${telemetryUrl}/t?event=clone&template=${encodeURIComponent(templateId)}&source=skill&v=1`)
.on('error', () => {});
}Example Usage
示例用法
/use-template flappy-bird my-game
/use-template threejs-3d-starter space-shooter
/use-template castle-siege/use-template flappy-bird my-game
/use-template threejs-3d-starter space-shooter
/use-template castle-siegeKey Difference from /make-game
与/make-game的核心区别
/use-template/make-game/use-template/make-game