@honor-minigame/cli
Version:
honor minigame pack cli
150 lines (129 loc) • 3.71 kB
JavaScript
import * as constanst from './constanst.js'
import * as paths from './paths.js'
import path from 'path'
import fs from 'fs-extra'
/**
* 校验当前工程是否存在必要的文件
*/
export function check() {
if (!fs.existsSync(paths.MANIFEST)) {
error(`请确认项目${paths.SRC}下存在文件:${paths.MANIFEST}`)
throw new Error(`请确认项目${paths.SRC}下存在文件:${paths.MANIFEST}`)
}
if (!fs.existsSync(paths.ENTRY)) {
error(`请确认项目${paths.SRC}下存在文件:${paths.ENTRY}`)
throw new Error(`请确认项目${paths.SRC}下存在文件:${paths.ENTRY}`)
}
}
export function get() {
try {
const config = fs.readJsonSync(paths.MANIFEST)
// 更新orientation字段
let hasUpdate = false
if (config && config.deviceOrientation && !config.orientation) {
config.orientation = config.deviceOrientation
hasUpdate = true
}
if (hasUpdate) {
fs.outputJsonSync(paths.MANIFEST, config)
}
return updateWorkerToSubPkg(config)
} catch (err) {
error(`读取小游戏配置文件出错!`)
error(err)
return {}
}
}
/**
* 将workers配置到分包中
*/
export function updateWorkerToSubPkg(manifest) {
if (!manifest || !manifest.workers || !manifest.workers.path || !manifest.workers.isSubpackage) {
return manifest
}
if (manifest.subpackages) {
// 查找是否已经包含
const hasWorkersPackage = manifest.subpackages.find(pack => pack.name === 'workers') !== undefined
if (hasWorkersPackage) {
return manifest
}
} else {
manifest.subpackages = []
}
// 添加一个新的子包对象到sub数组中
manifest.subpackages.push({
name: 'workers',
root: manifest.workers.path
})
// 更新本地manifest
try {
fs.outputJsonSync(paths.MANIFEST, manifest)
const workersPath = path.resolve(paths.SRC, manifest.workers.path)
const workersGameJs = path.resolve(workersPath, 'game.js')
if (fs.existsSync(workersPath) && !fs.existsSync(workersGameJs)) {
fs.outputFileSync(workersGameJs, '')
}
} catch (error) {
error(error)
}
return manifest
}
/**
* 获取workers路径
*/
export function getWorkersPath() {
const manifest = get()
if (!manifest || !manifest.workers) {
return ''
}
if (manifest.workers.path) {
return path.resolve(paths.SRC, manifest.workers.path)
}
return path.resolve(paths.SRC, manifest.workers)
}
/**
* 更新/添加 manifest 字段
* @param {Buffer} content manifest.json的二进制内容
* @param {Boolean} release是否是release版本
*/
export function add(key, value) {
const config = get()
config[key] = value
try {
fs.outputJsonSync(paths.MANIFEST, config, { spaces: 2 })
} catch (error) {
error(error)
}
}
/**
* 更新manifest文件
* @param {Buffer} content manifest.json的二进制内容
* @param {Boolean} release是否是release版本
*/
export function update({ content, release }) {
let manifest
try {
manifest = JSON.parse(content.toString())
} catch (err) {
error(`解析${constanst.MANIFEST}文件出错`)
throw err
}
// 更新config.debug
manifest.config = manifest.config || {}
manifest.config.debug = !release
// 更新最小版本号
if (!manifest.minPlatformVersion) {
manifest.minPlatformVersion = 101
}
// 联盟版本号写入
if (!manifest.allianceVersion) {
info('联盟包体需要将manifest中allianceVersion & minPlatformVersion进行写入')
manifest.allianceVersion = 1300
manifest.minPlatformVersion = 1000
if (manifest.plugins) {
manifest.plugins = {}
}
}
// 更新内容
return JSON.stringify(manifest, null, 2)
}