mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-21 08:59:07 +01:00
Merge plugin CLI into here (#404)
This commit is contained in:
2
npm/cli/.gitignore
vendored
Normal file
2
npm/cli/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
yaak
|
||||
yaak.exe
|
||||
30
npm/cli/bin/cli.js
Executable file
30
npm/cli/bin/cli.js
Executable file
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const path = require("path");
|
||||
const childProcess = require("child_process");
|
||||
const { BINARY_NAME, PLATFORM_SPECIFIC_PACKAGE_NAME } = require("../common");
|
||||
|
||||
function getBinaryPath() {
|
||||
try {
|
||||
if (!PLATFORM_SPECIFIC_PACKAGE_NAME) {
|
||||
throw new Error("unsupported platform");
|
||||
}
|
||||
return require.resolve(`${PLATFORM_SPECIFIC_PACKAGE_NAME}/bin/${BINARY_NAME}`);
|
||||
} catch (_) {
|
||||
return path.join(__dirname, "..", BINARY_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
const result = childProcess.spawnSync(getBinaryPath(), process.argv.slice(2), {
|
||||
stdio: "inherit"
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
if (result.signal) {
|
||||
process.kill(process.pid, result.signal);
|
||||
}
|
||||
|
||||
process.exit(result.status ?? 1);
|
||||
20
npm/cli/common.js
Normal file
20
npm/cli/common.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const BINARY_DISTRIBUTION_PACKAGES = {
|
||||
darwin_arm64: "@yaakapp/cli-darwin-arm64",
|
||||
darwin_x64: "@yaakapp/cli-darwin-x64",
|
||||
linux_arm64: "@yaakapp/cli-linux-arm64",
|
||||
linux_x64: "@yaakapp/cli-linux-x64",
|
||||
win32_x64: "@yaakapp/cli-win32-x64",
|
||||
win32_arm64: "@yaakapp/cli-win32-arm64"
|
||||
};
|
||||
|
||||
const BINARY_DISTRIBUTION_VERSION = require("./package.json").version;
|
||||
const BINARY_NAME = process.platform === "win32" ? "yaak.exe" : "yaak";
|
||||
const PLATFORM_SPECIFIC_PACKAGE_NAME =
|
||||
BINARY_DISTRIBUTION_PACKAGES[`${process.platform}_${process.arch}`];
|
||||
|
||||
module.exports = {
|
||||
BINARY_DISTRIBUTION_PACKAGES,
|
||||
BINARY_DISTRIBUTION_VERSION,
|
||||
BINARY_NAME,
|
||||
PLATFORM_SPECIFIC_PACKAGE_NAME
|
||||
};
|
||||
20
npm/cli/index.js
Normal file
20
npm/cli/index.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const path = require("path");
|
||||
const childProcess = require("child_process");
|
||||
const { PLATFORM_SPECIFIC_PACKAGE_NAME, BINARY_NAME } = require("./common");
|
||||
|
||||
function getBinaryPath() {
|
||||
try {
|
||||
if (!PLATFORM_SPECIFIC_PACKAGE_NAME) {
|
||||
throw new Error("unsupported platform");
|
||||
}
|
||||
return require.resolve(`${PLATFORM_SPECIFIC_PACKAGE_NAME}/bin/${BINARY_NAME}`);
|
||||
} catch (_) {
|
||||
return path.join(__dirname, BINARY_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.runBinary = function runBinary(...args) {
|
||||
childProcess.execFileSync(getBinaryPath(), args, {
|
||||
stdio: "inherit"
|
||||
});
|
||||
};
|
||||
97
npm/cli/install.js
Normal file
97
npm/cli/install.js
Normal file
@@ -0,0 +1,97 @@
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const zlib = require("node:zlib");
|
||||
const https = require("node:https");
|
||||
const {
|
||||
BINARY_DISTRIBUTION_VERSION,
|
||||
BINARY_NAME,
|
||||
PLATFORM_SPECIFIC_PACKAGE_NAME
|
||||
} = require("./common");
|
||||
|
||||
const fallbackBinaryPath = path.join(__dirname, BINARY_NAME);
|
||||
|
||||
function makeRequest(url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
https
|
||||
.get(url, (response) => {
|
||||
if (response.statusCode >= 200 && response.statusCode < 300) {
|
||||
const chunks = [];
|
||||
response.on("data", (chunk) => chunks.push(chunk));
|
||||
response.on("end", () => resolve(Buffer.concat(chunks)));
|
||||
} else if (
|
||||
response.statusCode >= 300 &&
|
||||
response.statusCode < 400 &&
|
||||
response.headers.location
|
||||
) {
|
||||
makeRequest(response.headers.location).then(resolve, reject);
|
||||
} else {
|
||||
reject(
|
||||
new Error(
|
||||
`npm responded with status code ${response.statusCode} when downloading package ${url}`
|
||||
)
|
||||
);
|
||||
}
|
||||
})
|
||||
.on("error", (error) => reject(error));
|
||||
});
|
||||
}
|
||||
|
||||
function extractFileFromTarball(tarballBuffer, filepath) {
|
||||
let offset = 0;
|
||||
while (offset < tarballBuffer.length) {
|
||||
const header = tarballBuffer.subarray(offset, offset + 512);
|
||||
offset += 512;
|
||||
|
||||
const fileName = header.toString("utf-8", 0, 100).replace(/\0.*/g, "");
|
||||
const fileSize = parseInt(header.toString("utf-8", 124, 136).replace(/\0.*/g, ""), 8);
|
||||
|
||||
if (fileName === filepath) {
|
||||
return tarballBuffer.subarray(offset, offset + fileSize);
|
||||
}
|
||||
|
||||
offset = (offset + fileSize + 511) & ~511;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function downloadBinaryFromNpm() {
|
||||
if (!PLATFORM_SPECIFIC_PACKAGE_NAME) {
|
||||
throw new Error(`Unsupported platform: ${process.platform}/${process.arch}`);
|
||||
}
|
||||
|
||||
const packageNameWithoutScope = PLATFORM_SPECIFIC_PACKAGE_NAME.split("/")[1];
|
||||
const tarballUrl = `https://registry.npmjs.org/${PLATFORM_SPECIFIC_PACKAGE_NAME}/-/${packageNameWithoutScope}-${BINARY_DISTRIBUTION_VERSION}.tgz`;
|
||||
const tarballDownloadBuffer = await makeRequest(tarballUrl);
|
||||
const tarballBuffer = zlib.unzipSync(tarballDownloadBuffer);
|
||||
|
||||
const binary = extractFileFromTarball(tarballBuffer, `package/bin/${BINARY_NAME}`);
|
||||
if (!binary) {
|
||||
throw new Error(`Could not find package/bin/${BINARY_NAME} in tarball`);
|
||||
}
|
||||
|
||||
fs.writeFileSync(fallbackBinaryPath, binary);
|
||||
fs.chmodSync(fallbackBinaryPath, "755");
|
||||
}
|
||||
|
||||
function isPlatformSpecificPackageInstalled() {
|
||||
try {
|
||||
if (!PLATFORM_SPECIFIC_PACKAGE_NAME) {
|
||||
return false;
|
||||
}
|
||||
require.resolve(`${PLATFORM_SPECIFIC_PACKAGE_NAME}/bin/${BINARY_NAME}`);
|
||||
return true;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isPlatformSpecificPackageInstalled()) {
|
||||
console.log("Platform package missing. Downloading Yaak CLI binary from npm...");
|
||||
downloadBinaryFromNpm().catch((err) => {
|
||||
console.error("Failed to install Yaak CLI binary:", err);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
} else {
|
||||
console.log("Platform package present. Using bundled Yaak CLI binary.");
|
||||
}
|
||||
25
npm/cli/package.json
Normal file
25
npm/cli/package.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "@yaakapp/cli",
|
||||
"version": "0.0.1",
|
||||
"main": "./index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/mountain-loop/yaak.git"
|
||||
},
|
||||
"scripts": {
|
||||
"postinstall": "node ./install.js",
|
||||
"prepublishOnly": "node ./prepublish.js"
|
||||
},
|
||||
"bin": {
|
||||
"yaak": "bin/cli.js",
|
||||
"yaakcli": "bin/cli.js"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@yaakapp/cli-darwin-x64": "0.0.1",
|
||||
"@yaakapp/cli-darwin-arm64": "0.0.1",
|
||||
"@yaakapp/cli-linux-arm64": "0.0.1",
|
||||
"@yaakapp/cli-linux-x64": "0.0.1",
|
||||
"@yaakapp/cli-win32-x64": "0.0.1",
|
||||
"@yaakapp/cli-win32-arm64": "0.0.1"
|
||||
}
|
||||
}
|
||||
5
npm/cli/prepublish.js
Normal file
5
npm/cli/prepublish.js
Normal file
@@ -0,0 +1,5 @@
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
|
||||
const readme = path.join(__dirname, "..", "..", "README.md");
|
||||
fs.copyFileSync(readme, path.join(__dirname, "README.md"));
|
||||
Reference in New Issue
Block a user