@honor-minigame/cli
Version:
honor minigame pack cli
73 lines (62 loc) • 2.51 kB
JavaScript
import fs from 'fs'
import fse from 'fs-extra'
import path from "path"
import { ENGINE_TYPE } from '../common/constant.js'
const setEngineType = (rootPath) => {
// 读取文件,设置引擎类型
const manifestJsonPath = path.join(rootPath, 'manifest.json')
// 先看入口文件 game.js > main.js > index.js
const gameEntryPath = path.join(rootPath, 'game.js')
const mainEntryPath = path.join(rootPath, 'main.js')
const indexEntryPath = path.join(rootPath, 'index.js')
const cocosSettingPath = path.join(rootPath, 'src', 'settings.json')
const cocosRuntimePath = path.join(rootPath, 'src', 'cocos2d-runtime.js')
if(fs.existsSync(manifestJsonPath)) {
// 读取manifest.json,判断是否为unity
const json = fse.readJSONSync(manifestJsonPath)
if (json && json.isUnity) {
return ENGINE_TYPE.UNITY
}
}
// cocos3.x判断
if (fs.existsSync(cocosSettingPath)) {
const settingsJson = fse.readJSONSync(cocosSettingPath)
if (settingsJson && settingsJson.CocosEngine) {
return ENGINE_TYPE.COCOS
}
}
// cocos2.x判断
if (fs.existsSync(cocosRuntimePath)) {
return ENGINE_TYPE.COCOS
}
if(fs.existsSync(gameEntryPath)) {
const fileContent = fs.readFileSync(gameEntryPath, { encoding: 'utf-8' })
return getEngineType(fileContent)
} else if(fs.existsSync(mainEntryPath)) {
// 读取main.js
const fileContent = fs.readFileSync(mainEntryPath, { encoding: 'utf-8' })
return getEngineType(fileContent)
} else if(fs.existsSync(indexEntryPath)) {
// 读取index.js
const fileContent = fs.readFileSync(indexEntryPath, { encoding: 'utf-8' })
return getEngineType(fileContent)
}
}
const getEngineType = (fileContent) => {
if (fileContent.indexOf('Laya.') > -1 || fileContent.indexOf('laya.') > -1 || fileContent.indexOf('LayaAir.') > -1){
return ENGINE_TYPE.LAYA
} else if (fileContent.indexOf('egret') > -1) {
return ENGINE_TYPE.EGRET
} else {
return ENGINE_TYPE.OTHTER
}
}
const updateMaifestJson = (rootPath, curType) => {
let engineType = curType ? curType : setEngineType(rootPath)
console.log(`### HONOR PACK ### 当前引擎类型:${engineType}`)
const manifestJsonPath = path.join(rootPath, 'manifest.json')
const manifest = fse.readJSONSync(manifestJsonPath)
manifest.engineType = engineType
fse.outputJSONSync(manifestJsonPath, manifest, { spaces: 2 })
}
export { updateMaifestJson }