UNPKG

@quick-game/cli

Version:

Command line interface for rapid qg development

120 lines (101 loc) 4.35 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 _index = require("./index.js"); var _fileWrapper = require("./lib/fileWrapper.js"); // import getLatestVersion from 'latest-version' const CWD = process.cwd(); /** * 生成小游戏工程 * @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 (_index.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, _index.log)(`\nRemoving ${_index.chalk.cyan(targetDir)}...`); await _index.fs.remove(targetDir); } else { const { action } = await _inquirer.default.prompt([ { name: 'action', type: 'list', message: `Target directory ${_index.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, _index.log)(`\nRemoving ${_index.chalk.cyan(targetDir)}...`); await _index.fs.remove(targetDir); } } } } await createImpl(name, targetDir, template); } async function createImpl(name, context, template) { (0, _index.logWithSpinner)(`✨`, `Creating project in ${_index.chalk.yellow(context)}.`); let templatePath = _path.default.join(__dirname, '../../', 'templates', template); if (!_index.fs.existsSync(templatePath)) { templatePath = _path.default.join(__dirname, '../../', 'templates', 'default'); } (0, _index.stopSpinner)(); (0, _index.logWithSpinner)(`📄`, 'Copy Files from templates...'); await _index.fs.copy(templatePath, context, { clobber: false }); (0, _fileWrapper.replaceFiles)({ appName: name }, ['src/manifest.json', 'package.json'].map((f) => { return _path.default.join(context, f); })); (0, _index.stopSpinner)(); (0, _index.log)(`📄 Successfully Copy Files from ${_index.chalk.yellow(templatePath)}.`); // log instructions (0, _index.log)(); (0, _index.log)(`🎉 Successfully created project ${_index.chalk.yellow(name)}.`); let cdTip; if (context === CWD) {// 如果小游戏工程的目录与当前执行目录一致,不显示cdTip cdTip = ''; } else { if (_path.default.relative(CWD, context) === name) { cdTip = _index.chalk.cyan(` ${_index.chalk.gray('$')} cd ${name}\n ${_index.chalk.gray('$')} qg build\n ${_index.chalk.gray('$')} qg server\n`); } else { cdTip = _index.chalk.cyan(` ${_index.chalk.gray('$')} cd ${context}\n ${_index.chalk.gray('$')} qg build\n ${_index.chalk.gray('$')} qg server\n`); } } (0, _index.log)(`👉 Get started with the following commands:\n\n` + cdTip); } /** * 生成工程目录 * @param name 项目名 */var _default = (...args) => { return create(...args).catch((err) => { (0, _index.error)('Creat MiniGame Failed!'); throw err; }); };exports.default = _default;