upload-assets-oss 0.0.9 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +265 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +231 -8
- package/package.json +4 -3
- package/dist/npm_plugin/npmQiniuPlugin.js +0 -63
- package/dist/oss_plugin/qiniu.js +0 -89
- package/dist/utils/log.js +0 -10
- package/dist/utils/utils.js +0 -12
- package/dist/vite_plugin/vitePluginQiniu.js +0 -38
- package/dist/webpack_plugin/webpackPluginQiniu.js +0 -17
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
|
|
26
|
+
// src/index.ts
|
|
27
|
+
var src_exports = {};
|
|
28
|
+
__export(src_exports, {
|
|
29
|
+
WebpackPluginQiniu: () => webpackPluginQiniu_default,
|
|
30
|
+
vitePluginQiniu: () => vitePluginQiniu
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(src_exports);
|
|
33
|
+
|
|
34
|
+
// src/vite_plugin/vitePluginQiniu.ts
|
|
35
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
36
|
+
|
|
37
|
+
// src/npm_plugin/npmQiniuPlugin.ts
|
|
38
|
+
var import_difference = __toESM(require("lodash/difference"), 1);
|
|
39
|
+
|
|
40
|
+
// src/oss_plugin/qiniu.ts
|
|
41
|
+
var import_qiniu = __toESM(require("qiniu"), 1);
|
|
42
|
+
|
|
43
|
+
// src/utils/log.ts
|
|
44
|
+
var import_chalk = __toESM(require("chalk"), 1);
|
|
45
|
+
var log = (text, color = "green") => {
|
|
46
|
+
console.log(import_chalk.default[color](text));
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// src/oss_plugin/qiniu.ts
|
|
50
|
+
var Qiniu = class {
|
|
51
|
+
options = {
|
|
52
|
+
accessKey: "",
|
|
53
|
+
secretKey: "",
|
|
54
|
+
bucket: ""
|
|
55
|
+
};
|
|
56
|
+
uploadToken;
|
|
57
|
+
formUploader;
|
|
58
|
+
bucketManager;
|
|
59
|
+
constructor(options) {
|
|
60
|
+
const { accessKey, secretKey, bucket } = options;
|
|
61
|
+
const mac = new import_qiniu.default.auth.digest.Mac(accessKey, secretKey);
|
|
62
|
+
const _options = {
|
|
63
|
+
scope: bucket
|
|
64
|
+
};
|
|
65
|
+
const putPolicy = new import_qiniu.default.rs.PutPolicy(_options);
|
|
66
|
+
const config = new import_qiniu.default.conf.Config();
|
|
67
|
+
this.options = options;
|
|
68
|
+
this.uploadToken = putPolicy.uploadToken(mac);
|
|
69
|
+
this.formUploader = new import_qiniu.default.form_up.FormUploader(config);
|
|
70
|
+
this.bucketManager = new import_qiniu.default.rs.BucketManager(mac, config);
|
|
71
|
+
}
|
|
72
|
+
putFile(key, filePath) {
|
|
73
|
+
const putExtra = new import_qiniu.default.form_up.PutExtra();
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
this.formUploader.putFile(
|
|
76
|
+
this.uploadToken,
|
|
77
|
+
key,
|
|
78
|
+
filePath,
|
|
79
|
+
putExtra,
|
|
80
|
+
function(respErr, respBody, respInfo) {
|
|
81
|
+
if (respErr) {
|
|
82
|
+
throw respErr;
|
|
83
|
+
}
|
|
84
|
+
if (respInfo.statusCode == 200) {
|
|
85
|
+
resolve(respInfo);
|
|
86
|
+
} else {
|
|
87
|
+
console.log(respInfo.statusCode);
|
|
88
|
+
console.log(respBody);
|
|
89
|
+
reject(respBody);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
getResouceList(prefix) {
|
|
96
|
+
const { bucket } = this.options;
|
|
97
|
+
var options = {
|
|
98
|
+
prefix
|
|
99
|
+
};
|
|
100
|
+
log("\u{1F5D3} \u6B63\u5728\u83B7\u53D6\u5386\u53F2\u6570\u636E...");
|
|
101
|
+
return new Promise((resolve, reject) => {
|
|
102
|
+
this.bucketManager.listPrefix(
|
|
103
|
+
bucket,
|
|
104
|
+
options,
|
|
105
|
+
function(err, respBody, respInfo) {
|
|
106
|
+
if (err) {
|
|
107
|
+
throw err;
|
|
108
|
+
}
|
|
109
|
+
if (respInfo.statusCode == 200) {
|
|
110
|
+
const resourceList = respBody.items.map((item) => item.key);
|
|
111
|
+
log("\u{1F44F} \u83B7\u53D6\u5386\u53F2\u6570\u636E\u6210\u529F\uFF0C\u6B63\u5728\u5BF9\u6BD4\u6587\u4EF6...\n");
|
|
112
|
+
resolve(resourceList);
|
|
113
|
+
} else {
|
|
114
|
+
console.log(respInfo.statusCode);
|
|
115
|
+
console.log(respBody);
|
|
116
|
+
reject(respBody);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
batchDeleteFile(filenameAry) {
|
|
123
|
+
const { bucket } = this.options;
|
|
124
|
+
let deleteOperations = [];
|
|
125
|
+
if (filenameAry && filenameAry.length > 1) {
|
|
126
|
+
deleteOperations = filenameAry.map(
|
|
127
|
+
(filename) => import_qiniu.default.rs.deleteOp(bucket, filename)
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
log("\u{1F921} \u6B63\u5728\u5220\u9664\u6587\u4EF6...");
|
|
131
|
+
return new Promise((resolve, reject) => {
|
|
132
|
+
this.bucketManager.batch(
|
|
133
|
+
deleteOperations,
|
|
134
|
+
function(err, respBody, respInfo) {
|
|
135
|
+
if (err) {
|
|
136
|
+
reject(err);
|
|
137
|
+
throw err;
|
|
138
|
+
} else {
|
|
139
|
+
log("\u{1F44F} \u5220\u9664\u5B8C\u6210\uFF01\n");
|
|
140
|
+
resolve(respInfo);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
);
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
var qiniu_default = Qiniu;
|
|
148
|
+
|
|
149
|
+
// src/utils/utils.ts
|
|
150
|
+
var import_path = __toESM(require("path"), 1);
|
|
151
|
+
var getProjectName = () => import_path.default.basename(process.cwd());
|
|
152
|
+
|
|
153
|
+
// src/npm_plugin/npmQiniuPlugin.ts
|
|
154
|
+
var projectName = getProjectName();
|
|
155
|
+
var NpmQiuniuPlugin = class {
|
|
156
|
+
options;
|
|
157
|
+
qiniu;
|
|
158
|
+
constructor(options) {
|
|
159
|
+
this.qiniu = new qiniu_default(options);
|
|
160
|
+
this.options = options;
|
|
161
|
+
if (options.rootName) {
|
|
162
|
+
projectName = options.rootName;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
apply({
|
|
166
|
+
filePathAry,
|
|
167
|
+
outputPath
|
|
168
|
+
}) {
|
|
169
|
+
this.batchUpload(filePathAry, outputPath);
|
|
170
|
+
}
|
|
171
|
+
async batchUpload(filePathAry, buildPath) {
|
|
172
|
+
const { isLog } = this.options;
|
|
173
|
+
const uploadData = {};
|
|
174
|
+
filePathAry.forEach((filename) => {
|
|
175
|
+
uploadData[`${projectName}/${filename}`] = `${buildPath}/${filename}`;
|
|
176
|
+
});
|
|
177
|
+
const uploadAry = await this.batchDelete(Object.keys(uploadData));
|
|
178
|
+
const len = uploadAry.length;
|
|
179
|
+
const maxIndex = len - 1;
|
|
180
|
+
if (len === 0) {
|
|
181
|
+
log(`\u{1F62D} \u6CA1\u6709\u53D1\u73B0\u9700\u8981\u4E0A\u4F20\u7684\u6587\u4EF6
|
|
182
|
+
`);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
log(`\u2B06\uFE0F \u5C06\u4E0A\u4F20 ${len} \u4E2A\u6587\u4EF6`);
|
|
186
|
+
uploadAry.forEach(async (key, i) => {
|
|
187
|
+
const filePath = uploadData[key];
|
|
188
|
+
isLog && log(`\u{1F680} \u6B63\u5728\u4E0A\u4F20\u7B2C ${i + 1} \u4E2A\u6587\u4EF6: ${key}`);
|
|
189
|
+
await this.qiniu.putFile(key, filePath);
|
|
190
|
+
if (maxIndex === i) {
|
|
191
|
+
log(`\u{1F44F} \u4E0A\u4F20\u5B8C\u6210\uFF01`);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
async batchDelete(uploadFilePathAry) {
|
|
196
|
+
const { forceDelete } = this.options;
|
|
197
|
+
const resourceList = await this.qiniu.getResouceList(projectName);
|
|
198
|
+
const deleteAry = forceDelete ? resourceList : (0, import_difference.default)(resourceList, uploadFilePathAry);
|
|
199
|
+
const uploadAry = forceDelete ? uploadFilePathAry : (0, import_difference.default)(uploadFilePathAry, resourceList);
|
|
200
|
+
if (deleteAry.length > 0) {
|
|
201
|
+
await this.qiniu.batchDeleteFile(deleteAry);
|
|
202
|
+
}
|
|
203
|
+
return uploadAry;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
var npmQiniuPlugin_default = NpmQiuniuPlugin;
|
|
207
|
+
|
|
208
|
+
// src/vite_plugin/vitePluginQiniu.ts
|
|
209
|
+
var PLUGIN_NAME = "vite-plugin-upload-oss-qiniu";
|
|
210
|
+
function vitePluginQiniu(options) {
|
|
211
|
+
const qiniu2 = new npmQiniuPlugin_default(options);
|
|
212
|
+
let outputPath = "";
|
|
213
|
+
const getFilePaths = (pathName) => {
|
|
214
|
+
const files = import_fs.default.readdirSync(pathName);
|
|
215
|
+
return files.map((item) => {
|
|
216
|
+
const filePath = `${pathName}/${item}`;
|
|
217
|
+
const stat = import_fs.default.lstatSync(filePath);
|
|
218
|
+
if (stat.isDirectory()) {
|
|
219
|
+
return getFilePaths(filePath);
|
|
220
|
+
} else {
|
|
221
|
+
return pathName === outputPath ? item : filePath.replace(`${outputPath}/`, "");
|
|
222
|
+
}
|
|
223
|
+
}).flat();
|
|
224
|
+
};
|
|
225
|
+
return {
|
|
226
|
+
name: PLUGIN_NAME,
|
|
227
|
+
apply: "build",
|
|
228
|
+
configResolved: async (config) => {
|
|
229
|
+
outputPath = config.build.outDir;
|
|
230
|
+
},
|
|
231
|
+
closeBundle() {
|
|
232
|
+
const filePathAry = getFilePaths(outputPath);
|
|
233
|
+
qiniu2.apply({
|
|
234
|
+
filePathAry,
|
|
235
|
+
outputPath
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// src/webpack_plugin/webpackPluginQiniu.ts
|
|
242
|
+
var PLUGIN_NAME2 = "webpack-plugin-upload-oss-qiniu";
|
|
243
|
+
var WebpackPluginQiniu = class {
|
|
244
|
+
qiniu;
|
|
245
|
+
constructor(options) {
|
|
246
|
+
this.qiniu = new npmQiniuPlugin_default(options);
|
|
247
|
+
}
|
|
248
|
+
apply(compiler) {
|
|
249
|
+
compiler.hooks.afterEmit.tapAsync(
|
|
250
|
+
PLUGIN_NAME2,
|
|
251
|
+
async (compilation, callback) => {
|
|
252
|
+
const filePathAry = Object.keys(compilation.assets);
|
|
253
|
+
const outputPath = compilation.options.output.path;
|
|
254
|
+
callback();
|
|
255
|
+
this.qiniu.apply({ filePathAry, outputPath });
|
|
256
|
+
}
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
var webpackPluginQiniu_default = WebpackPluginQiniu;
|
|
261
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
262
|
+
0 && (module.exports = {
|
|
263
|
+
WebpackPluginQiniu,
|
|
264
|
+
vitePluginQiniu
|
|
265
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
import qiniu from 'qiniu';
|
|
3
|
+
|
|
4
|
+
interface Options {
|
|
5
|
+
/**
|
|
6
|
+
* 七牛 Access Key
|
|
7
|
+
*/
|
|
8
|
+
accessKey: string;
|
|
9
|
+
/**
|
|
10
|
+
* 七牛 Secret Key
|
|
11
|
+
*/
|
|
12
|
+
secretKey: string;
|
|
13
|
+
/**
|
|
14
|
+
* 七牛 空间名
|
|
15
|
+
*/
|
|
16
|
+
bucket: string;
|
|
17
|
+
/**
|
|
18
|
+
* 上传文件前,先强制删除之前上传七牛云上的文件
|
|
19
|
+
*/
|
|
20
|
+
forceDelete?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* 文件上传的根目录名称,默认为项目名称
|
|
23
|
+
*/
|
|
24
|
+
rootName?: string;
|
|
25
|
+
/**
|
|
26
|
+
* 是否打印上传日志,默认为false
|
|
27
|
+
*/
|
|
28
|
+
isLog?: boolean;
|
|
29
|
+
}
|
|
30
|
+
declare class Qiniu {
|
|
31
|
+
options: {
|
|
32
|
+
accessKey: string;
|
|
33
|
+
secretKey: string;
|
|
34
|
+
bucket: string;
|
|
35
|
+
};
|
|
36
|
+
uploadToken: string;
|
|
37
|
+
formUploader: qiniu.form_up.FormUploader;
|
|
38
|
+
bucketManager: qiniu.rs.BucketManager;
|
|
39
|
+
constructor(options: Options);
|
|
40
|
+
putFile(key: string, filePath: string): Promise<unknown>;
|
|
41
|
+
getResouceList(prefix: string): Promise<string[]>;
|
|
42
|
+
batchDeleteFile(filenameAry: string[]): Promise<unknown>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
declare function vitePluginQiniu(options: Options): Plugin;
|
|
46
|
+
|
|
47
|
+
declare class NpmQiuniuPlugin {
|
|
48
|
+
options: Options;
|
|
49
|
+
qiniu: Qiniu;
|
|
50
|
+
constructor(options: Options);
|
|
51
|
+
apply({ filePathAry, outputPath, }: {
|
|
52
|
+
filePathAry: string[];
|
|
53
|
+
outputPath: string;
|
|
54
|
+
}): void;
|
|
55
|
+
batchUpload(filePathAry: string[], buildPath: string): Promise<void>;
|
|
56
|
+
batchDelete(uploadFilePathAry: string[]): Promise<string[]>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
declare class WebpackPluginQiniu {
|
|
60
|
+
qiniu: NpmQiuniuPlugin;
|
|
61
|
+
constructor(options: Options);
|
|
62
|
+
apply(compiler: any): void;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { WebpackPluginQiniu, vitePluginQiniu };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,231 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
// src/vite_plugin/vitePluginQiniu.ts
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
|
|
4
|
+
// src/npm_plugin/npmQiniuPlugin.ts
|
|
5
|
+
import difference from "lodash/difference";
|
|
6
|
+
|
|
7
|
+
// src/oss_plugin/qiniu.ts
|
|
8
|
+
import qiniu from "qiniu";
|
|
9
|
+
|
|
10
|
+
// src/utils/log.ts
|
|
11
|
+
import chalk from "chalk";
|
|
12
|
+
var log = (text, color = "green") => {
|
|
13
|
+
console.log(chalk[color](text));
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// src/oss_plugin/qiniu.ts
|
|
17
|
+
var Qiniu = class {
|
|
18
|
+
options = {
|
|
19
|
+
accessKey: "",
|
|
20
|
+
secretKey: "",
|
|
21
|
+
bucket: ""
|
|
22
|
+
};
|
|
23
|
+
uploadToken;
|
|
24
|
+
formUploader;
|
|
25
|
+
bucketManager;
|
|
26
|
+
constructor(options) {
|
|
27
|
+
const { accessKey, secretKey, bucket } = options;
|
|
28
|
+
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
|
|
29
|
+
const _options = {
|
|
30
|
+
scope: bucket
|
|
31
|
+
};
|
|
32
|
+
const putPolicy = new qiniu.rs.PutPolicy(_options);
|
|
33
|
+
const config = new qiniu.conf.Config();
|
|
34
|
+
this.options = options;
|
|
35
|
+
this.uploadToken = putPolicy.uploadToken(mac);
|
|
36
|
+
this.formUploader = new qiniu.form_up.FormUploader(config);
|
|
37
|
+
this.bucketManager = new qiniu.rs.BucketManager(mac, config);
|
|
38
|
+
}
|
|
39
|
+
putFile(key, filePath) {
|
|
40
|
+
const putExtra = new qiniu.form_up.PutExtra();
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
this.formUploader.putFile(
|
|
43
|
+
this.uploadToken,
|
|
44
|
+
key,
|
|
45
|
+
filePath,
|
|
46
|
+
putExtra,
|
|
47
|
+
function(respErr, respBody, respInfo) {
|
|
48
|
+
if (respErr) {
|
|
49
|
+
throw respErr;
|
|
50
|
+
}
|
|
51
|
+
if (respInfo.statusCode == 200) {
|
|
52
|
+
resolve(respInfo);
|
|
53
|
+
} else {
|
|
54
|
+
console.log(respInfo.statusCode);
|
|
55
|
+
console.log(respBody);
|
|
56
|
+
reject(respBody);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
getResouceList(prefix) {
|
|
63
|
+
const { bucket } = this.options;
|
|
64
|
+
var options = {
|
|
65
|
+
prefix
|
|
66
|
+
};
|
|
67
|
+
log("\u{1F5D3} \u6B63\u5728\u83B7\u53D6\u5386\u53F2\u6570\u636E...");
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
69
|
+
this.bucketManager.listPrefix(
|
|
70
|
+
bucket,
|
|
71
|
+
options,
|
|
72
|
+
function(err, respBody, respInfo) {
|
|
73
|
+
if (err) {
|
|
74
|
+
throw err;
|
|
75
|
+
}
|
|
76
|
+
if (respInfo.statusCode == 200) {
|
|
77
|
+
const resourceList = respBody.items.map((item) => item.key);
|
|
78
|
+
log("\u{1F44F} \u83B7\u53D6\u5386\u53F2\u6570\u636E\u6210\u529F\uFF0C\u6B63\u5728\u5BF9\u6BD4\u6587\u4EF6...\n");
|
|
79
|
+
resolve(resourceList);
|
|
80
|
+
} else {
|
|
81
|
+
console.log(respInfo.statusCode);
|
|
82
|
+
console.log(respBody);
|
|
83
|
+
reject(respBody);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
batchDeleteFile(filenameAry) {
|
|
90
|
+
const { bucket } = this.options;
|
|
91
|
+
let deleteOperations = [];
|
|
92
|
+
if (filenameAry && filenameAry.length > 1) {
|
|
93
|
+
deleteOperations = filenameAry.map(
|
|
94
|
+
(filename) => qiniu.rs.deleteOp(bucket, filename)
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
log("\u{1F921} \u6B63\u5728\u5220\u9664\u6587\u4EF6...");
|
|
98
|
+
return new Promise((resolve, reject) => {
|
|
99
|
+
this.bucketManager.batch(
|
|
100
|
+
deleteOperations,
|
|
101
|
+
function(err, respBody, respInfo) {
|
|
102
|
+
if (err) {
|
|
103
|
+
reject(err);
|
|
104
|
+
throw err;
|
|
105
|
+
} else {
|
|
106
|
+
log("\u{1F44F} \u5220\u9664\u5B8C\u6210\uFF01\n");
|
|
107
|
+
resolve(respInfo);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
var qiniu_default = Qiniu;
|
|
115
|
+
|
|
116
|
+
// src/utils/utils.ts
|
|
117
|
+
import path from "path";
|
|
118
|
+
var getProjectName = () => path.basename(process.cwd());
|
|
119
|
+
|
|
120
|
+
// src/npm_plugin/npmQiniuPlugin.ts
|
|
121
|
+
var projectName = getProjectName();
|
|
122
|
+
var NpmQiuniuPlugin = class {
|
|
123
|
+
options;
|
|
124
|
+
qiniu;
|
|
125
|
+
constructor(options) {
|
|
126
|
+
this.qiniu = new qiniu_default(options);
|
|
127
|
+
this.options = options;
|
|
128
|
+
if (options.rootName) {
|
|
129
|
+
projectName = options.rootName;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
apply({
|
|
133
|
+
filePathAry,
|
|
134
|
+
outputPath
|
|
135
|
+
}) {
|
|
136
|
+
this.batchUpload(filePathAry, outputPath);
|
|
137
|
+
}
|
|
138
|
+
async batchUpload(filePathAry, buildPath) {
|
|
139
|
+
const { isLog } = this.options;
|
|
140
|
+
const uploadData = {};
|
|
141
|
+
filePathAry.forEach((filename) => {
|
|
142
|
+
uploadData[`${projectName}/${filename}`] = `${buildPath}/${filename}`;
|
|
143
|
+
});
|
|
144
|
+
const uploadAry = await this.batchDelete(Object.keys(uploadData));
|
|
145
|
+
const len = uploadAry.length;
|
|
146
|
+
const maxIndex = len - 1;
|
|
147
|
+
if (len === 0) {
|
|
148
|
+
log(`\u{1F62D} \u6CA1\u6709\u53D1\u73B0\u9700\u8981\u4E0A\u4F20\u7684\u6587\u4EF6
|
|
149
|
+
`);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
log(`\u2B06\uFE0F \u5C06\u4E0A\u4F20 ${len} \u4E2A\u6587\u4EF6`);
|
|
153
|
+
uploadAry.forEach(async (key, i) => {
|
|
154
|
+
const filePath = uploadData[key];
|
|
155
|
+
isLog && log(`\u{1F680} \u6B63\u5728\u4E0A\u4F20\u7B2C ${i + 1} \u4E2A\u6587\u4EF6: ${key}`);
|
|
156
|
+
await this.qiniu.putFile(key, filePath);
|
|
157
|
+
if (maxIndex === i) {
|
|
158
|
+
log(`\u{1F44F} \u4E0A\u4F20\u5B8C\u6210\uFF01`);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
async batchDelete(uploadFilePathAry) {
|
|
163
|
+
const { forceDelete } = this.options;
|
|
164
|
+
const resourceList = await this.qiniu.getResouceList(projectName);
|
|
165
|
+
const deleteAry = forceDelete ? resourceList : difference(resourceList, uploadFilePathAry);
|
|
166
|
+
const uploadAry = forceDelete ? uploadFilePathAry : difference(uploadFilePathAry, resourceList);
|
|
167
|
+
if (deleteAry.length > 0) {
|
|
168
|
+
await this.qiniu.batchDeleteFile(deleteAry);
|
|
169
|
+
}
|
|
170
|
+
return uploadAry;
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
var npmQiniuPlugin_default = NpmQiuniuPlugin;
|
|
174
|
+
|
|
175
|
+
// src/vite_plugin/vitePluginQiniu.ts
|
|
176
|
+
var PLUGIN_NAME = "vite-plugin-upload-oss-qiniu";
|
|
177
|
+
function vitePluginQiniu(options) {
|
|
178
|
+
const qiniu2 = new npmQiniuPlugin_default(options);
|
|
179
|
+
let outputPath = "";
|
|
180
|
+
const getFilePaths = (pathName) => {
|
|
181
|
+
const files = fs.readdirSync(pathName);
|
|
182
|
+
return files.map((item) => {
|
|
183
|
+
const filePath = `${pathName}/${item}`;
|
|
184
|
+
const stat = fs.lstatSync(filePath);
|
|
185
|
+
if (stat.isDirectory()) {
|
|
186
|
+
return getFilePaths(filePath);
|
|
187
|
+
} else {
|
|
188
|
+
return pathName === outputPath ? item : filePath.replace(`${outputPath}/`, "");
|
|
189
|
+
}
|
|
190
|
+
}).flat();
|
|
191
|
+
};
|
|
192
|
+
return {
|
|
193
|
+
name: PLUGIN_NAME,
|
|
194
|
+
apply: "build",
|
|
195
|
+
configResolved: async (config) => {
|
|
196
|
+
outputPath = config.build.outDir;
|
|
197
|
+
},
|
|
198
|
+
closeBundle() {
|
|
199
|
+
const filePathAry = getFilePaths(outputPath);
|
|
200
|
+
qiniu2.apply({
|
|
201
|
+
filePathAry,
|
|
202
|
+
outputPath
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// src/webpack_plugin/webpackPluginQiniu.ts
|
|
209
|
+
var PLUGIN_NAME2 = "webpack-plugin-upload-oss-qiniu";
|
|
210
|
+
var WebpackPluginQiniu = class {
|
|
211
|
+
qiniu;
|
|
212
|
+
constructor(options) {
|
|
213
|
+
this.qiniu = new npmQiniuPlugin_default(options);
|
|
214
|
+
}
|
|
215
|
+
apply(compiler) {
|
|
216
|
+
compiler.hooks.afterEmit.tapAsync(
|
|
217
|
+
PLUGIN_NAME2,
|
|
218
|
+
async (compilation, callback) => {
|
|
219
|
+
const filePathAry = Object.keys(compilation.assets);
|
|
220
|
+
const outputPath = compilation.options.output.path;
|
|
221
|
+
callback();
|
|
222
|
+
this.qiniu.apply({ filePathAry, outputPath });
|
|
223
|
+
}
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
var webpackPluginQiniu_default = WebpackPluginQiniu;
|
|
228
|
+
export {
|
|
229
|
+
webpackPluginQiniu_default as WebpackPluginQiniu,
|
|
230
|
+
vitePluginQiniu
|
|
231
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "upload-assets-oss",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"typings": "./dist/index.d.ts",
|
|
@@ -13,8 +13,9 @@
|
|
|
13
13
|
},
|
|
14
14
|
"homepage": "https://github.com/jialongsu/upload-assets-oss#readme",
|
|
15
15
|
"scripts": {
|
|
16
|
-
"dev": "yarn build -- --watch --ignore-watch examples",
|
|
17
|
-
"build": "tsc -p tsconfig.json"
|
|
16
|
+
"dev": "yarn build:tsup -- --watch --ignore-watch examples",
|
|
17
|
+
"build": "tsc -p tsconfig.json",
|
|
18
|
+
"build:tsup": "tsup src/index.ts --format esm,cjs --dts --clean"
|
|
18
19
|
},
|
|
19
20
|
"dependencies": {
|
|
20
21
|
"chalk": "^4.1.2",
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @Author: Arno.su
|
|
3
|
-
* @Date: 2022-10-21 13:31:59
|
|
4
|
-
* @LastEditors: Arno.su
|
|
5
|
-
* @LastEditTime: 2022-10-21 16:56:10
|
|
6
|
-
*/
|
|
7
|
-
import difference from 'lodash/difference';
|
|
8
|
-
import Qiniu from '../oss_plugin/qiniu';
|
|
9
|
-
import { log } from '../utils/log';
|
|
10
|
-
import { getProjectName } from '../utils/utils';
|
|
11
|
-
let projectName = getProjectName();
|
|
12
|
-
class NpmQiuniuPlugin {
|
|
13
|
-
options;
|
|
14
|
-
qiniu;
|
|
15
|
-
constructor(options) {
|
|
16
|
-
this.qiniu = new Qiniu(options);
|
|
17
|
-
this.options = options;
|
|
18
|
-
if (options.rootName) {
|
|
19
|
-
projectName = options.rootName;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
apply({ filePathAry, outputPath, }) {
|
|
23
|
-
this.batchUpload(filePathAry, outputPath);
|
|
24
|
-
}
|
|
25
|
-
async batchUpload(filePathAry, buildPath) {
|
|
26
|
-
const { isLog } = this.options;
|
|
27
|
-
const uploadData = {};
|
|
28
|
-
filePathAry.forEach((filename) => {
|
|
29
|
-
uploadData[`${projectName}/${filename}`] = `${buildPath}/${filename}`;
|
|
30
|
-
});
|
|
31
|
-
const uploadAry = await this.batchDelete(Object.keys(uploadData));
|
|
32
|
-
const len = uploadAry.length;
|
|
33
|
-
const maxIndex = len - 1;
|
|
34
|
-
if (len === 0) {
|
|
35
|
-
log(`😭 没有发现需要上传的文件 \n`);
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
log(`⬆️ 将上传 ${len} 个文件`);
|
|
39
|
-
uploadAry.forEach(async (key, i) => {
|
|
40
|
-
const filePath = uploadData[key];
|
|
41
|
-
isLog && log(`🚀 正在上传第 ${i + 1} 个文件: ${key}`);
|
|
42
|
-
await this.qiniu.putFile(key, filePath);
|
|
43
|
-
if (maxIndex === i) {
|
|
44
|
-
log(`👏 上传完成!`);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
async batchDelete(uploadFilePathAry) {
|
|
49
|
-
const { forceDelete } = this.options;
|
|
50
|
-
const resourceList = await this.qiniu.getResouceList(projectName); // 获取之前上传七牛的文件
|
|
51
|
-
const deleteAry = forceDelete
|
|
52
|
-
? resourceList
|
|
53
|
-
: difference(resourceList, uploadFilePathAry); // 获取需要先在七牛上删除的文件
|
|
54
|
-
const uploadAry = forceDelete
|
|
55
|
-
? uploadFilePathAry
|
|
56
|
-
: difference(uploadFilePathAry, resourceList); // 获取需要上传的文件
|
|
57
|
-
if (deleteAry.length > 0) {
|
|
58
|
-
await this.qiniu.batchDeleteFile(deleteAry); // 删除文件
|
|
59
|
-
}
|
|
60
|
-
return uploadAry;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
export default NpmQiuniuPlugin;
|
package/dist/oss_plugin/qiniu.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import qiniu from 'qiniu';
|
|
2
|
-
import { log } from '../utils/log';
|
|
3
|
-
class Qiniu {
|
|
4
|
-
options = {
|
|
5
|
-
accessKey: '',
|
|
6
|
-
secretKey: '',
|
|
7
|
-
bucket: '',
|
|
8
|
-
};
|
|
9
|
-
uploadToken;
|
|
10
|
-
formUploader;
|
|
11
|
-
bucketManager;
|
|
12
|
-
constructor(options) {
|
|
13
|
-
const { accessKey, secretKey, bucket } = options;
|
|
14
|
-
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
|
|
15
|
-
const _options = {
|
|
16
|
-
scope: bucket,
|
|
17
|
-
};
|
|
18
|
-
const putPolicy = new qiniu.rs.PutPolicy(_options);
|
|
19
|
-
const config = new qiniu.conf.Config();
|
|
20
|
-
this.options = options;
|
|
21
|
-
this.uploadToken = putPolicy.uploadToken(mac);
|
|
22
|
-
this.formUploader = new qiniu.form_up.FormUploader(config);
|
|
23
|
-
this.bucketManager = new qiniu.rs.BucketManager(mac, config);
|
|
24
|
-
}
|
|
25
|
-
putFile(key, filePath) {
|
|
26
|
-
const putExtra = new qiniu.form_up.PutExtra();
|
|
27
|
-
return new Promise((resolve, reject) => {
|
|
28
|
-
this.formUploader.putFile(this.uploadToken, key, filePath, putExtra, function (respErr, respBody, respInfo) {
|
|
29
|
-
if (respErr) {
|
|
30
|
-
throw respErr;
|
|
31
|
-
}
|
|
32
|
-
if (respInfo.statusCode == 200) {
|
|
33
|
-
resolve(respInfo);
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
console.log(respInfo.statusCode);
|
|
37
|
-
console.log(respBody);
|
|
38
|
-
reject(respBody);
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
getResouceList(prefix) {
|
|
44
|
-
const { bucket } = this.options;
|
|
45
|
-
var options = {
|
|
46
|
-
// limit: 10,
|
|
47
|
-
prefix,
|
|
48
|
-
};
|
|
49
|
-
log('🗓 正在获取历史数据...');
|
|
50
|
-
return new Promise((resolve, reject) => {
|
|
51
|
-
this.bucketManager.listPrefix(bucket, options, function (err, respBody, respInfo) {
|
|
52
|
-
if (err) {
|
|
53
|
-
throw err;
|
|
54
|
-
}
|
|
55
|
-
if (respInfo.statusCode == 200) {
|
|
56
|
-
const resourceList = respBody.items.map((item) => item.key);
|
|
57
|
-
log('👏 获取历史数据成功,正在对比文件...\n');
|
|
58
|
-
resolve(resourceList);
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
console.log(respInfo.statusCode);
|
|
62
|
-
console.log(respBody);
|
|
63
|
-
reject(respBody);
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
batchDeleteFile(filenameAry) {
|
|
69
|
-
const { bucket } = this.options;
|
|
70
|
-
let deleteOperations = [];
|
|
71
|
-
if (filenameAry && filenameAry.length > 1) {
|
|
72
|
-
deleteOperations = filenameAry.map((filename) => qiniu.rs.deleteOp(bucket, filename));
|
|
73
|
-
}
|
|
74
|
-
log('🤡 正在删除文件...');
|
|
75
|
-
return new Promise((resolve, reject) => {
|
|
76
|
-
this.bucketManager.batch(deleteOperations, function (err, respBody, respInfo) {
|
|
77
|
-
if (err) {
|
|
78
|
-
reject(err);
|
|
79
|
-
throw err;
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
log('👏 删除完成!\n');
|
|
83
|
-
resolve(respInfo);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
export default Qiniu;
|
package/dist/utils/log.js
DELETED
package/dist/utils/utils.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @Author: Arno.su
|
|
3
|
-
* @Date: 2022-10-21 13:28:31
|
|
4
|
-
* @LastEditors: Arno.su
|
|
5
|
-
* @LastEditTime: 2022-10-21 13:37:09
|
|
6
|
-
*/
|
|
7
|
-
import path from 'path';
|
|
8
|
-
/**
|
|
9
|
-
* 获取项目名称
|
|
10
|
-
* @returns string
|
|
11
|
-
*/
|
|
12
|
-
export const getProjectName = () => path.basename(process.cwd());
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import NpmQiniuPlugin from '../npm_plugin/npmQiniuPlugin';
|
|
3
|
-
const PLUGIN_NAME = 'vite-plugin-upload-oss-qiniu';
|
|
4
|
-
export default function vitePluginQiniu(options) {
|
|
5
|
-
const qiniu = new NpmQiniuPlugin(options);
|
|
6
|
-
let outputPath = '';
|
|
7
|
-
const getFilePaths = (pathName) => {
|
|
8
|
-
const files = fs.readdirSync(pathName);
|
|
9
|
-
return files
|
|
10
|
-
.map((item) => {
|
|
11
|
-
const filePath = `${pathName}/${item}`;
|
|
12
|
-
const stat = fs.lstatSync(filePath);
|
|
13
|
-
if (stat.isDirectory()) {
|
|
14
|
-
return getFilePaths(filePath);
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
return pathName === outputPath
|
|
18
|
-
? item
|
|
19
|
-
: filePath.replace(`${outputPath}/`, '');
|
|
20
|
-
}
|
|
21
|
-
})
|
|
22
|
-
.flat();
|
|
23
|
-
};
|
|
24
|
-
return {
|
|
25
|
-
name: PLUGIN_NAME,
|
|
26
|
-
apply: 'build',
|
|
27
|
-
configResolved: async (config) => {
|
|
28
|
-
outputPath = config.build.outDir;
|
|
29
|
-
},
|
|
30
|
-
closeBundle() {
|
|
31
|
-
const filePathAry = getFilePaths(outputPath);
|
|
32
|
-
qiniu.apply({
|
|
33
|
-
filePathAry,
|
|
34
|
-
outputPath,
|
|
35
|
-
});
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import NpmQiniuPlugin from '../npm_plugin/npmQiniuPlugin';
|
|
2
|
-
const PLUGIN_NAME = 'webpack-plugin-upload-oss-qiniu';
|
|
3
|
-
class WebpackPluginQiniu {
|
|
4
|
-
qiniu;
|
|
5
|
-
constructor(options) {
|
|
6
|
-
this.qiniu = new NpmQiniuPlugin(options);
|
|
7
|
-
}
|
|
8
|
-
apply(compiler) {
|
|
9
|
-
compiler.hooks.afterEmit.tapAsync(PLUGIN_NAME, async (compilation, callback) => {
|
|
10
|
-
const filePathAry = Object.keys(compilation.assets);
|
|
11
|
-
const outputPath = compilation.options.output.path;
|
|
12
|
-
callback();
|
|
13
|
-
this.qiniu.apply({ filePathAry, outputPath });
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
export default WebpackPluginQiniu;
|