@dif.sh/cli 0.1.1

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 (4) hide show
  1. package/README.md +34 -0
  2. package/bin/dif.js +35 -0
  3. package/install.js +112 -0
  4. package/package.json +44 -0
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # @dif.sh/cli
2
+
3
+ The dif.sh CLI, packaged for npm.
4
+
5
+ ```sh
6
+ npm install -g @dif.sh/cli
7
+ dif --help
8
+ ```
9
+
10
+ Under the hood, this package downloads the matching Rust binary from the
11
+ [dif-sh/dif](https://github.com/dif-sh/dif) GitHub release matching its own
12
+ version, drops it in `node_modules/@dif.sh/cli/bin/`, and exposes a thin
13
+ Node shim as the `dif` command.
14
+
15
+ If you'd rather skip the wrapper entirely, install the binary directly:
16
+
17
+ ```sh
18
+ # macOS / Linux
19
+ curl https://dif.sh/install.sh | sh
20
+
21
+ # macOS via Homebrew
22
+ brew install dif-sh/tap/dif
23
+ ```
24
+
25
+ See [the main repo](https://github.com/dif-sh/dif) for full documentation.
26
+
27
+ ## Platforms
28
+
29
+ - macOS (Apple Silicon + Intel)
30
+ - Linux x86_64 + aarch64
31
+ - Windows x86_64
32
+
33
+ Other platforms fall through to a clear error message pointing at the GitHub
34
+ release page where you can download a binary manually.
package/bin/dif.js ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env node
2
+ // Thin Node shim that exec's the platform-matched Rust binary. The actual
3
+ // binary is downloaded by ./install.js as a postinstall step and lives
4
+ // alongside this file in the same `bin/` directory.
5
+ //
6
+ // We use spawnSync with `stdio: "inherit"` so the user's terminal sees the
7
+ // binary's output directly — no buffering, no signal-handling indirection.
8
+
9
+ "use strict";
10
+
11
+ const path = require("node:path");
12
+ const { spawnSync } = require("node:child_process");
13
+
14
+ const binaryName = process.platform === "win32" ? "dif.exe" : "dif";
15
+ const binaryPath = path.join(__dirname, binaryName);
16
+
17
+ const result = spawnSync(binaryPath, process.argv.slice(2), {
18
+ stdio: "inherit",
19
+ windowsHide: true,
20
+ });
21
+
22
+ if (result.error) {
23
+ if (result.error.code === "ENOENT") {
24
+ console.error(
25
+ `@dif.sh/cli: binary not found at ${binaryPath}.\n` +
26
+ `If you skipped postinstall, run \`node ${path.join(__dirname, "..", "install.js")}\` ` +
27
+ `or reinstall the package.`,
28
+ );
29
+ process.exit(127);
30
+ }
31
+ console.error(`@dif.sh/cli: ${result.error.message}`);
32
+ process.exit(1);
33
+ }
34
+
35
+ process.exit(result.status ?? 0);
package/install.js ADDED
@@ -0,0 +1,112 @@
1
+ #!/usr/bin/env node
2
+ // postinstall — download the platform-matched dif binary from the matching
3
+ // GitHub release and place it at ./bin/dif (or dif.exe on Windows).
4
+ //
5
+ // Version is read from this package's own package.json so each published
6
+ // wrapper version pairs with exactly one binary release.
7
+
8
+ "use strict";
9
+
10
+ const fs = require("node:fs");
11
+ const os = require("node:os");
12
+ const path = require("node:path");
13
+ const https = require("node:https");
14
+ const { execFileSync } = require("node:child_process");
15
+
16
+ const ROOT = __dirname;
17
+ const PKG = JSON.parse(fs.readFileSync(path.join(ROOT, "package.json"), "utf8"));
18
+ const VERSION = PKG.version;
19
+ const REPO = "dif-sh/dif";
20
+ const BIN_DIR = path.join(ROOT, "bin");
21
+
22
+ const TARGETS = {
23
+ "darwin-arm64": { archive: "dif-aarch64-apple-darwin.tar.gz", binary: "dif", ext: "tar.gz" },
24
+ "darwin-x64": { archive: "dif-x86_64-apple-darwin.tar.gz", binary: "dif", ext: "tar.gz" },
25
+ "linux-x64": { archive: "dif-x86_64-unknown-linux-gnu.tar.gz", binary: "dif", ext: "tar.gz" },
26
+ "linux-arm64": { archive: "dif-aarch64-unknown-linux-gnu.tar.gz", binary: "dif", ext: "tar.gz" },
27
+ "win32-x64": { archive: "dif-x86_64-pc-windows-msvc.zip", binary: "dif.exe", ext: "zip" },
28
+ };
29
+
30
+ function platformKey() {
31
+ return `${process.platform}-${process.arch}`;
32
+ }
33
+
34
+ function fail(msg) {
35
+ console.error(`@dif.sh/cli: ${msg}`);
36
+ process.exit(1);
37
+ }
38
+
39
+ function download(url, destPath) {
40
+ return new Promise((resolve, reject) => {
41
+ const file = fs.createWriteStream(destPath);
42
+ const handleResponse = (res) => {
43
+ if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
44
+ res.resume();
45
+ https.get(res.headers.location, handleResponse).on("error", reject);
46
+ return;
47
+ }
48
+ if (res.statusCode !== 200) {
49
+ reject(new Error(`HTTP ${res.statusCode} for ${url}`));
50
+ return;
51
+ }
52
+ res.pipe(file);
53
+ file.on("finish", () => file.close(resolve));
54
+ };
55
+ https.get(url, handleResponse).on("error", reject);
56
+ });
57
+ }
58
+
59
+ function extract(archivePath, ext, destDir) {
60
+ if (ext === "tar.gz") {
61
+ // `tar` is universally available on macOS and Linux. Windows uses zip.
62
+ execFileSync("tar", ["-xzf", archivePath, "-C", destDir], { stdio: "inherit" });
63
+ return;
64
+ }
65
+ if (ext === "zip") {
66
+ // PowerShell ships on Windows by default.
67
+ execFileSync(
68
+ "powershell",
69
+ ["-NoProfile", "-Command", `Expand-Archive -Path '${archivePath}' -DestinationPath '${destDir}' -Force`],
70
+ { stdio: "inherit" },
71
+ );
72
+ return;
73
+ }
74
+ throw new Error(`unknown archive type: ${ext}`);
75
+ }
76
+
77
+ async function main() {
78
+ const key = platformKey();
79
+ const target = TARGETS[key];
80
+ if (!target) {
81
+ fail(
82
+ `unsupported platform ${key}. ` +
83
+ `Install the binary manually from https://github.com/${REPO}/releases/v${VERSION}.`,
84
+ );
85
+ }
86
+
87
+ // CI / local dev path: if a pre-built binary is already provided (e.g. by
88
+ // `cargo build` in a monorepo dev loop), skip the download entirely.
89
+ const binaryPath = path.join(BIN_DIR, target.binary);
90
+ if (fs.existsSync(binaryPath)) {
91
+ process.exit(0);
92
+ }
93
+
94
+ fs.mkdirSync(BIN_DIR, { recursive: true });
95
+
96
+ const url = `https://github.com/${REPO}/releases/download/v${VERSION}/${target.archive}`;
97
+ const tmp = path.join(os.tmpdir(), `${PKG.name.replace(/[^a-z0-9]/gi, "_")}-${VERSION}.${target.ext}`);
98
+
99
+ try {
100
+ console.log(`@dif.sh/cli: downloading ${url}`);
101
+ await download(url, tmp);
102
+ extract(tmp, target.ext, BIN_DIR);
103
+ fs.chmodSync(binaryPath, 0o755);
104
+ console.log(`@dif.sh/cli: installed dif v${VERSION} for ${key}`);
105
+ } catch (err) {
106
+ fail(`installation failed: ${err.message}`);
107
+ } finally {
108
+ try { fs.unlinkSync(tmp); } catch (_) { /* best-effort */ }
109
+ }
110
+ }
111
+
112
+ main().catch((err) => fail(err.message));
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@dif.sh/cli",
3
+ "version": "0.1.1",
4
+ "description": "The dif.sh CLI. Experiments live in the repo. This package downloads the matching Rust binary on install.",
5
+ "license": "MIT",
6
+ "bin": {
7
+ "dif": "./bin/dif.js"
8
+ },
9
+ "files": [
10
+ "bin",
11
+ "install.js",
12
+ "README.md"
13
+ ],
14
+ "scripts": {
15
+ "postinstall": "node install.js"
16
+ },
17
+ "engines": {
18
+ "node": ">=18"
19
+ },
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "https://github.com/dif-sh/dif",
26
+ "directory": "cli/packages/cli"
27
+ },
28
+ "keywords": [
29
+ "experimentation",
30
+ "ab-testing",
31
+ "feature-flags",
32
+ "dif.sh",
33
+ "cli"
34
+ ],
35
+ "os": [
36
+ "darwin",
37
+ "linux",
38
+ "win32"
39
+ ],
40
+ "cpu": [
41
+ "x64",
42
+ "arm64"
43
+ ]
44
+ }