From 9273071683fd0c9bad7b7d9a0d9ee373c6b7b16b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Tue, 29 Sep 2020 11:35:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96pm2=E7=9A=84=E8=AF=BB?= =?UTF-8?q?=E5=8F=96;=E4=BC=98=E5=8C=96=E4=BF=A1=E6=81=AF=E8=BE=93?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 16 ++++++++++++++++ Readme.md | 23 ++++++++++++++++------- index.js | 25 +++++++++++++++++-------- lib/init.js | 25 +++++++++++++++++++------ lib/pm2.js | 12 +++++------- lib/tools.js | 4 +++- package.json | 14 ++++++++++---- 7 files changed, 86 insertions(+), 33 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ef7335 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +.DS_Store +.AppleDouble +.LSOverride +.vscode +.idea + +node_modules/ + + +# Thumbnails +._* + +# Files that might appear on external disk +.Spotlight-V100 +.Trashes + diff --git a/Readme.md b/Readme.md index 45d9ff4..56b4308 100644 --- a/Readme.md +++ b/Readme.md @@ -1,19 +1,24 @@ -# Five.js框架控制台工具 -> 一键安装、管理Five.js框架的脚本工具。 ->> 仅支持Linux/MacOS +``` + ____ _ _ + __ _ _ __ ___ | ___| ___| (_) + / _` | '_ ` _ \|___ \ _____ / __| | | +| (_| | | | | | |___) |_____| (__| | | + \__, |_| |_| |_|____/ \___|_|_| + |___/ 一键安装、管理Five.js框架的脚本工具 +``` + ## 安装 -> 需要全局安装 ```bash -npm i five-cli -g +npm i -g @gm5/cli ``` ## 用法 > 用法: `five-cli [command] args...` + Commands: - * init - 初始化一个版本(最低3.0.0),不指定则为最新版 - * start [prod|dev] - 运行当前的应用(可指定以开发环境还是生产环境启动) + * init - 初始化框架 + * start [prod|dev] - 运行当前的应用(可指定运行环境) * stop - 停止当前应用 * st|status - 查看当前应用状态 * r|restart - 重启当前应用 @@ -24,6 +29,10 @@ npm i five-cli -g ## 更新日志 +### v1.0.0 +* 脚本重构,支持windows +* 支持最新框架 + ### v0.4.1 * 优化项目构建 diff --git a/index.js b/index.js index bee0002..ab504a0 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ +#!/usr/bin/env node + /** * 脚手架 * @author yutent @@ -8,18 +10,26 @@ const fs = require('iofs') const chalk = require('chalk') const path = require('path') -const { do_init } = require('./lib/init') -const { run, stop, restart, remove, status } = require('./lib/pm2') -const { exec } = require('./lib/tools') +var pm2 +var pkg = require('./package.json') +var { do_init } = require('./lib/init') +var { start, stop, restart, remove, status } = require('./lib/pm2') -var pkg = fs.cat(path.join(__dirname, './package.json')) var { arch, platform, version } = process var os = { linux: 'Linux', darwin: 'MacOS', win: 'Windows' } var cwd = process.cwd() var args = process.argv.slice(2) var action = args.shift() -pkg = JSON.parse(pkg) +try { + pm2 = require('pm2/package.json') +} catch (error) { + print('#'.repeat(64)) + console.log(chalk.red('脚手架使用pm2作为守护进程, 请先安装pm2')) + console.log(chalk.green('npm i -g pm2')) + print('#'.repeat(64)) + process.exit() +} function print(...args) { args[0] = args[0].padEnd(20, ' ') @@ -43,7 +53,7 @@ function print_help() { print(`${logo()} v${pkg.version}, 作者: 宇天`) print('-'.repeat(64)) print('node版本: ' + version) - print('pm2 版本: v' + exec('pm2 -v').trim()) + print('pm2 版本: v' + pm2.version) print(`当前系统: ${os[platform]}(${arch})`) print('当前路径: ' + chalk.red.underline(cwd)) print('='.repeat(64)) @@ -66,7 +76,7 @@ switch (action) { break case 'start': - run(cwd, args[0]) + start(cwd, args[0]) break case 'stop': @@ -95,6 +105,5 @@ switch (action) { default: print_help() - // console.log(exec('pm2 -v').trim()) break } diff --git a/lib/init.js b/lib/init.js index bc9eccd..a9ac990 100644 --- a/lib/init.js +++ b/lib/init.js @@ -8,13 +8,14 @@ const fs = require('iofs') const path = require('path') const chalk = require('chalk') -const { exec } = require('./tools') +const { exec, read } = require('./tools') +const { start } = require('./pm2') function encode(obj) { return JSON.stringify(obj, null, 2) } -exports.do_init = function(dir) { +exports.do_init = async function(dir) { var files = fs.ls(dir) if (files && files.length) { console.log(chalk.red('当前目录非空, 初始化失败...')) @@ -144,8 +145,20 @@ app.listen(3000) path.join(dir, 'app.js') ) - console.log(chalk.green('目录初始化完成, 依赖安装中...')) - exec('npm i @gm5/core') - console.log(chalk.blue('依赖安装完成 ^_^')) - process.exit() + console.log(chalk.green('目录初始化完成!')) + + var confirm = await read('是否立即安装依赖? y/n: ') + if (confirm === 'y') { + console.log(chalk.green('依赖安装中...')) + exec('npm i @gm5/core') + console.log(chalk.blue('依赖安装完成 ^_^')) + } + + confirm = await read('是否立即运行? y/n: ') + + if (confirm === 'y') { + start(dir) + } else { + process.exit() + } } diff --git a/lib/pm2.js b/lib/pm2.js index 92d4894..b2b7833 100644 --- a/lib/pm2.js +++ b/lib/pm2.js @@ -13,7 +13,7 @@ const { exec, read } = require('./tools') var cpus = os.cpus() -exports.run = async function(dir, env) { +exports.start = async function(dir, env) { var confFile = path.join(dir, 'app.yaml') var run_env = process.env.NODE_ENV || 'development' var name, cluster, instances @@ -26,7 +26,7 @@ exports.run = async function(dir, env) { console.log( chalk.yellow('应用可能已经启动, 请确认是否在列表中, 以免重复启动!') ) - console.log(exec('pm2 ls')) + exec('pm2 ls') var act = await read( '请确认操作, 如已在列表中, 请回车或按Ctrl + C取消, 输入任意内容将会重新启动: ' ) @@ -34,7 +34,7 @@ exports.run = async function(dir, env) { var data = fs.cat(confFile).toString() data = data.replace(/NODE_ENV: [a-z]+/, `NODE_ENV: ${run_env}`) fs.echo(data, confFile) - console.log(exec('pm2 start app.yaml')) + exec('pm2 start app.yaml') console.log(chalk.blue('应用启动成功!!!')) } process.exit() @@ -117,7 +117,6 @@ exports.stop = async function(dir) { var confirm = await read('(你确定要停止应用吗? y/n): ') if (confirm === 'y') { exec('pm2 stop app.yaml') - console.log(exec('pm2 status app.yaml')) console.log(chalk.blue('应用已经停止.')) } } else { @@ -131,7 +130,7 @@ exports.restart = async function(dir) { if (fs.exists(confFile)) { var confirm = await read('(你确定要重启应用吗? y/n): ') if (confirm === 'y') { - console.log(exec('pm2 restart app.yaml')) + exec('pm2 restart app.yaml') console.log(chalk.blue('应用已经重启.')) } } else { @@ -150,7 +149,6 @@ exports.remove = async function(dir) { var confirm = await read('(请确认操作 y/n): ') if (confirm === 'y') { exec('pm2 delete app.yaml') - console.log(exec('pm2 status app.yaml')) console.log(chalk.blue('应用已经删除.')) } } else { @@ -162,7 +160,7 @@ exports.remove = async function(dir) { exports.status = async function(dir) { var confFile = path.join(dir, 'app.yaml') if (fs.exists(confFile)) { - console.log(exec('pm2 status app.yaml')) + exec('pm2 status app.yaml') } else { console.log(chalk.yellow('应用尚未配置启动...')) } diff --git a/lib/tools.js b/lib/tools.js index 9705a60..e363727 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -2,7 +2,9 @@ const child = require('child_process') const readline = require('readline') exports.exec = function(cmd) { - return child.execSync(cmd).toString() + return child.execSync(cmd, { + stdio: [process.stdin, process.stdout, process.stdout] + }) } var rl = readline.createInterface({ diff --git a/package.json b/package.json index 362f8a0..e6d7e6e 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,18 @@ { "name": "@gm5/cli", "description": "Five.js框架控制台工具", - "version": "0.7.0", - "author": { - "name": "yutent" - }, + "version": "1.0.0", + "author": "yutent ", "bin": { "five-cli": "index.js" }, + "dependencies": { + "chalk": "^4.0.0", + "iofs": "^1.5.0" + }, + "repository": { + "type": "git", + "url": "https://github.com/bytedo/gmf.cli.git" + }, "license": "MIT" }