UNPKG

@honor-minigame/cli

Version:

honor minigame pack cli

91 lines (82 loc) 2.76 kB
import path from 'path' import fs from 'fs-extra' import { fileURLToPath } from 'url' import { SIGN_MODE } from '../common/constant.js' const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) // 检查node版本 export function checkNodeVersion() { const version = process.version.slice(1).split('.')[0] if (version < 14) { console.warn(`### HONOR PACK ### Node版本过低,请升级到14以上版本`) return false } return true } // egret入口处理 export function patchEntryFile(rootPath) { const mainPath = path.join(rootPath, 'main.js') fs.writeFileSync(mainPath, '') const templatePath = path.join(__dirname, './../template/egretTemplate.txt') let template = fs.readFileSync(templatePath, { encoding: 'utf-8' }) template = template.replace('##require', `require("./cocos-runtime-egret.min.js")\nrequire("./index.js")\n`) // 读取index.html文件中的设置 const htmlPath = path.join(rootPath, 'index.html') if (fs.pathExistsSync(htmlPath)) { // 读取配置 const html = fs.readFileSync(htmlPath, { encoding: 'utf-8' }) const divList = html.match(/<div\/?[^>]+(>|$)/g) if (divList) { let str = '' divList.forEach((div) => { if (div && div.indexOf('data-') > -1) { const attrMap = div.match(/\s?([\w-]+)=['"]?[\w-:;%,.\s]+['"]?/g) attrMap.forEach((item) => { const [key, value] = item.split('=') str += `element.setAttribute("${key.trim()}", ${value.toString()});\n` }) } }) template = template.replace('##content', str) } } fs.writeFileSync(mainPath, template) } // 设置manifest.json文件 export function getManifestJson(manifestJsonPath, signMode, isAlliance) { const json = fs.readJSONSync(manifestJsonPath) let manifest = { package: json.package, name: json.name, versionName: json.versionName, versionCode: json.versionCode, minPlatformVersion: json.minPlatformVersion, icon: json.icon || '/logo.png', type: 'game', vConsole: signMode === SIGN_MODE.DEBUG, config: { debug: true, logLevel: 'debug' }, orientation: json.orientation } if (json.subpackages) { manifest.subpackages = json.subpackages } if (json.isUnity) { manifest.isUnity = json.isUnity } if (isAlliance) { manifest.allianceVersion = json.allianceVersion || 1300 } fs.outputJSONSync(manifestJsonPath, manifest) return manifest } export async function loadModule(path) { try { const module = await import(path) return module } catch (err) { console.error('加载失败:', err) } };