feat: 增加 cert:generate 和 arduino:install 脚本
This commit is contained in:
99
scripts/arduino-install.js
Normal file
99
scripts/arduino-install.js
Normal file
@@ -0,0 +1,99 @@
|
||||
import os from 'node:os';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import fsExtra from 'fs-extra';
|
||||
import fsPlus from 'fs-plus';
|
||||
import fetch from 'node-fetch';
|
||||
import decompress from 'decompress';
|
||||
import decompressUnzip from 'decompress-unzip';
|
||||
import * as tar from 'tar';
|
||||
import shell from 'shelljs';
|
||||
|
||||
|
||||
const version = '0.35.2';
|
||||
|
||||
|
||||
function getPlatformTarget() {
|
||||
const platform = os.platform();
|
||||
const arch = os.arch();
|
||||
if (platform === 'win32') {
|
||||
return arch === 'x64' ? 'arduino-cli_' + version + '_Windows_64bit.zip' : null;
|
||||
}
|
||||
if (platform === 'darwin') {
|
||||
return 'arduino-cli_' + version + '_macOS_64bit.tar.gz';
|
||||
}
|
||||
if (platform === 'linux') {
|
||||
if (arch === 'x64') return 'arduino-cli_' + version + '_Linux_64bit.tar.gz';
|
||||
if (arch === 'arm64') return 'arduino-cli_' + version + '_Linux_ARM64.tar.gz';
|
||||
if (arch.startsWith('arm')) return 'arduino-cli_' + version + '_Linux_ARMv7.tar.gz';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function arduinoInstall() {
|
||||
const filename = getPlatformTarget();
|
||||
if (!filename) {
|
||||
console.error('当前系统或架构不受支持');
|
||||
return;
|
||||
}
|
||||
|
||||
const url = `https://downloads.arduino.cc/arduino-cli/${filename}`;
|
||||
|
||||
const temp = path.resolve(process.cwd(), 'temp');
|
||||
await fsExtra.ensureDir(temp);
|
||||
const dest = path.resolve(temp, filename);
|
||||
const cliDirPath = path.resolve(process.cwd(), 'arduino-cli');
|
||||
await fsExtra.ensureDir(cliDirPath);
|
||||
let cliPath = '';
|
||||
if (os.platform() === 'win32') {
|
||||
cliPath = path.resolve(cliDirPath, 'arduino-cli.exe');
|
||||
} else {
|
||||
cliPath = path.resolve(cliDirPath, 'arduino-cli');
|
||||
}
|
||||
const configPath = path.resolve(cliDirPath, 'arduino-cli.json');
|
||||
const config = await fsExtra.readJson(path.resolve(process.cwd(), 'arduino-cli.json'));
|
||||
config.directories = {
|
||||
data: path.resolve(cliDirPath, 'Arduino15'),
|
||||
downloads: path.resolve(cliDirPath, 'staging'),
|
||||
user: path.resolve(cliDirPath, 'Arduino')
|
||||
};
|
||||
await fsExtra.writeJson(configPath, config, {
|
||||
spaces: ' '
|
||||
});
|
||||
if (!fsPlus.isFileSync(cliPath)) {
|
||||
console.log(`开始下载: ${url}`);
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) throw new Error(`下载失败: ${res.statusText}`);
|
||||
const fileStream = fs.createWriteStream(dest);
|
||||
await new Promise((resolve, reject) => {
|
||||
res.body.pipe(fileStream);
|
||||
res.body.on('error', reject);
|
||||
fileStream.on('finish', resolve);
|
||||
});
|
||||
|
||||
console.log('下载完成,开始解压...');
|
||||
if (filename.endsWith('.zip')) {
|
||||
await decompress(dest, cliDirPath, {
|
||||
plugins: [decompressUnzip()]
|
||||
});
|
||||
} else {
|
||||
await tar.x({
|
||||
file: dest,
|
||||
cwd: cliDirPath,
|
||||
strict: true
|
||||
});
|
||||
}
|
||||
fs.unlinkSync(dest);
|
||||
console.log('解压完成,arduino-cli可执行文件已准备好');
|
||||
}
|
||||
|
||||
shell.exec(`"${cliPath}" core update-index --config-file "${configPath}"`);
|
||||
console.log('\n开始下载: Arduino AVR');
|
||||
shell.exec(`"${cliPath}" core install arduino:avr@1.8.6 --config-file "${configPath}"`);
|
||||
console.log('\n开始下载: Arduino ESP8266');
|
||||
shell.exec(`"${cliPath}" core install esp8266:esp8266@3.1.1 --config-file "${configPath}"`);
|
||||
console.log('\n开始下载: Arduino ESP32');
|
||||
shell.exec(`"${cliPath}" core install esp32:esp32@2.0.15 --config-file "${configPath}"`);
|
||||
}
|
||||
|
||||
await arduinoInstall();
|
||||
Reference in New Issue
Block a user