UNPKG

@honor-minigame/cli

Version:

honor minigame pack cli

145 lines (130 loc) 4.55 kB
import path from 'path' import fs from 'fs-extra' import crypto from 'crypto' import { fileURLToPath } from 'url' import { SIGN_MODE } from '../common/constant.js' const __filename = fileURLToPath(import.meta.url) export const __dirname = path.dirname(__filename) export let projectPath = process.env.PATH_PROJECT || process.cwd() // 检查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) // 以原有 manifest 为基础进行覆盖,保留用户自定义的额外字段(如 enableBrotli 等), // 避免执行 pack/laya 等命令时丢失 manifest.json 中已存在的字段 let manifest = { ...json } manifest.icon = json.icon || '/logo.png' manifest.type = 'game' manifest.vConsole = signMode === SIGN_MODE.DEBUG manifest.config = { ...(json.config || {}), debug: signMode === SIGN_MODE.DEBUG, logLevel: 'debug' } if(json.orientation) { manifest.orientation = json.orientation } if (json.isUnity) { manifest.isUnity = json.isUnity } if (json.workers) { manifest.workers = json.workers } if (isAlliance || json.allianceVersion) { manifest.allianceVersion = json.allianceVersion || 1300 } if (json.subpackages) { manifest.subpackages = json.subpackages } if (json.plugins) { manifest.plugins = json.plugins } fs.outputJSONSync(manifestJsonPath, manifest, { spaces: 2 }) return manifest } export async function loadModule(path) { try { const module = await import(path) return module } catch (err) { console.error('加载失败:', err) } }; /** * 获取插件包的md5值以及覆盖配置里面的md5值 * @param {Array(String)} pluginPath 插件目录名称 */ export function writeMD5(projectPath, key) { const relativePath = path.join(projectPath, key) const files = fs.readdirSync(relativePath) const md5Obj = {} let isHasResFile = false files.sort() files.forEach(file => { const filePath = path.resolve(relativePath, file) const stat = fs.statSync(filePath) if (stat.isDirectory()) { return } const ext = path.extname(file) if (['.js', '.json'].indexOf(ext) < 0) { isHasResFile = true } if (file !== 'plugin.json') { md5Obj[file] = calculateFileMD5(fs.readFileSync(filePath)) } }) if (isHasResFile) { info('插件包目前支持js文件,暂时不支持放置其他类型文件,建议优化下重新打包') } const md5Arr = Object.entries(md5Obj) let md5Str = '' md5Arr.forEach(v => { md5Str += v[0] md5Str += v[1] }) const manifestPath = path.join(projectPath, 'manifest.json') let manifest = fs.readJSONSync(manifestPath) manifest.plugins[key].provider = calculateFileMD5(md5Str) fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2)) } function calculateFileMD5(input) { return crypto.createHash('md5').update(input).digest('hex'); }