方式1
通过装饰器模式
// CocosCreator构建
function publish(platform) {
return () => {
const env = tools.getEnv();
var configName; // 配置文件名字
var subDest;
switch (platform) {
case 'android':
configName = 'publish_android.json';
subDest = 'build/jsb-default';
break;
case 'ios':
configName = 'publish_ios.json';
subDest = 'build/jsb-default';
break;
case 'web':
configName = 'publish_web.json';
subDest = 'build';
break;
case 'win32':
configName = 'publish_win32.json';
subDest = 'build/jsb-default';
break;
}
const configPath = path.join(__dirname, configName); // 构建配置文件路径
const buildPath = path.join(env.SLOT249, 'build');
const dest = path.join(env.SLOT249, subDest);
const project = env.SLOT249;
const cmd = `"${env.COCOSCREATOR_EXE}" --path ${env.SLOT249} --build "configPath=${configPath};buildPath=${buildPath};dest=${dest};project=${project}"`;
return exec(cmd);
}
}
gulp.task('publish-android', gulp.series(publish('android')));
gulp.task('publish-ios', gulp.series(publish('ios')));
gulp.task('publish-web', gulp.series(publish('web')));
gulp.task('publish-win32', gulp.series(publish('win32')));
方式2
通过控制台传参数方式
const minimist = require('minimist');
var knownOptions = {
default: {
host: 'aliyun',
}
};
var options = minimist(process.argv.slice(2), knownOptions);
// 创建应用程序配置文件
// 控制台参数--host,用来标志配置文件是什么环境
function createAppCfg(cb) {
const host = options.host;
const env = tools.getEnv();
const cfg = tools.getAppCfg(host);
if (!cfg) {
cb(new Error(`在slotconfig.json文件中没有找到配置,${options.host}`));
return;
}
console.log(cfg);
const saveUrl = path.join(env.SLOT249, '/build/jsb-default/assets/main/slotconfig.json');
const content = JSON.stringify(cfg);
fs.writeFileSync(saveUrl, content);
cb();
}
gulp.task('t2', gulp.series(createAppCfg));
## 控制台执行
gulp t2 --host amazon