forked from iAmHades/aliyunoss-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (132 loc) · 4.55 KB
/
index.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict';
var path = require('path');
var fs = require('fs');
var oss = require('ali-oss');
var co = require('co');
var colors = require('colors');
var fileUtils = require('./utils/file');
var _ = require('./utils/object');
function AliyunossWebpackPlugin(options) {
if (
!options ||
!options.buildPath ||
!options.region ||
!options.accessKeyId ||
!options.accessKeySecret ||
!options.bucket
) {
console.info(
'Some parameters of the problem,please set as the follow:'
);
console.info(' new'.red + ' AliyunossWebpackPlugin({');
console.info(" buildPath:'your path',".yellow);
console.info(" region: 'your region',".yellow);
console.info(" accessKeyId: 'your accessKeyId',".yellow);
console.info(" accessKeySecret: 'your accessKeySecret',".yellow);
console.info(" bucket: 'your bucket'".yellow);
console.info(' })');
throw new Error('Some parameters of the problem');
}
this.fileArray = [];
this.options = _.extend({}, options);
}
AliyunossWebpackPlugin.prototype.apply = function (compiler) {
var _this = this;
if (compiler) {
compiler.plugin('done', function (compilation) {
_this.oposs();
});
} else {
_this.oposs();
}
};
AliyunossWebpackPlugin.prototype.oposs = function () {
var _this = this;
var deleteAll = _this.options.deleteAll || false;
var deletePrefix = _this.options.deletePrefix || '';
var generateObjectPath =
_this.options.generateObjectPath ||
function (fileName) {
return fileName;
};
var getObjectHeaders =
_this.options.getObjectHeaders ||
function () {
return {};
};
co(function* () {
'use strict';
var store = oss({
region: _this.options.region,
accessKeyId: _this.options.accessKeyId,
accessKeySecret: _this.options.accessKeySecret,
bucket: _this.options.bucket,
internal: _this.options.internal ? true : false,
});
//删除oss上代码
if (deleteAll) {
if (deletePrefix) {
var prefixList = yield store.list({
prefix: deletePrefix,
});
prefixList.objects = prefixList.objects || [];
yield Promise.all(
prefixList.objects.map((file) => {
co(function* () {
try {
yield store.delete(file.name);
console.log(
'delete '.red + file.name + ' success'.green
);
} catch (error) {
console.log(
'delete '.red + file.name + ' fail'.red
);
error.failObjectName = name;
return error;
}
});
})
);
} else {
var fileList = yield store.list();
var files = [];
if (fileList.objects) {
fileList.objects.forEach(function (file) {
files.push(file.name);
});
var result = yield store.deleteMulti(files, {
quiet: true,
});
}
}
}
//上传oss的新代码
fileUtils.eachFileSync(_this.options.buildPath, function (
filename,
stats
) {
_this.fileArray.push(filename);
});
var j = 0;
for (var i = 0; i < _this.fileArray.length; i++) {
var file = _this.fileArray[i];
var fileName = file.split('/').pop();
var ossFileName = generateObjectPath(fileName, file);
if (ossFileName) {
yield store.put(ossFileName, file, {
timeout: _this.options.timeout,
headers: getObjectHeaders(fileName),
});
console.log(
file + ' -- upload to ' + ossFileName + ' success'.green
);
} else {
console.log('skipping file ' + file);
}
}
}).catch(function (err) {
console.info(err);
});
};
module.exports = AliyunossWebpackPlugin;