@pisell/common 0.0.34 → 0.0.35

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.
@@ -7,11 +7,20 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try
7
7
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
8
8
  import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
9
9
  import fs from 'fs';
10
+ import mime from 'mime-types';
10
11
  import path from 'path';
11
12
  import { getServerConfig } from "../utils/index.js";
12
13
  var filterFiles = ['index.html', 'umi.server.js', 'umi.server.d.ts'];
13
14
  var s3Client = null;
14
15
  var bucketName = '';
16
+
17
+ /*******
18
+ * @title: 初始化
19
+ * @description:
20
+ * @return {*}
21
+ * @Author: zhiwei.Wang
22
+ * @Date: 2023-11-24 14:05
23
+ */
15
24
  var init = /*#__PURE__*/function () {
16
25
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
17
26
  var serverConfig, awsConfig;
@@ -23,12 +32,15 @@ var init = /*#__PURE__*/function () {
23
32
  return getServerConfig();
24
33
  case 3:
25
34
  serverConfig = _context.sent;
35
+ // aws配置
26
36
  awsConfig = serverConfig.aws;
27
37
  if (!awsConfig) {
28
38
  _context.next = 12;
29
39
  break;
30
40
  }
31
41
  bucketName = awsConfig.bucketName;
42
+
43
+ // 创建实例
32
44
  s3Client = new S3Client({
33
45
  region: awsConfig.region,
34
46
  credentials: {
@@ -63,48 +75,57 @@ var init = /*#__PURE__*/function () {
63
75
  // 上传文件到S3指定目录
64
76
  var uploadFile = /*#__PURE__*/function () {
65
77
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(directory, fileKey, filePath) {
66
- var fileStream, uploadParams, command, response;
78
+ var fileStream, contentType, uploadParams, command, response;
67
79
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
68
80
  while (1) switch (_context2.prev = _context2.next) {
69
81
  case 0:
70
- fileStream = fs.createReadStream(filePath);
82
+ fileStream = fs.createReadStream(filePath); // 根据文件扩展名获取MIME类型
83
+ contentType = mime.lookup(filePath);
71
84
  uploadParams = {
72
85
  Bucket: bucketName,
73
86
  Key: "static/".concat(directory, "/").concat(fileKey),
74
87
  // 指定目录路径
75
- Body: fileStream
88
+ Body: fileStream,
89
+ contentType: contentType,
90
+ ACL: 'public-read' // 设置ACL权限为公共读取
76
91
  };
77
92
  command = new PutObjectCommand(uploadParams);
78
- _context2.prev = 3;
79
- _context2.next = 6;
93
+ _context2.prev = 4;
94
+ _context2.next = 7;
80
95
  return s3Client.send(command);
81
- case 6:
96
+ case 7:
82
97
  response = _context2.sent;
83
98
  console.log("\u4E0A\u4F20\u6210\u529F: ".concat(fileKey));
84
99
  return _context2.abrupt("return", {
85
100
  fileName: fileKey,
86
101
  location: response.Location
87
102
  });
88
- case 11:
89
- _context2.prev = 11;
90
- _context2.t0 = _context2["catch"](3);
103
+ case 12:
104
+ _context2.prev = 12;
105
+ _context2.t0 = _context2["catch"](4);
91
106
  console.error("\u4E0A\u4F20\u5931\u8D25: ".concat(fileKey), _context2.t0);
92
107
  return _context2.abrupt("return", {
93
108
  fileName: fileKey,
94
109
  error: _context2.t0
95
110
  });
96
- case 15:
111
+ case 16:
97
112
  case "end":
98
113
  return _context2.stop();
99
114
  }
100
- }, _callee2, null, [[3, 11]]);
115
+ }, _callee2, null, [[4, 12]]);
101
116
  }));
102
117
  return function uploadFile(_x, _x2, _x3) {
103
118
  return _ref2.apply(this, arguments);
104
119
  };
105
120
  }();
106
121
 
