@kosli/cli 2.13.1-SNAPSHOT-b45c7e67 → 2.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/bin/kosli +13 -111
  2. package/install.js +6 -6
  3. package/package.json +9 -8
package/bin/kosli CHANGED
@@ -3,10 +3,6 @@
3
3
 
4
4
  const { spawnSync } = require("child_process");
5
5
  const path = require("path");
6
- const https = require("https");
7
- const http = require("http");
8
- const fs = require("fs");
9
- const os = require("os");
10
6
 
11
7
  const platform = process.platform;
12
8
  const arch = process.arch;
@@ -27,118 +23,24 @@ if (!SUPPORTED[platform] || !SUPPORTED[platform][arch]) {
27
23
 
28
24
  const packageName = `@kosli/cli-${platform}-${arch}`;
29
25
 
30
- // Try the optional platform package first (installed via npm install / npm install -g)
31
- let resolvedBinaryPath = null;
26
+ let binaryPath;
32
27
  try {
33
28
  const packageDir = path.dirname(
34
29
  require.resolve(`${packageName}/package.json`)
35
30
  );
36
31
  const binaryName = platform === "win32" ? "kosli.exe" : "kosli";
37
- resolvedBinaryPath = path.join(packageDir, "bin", binaryName);
38
- } catch (_) {
39
- // Optional dep not present — will fall back to on-demand download below (npx case)
40
- }
41
-
42
- async function run() {
43
- const binaryPath = resolvedBinaryPath ?? await getOrDownloadBinary();
44
-
45
- const result = spawnSync(binaryPath, process.argv.slice(2), { stdio: "inherit" });
46
- if (result.error) {
47
- process.stderr.write(`[kosli] Error: failed to run binary: ${result.error.message}\n`);
48
- process.exit(1);
49
- }
50
- process.exit(result.status ?? 1);
51
- }
52
-
53
- async function getOrDownloadBinary() {
54
- const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "package.json"), "utf8"));
55
- const version = pkg.version;
56
-
57
- if (!version || version === "0.0.0") {
58
- process.stderr.write(
59
- `[kosli] Error: package version is "${version}" — cannot determine which binary to download.\n` +
60
- `[kosli] Try: npm install -g @kosli/cli\n`
61
- );
62
- process.exit(1);
63
- }
64
-
65
- const binaryName = platform === "win32" ? "kosli.exe" : "kosli";
66
-
67
- // Cache under ~/.cache/kosli/<version>/<platform>-<arch>/kosli[.exe]
68
- const cacheDir = path.join(os.homedir(), ".cache", "kosli", version, `${platform}-${arch}`);
69
- const cachedBinary = path.join(cacheDir, binaryName);
70
-
71
- if (fs.existsSync(cachedBinary)) {
72
- return cachedBinary;
73
- }
74
-
75
- // Build GitHub release download URL
76
- // npm platform → goreleaser OS/arch naming
77
- const goOs = { darwin: "darwin", linux: "linux", win32: "windows" }[platform];
78
- const goArch = { x64: "amd64", arm64: "arm64", arm: "armv6" }[arch];
79
- const ext = platform === "win32" ? "zip" : "tar.gz";
80
- const versionNoV = version.replace(/^v/, "");
81
- const archiveName = `kosli_${versionNoV}_${goOs}_${goArch}.${ext}`;
82
- const downloadUrl = `https://github.com/kosli-dev/cli/releases/download/v${versionNoV}/${archiveName}`;
83
-
84
- process.stderr.write(`[kosli] Downloading ${archiveName}...\n`);
85
-
86
- const tmpFile = path.join(os.tmpdir(), `kosli-download-${process.pid}.${ext}`);
87
- try {
88
- await downloadToFile(downloadUrl, tmpFile);
89
-
90
- fs.mkdirSync(cacheDir, { recursive: true });
91
-
92
- // Extract just the binary from the archive.
93
- // The binary sits at the archive root (no wrapping directory).
94
- // `tar` on Linux/macOS handles tar.gz; bsdtar (Windows 10+) handles both tar.gz and zip.
95
- const tarFlags = ext === "tar.gz" ? ["-xzf"] : ["-xf"];
96
- const tarResult = spawnSync(
97
- "tar",
98
- [...tarFlags, tmpFile, binaryName, "-C", cacheDir],
99
- { stdio: ["ignore", "ignore", "pipe"] }
100
- );
101
- if (tarResult.status !== 0 || !fs.existsSync(cachedBinary)) {
102
- const detail = tarResult.stderr ? tarResult.stderr.toString().trim() : "unknown error";
103
- throw new Error(`Extraction failed: ${detail}`);
104
- }
105
-
106
- if (platform !== "win32") {
107
- fs.chmodSync(cachedBinary, 0o755);
108
- }
109
-
110
- return cachedBinary;
111
- } finally {
112
- try { fs.unlinkSync(tmpFile); } catch (_) {}
113
- }
114
- }
115
-
116
- function downloadToFile(url, dest) {
117
- return new Promise((resolve, reject) => {
118
- function get(url) {
119
- const mod = url.startsWith("https://") ? https : http;
120
- mod.get(url, (res) => {
121
- if ([301, 302, 307, 308].includes(res.statusCode)) {
122
- res.resume();
123
- return get(res.headers.location);
124
- }
125
- if (res.statusCode !== 200) {
126
- res.resume();
127
- return reject(new Error(`HTTP ${res.statusCode} from ${url}`));
128
- }
129
- const file = fs.createWriteStream(dest);
130
- res.pipe(file);
131
- file.on("finish", () => file.close(resolve));
132
- file.on("error", (err) => { fs.unlink(dest, () => {}); reject(err); });
133
- res.on("error", reject);
134
- }).on("error", reject);
135
- }
136
- get(url);
137
- });
32
+ binaryPath = path.join(packageDir, "bin", binaryName);
33
+ } catch (e) {
34
+ process.stderr.write(
35
+ `[kosli] Error: platform package ${packageName} is not installed.\n` +
36
+ `[kosli] Try reinstalling: npm install -g @kosli/cli\n`
37
+ );
38
+ process.exit(1);
138
39
  }
