-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (75 loc) · 2.29 KB
/
Copy pathindex.js
File metadata and controls
88 lines (75 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const OSS = require('ali-oss')
// 初始化 oss client
async function initOSS(options) {
options.region = options.region || 'oss-cn-beijing'
const client = new OSS({
accessKeyId: options.accessKeyId,
accessKeySecret: options.accessKeySecret,
bucket: options.bucket,
region: options.region
})
client.showDomain = fixDomain(options.domain)
return client
}
function fixDomain (domain = '') {
if (!domain) return
const reg = /\/+$/
if (reg.test(domain)) {
return `${domain.replace(reg, '/')}`
} else {
return `${domain}/`
}
}
// 提取file name 和 content
async function preUpload(modified) {
let files = []
modified.map(file => {
const fileName = file.getHashRelease().replace(/^\//, '')
const bufferContent = new Buffer(file.getContent())
const subpath = file.subpath.replace(/^\//, '')
files.push({
subpath,
name: fileName,
content: bufferContent
})
})
return files
}
// upload
async function upload(client, files = [], next) {
if (!client) {
throw new Error('oss client not found!!')
}
if (files.length <= 0) {
process.stdout.write(`\n${`upload-oss bucket >> ${client.options.bucket || ''}`.green.bold} [${fis.log.now(true)}] 上传完成!`)
typeof next === 'function' && next()
return false
}
const file = files.shift()
try {
const res = await client.put(file.name, file.content)
let url = client.showDomain ? `${client.showDomain}${res.name}`: res.url
process.stdout.write(`\n${`upload-oss bucket >> ${client.options.bucket || ''}`.green.bold} [${fis.log.now(true)}] ${file.subpath} ${`>>`.yellow.bold} ${url}`)
} catch (e) {
throw new Error(e)
}
upload(client, files, next)
}
/**
* deploy-qiniu 插件接口
* @param {Object} options 插件配置
* @param {Object} modified 修改了的文件列表(对应watch功能)
* @param {Object} total 所有文件列表
* @param {Function} next 调用下一个插件
* @return {undefined}
*/
module.exports = async (options, modified, total, next) => {
if (!options.accessKeyId || !options.accessKeySecret) {
throw new Error('options.accessKeyId and options.accessKeySecret is required!');
} else if (!options.bucket) {
throw new Error('options.bucket is required!');
}
const client = await initOSS(options)
const files = await preUpload(modified)
upload(client, files, next)
}