107
- // 递归上传文件夹内容
122
+ /*******
123
+ * @title: 递归上传文件夹内容
124
+ * @description:
125
+ * @return {*}
126
+ * @Author: zhiwei.Wang
127
+ * @Date: 2023-11-24 14:06
128
+ */
108
129
  export var uploadFolderContents = /*#__PURE__*/function () {
109
130
  var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(directory) {
110
131
  var folderPath,
@@ -124,20 +145,21 @@ export var uploadFolderContents = /*#__PURE__*/function () {
124
145
  while (1) switch (_context3.prev = _context3.next) {
125
146
  case 0:
126
147
  folderPath = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : path.resolve(__dirname, 'dist');
127
- files = fs.readdirSync(folderPath);
148
+ files = fs.readdirSync(folderPath); // promise all 储存
128
149
  uploadPromises = [];
129
150
  _iterator = _createForOfIteratorHelper(files);
130
151
  try {
131
152
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
132
153
  file = _step.value;
133
154
  filePath = path.join(folderPath, file);
134
- fileStats = fs.statSync(filePath);
155
+ fileStats = fs.statSync(filePath); // 文件上传, 文件夹递归循环
135
156
  if (fileStats.isFile()) {
136
- fileKey = path.relative(folderPath, filePath);
157
+ fileKey = path.relative(folderPath, filePath); // 过滤不需要上传的文件
137
158
  if (!filterFiles.includes(fileKey)) {
138
159
  uploadPromise = uploadFile(directory, fileKey, filePath);
139
160
  uploadPromises.push(uploadPromise);
140
161
  }
162
+ // 文件夹
141
163
  } else if (fileStats.isDirectory()) {
142
164
  subDirectory = path.join(directory, file);
143
165
  subFolderPromise = uploadFolderContents(subDirectory, filePath);
@@ -163,6 +185,14 @@ export var uploadFolderContents = /*#__PURE__*/function () {
163
185
  return _ref3.apply(this, arguments);
164
186
  };
165
187
  }();
188
+
189
+ /*******
190
+ * @title: 上传资源到oss
191
+ * @description:
192
+ * @return {*}
193
+ * @Author: zhiwei.Wang
194
+ * @Date: 2023-11-24 14:04
195
+ */
166
196
  export var uploadStaticToOSS = /*#__PURE__*/function () {
167
197
  var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(directory) {
168
198
  var folderPath,
@@ -81,7 +81,7 @@ function init() {
81
81
  */
82
82
  function _init() {
83
83
  _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
84
- var res, uploadCode, version, currentUserName, result, conn, releaseInstance;
84
+ var res, uploadCode, version, currentUserName, result, _fileName, conn, releaseInstance;
85
85
  return _regeneratorRuntime().wrap(function _callee$(_context) {
86
86
  while (1) switch (_context.prev = _context.next) {
87
87
  case 0:
@@ -151,35 +151,40 @@ function _init() {
151
151
  return uploadCode.build();
152
152
  case 30:
153
153
  console.log('✅ 打包完成');
154
- if (!(res.env === 'prod')) {
155
- _context.next = 34;
154
+ if (!(res.env === 'pre' || res.env === 'prod')) {
155
+ _context.next = 36;
156
156
  break;
157
157
  }
158
- _context.next = 34;
159
- return uploadStaticToOSS(res.project.fileName);
160
- case 34:
158
+ _fileName = res.project.fileName;
159
+ if (res.env === 'pre') {
160
+ _fileName = 'pre_' + _fileName;
161
+ }
162
+ // 上传oss
163
+ _context.next = 36;
164
+ return uploadStaticToOSS(_fileName);
165
+ case 36:
161
166
  console.log('开始压缩');
162
- _context.next = 37;
167
+ _context.next = 39;
163
168
  return uploadCode.compress();
164
- case 37:
169
+ case 39:
165
170
  console.log('✅ 压缩完成');
166
171
  console.log('开始连接服务器');
167
- _context.next = 41;
172
+ _context.next = 43;
168
173
  return uploadCode.connectServer();
169
- case 41:
174
+ case 43:
170
175
  conn = _context.sent;
171
176
  console.log('✅ 连接服务器完成');
172
177
  console.log('开始上传');
173
- _context.next = 46;
178
+ _context.next = 48;
174
179
  return uploadCode.serverUpload();
175
- case 46:
180
+ case 48:
176
181
  console.log('✅ 上传完成');
177
182
  console.log('开始解压');
178
- _context.next = 50;
183
+ _context.next = 52;
179
184
  return uploadCode.serverUnpack();
180
- case 50:
185
+ case 52:
181
186
  if (!res.is_release) {
182
- _context.next = 55;
187
+ _context.next = 57;
183
188
  break;
184
189
  }
185
190
  releaseInstance = new Release({
@@ -189,32 +194,32 @@ function _init() {
189
194
  conn: conn
190
195
  });
191
196
  console.log('开始发布');
192
- _context.next = 55;
193
- return releaseInstance.release();
194
- case 55:
195
197
  _context.next = 57;
196
- return uploadCode.disconnectServer();
198
+ return releaseInstance.release();
197
199
  case 57:
198
200
  _context.next = 59;
199
- return $(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["git pull --no-edit"])));
201
+ return uploadCode.disconnectServer();
200
202
  case 59:
201
203
  _context.next = 61;
202
- return $(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["git push --follow-tags"])));
204
+ return $(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["git pull --no-edit"])));
203
205
  case 61:
206
+ _context.next = 63;
207
+ return $(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["git push --follow-tags"])));
208
+ case 63:
204
209
  console.log('✅ 成功后推送到git');
205
- _context.next = 73;
210
+ _context.next = 75;
206
211
  break;
207
- case 64:
208
- _context.prev = 64;
212
+ case 66:
213
+ _context.prev = 66;
209
214
  _context.t0 = _context["catch"](26);
210
215
  console.log(_context.t0, '执行出错 请排查重试');
211
- _context.next = 69;
212
- return $(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["git reset HEAD~1"])));
213
- case 69:
214
216
  _context.next = 71;
215
- return $(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["git tag -d v", ""])), uploadCode.currentVersion);
217
+ return $(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["git reset HEAD~1"])));
216
218
  case 71:
217
219
  _context.next = 73;
220
+ return $(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["git tag -d v", ""])), uploadCode.currentVersion);
221
+ case 73:
222
+ _context.next = 75;
218
223
  return sendWebhook({
219
224
  title: "".concat(res.project.fileName, " ").concat(res.env, "\u73AF\u5883").concat(res.is_release ? '上传并发布' : '上传', "\u5931\u8D25"),
220
225
  content: [{
@@ -228,18 +233,18 @@ function _init() {
228
233
  value: version || ''
229
234
  }]
230
235
  });
231
- case 73:
236
+ case 75:
232
237
  if (!(res.project.fileName === 'my_pisel_shop')) {
233
- _context.next = 76;
238
+ _context.next = 78;
234
239
  break;
235
240
  }
236
- _context.next = 76;
241
+ _context.next = 78;
237
242
  return updateVersion({
238
243
  version: version,
239
244
  changelog: res.changelog
240
245
  }, res.env);
241
- case 76:
242
- _context.next = 78;
246
+ case 78:
247
+ _context.next = 80;
243
248
  return sendWebhook({
244
249
  title: "".concat(res.project.fileName, " ").concat(res.env, "\u73AF\u5883").concat(res.is_release ? '上传并发布' : '上传', "\u6210\u529F"),
245
250
  content: [{
@@ -253,13 +258,13 @@ function _init() {
253
258
  value: version || ''
254
259
  }]
255
260
  });
256
- case 78:
261
+ case 80:
257
262
  uploadCode.deleteLocalZip();
258
- case 79:
263
+ case 81:
259
264
  case "end":
260
265
  return _context.stop();
261
266
  }
262
- }, _callee, null, [[26, 64]]);
267
+ }, _callee, null, [[26, 66]]);
263
268
  }));
