Skip to content

Commit 6ff8358

Browse files
authored
Merge pull request #4 from zttonly/master
增加node使用上传脚本
2 parents 4d14c91 + e6f9a4b commit 6ff8358

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test
2+
node_modules
3+
package-lock.json
4+
yarn.lock

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- index.js 普通上传
88
- fsr.js 使用邮箱验证的安全方式上传
99
- webpack-plugin.js webpack插件
10+
- upload.js node使用上传插件,包含普通上传和fsr上传
1011

1112
## 配置
1213

@@ -33,6 +34,24 @@
3334
}
3435
```
3536

37+
### upload.js 使用
38+
39+
```js
40+
// 实例化
41+
const upload = new Upload({
42+
disableFsr: false, // 默认启用fsr 默认false
43+
host: 'http://host.com',
44+
receiver: 'http://xxx.com:8xxx/receiver',
45+
to: 'dest', // 目标机器路径
46+
files: [{[filenam]: [sourceCode]}], // 文件对象
47+
replace: [{from:'a', to:'b'}, {from: new RegExp('oldCDN', 'ig'), to: 'newCDN'}] // 替换内容
48+
});
49+
50+
// 开始上传
51+
upload.run();
52+
53+
```
54+
3655
### 服务端配置
3756

3857
[receiver.php](https://gist.github.com/jinzhan/131858820f998acca568b374dcfd88e2),部署到远程机器,并保证`receiver.php`能被正常访问

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deploy-files",
3-
"version": "0.2.4",
3+
"version": "0.2.5",
44
"description": "deploy files with node",
55
"main": "index.js",
66
"dependencies": {

upload.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* @file upload.js
3+
* @author zttonly <[email protected]>
4+
*/
5+
6+
const md5 = require('md5');
7+
const upload = require('./index');
8+
const fsrUpload = require('./fsr');
9+
10+
/*
11+
* node使用, 上传
12+
*
13+
* 参数说明:
14+
* disableFsr: 默认启用fsr 默认false
15+
* host: fsr上传 host
16+
* receiver: fsr上传 receiver
17+
* to: fsr上传 to
18+
* files: 文件对象{[filenam]: [sourceCode]}
19+
* replace: 替换内容 [{from:'', to:''}]
20+
*
21+
* **/
22+
23+
class Upload {
24+
constructor(options = {}) {
25+
this.options = options;
26+
this.uploadOptions = {
27+
host: options.host,
28+
receiver: options.receiver,
29+
retry: 2,
30+
aborted: false
31+
};
32+
this.throttle = options.throttle || 200;
33+
this.files = options.files;
34+
this.to = options.to;
35+
this.replace = options.replace;
36+
37+
this._running = false;
38+
this._deployFiles = {};
39+
}
40+
41+
run(cb) {
42+
if (this.running) {
43+
this.uploadOptions.aborted = true;
44+
clearTimeout(this.running);
45+
this.running = null;
46+
}
47+
const options = this.options;
48+
// 过滤掉已经上传成功的文件
49+
const targetFiles = this.filterFiles(this.files);
50+
const uploadTargets = Object.keys(targetFiles).map(filename => {
51+
return {
52+
host: options.host,
53+
receiver: options.receiver,
54+
content: this.getContent(filename, targetFiles[filename]),
55+
to: options.to,
56+
subpath: filename.replace(/\?.*$/, '')
57+
};
58+
});
59+
60+
// 是否FIS安全部署服务
61+
const uploadHandler = options.disableFsr ? upload : fsrUpload;
62+
const startTime = Date.now();
63+
this.running = setTimeout(() => {
64+
this.uploadOptions.aborted = false;
65+
uploadHandler(uploadTargets, this.uploadOptions, () => {
66+
// 对于存在hash的文件,使用 1 作为flag
67+
// 对于 tpl、html 这种没有 hash 的文件,使用内容的 md5 作为flag
68+
Object.keys(targetFiles).forEach(filename => {
69+
if (targetFiles[filename]) {
70+
this._deployFiles[filename] = md5(targetFiles[filename]);
71+
}
72+
});
73+
if (cb) {
74+
return cb();
75+
}
76+
console.log('\n');
77+
console.log('Upload completed in ' + (Date.now() - startTime) + 'ms.');
78+
});
79+
}, this.throttle);
80+
}
81+
82+
filterFiles(files) {
83+
const targetFiles = {};
84+
// 过滤掉已经上传成功的文件
85+
Object.keys(files).forEach(filename => {
86+
if (this._deployFiles[filename] && (
87+
this._deployFiles[filename] === md5(files[filename])
88+
)) {
89+
return;
90+
}
91+
targetFiles[filename] = files[filename];
92+
});
93+
return targetFiles;
94+
}
95+
96+
getContent(filename, source) {
97+
const isContainCdn = /\.(css|js|html|tpl)$/.test(filename);
98+
if (isContainCdn) {
99+
source = source.toString();
100+
this.replace.forEach(re => {
101+
const reg = typeof re.from === 'string' ? new RegExp(re.from, 'ig') : re.from;
102+
source = source.replace(reg, re.to);
103+
});
104+
}
105+
return source;
106+
}
107+
}
108+
109+
module.exports = Upload;

0 commit comments

Comments
 (0)