UNPKG

@vivo-minigame/cli

Version:

Command line interface for rapid Vivo minigame development

139 lines (116 loc) 5.29 kB
"use strict";var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");_Object$defineProperty(exports, "__esModule", { value: true });exports.default = void 0;var _path = _interopRequireDefault(require("path")); var _inquirer = _interopRequireDefault(require("inquirer")); var _latestVersion = _interopRequireDefault(require("latest-version")); var _cliSharedUtils = require("@vivo-minigame/cli-shared-utils"); var _fileWrapper = require("./util/fileWrapper"); const CWD = process.cwd(); const 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 = false }) { // 是否是在当前目录创建小游戏工程,也就是projectName指定为. const inCurrent = projectName === '.'; // 如果是在当前目录创建小游戏工程,name 取当前目录的目录名 const name = inCurrent ? _path.default.relative(_path.default.join(cwd, '../'), cwd) : projectName; const targetDir = _path.default.resolve(cwd, projectName || '.'); 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: false }] }] ); if (!action) { return; } else if (action === 'overwrite') { (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); if (!_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: false }); (0, _fileWrapper.replaceFiles)({ appName: name, serviceVersion: latest }, ['src/manifest.json', 'package.json'].map((f) => { return _path.default.join(context, f); })); (0, _cliSharedUtils.stopSpinner)(); (0, _cliSharedUtils.log)(`📄 Successfully Copy Files from ${_cliSharedUtils.chalk.yellow(templatePath)}.`); // install deps (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; if (context === CWD) {// 如果小游戏工程的目录与当前执行目录一致,不显示cdTip cdTip = ''; } else { if (_path.default.relative(CWD, context) === name) { cdTip = _cliSharedUtils.chalk.cyan(` ${_cliSharedUtils.chalk.gray('$')} cd ${name}\n`); } else { cdTip = _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('$')} ${packageManager === 'yarn' ? 'yarn server' : packageManager === 'pnpm' ? 'pnpm run server' : 'npm run server'}`) ); } /** * 生成工程目录 * @param name 项目名 */var _default = (...args) => { return create(...args).catch((err) => { (0, _cliSharedUtils.error)('Creat MiniGame Failed!'); throw err; }); };exports.default = _default;