Skip to content

Commit 30a3d9d

Browse files
committed
Merge branch 'main' into deploy
2 parents 391ce2f + 16d4c78 commit 30a3d9d

File tree

10 files changed

+735
-5
lines changed

10 files changed

+735
-5
lines changed

avr/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
8-
"postinstall": "node ./postinstall.js"
8+
"postinstall": "node ./postinstall.js",
9+
"uninstall": "node ./uninstall.js"
910
},
1011
"author": "",
1112
"license": "ISC"

avr/uninstall.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
// 确保 __dirname 有值,如果没有则使用当前工作目录
5+
const srcDir = __dirname || "";
6+
// 确保目标目录有值,空字符串会导致解压到当前目录
7+
const destDir = process.env.AILY_SDK_PATH || "";
8+
9+
// 使用传统的回调式 API 并用 Promise 包装
10+
function readdir(dir) {
11+
return new Promise((resolve, reject) => {
12+
fs.readdir(dir, (err, files) => {
13+
if (err) reject(err);
14+
else resolve(files);
15+
});
16+
});
17+
}
18+
19+
// 自定义递归删除函数
20+
function deleteFolderRecursive(dirPath) {
21+
if (fs.existsSync(dirPath)) {
22+
fs.readdirSync(dirPath).forEach(file => {
23+
const curPath = path.join(dirPath, file);
24+
if (fs.lstatSync(curPath).isDirectory()) {
25+
// 递归删除子目录
26+
deleteFolderRecursive(curPath);
27+
} else {
28+
// 删除文件,忽略错误
29+
try {
30+
fs.unlinkSync(curPath);
31+
} catch (e) {
32+
console.warn(`无法删除文件: ${curPath}`, e);
33+
}
34+
}
35+
});
36+
// 删除目录本身
37+
try {
38+
fs.rmdirSync(dirPath);
39+
} catch (e) {
40+
console.warn(`无法删除目录: ${dirPath}`, e);
41+
// 如果删除失败,尝试重命名
42+
try {
43+
const tempPath = path.join(path.dirname(dirPath), `_temp_${Date.now()}`);
44+
fs.renameSync(dirPath, tempPath);
45+
deleteFolderRecursive(tempPath);
46+
} catch (renameErr) {
47+
console.warn(`重命名删除也失败: ${dirPath}`, renameErr);
48+
}
49+
}
50+
}
51+
}
52+
53+
// 重试函数封装
54+
async function withRetry(fn, maxRetries = 3, retryDelay = 1000) {
55+
let lastError;
56+
57+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
58+
try {
59+
return await fn();
60+
} catch (error) {
61+
lastError = error;
62+
console.log(`操作失败 (尝试 ${attempt}/${maxRetries}): ${error.message}`);
63+
64+
if (attempt < maxRetries) {
65+
console.log(`等待 ${retryDelay / 1000} 秒后重试...`);
66+
await new Promise(resolve => setTimeout(resolve, retryDelay));
67+
}
68+
}
69+
}
70+
71+
throw new Error(`经过 ${maxRetries} 次尝试后操作仍然失败: ${lastError.message}`);
72+
}
73+
74+
async function handler(file) {
75+
// 将文件名中的@替换为_,这对应postinstall.js中的重命名逻辑
76+
const baseName = path.basename(file, '.7z');
77+
const folderName = baseName.replace('@', '_');
78+
const folderPath = path.join(destDir, folderName);
79+
80+
console.log(`准备删除文件夹: ${folderPath}`);
81+
82+
// 判断是否存在目标目录,如果存在则删除
83+
if (fs.existsSync(folderPath)) {
84+
try {
85+
deleteFolderRecursive(folderPath);
86+
console.log(`已删除目录: ${folderPath}`);
87+
} catch (err) {
88+
console.error(`无法删除目录: ${folderPath}`, err);
89+
throw new Error(`删除目录失败: ${folderPath}`);
90+
}
91+
} else {
92+
console.log(`目录不存在,跳过删除: ${folderPath}`);
93+
}
94+
}
95+
96+
// 使用 Promise 和 async/await 简化异步操作
97+
async function removeExtractedArchives() {
98+
try {
99+
// 确保源目录存在
100+
if (!fs.existsSync(srcDir)) {
101+
console.error(`源目录不存在: ${srcDir}`);
102+
return;
103+
}
104+
105+
// 确保目标目录存在
106+
if (!destDir) {
107+
console.error('未设置目标目录');
108+
return;
109+
}
110+
111+
if (!fs.existsSync(destDir)) {
112+
console.log(`目标目录不存在: ${destDir}`);
113+
return;
114+
}
115+
116+
// 读取目录并过滤出 .7z 文件
117+
const files = await readdir(srcDir);
118+
const archiveFiles = files.filter(file => path.extname(file).toLowerCase() === '.7z');
119+
120+
console.log(`找到 ${archiveFiles.length} 个 .7z 文件对应的文件夹需要删除`);
121+
122+
// 处理每个压缩文件对应的文件夹
123+
for (const file of archiveFiles) {
124+
if (!file) {
125+
console.error('文件名为空,跳过');
126+
continue;
127+
}
128+
129+
try {
130+
await withRetry(async () => {
131+
await handler(file);
132+
}, 3, 2000); // 重试3次,每次间隔2秒
133+
} catch (error) {
134+
console.error(`删除 ${file} 对应的文件夹失败:`, error);
135+
}
136+
}
137+
} catch (err) {
138+
console.error('无法读取目录:', err);
139+
}
140+
}
141+
142+
// 执行主函数
143+
removeExtractedArchives().catch(function (err) {
144+
console.error('执行失败:', err);
145+
});

