@oppo-minigame/cli
Version:
Command line interface for rapid OPPO minigame development
1 lines • 7.92 kB
JavaScript
const path=require("path"),fs=require("fs-extra"),chalk=require("chalk"),shell=require("shelljs"),SparkMD5=require("spark-md5"),prelude="// modules are defined as an array\n// [ module function, map of requireuires ]\n//\n// map of requireuires is short require name -> numeric require\n//\n// anything defined in a previous bundle is accessed via the\n// orig method which is the requireuire for previous bundles\n\n(function() {\n\n function outer(modules, cache, entry) {\n // Save the require = require() previous bundle to this closure if any\n //console.log('modules', modules, entry)\n var previousRequire = typeof __require__ == \"function\" && __require__;\n \n function newRequire(name, jumped){\n if(!cache[name]) {\n if(!modules[name]) {\n // if we cannot find the module within our internal map or\n // cache jump to the current global require ie. the last bundle\n // that was added to the page.\n var currentRequire = typeof __require__ == \"function\" && __require__;\n if (!jumped && currentRequire) return currentRequire(name, true);\n\n // If there are other bundles on this page the require = require() the\n // previous one is saved to 'previousRequire'. Repeat this as\n // many times as there are bundles until the module is found or\n // we exhaust the require chain.\n if (previousRequire) return previousRequire(name, true);\n var err = new Error('Cannot find module \\'' + name + '\\'');\n err.code = 'MODULE_NOT_FOUND';\n throw err;\n }\n var m = cache[name] = {exports:{}};\n modules[name][0].call(m.exports, function(x){\n var id = modules[name][1][x];\n return newRequire(id ? id : x);\n },m,m.exports,outer,modules,cache,entry);\n }\n return cache[name].exports;\n }\n for(var i=0;i<entry.length;i++) newRequire(entry[i]);\n \n // Override the current require with this new one\n return newRequire;\n }\n \n return outer;\n \n })()",{sign:sign}=require("../sign/signer"),CONFIG=require("../config/config"),{traverseDirSync:traverseDirSync,loadBabelModule:loadBabelModule,copyFiles:copyFiles,createRpkQrcode:createRpkQrcode}=require("../utils");function checkPluginConf(e){return!(!e.plugins||!(e.plugins["laya-library"]||e.plugins["cocos-library"]||e.plugins["egret-library"]))}function checkSubpackageConf(e,n){let r={};n.subpackages.forEach(n=>{if(n.name&&n.root)if(r[n.name])console.log(chalk.red(`[配置错误]分包名重复: ${n.name}`)),shell.exit(1);else if(n.name==CONFIG.MAIN_PACK_NAME)console.log(chalk.red(`[配置错误]分包名称不能为main: [${n.name}]`)),shell.exit(1);else{let i=path.resolve(e,n.root);if(fs.existsSync(i)){fs.statSync(i).isDirectory()||".js"===path.extname(n.root)?r[n.name]=n:(console.log(chalk.red(`[配置错误]分包[${n.name}] root必须是目录或者.js文件`)),shell.exit(1))}else console.log(chalk.red(`[配置错误]分包[${n.name}] root 不存在`)),shell.exit(1)}else console.log(chalk.red("[配置错误]分包配置必须包含name和root属性")),shell.exit(1)}),console.log(chalk.green("### 分包配置检查完成 ### 准备打包"))}async function generatePluginsMainRPK(e,n,r,i,t,o){const s=path.join(e,CONFIG.MAIN_PLUGIN_NAME),a=path.join(n,CONFIG.TMP_RPKS);let l=["laya-library","cocos-library","egret-library"].map(n=>path.join(e,n));if(o){const{subpackCollections:e,mainpackCollection:n}=o;l=[...l,...getExcludes(null,e,n,CONFIG.MAIN_PACK_NAME,"all")]}await buildDir(!1,e,s,!0,!1,l,{},t),await signDir(CONFIG.MAIN_PLUGIN_NAME,s,a,r,i,!0);let c="";try{let e=t?`${r.package}.rpk`:`${r.package}.signed.rpk`;c=path.join(n,e),fs.copySync(c,path.join(a,e))}catch(e){console.log(chalk.red(`### 构建js失败 ### ${e}`))}return fs.removeSync(s),c}async function signDir(e,n,r,i,t,o,s,a=null,l=[]){return new Promise((c,u)=>{let h=e||i.package;console.log(chalk.green(`### 开始签名rpk ###: ${h}`)),sign({input:n,output:r,rpkName:h,signFiles:t,excludes:l},o,e=>{console.log(chalk.green(`### 完成签名rpk ###: ${e}`));const n=o?`${h}.rpk`:`${h}.signed.rpk`;s&&(console.log(chalk.green("正在启动服务,生成二维码:")),createRpkQrcode(h,n,a)),c(path.join(r,n))})})}async function buildDir(e,n,r,i,t,o=[],s={},a=!0){fs.ensureDirSync(r),i&&(o.push(path.resolve(n,CONFIG.QUICKGAME_DIR)),o.push(path.resolve(n,CONFIG.SIGN_DIR_NAME)),o.push(path.resolve(n,CONFIG.BUILD_PATH)),o.push(path.resolve(n,CONFIG.DEST_DIR)),CONFIG.EXCLUDES.forEach(e=>{o.push(path.resolve(n,e))}));try{copyFiles(n,r,[],o)}catch(e){console.log(chalk.red(`### 构建js失败 ### ${e}`))}}function generateProvider(e,n){const r=Object.keys(n.plugins)[0];fs.existsSync(path.join(e,r))||(console.log(chalk.red(`找不到引擎插件目录 ${r}`)),shell.exit(1));const i=fs.readdirSync(path.join(e,r));let t="";for(const n of i){if(fs.lstatSync(path.join(e,`${r}/${n}`)).isDirectory()){t=n;break}}const o=[];i.forEach(n=>{"plugin.json"!=n&&n!=t&&o.push(SparkMD5.hash(fs.readFileSync(path.join(e,r,n),{encoding:"utf8"})))});const s=SparkMD5.hash(o.join(""));n.plugins[r].provider=s,fs.writeFileSync(path.join(e,"manifest.json"),JSON.stringify(n))}function getExcludes(e,n,r,i,t="all"){let o=[];for(let r in n){let i=n[r];null!=e&&i.path==e.path||(o=o.concat(i[t])),fs.statSync(i.path).isDirectory()&&o.push(i.path)}return i!=CONFIG.MAIN_PACK_NAME&&(o=o.concat(r[t])),o}function collectPackFiles(e,n=null){let r={path:e,all:[],js:[],other:[]};if(fs.statSync(e).isDirectory()){const i=[];traverseDirSync(e,i,n),i.forEach(e=>{r.all.push(e),".js"==path.extname(e)?r.js.push(e):r.other.push(e)})}else n&&-1!=n.indexOf(e)||(r.all.push(e),r.js.push(e));return r}function mkDirs(e,n){let r=e,i=path.join(r,CONFIG.BUILD_PATH),t=path.join(r,CONFIG.DEST_DIR);fs.emptyDirSync(t),fs.emptyDirSync(i),console.log(chalk.green("[准备工作]build&dist 文件夹已清空"));let o=path.join(r,CONFIG.SIGN_DIR_NAME),s=path.resolve(path.resolve(__dirname,"../../sign"));fs.existsSync(o)||(console.log(chalk.yellow("### 缺少签名文件 ### 使用默认签名和证书")),copyFiles(s,o));let a=n?CONFIG.SIGN_DEBUG_DIR:CONFIG.SIGN_RELEASE_DIR,l=path.join(o,a,CONFIG.PRIVATE_FILE_NAME),c=path.join(o,a,CONFIG.CERTIFICATE_FILE_NAME);return fs.existsSync(l)&&fs.existsSync(c)||(n?(console.log(chalk.yellow("### 缺少签名文件 ### 使用默认签名和证书")),copyFiles(path.join(s,a),path.join(o,a))):(console.log(chalk.red(`[签名文件错误]没有 release 的签名和证书, 请保证以下路径的文件存在,私钥:${l}, 证书:${c}`)),shell.exit(1))),{signFiles:{privatekey:l,certificate:c},targetDir:i,distDir:t}}function getExposeName(e){return path.posix.sep+path.relative(process.cwd(),e).split(path.win32.sep).join(path.posix.sep)}function checkProject(e){let n=null,r=["manifest.json","main.js"];r.forEach(n=>{let r=path.join(e,n);fs.existsSync(r)||(console.log(chalk.red(`[错误]缺少文件 ${n}, 确保改路径下文件存在:${r}`)),shell.exit(1))});let i=path.join(e,r[0]);try{n=JSON.parse(fs.readFileSync(i).toString())}catch(e){console.log(chalk.red(`[error]parse config file error: ${i}`)),shell.exit(1)}if(n){let e=/^[a-zA-Z]+[0-9a-zA-Z_]*(\.[a-zA-Z]+[0-9a-zA-Z_]*)*$/;n.package?e.test(n.package)||(console.log(chalk.red("[配置错误]包名不合法")),shell.exit(1)):(console.log(chalk.red("[配置错误]必须包含包名")),shell.exit(1))}return n}module.exports={checkPluginConf:checkPluginConf,checkSubpackageConf:checkSubpackageConf,generatePluginsMainRPK:generatePluginsMainRPK,signDir:signDir,buildDir:buildDir,generateProvider:generateProvider,getExcludes:getExcludes,collectPackFiles:collectPackFiles,mkDirs:mkDirs,checkProject:checkProject};