@vivo-minigame/cli
Version:
Command line interface for rapid Vivo minigame development
15 lines • 4.32 kB
JavaScript
;var _Object$defineProperty=require("@babel/runtime-corejs2/core-js/object/define-property"),_interopRequireDefault=require("@babel/runtime-corejs2/helpers/interopRequireDefault");_Object$defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _path=_interopRequireDefault(require("path")),_inquirer=_interopRequireDefault(require("inquirer")),_latestVersion=_interopRequireDefault(require("latest-version")),_cliSharedUtils=require("@vivo-minigame/cli-shared-utils"),_fileWrapper=require("./util/fileWrapper");const CWD=process.cwd(),pkg=require("../../package.json");/**
* 生成小游戏工程
* @param {String} projectName 工程名
* @param {Object} options
* @param {String} options.cwd 要创建小游戏工程的路径
* @param {String} options.template 创建小游戏工程使用的模板
* @param {String} options.force 是否强制覆盖,如果指定目录已经存在
*/async function create(projectName,{cwd=CWD,template,force=!1}){// 是否是在当前目录创建小游戏工程,也就是projectName指定为.
const inCurrent="."===projectName,name=inCurrent?_path.default.relative(_path.default.join(cwd,"../"),cwd):projectName,targetDir=_path.default.resolve(cwd,projectName||".");// 如果是在当前目录创建小游戏工程,name 取当前目录的目录名
if(_cliSharedUtils.fs.existsSync(targetDir))if(inCurrent){const{ok}=await _inquirer.default.prompt([{name:"ok",type:"confirm",message:cwd===CWD?"Generate project in current directory?":`Generate project in ${cwd}?`}]);if(!ok)return}else if(force)(0,_cliSharedUtils.log)(`\nRemoving ${_cliSharedUtils.chalk.cyan(targetDir)}...`),await _cliSharedUtils.fs.remove(targetDir);else{const{action}=await _inquirer.default.prompt([{name:"action",type:"list",message:`Target directory ${_cliSharedUtils.chalk.cyan(targetDir)} already exists. Pick an action:`,choices:[{name:"Overwrite",value:"overwrite"},{name:"Merge",value:"merge"},{name:"Cancel",value:!1}]}]);if(!action)return;"overwrite"===action&&((0,_cliSharedUtils.log)(`\nRemoving ${_cliSharedUtils.chalk.cyan(targetDir)}...`),await _cliSharedUtils.fs.remove(targetDir))}await createImpl(name,targetDir,template)}async function createImpl(name,context,template){(0,_cliSharedUtils.logWithSpinner)(`✨`,`Creating project in ${_cliSharedUtils.chalk.yellow(context)}.`);let templatePath=_path.default.join(__dirname,"../../","templates",template);_cliSharedUtils.fs.existsSync(templatePath)||(templatePath=_path.default.join(__dirname,"../../","templates","default"));const latest=await(0,_latestVersion.default)("@vivo-minigame/cli-service");(0,_cliSharedUtils.stopSpinner)(),(0,_cliSharedUtils.logWithSpinner)(`📄`,"Copy Files from templates..."),await _cliSharedUtils.fs.copy(templatePath,context,{clobber:!1}),(0,_fileWrapper.replaceFiles)({appName:name,serviceVersion:latest},["src/manifest.json","package.json"].map(f=>_path.default.join(context,f))),(0,_cliSharedUtils.stopSpinner)(),(0,_cliSharedUtils.log)(`📄 Successfully Copy Files from ${_cliSharedUtils.chalk.yellow(templatePath)}.`),(0,_cliSharedUtils.log)(`📦 we are installing dependencies for you. this might take a while...`),(0,_cliSharedUtils.log)(`📦 if failed, you should install dependencies by yourself with ${_cliSharedUtils.chalk.green(`\`npm install\``)}`);const packageManager=((0,_cliSharedUtils.hasYarn)()?"yarn":null)||((0,_cliSharedUtils.hasPnpm3OrLater)()?"pnpm":"npm");// await installDeps(context, packageManager)
// log instructions
(0,_cliSharedUtils.log)(),(0,_cliSharedUtils.log)(`🎉 Successfully created project ${_cliSharedUtils.chalk.yellow(name)}.`);let cdTip;cdTip=context===CWD?"":_path.default.relative(CWD,context)===name?_cliSharedUtils.chalk.cyan(` ${_cliSharedUtils.chalk.gray("$")} cd ${name}\n`):_cliSharedUtils.chalk.cyan(` ${_cliSharedUtils.chalk.gray("$")} cd ${context}\n`),(0,_cliSharedUtils.log)(`👉 Get started with the following commands:\n\n`+cdTip+_cliSharedUtils.chalk.cyan(` ${_cliSharedUtils.chalk.gray("$")} ${"yarn"===packageManager?"yarn server":"pnpm"===packageManager?"pnpm run server":"npm run server"}`))}/**
* 生成工程目录
* @param name 项目名
*/var _default=(...args)=>create(...args).catch(err=>{throw(0,_cliSharedUtils.error)("Creat MiniGame Failed!"),err});exports.default=_default;