264
269
  return _init.apply(this, arguments);
265
270
  }
@@ -35,6 +35,7 @@ __export(aws_exports, {
35
35
  module.exports = __toCommonJS(aws_exports);
36
36
  var import_client_s3 = require("@aws-sdk/client-s3");
37
37
  var import_fs = __toESM(require("fs"));
38
+ var import_mime_types = __toESM(require("mime-types"));
38
39
  var import_path = __toESM(require("path"));
39
40
  var import_utils = require("../utils/index.js");
40
41
  var filterFiles = ["index.html", "umi.server.js", "umi.server.d.ts"];
@@ -66,11 +67,15 @@ var init = async () => {
66
67
  };
67
68
  var uploadFile = async (directory, fileKey, filePath) => {
68
69
  const fileStream = import_fs.default.createReadStream(filePath);
70
+ const contentType = import_mime_types.default.lookup(filePath);
69
71
  const uploadParams = {
70
72
  Bucket: bucketName,
71
73
  Key: `static/${directory}/${fileKey}`,
72
74
  // 指定目录路径
73
- Body: fileStream
75
+ Body: fileStream,
76
+ contentType,
77
+ ACL: "public-read"
78
+ // 设置ACL权限为公共读取
74
79
  };
75
80
  const command = new import_client_s3.PutObjectCommand(uploadParams);
76
81
  try {
@@ -132,8 +132,12 @@ async function init() {
132
132
  console.log("开始打包");
133
133
  await uploadCode.build();
134
134
  console.log("✅ 打包完成");
135
- if (res.env === "prod") {
136
- await (0, import_aws.uploadStaticToOSS)(res.project.fileName);
135
+ if (res.env === "pre" || res.env === "prod") {
136
+ let _fileName = res.project.fileName;
137
+ if (res.env === "pre") {
138
+ _fileName = "pre_" + _fileName;
139
+ }
140
+ await (0, import_aws.uploadStaticToOSS)(_fileName);
137
141
  }
138
142
  console.log("开始压缩");
139
143
  await uploadCode.compress();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pisell/common",
3
- "version": "0.0.34",
3
+ "version": "0.0.35",
4
4
  "description": "A collection of reusable UI components for web development",
5
5
  "license": "MIT",
6
6
  "sideEffects": [
@@ -64,6 +64,7 @@
64
64
  "array-move": "^3.0.1",
65
65
  "dotenv": "^16.0.3",
66
66
  "inquirer": "^9.2.6",
67
+ "mime-types": "^2.1.35",
67
68
  "npm-check": "^6.0.1",
68
69
  "react-beautiful-dnd": "^13.1.0",
69
70
  "react-color": "^2.19.3",