fix(tools): CLI 守卫防 argv[1] 空崩溃 + check-cocos 宿主版本缺失早退
- C1: 4 个 CLI 脚本守卫加 process.argv[1] 空值短路,避免动态 import 时 pathToFileURL(undefined) 崩溃 - I1: check-cocos-version CLI 在宿主 creator.version 为 null 时报错 exit 1,避免 CI 虚假 OK - M1: scaffold 测试收紧 UUID 正则 - 新增 cli-entry 测试覆盖动态 import 场景 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -20,7 +20,7 @@ export function bumpVersion(projectDirs, version) {
|
||||
}
|
||||
|
||||
// CLI 入口:bump 宿主 + 所有子游戏到指定版本
|
||||
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
|
||||
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
||||
const version = process.argv[2];
|
||||
if (!version) {
|
||||
console.error('用法: npm run bump-cocos <version> 例: npm run bump-cocos 3.8.9');
|
||||
|
||||
@@ -16,8 +16,12 @@ export function checkVersions(hostProject, gamesDir) {
|
||||
}
|
||||
|
||||
// CLI 入口:有不一致则退出码 1
|
||||
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
|
||||
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
||||
const { expected, mismatches } = checkVersions(HOST_PROJECT, GAMES_DIR);
|
||||
if (expected == null) {
|
||||
console.error('错误: 宿主工程缺少 creator.version');
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`宿主 Cocos 版本: ${expected}`);
|
||||
if (mismatches.length === 0) {
|
||||
console.log('OK: 所有子游戏工程版本一致。');
|
||||
|
||||
@@ -28,7 +28,7 @@ export function newGame(name, opts = {}) {
|
||||
}
|
||||
|
||||
// CLI 入口
|
||||
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
|
||||
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
||||
const name = process.argv[2];
|
||||
if (!name) {
|
||||
console.error('用法: npm run new-game <name>');
|
||||
|
||||
@@ -19,7 +19,7 @@ export function setupLinks(gamesDir, frameworkSrc) {
|
||||
}
|
||||
|
||||
// CLI 入口
|
||||
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
|
||||
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
||||
const results = setupLinks(GAMES_DIR, FRAMEWORK_SRC);
|
||||
if (results.length === 0) {
|
||||
console.log('games/ 下暂无子游戏工程。');
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||
import { dirname, join } from 'node:path';
|
||||
|
||||
const scriptsDir = join(dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const CLI = ['setup-links.mjs', 'new-game.mjs', 'check-cocos-version.mjs', 'bump-cocos.mjs'];
|
||||
|
||||
for (const name of CLI) {
|
||||
test(`${name} 被动态 import 时不崩溃(argv[1] undefined)`, () => {
|
||||
const url = pathToFileURL(join(scriptsDir, name)).href;
|
||||
const r = spawnSync(process.execPath,
|
||||
['--input-type=module', '-e', `import(${JSON.stringify(url)}).then(()=>process.exit(0))`],
|
||||
{ encoding: 'utf8' });
|
||||
assert.equal(r.status, 0, `stderr: ${r.stderr}`);
|
||||
assert.ok(!/TypeError/.test(r.stderr), `不应出现 TypeError: ${r.stderr}`);
|
||||
});
|
||||
}
|
||||
@@ -42,7 +42,7 @@ test('setProjectIdentity 改 name 并生成新 uuid', () => {
|
||||
const pkg = setProjectIdentity(dest, 'doudizhu');
|
||||
assert.equal(pkg.name, 'doudizhu');
|
||||
assert.notEqual(pkg.uuid, 'seed-uuid');
|
||||
assert.match(pkg.uuid, /^[0-9a-f-]{36}$/);
|
||||
assert.match(pkg.uuid, /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/);
|
||||
const onDisk = JSON.parse(readFileSync(join(dest, 'package.json'), 'utf8'));
|
||||
assert.equal(onDisk.name, 'doudizhu');
|
||||
assert.equal(onDisk.creator.version, '3.8.8'); // 引擎版本保留
|
||||
|
||||
Reference in New Issue
Block a user