esp32/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
8-
"postinstall": "node ./postinstall.js"
8+
"postinstall": "node ./postinstall.js",
9+
"uninstall": "node ./uninstall.js"
910
},
1011
"author": "",
1112
"license": "ISC"

esp32/uninstall.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
// 确保 __dirname 有值,如果没有则使用当前工作目录
5+
const srcDir = __dirname || "";
6+
// 确保目标目录有值,空字符串会导致解压到当前目录
7+
const destDir = process.env.AILY_SDK_PATH || "";
8+
9+
// 使用传统的回调式 API 并用 Promise 包装
10+
function readdir(dir) {
11+
return new Promise((resolve, reject) => {
12+
fs.readdir(dir, (err, files) => {
13+
if (err) reject(err);
14+
else resolve(files);
15+
});
16+
});
17+
}
18+
19+
// 自定义递归删除函数
20+
function deleteFolderRecursive(dirPath) {
21+
if (fs.existsSync(dirPath)) {
22+
fs.readdirSync(dirPath).forEach(file => {
23+
const curPath = path.join(dirPath, file);
24+
if (fs.lstatSync(curPath).isDirectory()) {
25+
// 递归删除子目录
26+
deleteFolderRecursive(curPath);
27+
} else {
28+
// 删除文件,忽略错误
29+
try {
30+
fs.unlinkSync(curPath);
31+
} catch (e) {
32+
console.warn(`无法删除文件: ${curPath}`, e);
33+
}
34+
}
35+
});
36+
// 删除目录本身
37+
try {
38+
fs.rmdirSync(dirPath);
39+
} catch (e) {
40+
console.warn(`无法删除目录: ${dirPath}`, e);
41+
// 如果删除失败,尝试重命名
42+
try {
43+
const tempPath = path.join(path.dirname(dirPath), `_temp_${Date.now()}`);
44+
fs.renameSync(dirPath, tempPath);
45+
deleteFolderRecursive(tempPath);
46+
} catch (renameErr) {
47+
console.warn(`重命名删除也失败: ${dirPath}`, renameErr);
48+
}
49+
}
50+
}
51+
}
52+
53+
// 重试函数封装
54+
async function withRetry(fn, maxRetries = 3, retryDelay = 1000) {
55+
let lastError;
56+
57+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
58+
try {
59+
return await fn();
60+
} catch (error) {
61+
lastError = error;
62+
console.log(`操作失败 (尝试 ${attempt}/${maxRetries}): ${error.message}`);
63+
64+
if (attempt < maxRetries) {
65+
console.log(`等待 ${retryDelay / 1000} 秒后重试...`);
66+
await new Promise(resolve => setTimeout(resolve, retryDelay));
67+
}
68+
}
69+
}
70+
71+
throw new Error(`经过 ${maxRetries} 次尝试后操作仍然失败: ${lastError.message}`);
72+
}
73+
74+
async function handler(file) {
75+
// 将文件名中的@替换为_,这对应postinstall.js中的重命名逻辑
76+
const baseName = path.basename(file, '.7z');
77+
const folderName = baseName.replace('@', '_');
78+
const folderPath = path.join(destDir, folderName);
79+
80+
console.log(`准备删除文件夹: ${folderPath}`);
81+
82+
// 判断是否存在目标目录,如果存在则删除
83+
if (fs.existsSync(folderPath)) {
84+
try {
85+
deleteFolderRecursive(folderPath);
86+
console.log(`已删除目录: ${folderPath}`);
87+
} catch (err) {
88+
console.error(`无法删除目录: ${folderPath}`, err);
89+
throw new Error(`删除目录失败: ${folderPath}`);
90+
}
91+
} else {
92+
console.log(`目录不存在,跳过删除: ${folderPath}`);
93+
}
94+
}
95+
96+
// 使用 Promise 和 async/await 简化异步操作
97+
async function removeExtractedArchives() {
98+
try {
99+
// 确保源目录存在
100+
if (!fs.existsSync(srcDir)) {
101+
console.error(`源目录不存在: ${srcDir}`);
102+
return;
103+
}
104+
105+
// 确保目标目录存在
106+
if (!destDir) {
107+
console.error('未设置目标目录');
108+
return;
109+
}
110+
111+
if (!fs.existsSync(destDir)) {
112+
console.log(`目标目录不存在: ${destDir}`);
113+
return;
114+
}
115+
116+
// 读取目录并过滤出 .7z 文件
117+
const files = await readdir(srcDir);
118+
const archiveFiles = files.filter(file => path.extname(file).toLowerCase() === '.7z');
119+
120+
console.log(`找到 ${archiveFiles.length} 个 .7z 文件对应的文件夹需要删除`);
121+
122+
// 处理每个压缩文件对应的文件夹
123+
for (const file of archiveFiles) {
124+
if (!file) {
125+
console.error('文件名为空,跳过');
126+
continue;
127+
}
128+
129+
try {
130+
await withRetry(async () => {
131+
await handler(file);
132+
}, 3, 2000); // 重试3次,每次间隔2秒
133+
} catch (error) {
134+
console.error(`删除 ${file} 对应的文件夹失败:`, error);
135+
}
136+
}
137+
} catch (err) {
138+
console.error('无法读取目录:', err);
139+
}
140+
}
141+
142+
// 执行主函数
143+
removeExtractedArchives().catch(function (err) {
144+
console.error('执行失败:', err);
145+
});

renesas_uno/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
8-
"postinstall": "node ./postinstall.js"
8+
"postinstall": "node ./postinstall.js",
9+
"uninstall": "node ./uninstall.js"
910
},
1011
"author": "",
1112
"license": "ISC"

0 commit comments

Comments
 (0)