feat: npm CLI

This commit is contained in:
Daniel Hougaard
2024-11-12 20:36:09 +04:00
parent a55fe2b788
commit 3b02eedca6
6 changed files with 200 additions and 77 deletions
+64 -67
View File
@@ -1,4 +1,4 @@
const { execSync } = require("child_process");
const childProcess = require("child_process");
const fs = require("fs");
const stream = require("node:stream");
const tar = require("tar");
@@ -10,79 +10,76 @@ const supportedPlatforms = ["linux", "darwin", "win32", "freebsd"];
const outputDir = "bin";
const getPlatform = () => {
const platform = process.platform;
if (!supportedPlatforms.includes(platform)) {
console.error(
"Your platform doesn't seem to be of type darwin, linux or windows"
);
process.exit(1);
}
return platform;
const platform = process.platform;
if (!supportedPlatforms.includes(platform)) {
console.error("Your platform doesn't seem to be of type darwin, linux or windows");
process.exit(1);
}
return platform;
};
const getArchitecture = () => {
const architecture = execSync("uname -m").toString().trim();
let arch = "";
if (architecture === "x86_64" || architecture === "amd64") {
arch = "amd64";
} else if (architecture === "arm64" || architecture === "aarch64") {
arch = "arm64";
} else if (architecture.startsWith("armv5")) {
arch = "armv5";
} else if (architecture.startsWith("armv6")) {
arch = "armv6";
} else if (architecture.startsWith("armv7")) {
arch = "armv7";
} else if (architecture === "i386" || architecture === "i686") {
arch = "i386";
} else {
console.error(
"Your architecture doesn't seem to be supported. Your architecture is",
architecture
);
process.exit(1);
}
return arch;
const architecture = childProcess.execSync("uname -m").toString().trim();
let arch = "";
if (architecture === "x86_64" || architecture === "amd64") {
arch = "amd64";
} else if (architecture === "arm64" || architecture === "aarch64") {
arch = "arm64";
} else if (architecture.startsWith("armv5")) {
arch = "armv5";
} else if (architecture.startsWith("armv6")) {
arch = "armv6";
} else if (architecture.startsWith("armv7")) {
arch = "armv7";
} else if (architecture === "i386" || architecture === "i686") {
arch = "i386";
} else {
console.error("Your architecture doesn't seem to be supported. Your architecture is", architecture);
process.exit(1);
}
return arch;
};
function main() {
const PLATFORM = getPlatform();
const ARCH = getArchitecture();
const NUMERIC_RELEASE_VERSION = packageJSON.version;
const LATEST_RELEASE_VERSION = `v${NUMERIC_RELEASE_VERSION}`;
const downloadLink = `https://github.com/Infisical/infisical/releases/download/infisical-cli/${LATEST_RELEASE_VERSION}/infisical_${NUMERIC_RELEASE_VERSION}_${PLATFORM}_${ARCH}.tar.gz`;
async function main() {
const PLATFORM = getPlatform();
const ARCH = getArchitecture();
const NUMERIC_RELEASE_VERSION = packageJSON.version;
const LATEST_RELEASE_VERSION = `v${NUMERIC_RELEASE_VERSION}`;
const downloadLink = `https://github.com/Infisical/infisical/releases/download/infisical-cli/${LATEST_RELEASE_VERSION}/infisical_${NUMERIC_RELEASE_VERSION}_${PLATFORM}_${ARCH}.tar.gz`;
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
fetch(downloadLink, {
headers: {
Accept: "application/octet-stream",
},
})
.then(async (response) => {
if (!response.ok) {
throw new Error(
`Failed to fetch: ${response.status} - ${response.statusText}`
);
}
// Ensure the output directory exists
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
return new Promise((resolve, reject) => {
const outStream = stream.Readable.fromWeb(response.body)
.pipe(zlib.createGunzip())
.pipe(
tar.x({
C: path.join(outputDir),
filter: (path) => path === "infisical",
})
);
// Download the latest CLI binary
try {
const response = await fetch(downloadLink, {
headers: {
Accept: "application/octet-stream"
}
});
outStream.on("error", reject);
outStream.on("close", resolve);
});
})
.catch((error) => {
console.error("Error downloading or extracting the asset:", error);
});
}
if (!response.ok) {
throw new Error(`Failed to fetch: ${response.status} - ${response.statusText}`);
}
return await new Promise((resolve, reject) => {
const outStream = stream.Readable.fromWeb(response.body)
.pipe(zlib.createGunzip())
.pipe(
tar.x({
C: path.join(outputDir),
filter: path => path === "infisical"
})
);
outStream.on("error", reject);
outStream.on("close", resolve);
});
} catch (error) {
console.error("Error downloading or extracting the asset:", error);
process.exit(1);
}
}
main();