修复 .cocos-project.json 不存在时可能崩溃的问题

This commit is contained in:
caizhitao
2020-07-11 19:43:07 +08:00
parent 6ab80f4729
commit b2bce9a108
3 changed files with 18 additions and 5 deletions

View File

@@ -51,7 +51,7 @@
### Cocos Creator 2.4.+
不支持(后续可能会有计划支持)
目前不支持(后续可能会有计划支持)
### Cocos Creator 2.3.3

View File

@@ -14,8 +14,18 @@ export class CocosProjectConfig {
projectName: string | undefined;
fromJson(json: any): CocosProjectConfig {
this.engineVersion = json["engine_version"];
this.projectName = json["projectName"];
this.engineVersion = this._getJson(json, "engineVersion");
this.projectName = this._getJson(json, "projectName");
return this;
}
private _getJson(json: any, key: string) {
if (json) {
let value = json[key];
if (value != null && value != undefined) {
return value;
}
}
return undefined;
}
}

View File

@@ -60,7 +60,7 @@ export class TaskConfig {
/**
* Cocos 项目配置
*/
cocosProject: CocosProjectConfig;
cocosProject: CocosProjectConfig | undefined;
constructor(buildOutputDirPath: string) {
// console.log("当前执行文件所在目录路径", __dirname);
@@ -76,7 +76,10 @@ export class TaskConfig {
this.buildOutputMainJsFilePath = path.join(this.buildOutputDirPath, "main.js");
this.buildOutputLoaderPluginJsFilePath = path.join(this.buildOutputDirPath, "src/assets/loaderplugin.js");
this.inputLoaderPluginJsFilePath = path.join(path.dirname(process.argv[1]), "loaderplugin.js");
this.cocosProject = new CocosProjectConfig().fromJson(JSON.parse(fs.readFileSync(this.buildOutputCocosProjectJsonFilePath).toString()));
if (fs.existsSync(this.buildOutputCocosProjectJsonFilePath)) {
this.cocosProject = new CocosProjectConfig().fromJson(JSON.parse(fs.readFileSync(this.buildOutputCocosProjectJsonFilePath).toString()));
}
console.log("初始化项目配置");
console.log(this);