@iflyrpa/share 0.0.2 → 0.0.4
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/{src/index.ts → dist/index.d.ts} +0 -1
- package/dist/index.js +201 -0
- package/dist/index.mjs +113 -134
- package/dist/types.d.ts +72 -0
- package/dist/utils.d.ts +14 -0
- package/package.json +8 -9
- package/dist/index.cjs +0 -178
- package/src/types.ts +0 -78
- package/src/utils.ts +0 -211
package/dist/index.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.n = function(module) {
|
|
5
|
+
var getter = module && module.__esModule ? function() {
|
|
6
|
+
return module['default'];
|
|
7
|
+
} : function() {
|
|
8
|
+
return module;
|
|
9
|
+
};
|
|
10
|
+
__webpack_require__.d(getter, {
|
|
11
|
+
a: getter
|
|
12
|
+
});
|
|
13
|
+
return getter;
|
|
14
|
+
};
|
|
15
|
+
})();
|
|
16
|
+
(()=>{
|
|
17
|
+
__webpack_require__.d = function(exports1, definition) {
|
|
18
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: definition[key]
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
(()=>{
|
|
25
|
+
__webpack_require__.o = function(obj, prop) {
|
|
26
|
+
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
27
|
+
};
|
|
28
|
+
})();
|
|
29
|
+
(()=>{
|
|
30
|
+
__webpack_require__.r = function(exports1) {
|
|
31
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
32
|
+
value: 'Module'
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
35
|
+
value: true
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
})();
|
|
39
|
+
var __webpack_exports__ = {};
|
|
40
|
+
__webpack_require__.r(__webpack_exports__);
|
|
41
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
42
|
+
getFilenameFromUrl: ()=>getFilenameFromUrl,
|
|
43
|
+
semver: ()=>semver,
|
|
44
|
+
pathExists: ()=>pathExists,
|
|
45
|
+
success: ()=>success,
|
|
46
|
+
ensureFile: ()=>ensureFile,
|
|
47
|
+
ensureFileSync: ()=>ensureFileSync,
|
|
48
|
+
isNil: ()=>isNil,
|
|
49
|
+
downloadImage: ()=>downloadImage,
|
|
50
|
+
fetchJSON: ()=>fetchJSON,
|
|
51
|
+
sleep: ()=>sleep,
|
|
52
|
+
writeFile: ()=>writeFile
|
|
53
|
+
});
|
|
54
|
+
const external_node_fs_namespaceObject = require("node:fs");
|
|
55
|
+
var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject);
|
|
56
|
+
const external_node_https_namespaceObject = require("node:https");
|
|
57
|
+
var external_node_https_default = /*#__PURE__*/ __webpack_require__.n(external_node_https_namespaceObject);
|
|
58
|
+
const external_node_path_namespaceObject = require("node:path");
|
|
59
|
+
var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
|
|
60
|
+
function pathExists(path) {
|
|
61
|
+
return new Promise((resolve)=>{
|
|
62
|
+
external_node_fs_default().stat(path, (err)=>{
|
|
63
|
+
resolve(!err);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
function ensureFileSync(filePath) {
|
|
68
|
+
const dirPath = external_node_path_default().dirname(filePath);
|
|
69
|
+
try {
|
|
70
|
+
if (!external_node_fs_default().existsSync(dirPath)) external_node_fs_default().mkdirSync(dirPath, {
|
|
71
|
+
recursive: true
|
|
72
|
+
});
|
|
73
|
+
} catch (err) {
|
|
74
|
+
if (err instanceof Error) throw new Error(`Error creating directory: ${err.message}`);
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
if (!external_node_fs_default().existsSync(filePath)) external_node_fs_default().writeFileSync(filePath, "");
|
|
78
|
+
} catch (err) {
|
|
79
|
+
if (err instanceof Error) throw new Error(`Error creating file: ${err.message}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function writeFile(filePath, data) {
|
|
83
|
+
return new Promise((resolve, reject)=>{
|
|
84
|
+
external_node_fs_default().writeFile(filePath, data, (err)=>{
|
|
85
|
+
if (err) {
|
|
86
|
+
console.error("Error writing file:", err);
|
|
87
|
+
reject();
|
|
88
|
+
} else {
|
|
89
|
+
console.log("File written successfully");
|
|
90
|
+
resolve();
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
function compareVersions(v1, v2) {
|
|
96
|
+
const parts1 = v1.split(".").map(Number);
|
|
97
|
+
const parts2 = v2.split(".").map(Number);
|
|
98
|
+
for(let i = 0; i < Math.max(parts1.length, parts2.length); i++){
|
|
99
|
+
const num1 = i < parts1.length ? parts1[i] : 0;
|
|
100
|
+
const num2 = i < parts2.length ? parts2[i] : 0;
|
|
101
|
+
if (num1 > num2) return 1;
|
|
102
|
+
if (num1 < num2) return -1;
|
|
103
|
+
}
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
|
106
|
+
const semver = {
|
|
107
|
+
gt: (v1, v2)=>1 === compareVersions(v1, v2)
|
|
108
|
+
};
|
|
109
|
+
function fetchJSON(url) {
|
|
110
|
+
return new Promise((resolve, reject)=>{
|
|
111
|
+
external_node_https_default().get(url, (res)=>{
|
|
112
|
+
let data = "";
|
|
113
|
+
res.on("data", (chunk)=>{
|
|
114
|
+
data += chunk;
|
|
115
|
+
});
|
|
116
|
+
res.on("end", ()=>{
|
|
117
|
+
try {
|
|
118
|
+
const parsedData = JSON.parse(data);
|
|
119
|
+
resolve(parsedData);
|
|
120
|
+
} catch (e) {
|
|
121
|
+
if (e instanceof Error) reject(new Error(`Error parsing JSON: ${e.message}`));
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}).on("error", (err)=>{
|
|
125
|
+
reject(new Error(`Request failed: ${err.message}`));
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
function isNil(value) {
|
|
130
|
+
return null == value;
|
|
131
|
+
}
|
|
132
|
+
function ensureFile(filePath) {
|
|
133
|
+
return new Promise((resolve, reject)=>{
|
|
134
|
+
const dirPath = external_node_path_default().dirname(filePath);
|
|
135
|
+
external_node_fs_default().stat(dirPath, (err, stats)=>{
|
|
136
|
+
if (err) {
|
|
137
|
+
if ("ENOENT" !== err.code) return reject(err);
|
|
138
|
+
external_node_fs_default().mkdir(dirPath, {
|
|
139
|
+
recursive: true
|
|
140
|
+
}, (err)=>{
|
|
141
|
+
if (err) return reject(err);
|
|
142
|
+
createFile();
|
|
143
|
+
});
|
|
144
|
+
} else if (stats.isDirectory()) checkFile();
|
|
145
|
+
else reject(new Error(`${dirPath} is not a directory`));
|
|
146
|
+
});
|
|
147
|
+
function checkFile() {
|
|
148
|
+
external_node_fs_default().stat(filePath, (err, stats)=>{
|
|
149
|
+
if (err) {
|
|
150
|
+
if ("ENOENT" === err.code) createFile();
|
|
151
|
+
else reject(err);
|
|
152
|
+
} else if (stats.isFile()) resolve();
|
|
153
|
+
else reject(new Error(`${filePath} is not a file`));
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
function createFile() {
|
|
157
|
+
external_node_fs_default().writeFile(filePath, "", (err)=>{
|
|
158
|
+
if (err) reject(err);
|
|
159
|
+
else resolve();
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
async function downloadImage(url, savePath) {
|
|
165
|
+
await ensureFile(savePath);
|
|
166
|
+
return new Promise((resolve, reject)=>{
|
|
167
|
+
external_node_https_default().get(url, (response)=>{
|
|
168
|
+
if (200 === response.statusCode) {
|
|
169
|
+
const fileStream = external_node_fs_default().createWriteStream(savePath);
|
|
170
|
+
response.pipe(fileStream);
|
|
171
|
+
fileStream.on("finish", ()=>{
|
|
172
|
+
fileStream.close();
|
|
173
|
+
console.log("下载完成,文件已保存至:", savePath);
|
|
174
|
+
resolve(savePath);
|
|
175
|
+
});
|
|
176
|
+
} else {
|
|
177
|
+
console.log("图片下载失败:", response.statusCode);
|
|
178
|
+
response.resume();
|
|
179
|
+
reject();
|
|
180
|
+
}
|
|
181
|
+
}).on("error", (error)=>{
|
|
182
|
+
console.error("请求图片时发生错误:", error.message);
|
|
183
|
+
reject();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
function getFilenameFromUrl(imageUrl) {
|
|
188
|
+
const parsedUrl = new URL(imageUrl);
|
|
189
|
+
return external_node_path_default().basename(parsedUrl.pathname);
|
|
190
|
+
}
|
|
191
|
+
const sleep = (ms)=>new Promise((resolve)=>setTimeout(resolve, ms));
|
|
192
|
+
const success = (data, message)=>({
|
|
193
|
+
code: 0,
|
|
194
|
+
message: message || "发布成功",
|
|
195
|
+
data
|
|
196
|
+
});
|
|
197
|
+
var __webpack_export_target__ = exports;
|
|
198
|
+
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
199
|
+
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
|
|
200
|
+
value: true
|
|
201
|
+
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,162 +1,141 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__ from "node:fs";
|
|
2
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_https_626f33a7__ from "node:https";
|
|
3
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__ from "node:path";
|
|
4
|
+
function pathExists(path) {
|
|
5
|
+
return new Promise((resolve)=>{
|
|
6
|
+
__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].stat(path, (err)=>{
|
|
7
|
+
resolve(!err);
|
|
8
|
+
});
|
|
9
9
|
});
|
|
10
|
-
});
|
|
11
10
|
}
|
|
12
11
|
function ensureFileSync(filePath) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
throw new Error(`Error creating directory: ${err.message}`);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
try {
|
|
24
|
-
if (!fs.existsSync(filePath)) {
|
|
25
|
-
fs.writeFileSync(filePath, "");
|
|
12
|
+
const dirPath = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].dirname(filePath);
|
|
13
|
+
try {
|
|
14
|
+
if (!__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].existsSync(dirPath)) __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].mkdirSync(dirPath, {
|
|
15
|
+
recursive: true
|
|
16
|
+
});
|
|
17
|
+
} catch (err) {
|
|
18
|
+
if (err instanceof Error) throw new Error(`Error creating directory: ${err.message}`);
|
|
26
19
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
try {
|
|
21
|
+
if (!__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].existsSync(filePath)) __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].writeFileSync(filePath, "");
|
|
22
|
+
} catch (err) {
|
|
23
|
+
if (err instanceof Error) throw new Error(`Error creating file: ${err.message}`);
|
|
30
24
|
}
|
|
31
|
-
}
|
|
32
25
|
}
|
|
33
26
|
function writeFile(filePath, data) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
27
|
+
return new Promise((resolve, reject)=>{
|
|
28
|
+
__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].writeFile(filePath, data, (err)=>{
|
|
29
|
+
if (err) {
|
|
30
|
+
console.error("Error writing file:", err);
|
|
31
|
+
reject();
|
|
32
|
+
} else {
|
|
33
|
+
console.log("File written successfully");
|
|
34
|
+
resolve();
|
|
35
|
+
}
|
|
36
|
+
});
|
|
43
37
|
});
|
|
44
|
-
});
|
|
45
38
|
}
|
|
46
39
|
function compareVersions(v1, v2) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
return 0;
|
|
40
|
+
const parts1 = v1.split(".").map(Number);
|
|
41
|
+
const parts2 = v2.split(".").map(Number);
|
|
42
|
+
for(let i = 0; i < Math.max(parts1.length, parts2.length); i++){
|
|
43
|
+
const num1 = i < parts1.length ? parts1[i] : 0;
|
|
44
|
+
const num2 = i < parts2.length ? parts2[i] : 0;
|
|
45
|
+
if (num1 > num2) return 1;
|
|
46
|
+
if (num1 < num2) return -1;
|
|
47
|
+
}
|
|
48
|
+
return 0;
|
|
58
49
|
}
|
|
59
50
|
const semver = {
|
|
60
|
-
|
|
51
|
+
gt: (v1, v2)=>1 === compareVersions(v1, v2)
|
|
61
52
|
};
|
|
62
53
|
function fetchJSON(url) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
reject(new Error(`Request failed: ${err.message}`));
|
|
54
|
+
return new Promise((resolve, reject)=>{
|
|
55
|
+
__WEBPACK_EXTERNAL_MODULE_node_https_626f33a7__["default"].get(url, (res)=>{
|
|
56
|
+
let data = "";
|
|
57
|
+
res.on("data", (chunk)=>{
|
|
58
|
+
data += chunk;
|
|
59
|
+
});
|
|
60
|
+
res.on("end", ()=>{
|
|
61
|
+
try {
|
|
62
|
+
const parsedData = JSON.parse(data);
|
|
63
|
+
resolve(parsedData);
|
|
64
|
+
} catch (e) {
|
|
65
|
+
if (e instanceof Error) reject(new Error(`Error parsing JSON: ${e.message}`));
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}).on("error", (err)=>{
|
|
69
|
+
reject(new Error(`Request failed: ${err.message}`));
|
|
70
|
+
});
|
|
81
71
|
});
|
|
82
|
-
});
|
|
83
72
|
}
|
|
84
73
|
function isNil(value) {
|
|
85
|
-
|
|
74
|
+
return null == value;
|
|
86
75
|
}
|
|
87
76
|
function ensureFile(filePath) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
if (err) {
|
|
111
|
-
if (err.code === "ENOENT") {
|
|
112
|
-
createFile();
|
|
113
|
-
} else {
|
|
114
|
-
reject(err);
|
|
115
|
-
}
|
|
116
|
-
} else if (stats.isFile()) {
|
|
117
|
-
resolve();
|
|
118
|
-
} else {
|
|
119
|
-
reject(new Error(`${filePath} is not a file`));
|
|
77
|
+
return new Promise((resolve, reject)=>{
|
|
78
|
+
const dirPath = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].dirname(filePath);
|
|
79
|
+
__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].stat(dirPath, (err, stats)=>{
|
|
80
|
+
if (err) {
|
|
81
|
+
if ("ENOENT" !== err.code) return reject(err);
|
|
82
|
+
__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].mkdir(dirPath, {
|
|
83
|
+
recursive: true
|
|
84
|
+
}, (err)=>{
|
|
85
|
+
if (err) return reject(err);
|
|
86
|
+
createFile();
|
|
87
|
+
});
|
|
88
|
+
} else if (stats.isDirectory()) checkFile();
|
|
89
|
+
else reject(new Error(`${dirPath} is not a directory`));
|
|
90
|
+
});
|
|
91
|
+
function checkFile() {
|
|
92
|
+
__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].stat(filePath, (err, stats)=>{
|
|
93
|
+
if (err) {
|
|
94
|
+
if ("ENOENT" === err.code) createFile();
|
|
95
|
+
else reject(err);
|
|
96
|
+
} else if (stats.isFile()) resolve();
|
|
97
|
+
else reject(new Error(`${filePath} is not a file`));
|
|
98
|
+
});
|
|
120
99
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
reject(err);
|
|
127
|
-
} else {
|
|
128
|
-
resolve();
|
|
100
|
+
function createFile() {
|
|
101
|
+
__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].writeFile(filePath, "", (err)=>{
|
|
102
|
+
if (err) reject(err);
|
|
103
|
+
else resolve();
|
|
104
|
+
});
|
|
129
105
|
}
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
});
|
|
106
|
+
});
|
|
133
107
|
}
|
|
134
108
|
async function downloadImage(url, savePath) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
109
|
+
await ensureFile(savePath);
|
|
110
|
+
return new Promise((resolve, reject)=>{
|
|
111
|
+
__WEBPACK_EXTERNAL_MODULE_node_https_626f33a7__["default"].get(url, (response)=>{
|
|
112
|
+
if (200 === response.statusCode) {
|
|
113
|
+
const fileStream = __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].createWriteStream(savePath);
|
|
114
|
+
response.pipe(fileStream);
|
|
115
|
+
fileStream.on("finish", ()=>{
|
|
116
|
+
fileStream.close();
|
|
117
|
+
console.log("下载完成,文件已保存至:", savePath);
|
|
118
|
+
resolve(savePath);
|
|
119
|
+
});
|
|
120
|
+
} else {
|
|
121
|
+
console.log("图片下载失败:", response.statusCode);
|
|
122
|
+
response.resume();
|
|
123
|
+
reject();
|
|
124
|
+
}
|
|
125
|
+
}).on("error", (error)=>{
|
|
126
|
+
console.error("请求图片时发生错误:", error.message);
|
|
127
|
+
reject();
|
|
145
128
|
});
|
|
146
|
-
} else {
|
|
147
|
-
console.log("\u56FE\u7247\u4E0B\u8F7D\u5931\u8D25:", response.statusCode);
|
|
148
|
-
response.resume();
|
|
149
|
-
reject();
|
|
150
|
-
}
|
|
151
|
-
}).on("error", (error) => {
|
|
152
|
-
console.error("\u8BF7\u6C42\u56FE\u7247\u65F6\u53D1\u751F\u9519\u8BEF:", error.message);
|
|
153
|
-
reject();
|
|
154
129
|
});
|
|
155
|
-
});
|
|
156
130
|
}
|
|
157
131
|
function getFilenameFromUrl(imageUrl) {
|
|
158
|
-
|
|
159
|
-
|
|
132
|
+
const parsedUrl = new URL(imageUrl);
|
|
133
|
+
return __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].basename(parsedUrl.pathname);
|
|
160
134
|
}
|
|
161
|
-
|
|
162
|
-
|
|
135
|
+
const sleep = (ms)=>new Promise((resolve)=>setTimeout(resolve, ms));
|
|
136
|
+
const success = (data, message)=>({
|
|
137
|
+
code: 0,
|
|
138
|
+
message: message || "发布成功",
|
|
139
|
+
data
|
|
140
|
+
});
|
|
141
|
+
export { downloadImage, ensureFile, ensureFileSync, fetchJSON, getFilenameFromUrl, isNil, pathExists, semver, sleep, success, writeFile };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { Page } from "playwright-core";
|
|
2
|
+
export type { Locator, Response, ElectronApplication } from "playwright-core";
|
|
3
|
+
export interface CookiesSetDetails {
|
|
4
|
+
/**
|
|
5
|
+
* The URL to associate the cookie with. The promise will be rejected if the URL is
|
|
6
|
+
* invalid.
|
|
7
|
+
*/
|
|
8
|
+
url: string;
|
|
9
|
+
/**
|
|
10
|
+
* The name of the cookie. Empty by default if omitted.
|
|
11
|
+
*/
|
|
12
|
+
name?: string;
|
|
13
|
+
/**
|
|
14
|
+
* The value of the cookie. Empty by default if omitted.
|
|
15
|
+
*/
|
|
16
|
+
value?: string;
|
|
17
|
+
/**
|
|
18
|
+
* The domain of the cookie; this will be normalized with a preceding dot so that
|
|
19
|
+
* it's also valid for subdomains. Empty by default if omitted.
|
|
20
|
+
*/
|
|
21
|
+
domain?: string;
|
|
22
|
+
/**
|
|
23
|
+
* The path of the cookie. Empty by default if omitted.
|
|
24
|
+
*/
|
|
25
|
+
path?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Whether the cookie should be marked as Secure. Defaults to false unless Same
|
|
28
|
+
* Site=None attribute is used.
|
|
29
|
+
*/
|
|
30
|
+
secure?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Whether the cookie should be marked as HTTP only. Defaults to false.
|
|
33
|
+
*/
|
|
34
|
+
httpOnly?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* The expiration date of the cookie as the number of seconds since the UNIX epoch.
|
|
37
|
+
* If omitted then the cookie becomes a session cookie and will not be retained
|
|
38
|
+
* between sessions.
|
|
39
|
+
*/
|
|
40
|
+
expirationDate?: number;
|
|
41
|
+
/**
|
|
42
|
+
* The Same Site policy to apply to this cookie. Can be `unspecified`,
|
|
43
|
+
* `no_restriction`, `lax` or `strict`. Default is `lax`.
|
|
44
|
+
*/
|
|
45
|
+
sameSite?: "unspecified" | "no_restriction" | "lax" | "strict";
|
|
46
|
+
}
|
|
47
|
+
export type CookieMap = CookiesSetDetails[];
|
|
48
|
+
export interface PageParams {
|
|
49
|
+
cookies?: CookieMap;
|
|
50
|
+
url: string;
|
|
51
|
+
show?: boolean;
|
|
52
|
+
}
|
|
53
|
+
export interface LoggerImplement {
|
|
54
|
+
debug(...msg: unknown[]): void;
|
|
55
|
+
info(...msg: unknown[]): void;
|
|
56
|
+
warn(...msg: unknown[]): void;
|
|
57
|
+
error(prefix: string, err?: unknown): void;
|
|
58
|
+
}
|
|
59
|
+
export interface AutomateTask {
|
|
60
|
+
debug?: boolean;
|
|
61
|
+
cachePath: string;
|
|
62
|
+
forceUpdate?: boolean;
|
|
63
|
+
logger: LoggerImplement;
|
|
64
|
+
getTmpPath(): string;
|
|
65
|
+
createPage(pageParams: PageParams): Promise<Page>;
|
|
66
|
+
}
|
|
67
|
+
export type ResponseData<T = any> = {
|
|
68
|
+
code: number;
|
|
69
|
+
message: string;
|
|
70
|
+
data: T;
|
|
71
|
+
};
|
|
72
|
+
export type CommonAction<T = any, D = any> = (task: AutomateTask, params: T) => Promise<ResponseData<D>>;
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ResponseData } from "./types";
|
|
2
|
+
export declare function pathExists(path: string): Promise<unknown>;
|
|
3
|
+
export declare function ensureFileSync(filePath: string): void;
|
|
4
|
+
export declare function writeFile(filePath: string, data: string): Promise<void>;
|
|
5
|
+
export declare const semver: {
|
|
6
|
+
gt: (v1: string, v2: string) => boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare function fetchJSON<T>(url: string): Promise<T>;
|
|
9
|
+
export declare function isNil(value: unknown): value is null | undefined;
|
|
10
|
+
export declare function ensureFile(filePath: string): Promise<void>;
|
|
11
|
+
export declare function downloadImage(url: string, savePath: string): Promise<string>;
|
|
12
|
+
export declare function getFilenameFromUrl(imageUrl: string): string;
|
|
13
|
+
export declare const sleep: (ms: number) => Promise<unknown>;
|
|
14
|
+
export declare const success: <T>(data: T, message?: string) => ResponseData<T>;
|
package/package.json
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iflyrpa/share",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "./dist/index.
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
|
-
"types": "./
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
8
|
"author": "bijinfeng",
|
|
9
9
|
"files": [
|
|
10
|
-
"dist"
|
|
11
|
-
"src"
|
|
10
|
+
"dist"
|
|
12
11
|
],
|
|
13
12
|
"devDependencies": {
|
|
14
|
-
"
|
|
15
|
-
"
|
|
13
|
+
"@rslib/core": "^0.4.1",
|
|
14
|
+
"playwright-core": "1.46.1"
|
|
16
15
|
},
|
|
17
16
|
"scripts": {
|
|
18
|
-
"build": "
|
|
19
|
-
"dev": "
|
|
17
|
+
"build": "rslib build",
|
|
18
|
+
"dev": "rslib build --watch"
|
|
20
19
|
}
|
|
21
20
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const fs = require('node:fs');
|
|
4
|
-
const https = require('node:https');
|
|
5
|
-
const path = require('node:path');
|
|
6
|
-
|
|
7
|
-
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
8
|
-
|
|
9
|
-
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
10
|
-
const https__default = /*#__PURE__*/_interopDefaultCompat(https);
|
|
11
|
-
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
|
|
12
|
-
|
|
13
|
-
function pathExists(path2) {
|
|
14
|
-
return new Promise((resolve) => {
|
|
15
|
-
fs__default.stat(path2, (err) => {
|
|
16
|
-
resolve(!err);
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
function ensureFileSync(filePath) {
|
|
21
|
-
const dirPath = path__default.dirname(filePath);
|
|
22
|
-
try {
|
|
23
|
-
if (!fs__default.existsSync(dirPath)) {
|
|
24
|
-
fs__default.mkdirSync(dirPath, { recursive: true });
|
|
25
|
-
}
|
|
26
|
-
} catch (err) {
|
|
27
|
-
if (err instanceof Error) {
|
|
28
|
-
throw new Error(`Error creating directory: ${err.message}`);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
try {
|
|
32
|
-
if (!fs__default.existsSync(filePath)) {
|
|
33
|
-
fs__default.writeFileSync(filePath, "");
|
|
34
|
-
}
|
|
35
|
-
} catch (err) {
|
|
36
|
-
if (err instanceof Error) {
|
|
37
|
-
throw new Error(`Error creating file: ${err.message}`);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
function writeFile(filePath, data) {
|
|
42
|
-
return new Promise((resolve, reject) => {
|
|
43
|
-
fs__default.writeFile(filePath, data, (err) => {
|
|
44
|
-
if (err) {
|
|
45
|
-
console.error("Error writing file:", err);
|
|
46
|
-
reject();
|
|
47
|
-
} else {
|
|
48
|
-
console.log("File written successfully");
|
|
49
|
-
resolve();
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
function compareVersions(v1, v2) {
|
|
55
|
-
const parts1 = v1.split(".").map(Number);
|
|
56
|
-
const parts2 = v2.split(".").map(Number);
|
|
57
|
-
for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
|
|
58
|
-
const num1 = i < parts1.length ? parts1[i] : 0;
|
|
59
|
-
const num2 = i < parts2.length ? parts2[i] : 0;
|
|
60
|
-
if (num1 > num2)
|
|
61
|
-
return 1;
|
|
62
|
-
if (num1 < num2)
|
|
63
|
-
return -1;
|
|
64
|
-
}
|
|
65
|
-
return 0;
|
|
66
|
-
}
|
|
67
|
-
const semver = {
|
|
68
|
-
gt: (v1, v2) => compareVersions(v1, v2) === 1
|
|
69
|
-
};
|
|
70
|
-
function fetchJSON(url) {
|
|
71
|
-
return new Promise((resolve, reject) => {
|
|
72
|
-
https__default.get(url, (res) => {
|
|
73
|
-
let data = "";
|
|
74
|
-
res.on("data", (chunk) => {
|
|
75
|
-
data += chunk;
|
|
76
|
-
});
|
|
77
|
-
res.on("end", () => {
|
|
78
|
-
try {
|
|
79
|
-
const parsedData = JSON.parse(data);
|
|
80
|
-
resolve(parsedData);
|
|
81
|
-
} catch (e) {
|
|
82
|
-
if (e instanceof Error) {
|
|
83
|
-
reject(new Error(`Error parsing JSON: ${e.message}`));
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
}).on("error", (err) => {
|
|
88
|
-
reject(new Error(`Request failed: ${err.message}`));
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
function isNil(value) {
|
|
93
|
-
return value === null || value === void 0;
|
|
94
|
-
}
|
|
95
|
-
function ensureFile(filePath) {
|
|
96
|
-
return new Promise((resolve, reject) => {
|
|
97
|
-
const dirPath = path__default.dirname(filePath);
|
|
98
|
-
fs__default.stat(dirPath, (err, stats) => {
|
|
99
|
-
if (err) {
|
|
100
|
-
if (err.code === "ENOENT") {
|
|
101
|
-
fs__default.mkdir(dirPath, { recursive: true }, (err2) => {
|
|
102
|
-
if (err2) {
|
|
103
|
-
return reject(err2);
|
|
104
|
-
}
|
|
105
|
-
createFile();
|
|
106
|
-
});
|
|
107
|
-
} else {
|
|
108
|
-
return reject(err);
|
|
109
|
-
}
|
|
110
|
-
} else if (stats.isDirectory()) {
|
|
111
|
-
checkFile();
|
|
112
|
-
} else {
|
|
113
|
-
reject(new Error(`${dirPath} is not a directory`));
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
function checkFile() {
|
|
117
|
-
fs__default.stat(filePath, (err, stats) => {
|
|
118
|
-
if (err) {
|
|
119
|
-
if (err.code === "ENOENT") {
|
|
120
|
-
createFile();
|
|
121
|
-
} else {
|
|
122
|
-
reject(err);
|
|
123
|
-
}
|
|
124
|
-
} else if (stats.isFile()) {
|
|
125
|
-
resolve();
|
|
126
|
-
} else {
|
|
127
|
-
reject(new Error(`${filePath} is not a file`));
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
function createFile() {
|
|
132
|
-
fs__default.writeFile(filePath, "", (err) => {
|
|
133
|
-
if (err) {
|
|
134
|
-
reject(err);
|
|
135
|
-
} else {
|
|
136
|
-
resolve();
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
async function downloadImage(url, savePath) {
|
|
143
|
-
await ensureFile(savePath);
|
|
144
|
-
return new Promise((resolve, reject) => {
|
|
145
|
-
https__default.get(url, (response) => {
|
|
146
|
-
if (response.statusCode === 200) {
|
|
147
|
-
const fileStream = fs__default.createWriteStream(savePath);
|
|
148
|
-
response.pipe(fileStream);
|
|
149
|
-
fileStream.on("finish", () => {
|
|
150
|
-
fileStream.close();
|
|
151
|
-
console.log("\u4E0B\u8F7D\u5B8C\u6210\uFF0C\u6587\u4EF6\u5DF2\u4FDD\u5B58\u81F3:", savePath);
|
|
152
|
-
resolve(savePath);
|
|
153
|
-
});
|
|
154
|
-
} else {
|
|
155
|
-
console.log("\u56FE\u7247\u4E0B\u8F7D\u5931\u8D25:", response.statusCode);
|
|
156
|
-
response.resume();
|
|
157
|
-
reject();
|
|
158
|
-
}
|
|
159
|
-
}).on("error", (error) => {
|
|
160
|
-
console.error("\u8BF7\u6C42\u56FE\u7247\u65F6\u53D1\u751F\u9519\u8BEF:", error.message);
|
|
161
|
-
reject();
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
function getFilenameFromUrl(imageUrl) {
|
|
166
|
-
const parsedUrl = new URL(imageUrl);
|
|
167
|
-
return path__default.basename(parsedUrl.pathname);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
exports.downloadImage = downloadImage;
|
|
171
|
-
exports.ensureFile = ensureFile;
|
|
172
|
-
exports.ensureFileSync = ensureFileSync;
|
|
173
|
-
exports.fetchJSON = fetchJSON;
|
|
174
|
-
exports.getFilenameFromUrl = getFilenameFromUrl;
|
|
175
|
-
exports.isNil = isNil;
|
|
176
|
-
exports.pathExists = pathExists;
|
|
177
|
-
exports.semver = semver;
|
|
178
|
-
exports.writeFile = writeFile;
|
package/src/types.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import type { Page } from "playwright";
|
|
2
|
-
|
|
3
|
-
export type { Locator, Response, ElectronApplication } from "playwright-core";
|
|
4
|
-
|
|
5
|
-
export interface CookiesSetDetails {
|
|
6
|
-
/**
|
|
7
|
-
* The URL to associate the cookie with. The promise will be rejected if the URL is
|
|
8
|
-
* invalid.
|
|
9
|
-
*/
|
|
10
|
-
url: string;
|
|
11
|
-
/**
|
|
12
|
-
* The name of the cookie. Empty by default if omitted.
|
|
13
|
-
*/
|
|
14
|
-
name?: string;
|
|
15
|
-
/**
|
|
16
|
-
* The value of the cookie. Empty by default if omitted.
|
|
17
|
-
*/
|
|
18
|
-
value?: string;
|
|
19
|
-
/**
|
|
20
|
-
* The domain of the cookie; this will be normalized with a preceding dot so that
|
|
21
|
-
* it's also valid for subdomains. Empty by default if omitted.
|
|
22
|
-
*/
|
|
23
|
-
domain?: string;
|
|
24
|
-
/**
|
|
25
|
-
* The path of the cookie. Empty by default if omitted.
|
|
26
|
-
*/
|
|
27
|
-
path?: string;
|
|
28
|
-
/**
|
|
29
|
-
* Whether the cookie should be marked as Secure. Defaults to false unless Same
|
|
30
|
-
* Site=None attribute is used.
|
|
31
|
-
*/
|
|
32
|
-
secure?: boolean;
|
|
33
|
-
/**
|
|
34
|
-
* Whether the cookie should be marked as HTTP only. Defaults to false.
|
|
35
|
-
*/
|
|
36
|
-
httpOnly?: boolean;
|
|
37
|
-
/**
|
|
38
|
-
* The expiration date of the cookie as the number of seconds since the UNIX epoch.
|
|
39
|
-
* If omitted then the cookie becomes a session cookie and will not be retained
|
|
40
|
-
* between sessions.
|
|
41
|
-
*/
|
|
42
|
-
expirationDate?: number;
|
|
43
|
-
/**
|
|
44
|
-
* The Same Site policy to apply to this cookie. Can be `unspecified`,
|
|
45
|
-
* `no_restriction`, `lax` or `strict`. Default is `lax`.
|
|
46
|
-
*/
|
|
47
|
-
sameSite?: "unspecified" | "no_restriction" | "lax" | "strict";
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export type CookieMap = CookiesSetDetails[];
|
|
51
|
-
|
|
52
|
-
export interface PageParams {
|
|
53
|
-
cookies?: CookieMap;
|
|
54
|
-
url: string;
|
|
55
|
-
show?: boolean;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export interface LoggerImplement {
|
|
59
|
-
debug(...msg: unknown[]): void;
|
|
60
|
-
info(...msg: unknown[]): void;
|
|
61
|
-
warn(...msg: unknown[]): void;
|
|
62
|
-
error(prefix: string, err?: unknown): void;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export interface AutomateTask {
|
|
66
|
-
debug?: boolean; // debug 模式下可视化展示所有操作
|
|
67
|
-
cachePath: string; // 缓存目录
|
|
68
|
-
forceUpdate?: boolean; // 是否使用远程最新版本
|
|
69
|
-
logger: LoggerImplement;
|
|
70
|
-
|
|
71
|
-
getTmpPath(): string;
|
|
72
|
-
createPage(pageParams: PageParams): Promise<Page>;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export type CommonAction<T = any, D = any> = (
|
|
76
|
-
task: AutomateTask,
|
|
77
|
-
params: T,
|
|
78
|
-
) => Promise<D>;
|
package/src/utils.ts
DELETED
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import https from "node:https";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
|
|
5
|
-
export function pathExists(path: string) {
|
|
6
|
-
return new Promise((resolve) => {
|
|
7
|
-
fs.stat(path, (err) => {
|
|
8
|
-
resolve(!err);
|
|
9
|
-
});
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function ensureFileSync(filePath: string) {
|
|
14
|
-
const dirPath = path.dirname(filePath);
|
|
15
|
-
|
|
16
|
-
// 检查目录是否存在
|
|
17
|
-
try {
|
|
18
|
-
if (!fs.existsSync(dirPath)) {
|
|
19
|
-
// 目录不存在,需要创建
|
|
20
|
-
fs.mkdirSync(dirPath, { recursive: true });
|
|
21
|
-
}
|
|
22
|
-
} catch (err) {
|
|
23
|
-
if (err instanceof Error) {
|
|
24
|
-
throw new Error(`Error creating directory: ${err.message}`);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// 检查文件是否存在
|
|
29
|
-
try {
|
|
30
|
-
if (!fs.existsSync(filePath)) {
|
|
31
|
-
// 文件不存在,创建文件
|
|
32
|
-
fs.writeFileSync(filePath, "");
|
|
33
|
-
}
|
|
34
|
-
} catch (err) {
|
|
35
|
-
if (err instanceof Error) {
|
|
36
|
-
throw new Error(`Error creating file: ${err.message}`);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function writeFile(filePath: string, data: string) {
|
|
42
|
-
return new Promise<void>((resolve, reject) => {
|
|
43
|
-
fs.writeFile(filePath, data, (err) => {
|
|
44
|
-
if (err) {
|
|
45
|
-
console.error("Error writing file:", err);
|
|
46
|
-
reject();
|
|
47
|
-
} else {
|
|
48
|
-
console.log("File written successfully");
|
|
49
|
-
resolve();
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function compareVersions(v1: string, v2: string) {
|
|
56
|
-
// 将两个版本号分割成数字数组
|
|
57
|
-
const parts1 = v1.split(".").map(Number);
|
|
58
|
-
const parts2 = v2.split(".").map(Number);
|
|
59
|
-
|
|
60
|
-
// 比较每个分割后的数字
|
|
61
|
-
for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
|
|
62
|
-
// 如果某个版本号在这一段没有数字(即该段默认为0)
|
|
63
|
-
const num1 = i < parts1.length ? parts1[i] : 0;
|
|
64
|
-
const num2 = i < parts2.length ? parts2[i] : 0;
|
|
65
|
-
|
|
66
|
-
// 比较两个数字
|
|
67
|
-
if (num1 > num2) return 1;
|
|
68
|
-
if (num1 < num2) return -1;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// 如果所有的数字都相等,那么版本号相同
|
|
72
|
-
return 0;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export const semver = {
|
|
76
|
-
gt: (v1: string, v2: string) => compareVersions(v1, v2) === 1,
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
export function fetchJSON<T>(url: string) {
|
|
80
|
-
return new Promise<T>((resolve, reject) => {
|
|
81
|
-
https
|
|
82
|
-
.get(url, (res) => {
|
|
83
|
-
let data = "";
|
|
84
|
-
|
|
85
|
-
res.on("data", (chunk) => {
|
|
86
|
-
data += chunk;
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
res.on("end", () => {
|
|
90
|
-
try {
|
|
91
|
-
const parsedData = JSON.parse(data);
|
|
92
|
-
resolve(parsedData);
|
|
93
|
-
} catch (e) {
|
|
94
|
-
if (e instanceof Error) {
|
|
95
|
-
reject(new Error(`Error parsing JSON: ${e.message}`));
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
})
|
|
100
|
-
.on("error", (err) => {
|
|
101
|
-
reject(new Error(`Request failed: ${err.message}`));
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export function isNil(value: unknown): value is null | undefined {
|
|
107
|
-
return value === null || value === undefined;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export function ensureFile(filePath: string) {
|
|
111
|
-
return new Promise<void>((resolve, reject) => {
|
|
112
|
-
const dirPath = path.dirname(filePath);
|
|
113
|
-
|
|
114
|
-
// 检查目录是否存在
|
|
115
|
-
fs.stat(dirPath, (err, stats) => {
|
|
116
|
-
if (err) {
|
|
117
|
-
if (err.code === "ENOENT") {
|
|
118
|
-
// 目录不存在,需要创建
|
|
119
|
-
fs.mkdir(dirPath, { recursive: true }, (err) => {
|
|
120
|
-
if (err) {
|
|
121
|
-
return reject(err);
|
|
122
|
-
}
|
|
123
|
-
createFile();
|
|
124
|
-
});
|
|
125
|
-
} else {
|
|
126
|
-
return reject(err);
|
|
127
|
-
}
|
|
128
|
-
} else if (stats.isDirectory()) {
|
|
129
|
-
// 目录已存在,检查文件
|
|
130
|
-
checkFile();
|
|
131
|
-
} else {
|
|
132
|
-
// 路径不是目录
|
|
133
|
-
reject(new Error(`${dirPath} is not a directory`));
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
function checkFile() {
|
|
138
|
-
fs.stat(filePath, (err, stats) => {
|
|
139
|
-
if (err) {
|
|
140
|
-
if (err.code === "ENOENT") {
|
|
141
|
-
// 文件不存在,创建文件
|
|
142
|
-
createFile();
|
|
143
|
-
} else {
|
|
144
|
-
reject(err);
|
|
145
|
-
}
|
|
146
|
-
} else if (stats.isFile()) {
|
|
147
|
-
// 文件已存在
|
|
148
|
-
resolve();
|
|
149
|
-
} else {
|
|
150
|
-
// 路径存在,但不是文件
|
|
151
|
-
reject(new Error(`${filePath} is not a file`));
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function createFile() {
|
|
157
|
-
fs.writeFile(filePath, "", (err) => {
|
|
158
|
-
if (err) {
|
|
159
|
-
reject(err);
|
|
160
|
-
} else {
|
|
161
|
-
resolve();
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// 下载图片并保存到指定路径
|
|
169
|
-
export async function downloadImage(
|
|
170
|
-
url: string,
|
|
171
|
-
savePath: string,
|
|
172
|
-
): Promise<string> {
|
|
173
|
-
await ensureFile(savePath);
|
|
174
|
-
|
|
175
|
-
return new Promise<string>((resolve, reject) => {
|
|
176
|
-
https
|
|
177
|
-
.get(url, (response) => {
|
|
178
|
-
// 检查响应状态码是否表明成功接收图片
|
|
179
|
-
if (response.statusCode === 200) {
|
|
180
|
-
// 创建文件写入流
|
|
181
|
-
const fileStream = fs.createWriteStream(savePath);
|
|
182
|
-
// 将响应流重定向到文件写入流
|
|
183
|
-
response.pipe(fileStream);
|
|
184
|
-
|
|
185
|
-
// 文件结束写入后关闭文件流
|
|
186
|
-
fileStream.on("finish", () => {
|
|
187
|
-
fileStream.close();
|
|
188
|
-
console.log("下载完成,文件已保存至:", savePath);
|
|
189
|
-
resolve(savePath);
|
|
190
|
-
});
|
|
191
|
-
} else {
|
|
192
|
-
// 如果响应状态码不是 200,打印错误信息
|
|
193
|
-
console.log("图片下载失败:", response.statusCode);
|
|
194
|
-
// 消费响应数据以释放内存
|
|
195
|
-
response.resume();
|
|
196
|
-
reject();
|
|
197
|
-
}
|
|
198
|
-
})
|
|
199
|
-
.on("error", (error) => {
|
|
200
|
-
// 如果请求过程中出现错误,打印错误信息并确保文件流关闭
|
|
201
|
-
console.error("请求图片时发生错误:", error.message);
|
|
202
|
-
reject();
|
|
203
|
-
});
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// 从 URL 中获取文件名
|
|
208
|
-
export function getFilenameFromUrl(imageUrl: string) {
|
|
209
|
-
const parsedUrl = new URL(imageUrl); // 使用 URL 构造函数解析完整的 URL
|
|
210
|
-
return path.basename(parsedUrl.pathname); // 获取路径名的最后一部分作为文件名
|
|
211
|
-
}
|