139
40
 
140
- run().catch((err) => {
141
- process.stderr.write(`[kosli] Error: ${err.message}\n`);
142
- process.stderr.write(`[kosli] Try: npm install -g @kosli/cli\n`);
41
+ const result = spawnSync(binaryPath, process.argv.slice(2), { stdio: "inherit" });
42
+ if (result.error) {
43
+ process.stderr.write(`[kosli] Error: failed to run binary: ${result.error.message}\n`);
143
44
  process.exit(1);
144
- });
45
+ }
46
+ process.exit(result.status ?? 1);
package/install.js CHANGED
@@ -3,7 +3,6 @@
3
3
  // Postinstall script: validates that the platform binary was installed correctly.
4
4
  // Runs after `npm install @kosli/cli`.
5
5
 
6
- const { execFileSync } = require("child_process");
7
6
  const path = require("path");
8
7
  const fs = require("fs");
9
8
 
@@ -18,10 +17,11 @@ const arch = process.arch;
18
17
 
19
18
  if (!SUPPORTED[platform] || !SUPPORTED[platform][arch]) {
20
19
  process.stderr.write(
21
- `[kosli] Note: ${platform}/${arch} is not a supported platform.\n` +
22
- `[kosli] The kosli binary will not be available on this system.\n`
20
+ `[kosli] Error: ${platform}/${arch} is not a supported platform.\n` +
21
+ `[kosli] See https://github.com/kosli-dev/cli for supported platforms.\n` +
22
+ `[kosli] Use --ignore-scripts to skip this check.\n`
23
23
  );
24
- process.exit(0);
24
+ process.exit(1);
25
25
  }
26
26
 
27
27
  const packageName = `@kosli/cli-${platform}-${arch}`;
@@ -52,10 +52,10 @@ if (!fs.existsSync(binaryPath)) {
52
52
  }
53
53
 
54
54
  try {
55
- execFileSync(binaryPath, ["version"], { stdio: "ignore" });
55
+ fs.accessSync(binaryPath, fs.constants.X_OK);
56
56
  } catch (e) {
57
57
  process.stderr.write(
58
- `[kosli] Error: binary validation failed: ${e.message}\n` +
58
+ `[kosli] Error: binary is not executable: ${e.message}\n` +
59
59
  `[kosli] Try reinstalling: npm install -g @kosli/cli\n`
60
60
  );
61
61
  process.exit(1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kosli/cli",
3
- "version": "2.13.1-SNAPSHOT-b45c7e67",
3
+ "version": "2.15.0",
4
4
  "description": "CLI client for reporting compliance events to https://kosli.com",
5
5
  "license": "MIT",
6
6
  "author": "Kosli Inc. <hello@kosli.com>",
@@ -31,16 +31,17 @@
31
31
  "install.js"
32
32
  ],
33
33
  "scripts": {
34
+ "prepack": "test -f bin/kosli || (echo 'ERROR: bin/kosli is missing' && exit 1) && test -f install.js || (echo 'ERROR: install.js is missing' && exit 1)",
34
35
  "postinstall": "node install.js"
35
36
  },
36
37
  "optionalDependencies": {
37
- "@kosli/cli-darwin-arm64": "2.13.1-SNAPSHOT-b45c7e67",
38
- "@kosli/cli-darwin-x64": "2.13.1-SNAPSHOT-b45c7e67",
39
- "@kosli/cli-linux-arm": "2.13.1-SNAPSHOT-b45c7e67",
40
- "@kosli/cli-linux-arm64": "2.13.1-SNAPSHOT-b45c7e67",
41
- "@kosli/cli-linux-x64": "2.13.1-SNAPSHOT-b45c7e67",
42
- "@kosli/cli-win32-arm64": "2.13.1-SNAPSHOT-b45c7e67",
43
- "@kosli/cli-win32-x64": "2.13.1-SNAPSHOT-b45c7e67"
38
+ "@kosli/cli-darwin-arm64": "2.15.0",
39
+ "@kosli/cli-darwin-x64": "2.15.0",
40
+ "@kosli/cli-linux-arm": "2.15.0",
41
+ "@kosli/cli-linux-arm64": "2.15.0",
42
+ "@kosli/cli-linux-x64": "2.15.0",
43
+ "@kosli/cli-win32-arm64": "2.15.0",
44
+ "@kosli/cli-win32-x64": "2.15.0"
44
45
  },
45
46
  "publishConfig": {
46
47
  "registry": "https://registry.npmjs.org",