最近转型成了无情的流水线工程师,天天就是帮组里的同事编写构建流水线。
需求
目前有个需求就是希望可以在流水线构建的过程中,指定自定义路径的引擎。
困境
翻遍了整个Cocos文档和论坛,在Google上找了2-3个月都没找到2.X的项目,究竟要怎么样才能在命令行构建的时候指定自定义引擎路径。3.X官方倒是给命令行新增了参数,用于指定自定义引擎的位置。
3.x可以通过–engine参数来手动指定引擎路径 https://docs.cocos.com/creator/3.8/manual/zh/editor/publish/publish-in-command-line.html
解决
今天在翻看定制化引擎的时候,偶然发现了这个技巧,超级无敌简单。就是在项目的根目录下创建一个local文件夹,然后在里面设置一个settings.json,就可以轻松指定项目所使用的自定义引擎路径了。

我这里还写了个nodejs的脚本来辅助创建,分享给大家使用:
保存为customEnginePath.js
const fs = require('fs');
const path = require('path');
// Parse command line arguments
const args = process.argv.slice(2);
let customEnginePath = null;
// Parse --path argument
for (let i = 0; i < args.length; i++) {
if (args[i] === '--path' && i + 1 < args.length) {
customEnginePath = args[i + 1];
break;
}
}
// Get current working directory absolute path
const currentDir = process.cwd();
if (!customEnginePath) {
console.error('❌ Custom engine path required!');
}
// Build engine path - use custom path if provided, otherwise use default
const enginePath = path.resolve(customEnginePath);
// settings.json file path
const settingsPath = path.join(currentDir, 'local', 'settings.json');
try {
// Ensure local directory exists
const localDir = path.dirname(settingsPath);
if (!fs.existsSync(localDir)) {
fs.mkdirSync(localDir, { recursive: true });
console.log('📁 Created local directory');
}
// Create complete settings configuration object
const settings = {
"use-global-engine-setting": false,
"use-default-js-engine": false,
"js-engine-path": enginePath,
"use-default-cpp-engine": true,
"cpp-engine-path": ""
};
// Write configuration to file with beautiful formatting
const settingsContent = JSON.stringify(settings, null, 2);
fs.writeFileSync(settingsPath, settingsContent, 'utf8');
console.log('✅ Successfully generated/updated local/settings.json');
console.log(`📁 js-engine-path set to: ${enginePath}`);
console.log('🎯 Using custom engine path specified via --path argument');
// Show whether file was newly created
if (!fs.existsSync(settingsPath)) {
console.log('🆕 Created new settings.json file');
} else {
console.log('🔄 Updated existing settings.json file');
}
} catch (error) {
console.error('❌ Operation failed:', error.message);
console.log('💡 Please check file permissions or disk space');
}
使用方法:
在项目根目录下运行这个脚本
node customEnginePath.js --path=<Your Custom Engine Path>
使用CocosCreator构建的时候,就会自动使用你指定的引擎进行构建。
CocosCreator内看不到这个设置是正常的,构建的时候是会使用自定义引擎进行构建的。
如果运行完脚本,但是Creator内构建的时候会提示“The first argument muse be of type string or an instance of buffer xxxx”的,就是Creator的bug,要手动进自定义引擎的面板,勾选一下全局JS引擎,然后再取消勾选全局JS引擎,这样子Creator内构建才不会报错。