Skip to content

Commit 4805123

Browse files
author
guowangyang
committed
feat: 支持多个目录
1 parent 58f4d81 commit 4805123

File tree

6 files changed

+114
-26
lines changed

6 files changed

+114
-26
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ server.pwd
44

55
watch-auto-deploy.tar.gz
66
package-lock.json
7+
78
temp/
8-
temp2/
9+
temp2/
10+
temp3/
11+
temp5/

README.md

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
1-
## 监听文件并自动部署
1+
# 监听文件并自动部署
22

33
主要是在服务器上工作,监听目标文件夹/目标文件,发生变化就自动执行部署命令。
44

5+
## 注意点
56

7+
1. `fs.watch`不可靠,即使只改动一次,也会触发好几次。所以这里用时间信息作为判断,如果修改时间/size等信息变化了,则说明文件有变化。
8+
2. fs.existsSync 判断文件/文件夹是否存在
69

7-
`fs.watch`不可靠,即使只改动一次,也会触发好几次。所以这里用时间信息作为判断,如果修改时间/size等信息变化了,则说明文件有变化。
810

911

12+
## Getting Started
13+
14+
部署本项目前,需要将服务器密码写在根目录下的server.pwd中,这个文件不会上传到Git中。比如:
15+
16+
```
17+
hostName=xxxx
18+
hostPwd=xxxx
19+
```
1020

1121
- npm instll
1222
安装依赖
23+
- npm run dev
24+
开发环境启动项目
1325
- npm run start
14-
启动项目
15-
- npm run build
26+
正式环境启动项目
27+
- npm run deploy
1628
部署本项目
1729

1830

31+
## 特征
32+
33+
- 支持多个监听目录
34+
- 支持多个服务器,变量控制
35+
36+
## 核心原理
37+
38+
1. 解压,`tar -zxvf ${sourceFile} -C ${targetDir}`
39+
2. 如果是后端项目,会执行`pm2 start`
1940

2041

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "主要是在服务器上工作,监听目标文件夹/目标文件,发生变化就自动执行部署命令。",
55
"main": "index.js",
66
"scripts": {
7-
"start": "node src/index.js",
7+
"dev": "node src/index.js",
8+
"start": "NODE_ENV=production node src/index.js",
89
"deploy": "sh deploy.sh"
910
},
1011
"author": "",

src/config.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const path = require('path');
2+
3+
const GROUP_WATCH_DEPLOY_PATH = [{
4+
// 监听目录
5+
watchPath: '/root/guowangyang/watch-to-deploy-dir',
6+
// 项目部署目录
7+
deployPath: '/root/guowangyang/deploy-dir',
8+
}, {
9+
watchPath: '/root/group/watch-to-deploy-dir',
10+
deployPath: '/root/group/deploy-dir',
11+
}]
12+
13+
const MY_WATCH_DEPLOY_PATH = [{
14+
watchPath: '/root/watch-to-deploy-dir',
15+
deployPath: '/root/deploy-dir',
16+
}]
17+
18+
const DEV_WATCH_DEPLOY_PATH = [{
19+
watchPath: path.resolve(__dirname, '../temp'),
20+
deployPath: path.resolve(__dirname, '../temp2'),
21+
}, {
22+
watchPath: path.resolve(__dirname, '../temp3'),
23+
deployPath: path.resolve(__dirname, '../temp5'),
24+
}]
25+
26+
module.exports = {
27+
GROUP_WATCH_DEPLOY_PATH,
28+
MY_WATCH_DEPLOY_PATH,
29+
DEV_WATCH_DEPLOY_PATH
30+
}

src/index.js

+28-20
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,11 @@ const fs = require('fs');
22
const path = require('path');
33
const execa = require('execa');
44

5-
// const WATCH_PATH = path.resolve(__dirname, '../temp');
6-
// const DEPLOY_PATH = path.resolve(__dirname, '../temp2');
5+
const { getWatchAndDeployPath } = require('./utils')
76

87
const IS_GROUP = true;
98

