优化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
npm i five-cli -g
npm i -g @gm5/cli
```
## 用法
> 用法: `five-cli [command] args...`
+ Commands:
* init <ver> - 初始化一个版本(最低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
* 优化项目构建

View File

@ -1,3 +1,5 @@
#!/usr/bin/env node
/**
* 脚手架
* @author yutent<yutent.io@gmail.com>
@ -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
}

View File

@ -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('目录初始化完成, 依赖安装中...'))
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()
}
}

View File

@ -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('应用尚未配置启动...'))
}

View File

@ -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({

View File

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