mirror of
https://github.com/larksuite/cli.git
synced 2026-07-03 22:24:31 +08:00
101 lines
2.7 KiB
JavaScript
101 lines
2.7 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
const https = require("https");
|
|
const { execSync } = require("child_process");
|
|
const os = require("os");
|
|
|
|
const VERSION = require("../package.json").version;
|
|
const REPO = "larksuite/cli";
|
|
const NAME = "lark-cli";
|
|
|
|
const PLATFORM_MAP = {
|
|
darwin: "darwin",
|
|
linux: "linux",
|
|
win32: "windows",
|
|
};
|
|
|
|
const ARCH_MAP = {
|
|
x64: "amd64",
|
|
arm64: "arm64",
|
|
};
|
|
|
|
const platform = PLATFORM_MAP[process.platform];
|
|
const arch = ARCH_MAP[process.arch];
|
|
|
|
if (!platform || !arch) {
|
|
console.error(
|
|
`Unsupported platform: ${process.platform}-${process.arch}`
|
|
);
|
|
process.exit(1);
|
|
}
|
|
|
|
const isWindows = process.platform === "win32";
|
|
const ext = isWindows ? ".zip" : ".tar.gz";
|
|
const archiveName = `${NAME}-${VERSION}-${platform}-${arch}${ext}`;
|
|
const url = `https://github.com/${REPO}/releases/download/v${VERSION}/${archiveName}`;
|
|
const binDir = path.join(__dirname, "..", "bin");
|
|
const dest = path.join(binDir, NAME + (isWindows ? ".exe" : ""));
|
|
|
|
fs.mkdirSync(binDir, { recursive: true });
|
|
|
|
function download(url, destPath) {
|
|
return new Promise((resolve, reject) => {
|
|
const client = url.startsWith("https") ? https : require("http");
|
|
client
|
|
.get(url, (res) => {
|
|
if (res.statusCode === 302 || res.statusCode === 301) {
|
|
return download(res.headers.location, destPath).then(
|
|
resolve,
|
|
reject
|
|
);
|
|
}
|
|
if (res.statusCode !== 200) {
|
|
return reject(
|
|
new Error(`Download failed with status ${res.statusCode}: ${url}`)
|
|
);
|
|
}
|
|
const file = fs.createWriteStream(destPath);
|
|
res.pipe(file);
|
|
file.on("finish", () => {
|
|
file.close();
|
|
resolve();
|
|
});
|
|
})
|
|
.on("error", reject);
|
|
});
|
|
}
|
|
|
|
async function install() {
|
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "lark-cli-"));
|
|
const archivePath = path.join(tmpDir, archiveName);
|
|
|
|
try {
|
|
await download(url, archivePath);
|
|
|
|
if (isWindows) {
|
|
execSync(
|
|
`powershell -Command "Expand-Archive -Path '${archivePath}' -DestinationPath '${tmpDir}'"`,
|
|
{ stdio: "ignore" }
|
|
);
|
|
} else {
|
|
execSync(`tar -xzf "${archivePath}" -C "${tmpDir}"`, {
|
|
stdio: "ignore",
|
|
});
|
|
}
|
|
|
|
const binaryName = NAME + (isWindows ? ".exe" : "");
|
|
const extractedBinary = path.join(tmpDir, binaryName);
|
|
|
|
fs.copyFileSync(extractedBinary, dest);
|
|
fs.chmodSync(dest, 0o755);
|
|
console.log(`${NAME} v${VERSION} installed successfully`);
|
|
} finally {
|
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
install().catch((err) => {
|
|
console.error(`Failed to install ${NAME}:`, err.message);
|
|
process.exit(1);
|
|
});
|