10-
// 监听目录
11-
const WATCH_PATH = IS_GROUP ? '/root/guowangyang/watch-to-deploy-dir' : '/root/watch-to-deploy-dir';
12-
13-
// 项目部署目录
14-
const DEPLOY_PATH = IS_GROUP ? '/root/guowangyang/deploy-dir' : '/root/deploy-dir';
15-
16-
const fileStatMap = new Map()
9+
const watchAndDeployPath = getWatchAndDeployPath(IS_GROUP)
1710

1811

1912
function getStatInfo(info) {
@@ -22,24 +15,39 @@ function getStatInfo(info) {
2215
return stat
2316
}
2417

25-
async function main() {
26-
fs.readdir(WATCH_PATH, 'utf-8', async (err, data) => {
18+
function initFileStat(watchPath, deployPath, fileStatMap) {
19+
if (!fs.existsSync(watchPath)) {
20+
console.log('\x1B[31m%s\x1B[0m', '\n监听目录不存在\n');
21+
return;
22+
}
23+
if (!fs.existsSync(deployPath)) {
24+
console.log('\x1B[31m%s\x1B[0m', '\n部署目录不存在!\n');
25+
return;
26+
}
27+
28+
fs.readdir(watchPath, 'utf-8', async (err, data) => {
2729
data.map(item => {
28-
const info = fs.statSync(path.resolve(WATCH_PATH, item))
30+
const info = fs.statSync(path.resolve(watchPath, item))
2931

3032
if (info.isFile()) {
3133
fileStatMap.set(item, getStatInfo(info))
3234
console.log(`fileStatMap: `, fileStatMap)
3335
}
3436
})
3537

36-
await watch()
38+
await watch(watchPath, deployPath, fileStatMap)
39+
})
40+
}
41+
42+
async function main() {
43+
watchAndDeployPath.map(item => {
44+
initFileStat(item.watchPath, item.deployPath, item.fileStatMap)
3745
})
3846
}
3947

40-
async function watch() {
41-
fs.watch(WATCH_PATH, {}, async (eventType, filename) => {
42-
const info = fs.statSync(path.resolve(WATCH_PATH, filename))
48+
async function watch(watchPath, deployPath, fileStatMap) {
49+
fs.watch(watchPath, {}, async (eventType, filename) => {
50+
const info = fs.statSync(path.resolve(watchPath, filename))
4351

4452
const stat = getStatInfo(info)
4553
const originStat = fileStatMap.get(filename)
@@ -49,20 +57,20 @@ async function watch() {
4957
console.log(`stat: ${stat}, originStat: ${originStat}`)
5058

5159
try {
52-
await deploy(filename)
60+
await deploy(filename, watchPath, deployPath)
5361
} catch(err) {
5462
console.log(`err: ${err}`)
5563
}
5664
}
5765
})
5866
}
5967

60-
async function deploy(filename) {
68+
async function deploy(filename, watchPath, deployPath,) {
6169
// 默认都是以 .tar.gz 结尾
6270

6371
const project = filename.replace(/\.tar\.gz$/, '')
64-
const targetDir = path.resolve(DEPLOY_PATH, project)
65-
const sourceFile = path.resolve(WATCH_PATH, filename);
72+
const targetDir = path.resolve(deployPath, project)
73+
const sourceFile = path.resolve(watchPath, filename);
6674

6775
if (!fs.existsSync( sourceFile ) ) {
6876
return

src/utils.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const {
2+
MY_WATCH_DEPLOY_PATH,
3+
GROUP_WATCH_DEPLOY_PATH,
4+
DEV_WATCH_DEPLOY_PATH
5+
} = require('./config');
6+
7+
const isDev = process.env.NODE_ENV !== 'production';
8+
9+
function getWatchAndDeployPath(isGroup) {
10+
let list = MY_WATCH_DEPLOY_PATH;
11+
if (isDev) {
12+
list = DEV_WATCH_DEPLOY_PATH;
13+
} else if (isGroup) {
14+
list = GROUP_WATCH_DEPLOY_PATH;
15+
}
16+
17+
return list.map(item => ({
18+
...item,
19+
fileStatMap: new Map()
20+
}));
21+
}
22+
23+
module.exports = {
24+
getWatchAndDeployPath
25+
}

0 commit comments

Comments
 (0)