upload-assets-oss 0.0.1

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/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # Vue 3 + TypeScript + Vite
2
+
3
+ This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
4
+
5
+ ## Recommended IDE Setup
6
+
7
+ - [VS Code](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar)
8
+
9
+ ## Type Support For `.vue` Imports in TS
10
+
11
+ Since TypeScript cannot handle type information for `.vue` imports, they are shimmed to be a generic Vue component type by default. In most cases this is fine if you don't really care about component prop types outside of templates. However, if you wish to get actual prop types in `.vue` imports (for example to get props validation when using manual `h(...)` calls), you can enable Volar's Take Over mode by following these steps:
12
+
13
+ 1. Run `Extensions: Show Built-in Extensions` from VS Code's command palette, look for `TypeScript and JavaScript Language Features`, then right click and select `Disable (Workspace)`. By default, Take Over mode will enable itself if the default TypeScript extension is disabled.
14
+ 2. Reload the VS Code window by running `Developer: Reload Window` from the command palette.
15
+
16
+ You can learn more about Take Over mode [here](https://github.com/johnsoncodehk/volar/discussions/471).
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_lodash_es = __toESM(require("lodash-es"), 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 : import_lodash_es.default.difference(resourceList, uploadFilePathAry);
199
+ const uploadAry = forceDelete ? uploadFilePathAry : import_lodash_es.default.difference(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
+ });
@@ -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 ADDED
@@ -0,0 +1,231 @@
1
+ // src/vite_plugin/vitePluginQiniu.ts
2
+ import fs from "fs";
3
+
4
+ // src/npm_plugin/npmQiniuPlugin.ts
5
+ import lodash from "lodash-es";
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 : lodash.difference(resourceList, uploadFilePathAry);
166
+ const uploadAry = forceDelete ? uploadFilePathAry : lodash.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
+ };