优化pm2的读取;优化信息输出

master
宇天 2020-09-29 11:35:49 +08:00
parent a1aee3760e
commit 9273071683
7 changed files with 86 additions and 33 deletions

16
.gitignore vendored Normal file
View File

@ -0,0 +1,16 @@
.DS_Store
.AppleDouble
.LSOverride
.vscode
.idea
node_modules/
# Thumbnails
._*
# Files that might appear on external disk
.Spotlight-V100
.Trashes

View File

@ -1,19 +1,24 @@
# Five.js框架控制台工具 ```
> 一键安装、管理Five.js框架的脚本工具。 ____ _ _
>> 仅支持Linux/MacOS __ _ _ __ ___ | ___| ___| (_)
/ _` | '_ ` _ \|___ \ _____ / __| | |
| (_| | | | | | |___) |_____| (__| | |
\__, |_| |_| |_|____/ \___|_|_|
|___/ 一键安装、管理Five.js框架的脚本工具
```
## 安装 ## 安装
> 需要全局安装
```bash ```bash
npm i five-cli -g npm i -g @gm5/cli
``` ```
## 用法 ## 用法
> 用法: `five-cli [command] args...` > 用法: `five-cli [command] args...`
+ Commands: + Commands:
* init <ver> - 初始化一个版本(最低3.0.0),不指定则为最新版 * init - 初始化框架
* start [prod|dev] - 运行当前的应用(可指定以开发环境还是生产环境启动) * start [prod|dev] - 运行当前的应用(可指定运行环境)
* stop - 停止当前应用 * stop - 停止当前应用
* st|status - 查看当前应用状态 * st|status - 查看当前应用状态
* r|restart - 重启当前应用 * r|restart - 重启当前应用
@ -24,6 +29,10 @@ npm i five-cli -g
## 更新日志 ## 更新日志
### v1.0.0
* 脚本重构,支持windows
* 支持最新框架
### v0.4.1 ### v0.4.1
* 优化项目构建 * 优化项目构建

View File

@ -1,3 +1,5 @@
#!/usr/bin/env node
/** /**
* 脚手架 * 脚手架
* @author yutent<yutent.io@gmail.com> * @author yutent<yutent.io@gmail.com>
@ -8,18 +10,26 @@ const fs = require('iofs')
const chalk = require('chalk') const chalk = require('chalk')
const path = require('path') const path = require('path')
const { do_init } = require('./lib/init') var pm2
const { run, stop, restart, remove, status } = require('./lib/pm2') var pkg = require('./package.json')
const { exec } = require('./lib/tools') 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 { arch, platform, version } = process
var os = { linux: 'Linux', darwin: 'MacOS', win: 'Windows' } var os = { linux: 'Linux', darwin: 'MacOS', win: 'Windows' }
var cwd = process.cwd() var cwd = process.cwd()
var args = process.argv.slice(2) var args = process.argv.slice(2)
var action = args.shift() 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) { function print(...args) {
args[0] = args[0].padEnd(20, ' ') args[0] = args[0].padEnd(20, ' ')
@ -43,7 +53,7 @@ function print_help() {
print(`${logo()} v${pkg.version}, 作者: 宇天`) print(`${logo()} v${pkg.version}, 作者: 宇天`)
print('-'.repeat(64)) print('-'.repeat(64))
print('node版本: ' + version) print('node版本: ' + version)
print('pm2 版本: v' + exec('pm2 -v').trim()) print('pm2 版本: v' + pm2.version)
print(`当前系统: ${os[platform]}(${arch})`) print(`当前系统: ${os[platform]}(${arch})`)
print('当前路径: ' + chalk.red.underline(cwd)) print('当前路径: ' + chalk.red.underline(cwd))
print('='.repeat(64)) print('='.repeat(64))
@ -66,7 +76,7 @@ switch (action) {
break break
case 'start': case 'start':
run(cwd, args[0]) start(cwd, args[0])
break break
case 'stop': case 'stop':
@ -95,6 +105,5 @@ switch (action) {
default: default:
print_help() print_help()
// console.log(exec('pm2 -v').trim())
break break
} }

View File

@ -8,13 +8,14 @@ const fs = require('iofs')
const path = require('path') const path = require('path')
const chalk = require('chalk') const chalk = require('chalk')
const { exec } = require('./tools') const { exec, read } = require('./tools')
const { start } = require('./pm2')
function encode(obj) { function encode(obj) {
return JSON.stringify(obj, null, 2) return JSON.stringify(obj, null, 2)
} }
exports.do_init = function(dir) { exports.do_init = async function(dir) {
var files = fs.ls(dir) var files = fs.ls(dir)
if (files && files.length) { if (files && files.length) {
console.log(chalk.red('当前目录非空, 初始化失败...')) console.log(chalk.red('当前目录非空, 初始化失败...'))
@ -144,8 +145,20 @@ app.listen(3000)
path.join(dir, 'app.js') path.join(dir, 'app.js')
) )
console.log(chalk.green('目录初始化完成, 依赖安装中...')) console.log(chalk.green('目录初始化完成!'))
var confirm = await read('是否立即安装依赖? y/n: ')
if (confirm === 'y') {
console.log(chalk.green('依赖安装中...'))
exec('npm i @gm5/core') exec('npm i @gm5/core')
console.log(chalk.blue('依赖安装完成 ^_^')) console.log(chalk.blue('依赖安装完成 ^_^'))
}
confirm = await read('是否立即运行? y/n: ')
if (confirm === 'y') {
start(dir)
} else {
process.exit() process.exit()
}
} }

View File

@ -13,7 +13,7 @@ const { exec, read } = require('./tools')
var cpus = os.cpus() var cpus = os.cpus()
exports.run = async function(dir, env) { exports.start = async function(dir, env) {
var confFile = path.join(dir, 'app.yaml') var confFile = path.join(dir, 'app.yaml')
var run_env = process.env.NODE_ENV || 'development' var run_env = process.env.NODE_ENV || 'development'
var name, cluster, instances var name, cluster, instances
@ -26,7 +26,7 @@ exports.run = async function(dir, env) {
console.log( console.log(
chalk.yellow('应用可能已经启动, 请确认是否在列表中, 以免重复启动!') chalk.yellow('应用可能已经启动, 请确认是否在列表中, 以免重复启动!')
) )
console.log(exec('pm2 ls')) exec('pm2 ls')
var act = await read( var act = await read(
'请确认操作, 如已在列表中, 请回车或按Ctrl + C取消, 输入任意内容将会重新启动: ' '请确认操作, 如已在列表中, 请回车或按Ctrl + C取消, 输入任意内容将会重新启动: '
) )
@ -34,7 +34,7 @@ exports.run = async function(dir, env) {
var data = fs.cat(confFile).toString() var data = fs.cat(confFile).toString()
data = data.replace(/NODE_ENV: [a-z]+/, `NODE_ENV: ${run_env}`) data = data.replace(/NODE_ENV: [a-z]+/, `NODE_ENV: ${run_env}`)
fs.echo(data, confFile) fs.echo(data, confFile)
console.log(exec('pm2 start app.yaml')) exec('pm2 start app.yaml')
console.log(chalk.blue('应用启动成功!!!')) console.log(chalk.blue('应用启动成功!!!'))
} }
process.exit() process.exit()
@ -117,7 +117,6 @@ exports.stop = async function(dir) {
var confirm = await read('(你确定要停止应用吗? y/n): ') var confirm = await read('(你确定要停止应用吗? y/n): ')
if (confirm === 'y') { if (confirm === 'y') {
exec('pm2 stop app.yaml') exec('pm2 stop app.yaml')
console.log(exec('pm2 status app.yaml'))
console.log(chalk.blue('应用已经停止.')) console.log(chalk.blue('应用已经停止.'))
} }
} else { } else {
@ -131,7 +130,7 @@ exports.restart = async function(dir) {
if (fs.exists(confFile)) { if (fs.exists(confFile)) {
var confirm = await read('(你确定要重启应用吗? y/n): ') var confirm = await read('(你确定要重启应用吗? y/n): ')
if (confirm === 'y') { if (confirm === 'y') {
console.log(exec('pm2 restart app.yaml')) exec('pm2 restart app.yaml')
console.log(chalk.blue('应用已经重启.')) console.log(chalk.blue('应用已经重启.'))
} }
} else { } else {
@ -150,7 +149,6 @@ exports.remove = async function(dir) {
var confirm = await read('(请确认操作 y/n): ') var confirm = await read('(请确认操作 y/n): ')
if (confirm === 'y') { if (confirm === 'y') {
exec('pm2 delete app.yaml') exec('pm2 delete app.yaml')
console.log(exec('pm2 status app.yaml'))
console.log(chalk.blue('应用已经删除.')) console.log(chalk.blue('应用已经删除.'))
} }
} else { } else {
@ -162,7 +160,7 @@ exports.remove = async function(dir) {
exports.status = async function(dir) { exports.status = async function(dir) {
var confFile = path.join(dir, 'app.yaml') var confFile = path.join(dir, 'app.yaml')
if (fs.exists(confFile)) { if (fs.exists(confFile)) {
console.log(exec('pm2 status app.yaml')) exec('pm2 status app.yaml')
} else { } else {
console.log(chalk.yellow('应用尚未配置启动...')) console.log(chalk.yellow('应用尚未配置启动...'))
} }

View File

@ -2,7 +2,9 @@ const child = require('child_process')
const readline = require('readline') const readline = require('readline')
exports.exec = function(cmd) { exports.exec = function(cmd) {
return child.execSync(cmd).toString() return child.execSync(cmd, {
stdio: [process.stdin, process.stdout, process.stdout]
})
} }
var rl = readline.createInterface({ var rl = readline.createInterface({

View File

@ -1,12 +1,18 @@
{ {
"name": "@gm5/cli", "name": "@gm5/cli",
"description": "Five.js框架控制台工具", "description": "Five.js框架控制台工具",
"version": "0.7.0", "version": "1.0.0",
"author": { "author": "yutent <yutent.io@gmail.com>",
"name": "yutent"
},
"bin": { "bin": {
"five-cli": "index.js" "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" "license": "MIT"
} }