@oppo-minigame/cli
Version:
Command line interface for rapid OPPO minigame development
1 lines • 3.37 kB
JavaScript
const path=require("path"),fs=require("fs-extra"),chalk=require("chalk"),{copySignFile:copySignFile,createMainifestJson:createMainifestJson,getSuggestPackNameJson:getSuggestPackNameJson,prePackWorker:prePackWorker,packSpinner:packSpinner,execInstallRpkProcess:execInstallRpkProcess}=require("../utils"),packer=require("../pack"),CONFIG=require("../config/config");async function generate(e,a=!0){let c=path.resolve(e.pub_dir||process.cwd());const t=prePackWorker(e.worker,c);let i=packer.checkProject(c,a);i.workers=t,fs.writeFileSync(path.join(c,"manifest.json"),JSON.stringify(i));let s=packer.mkDirs(c,a),n=getSubpacks(i),r="";n&&n.length>0?(console.log(chalk.green("###包含分包### 开始打包")),r=await generateWithSubpacksV28(e.buildJs,c,s,i,a,e.qrcode)):(console.log(chalk.green("###不包含分包### 开始打包")),r=await generateNoSubpacks(e.buildJs,c,s,i,a,e.qrcode)),e.autoInstall&&await execInstallRpkProcess(r)}async function generateNoSubpacks(e,a,c,t,i,s,n=!0){fs.emptyDirSync(c.distDir);let r=collectSubpacks(a,t.subpackages),o=collectMainPacks(a,r),l="";return packer.checkPluginConf(t)?(console.log(chalk.green("###包含插件信息### 将开始打插件包")),packer.generateProvider(a,t),await packer.signDir(null,a,c.distDir,t,c.signFiles,i),l=await packer.generatePluginsMainRPK(a,c.distDir,t,c.signFiles,i,{subpackCollections:r,mainpackCollection:o})):(console.log(chalk.green("###不包含插件信息### 将不会打插件包")),l=await packer.signDir(null,a,c.distDir,t,c.signFiles,i,s)),l}async function generateWithSubpacks(e,a,c,t,i,s){packer.checkSubpackageConf(a,t);let n=collectSubpacks(a,t.subpackages),r=collectMainPacks(a,n),o=path.join(c.targetDir,CONFIG.MAIN_PACK_NAME),l=path.join(c.distDir,CONFIG.TMP_RPKS);await packer.buildDir(!t.plugins&&e,a,o,!0,!0,packer.getExcludes(null,n,r,CONFIG.MAIN_PACK_NAME,"all"),{},i),await packer.signDir(CONFIG.MAIN_PACK_NAME,o,l,t,c.signFiles,!0);let p={};for(const s of t.subpackages){const o=path.resolve(path.join(c.targetDir,s.name)),k=path.resolve(path.join(a,s.root)),g=path.join(k,"main.js"),u=path.join(k,"game.js");s.path=k,l=path.resolve(path.join(c.distDir,CONFIG.TMP_RPKS));const h=fs.statSync(k).isDirectory();if(!s.root.includes(".js")&&h&&(fs.existsSync(g)||fs.writeFileSync(g,"console.log('')"),!fs.existsSync(u))){const e=fs.readFileSync(g,"utf8").toString();fs.writeFileSync(u,e);const a="require('"+s.name+"/game.js')";fs.writeFileSync(g,a)}await packer.buildDir(e,k,o,!1,!0,packer.getExcludes(s,n,r,s.name,"all"),p,i),await packer.signDir(s.name,o,l,t,c.signFiles,!0)}if(!t.plugins){l=path.join(c.distDir,CONFIG.TMP_RPKS);try{let e=i?`${t.package}.rpk`:`${t.package}.signed.rpk`;fs.copySync(path.join(c.distDir,e),path.join(l,e))}catch(e){console.log(chalk.red(`### 构建js失败 ### ${e}`))}}const k=await packer.signDir(null,l,c.distDir,t,c.signFiles,i,s);return fs.removeSync(l),fs.removeSync(o),k}async function generateWithSubpacksV28(e,a,c,t,i,s){return await generateNoSubpacks(e,a,c,t,i,!1),await generateWithSubpacks(e,a,c,t,i,s)}function collectSubpacks(e,a){let c={};return a.forEach(a=>{c[a.name]=packer.collectPackFiles(path.resolve(path.join(e,a.root)))}),c}function collectMainPacks(e,a){let c=[];return Object.values(a).forEach(e=>{c=c.concat(e.all)}),packer.collectPackFiles(e,c)}function getSubpacks(e){return e.subpackages}module.exports=generate;