@agencycli/agencycli 0.1.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/install.js +216 -0
  2. package/package.json +42 -0
  3. package/run.js +72 -0
package/install.js ADDED
@@ -0,0 +1,216 @@
1
+ #!/usr/bin/env node
2
+
3
+ "use strict";
4
+
5
+ const { execSync } = require("child_process");
6
+ const fs = require("fs");
7
+ const path = require("path");
8
+ const https = require("https");
9
+ const http = require("http");
10
+
11
+ const PACKAGE = require("./package.json");
12
+ const VERSION = `v${PACKAGE.version}`;
13
+ const NAME = "agencycli";
14
+
15
+ const GITHUB_REPO = "chenhg5/agencycli";
16
+ const GITEE_REPO = "cg33/agencycli";
17
+
18
+ const PLATFORM_MAP = {
19
+ darwin: "darwin",
20
+ linux: "linux",
21
+ win32: "windows",
22
+ };
23
+
24
+ const ARCH_MAP = {
25
+ x64: "amd64",
26
+ arm64: "arm64",
27
+ };
28
+
29
+ function getPlatformInfo() {
30
+ const platform = PLATFORM_MAP[process.platform];
31
+ const arch = ARCH_MAP[process.arch];
32
+ if (!platform || !arch) {
33
+ throw new Error(
34
+ `Unsupported platform: ${process.platform}/${process.arch}. ` +
35
+ `Supported: linux/darwin/windows × amd64/arm64`
36
+ );
37
+ }
38
+ const ext = platform === "windows" ? ".zip" : ".tar.gz";
39
+ const filename = `${NAME}-${VERSION}-${platform}-${arch}${ext}`;
40
+ return { platform, arch, ext, filename };
41
+ }
42
+
43
+ function getDownloadURLs(filename) {
44
+ return [
45
+ `https://github.com/${GITHUB_REPO}/releases/download/${VERSION}/${filename}`,
46
+ `https://gitee.com/${GITEE_REPO}/releases/download/${VERSION}/${filename}`,
47
+ ];
48
+ }
49
+
50
+ function fetch(url, redirects = 5) {
51
+ return new Promise((resolve, reject) => {
52
+ if (redirects <= 0) return reject(new Error("Too many redirects"));
53
+ const mod = url.startsWith("https") ? https : http;
54
+ mod
55
+ .get(url, { headers: { "User-Agent": "agencycli-npm-installer" } }, (res) => {
56
+ if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
57
+ return resolve(fetch(res.headers.location, redirects - 1));
58
+ }
59
+ if (res.statusCode !== 200) {
60
+ res.resume();
61
+ return reject(new Error(`HTTP ${res.statusCode} for ${url}`));
62
+ }
63
+ const chunks = [];
64
+ res.on("data", (c) => chunks.push(c));
65
+ res.on("end", () => resolve(Buffer.concat(chunks)));
66
+ res.on("error", reject);
67
+ })
68
+ .on("error", reject);
69
+ });
70
+ }
71
+
72
+ async function download(urls) {
73
+ for (const url of urls) {
74
+ try {
75
+ console.log(`[agencycli] Downloading from ${url}`);
76
+ const data = await fetch(url);
77
+ console.log(`[agencycli] Downloaded ${(data.length / 1024 / 1024).toFixed(1)} MB`);
78
+ return data;
79
+ } catch (err) {
80
+ console.warn(`[agencycli] Failed: ${err.message}, trying next source…`);
81
+ }
82
+ }
83
+ throw new Error(
84
+ `[agencycli] Could not download binary from any source.\n` +
85
+ ` Tried: ${urls.join(", ")}\n` +
86
+ ` Manual download: https://github.com/${GITHUB_REPO}/releases`
87
+ );
88
+ }
89
+
90
+ function extractTarGz(buffer, destDir, binaryName) {
91
+ const tmpFile = path.join(destDir, "_tmp.tar.gz");
92
+ fs.writeFileSync(tmpFile, buffer);
93
+ try {
94
+ execSync(`tar xzf "${tmpFile}" -C "${destDir}"`, { stdio: "pipe" });
95
+ } finally {
96
+ fs.unlinkSync(tmpFile);
97
+ }
98
+ const extracted = fs.readdirSync(destDir).find(
99
+ (f) => f.startsWith(NAME) && !f.endsWith(".tar.gz")
100
+ );
101
+ if (extracted && extracted !== binaryName) {
102
+ fs.renameSync(path.join(destDir, extracted), path.join(destDir, binaryName));
103
+ }
104
+ }
105
+
106
+ function extractZip(buffer, destDir, binaryName) {
107
+ const tmpFile = path.join(destDir, "_tmp.zip");
108
+ fs.writeFileSync(tmpFile, buffer);
109
+ try {
110
+ try {
111
+ execSync(`unzip -o "${tmpFile}" -d "${destDir}"`, { stdio: "pipe" });
112
+ } catch {
113
+ execSync(
114
+ `powershell -Command "Expand-Archive -Force '${tmpFile}' '${destDir}'"`,
115
+ { stdio: "pipe" }
116
+ );
117
+ }
118
+ } finally {
119
+ try { fs.unlinkSync(tmpFile); } catch {}
120
+ }
121
+ const extracted = fs.readdirSync(destDir).find(
122
+ (f) => f.startsWith(NAME) && f.endsWith(".exe")
123
+ );
124
+ if (extracted && extracted !== binaryName) {
125
+ fs.renameSync(path.join(destDir, extracted), path.join(destDir, binaryName));
126
+ }
127
+ }
128
+
129
+ // parseVersion splits "1.2.3-beta.1" into { nums: [1,2,3], pre: "beta.1" }
130
+ function parseVersion(v) {
131
+ v = v.replace(/^v/, "").trim();
132
+ const [base, ...rest] = v.split("-");
133
+ const nums = base.split(".").map(Number);
134
+ return { nums, pre: rest.join("-") };
135
+ }
136
+
137
+ // isNewerOrEqual returns true if installed >= expected
138
+ function isNewerOrEqual(installed, expected) {
139
+ const a = parseVersion(installed);
140
+ const b = parseVersion(expected);
141
+ const len = Math.max(a.nums.length, b.nums.length);
142
+ for (let i = 0; i < len; i++) {
143
+ const av = a.nums[i] || 0;
144
+ const bv = b.nums[i] || 0;
145
+ if (av > bv) return true;
146
+ if (av < bv) return false;
147
+ }
148
+ if (!a.pre && b.pre) return true;
149
+ if (a.pre && !b.pre) return false;
150
+ return a.pre >= b.pre;
151
+ }
152
+
153
+ async function main() {
154
+ const { platform, arch, ext, filename } = getPlatformInfo();
155
+ console.log(`[agencycli] Platform: ${platform}/${arch}`);
156
+
157
+ const binDir = path.join(__dirname, "bin");
158
+ fs.mkdirSync(binDir, { recursive: true });
159
+
160
+ const binaryName = platform === "windows" ? `${NAME}.exe` : NAME;
161
+ const binaryPath = path.join(binDir, binaryName);
162
+
163
+ if (fs.existsSync(binaryPath)) {
164
+ try {
165
+ const out = execSync(`"${binaryPath}" version`, { encoding: "utf8", timeout: 5000 });
166
+ const expectedVer = VERSION.slice(1); // strip leading "v"
167
+ if (out.includes(expectedVer)) {
168
+ console.log(`[agencycli] Binary ${VERSION} already installed, skipping.`);
169
+ return;
170
+ }
171
+ const match = out.match(/(\d+\.\d+\.\d+[^\s]*)/);
172
+ if (match && isNewerOrEqual(match[1], expectedVer)) {
173
+ console.log(`[agencycli] Binary ${match[1]} is newer than ${VERSION}, skipping.`);
174
+ return;
175
+ }
176
+ console.log(`[agencycli] Existing binary is outdated, upgrading to ${VERSION}…`);
177
+ fs.unlinkSync(binaryPath);
178
+ } catch {
179
+ console.log(`[agencycli] Replacing existing binary with ${VERSION}…`);
180
+ fs.unlinkSync(binaryPath);
181
+ }
182
+ }
183
+
184
+ const urls = getDownloadURLs(filename);
185
+ const data = await download(urls);
186
+
187
+ if (ext === ".tar.gz") {
188
+ extractTarGz(data, binDir, binaryName);
189
+ } else {
190
+ extractZip(data, binDir, binaryName);
191
+ }
192
+
193
+ if (platform !== "windows") {
194
+ fs.chmodSync(binaryPath, 0o755);
195
+ }
196
+
197
+ if (platform === "darwin") {
198
+ try {
199
+ execSync(`xattr -d com.apple.quarantine "${binaryPath}"`, { stdio: "pipe" });
200
+ console.log(`[agencycli] Removed macOS quarantine attribute.`);
201
+ } catch {
202
+ // attribute may not exist — that's fine
203
+ }
204
+ }
205
+
206
+ console.log(`[agencycli] Installed to ${binaryPath}`);
207
+ }
208
+
209
+ main().catch((err) => {
210
+ console.error(err.message);
211
+ console.error(
212
+ "[agencycli] Installation failed. Download manually:\n" +
213
+ ` https://github.com/${GITHUB_REPO}/releases/tag/${VERSION}`
214
+ );
215
+ process.exit(1);
216
+ });
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@agencycli/agencycli",
3
+ "version": "0.1.0",
4
+ "description": "AI agent organisation and context management — hire agents into teams, assign tasks, and automate workflows",
5
+ "keywords": [
6
+ "ai-agent",
7
+ "claude-code",
8
+ "codex",
9
+ "gemini",
10
+ "cursor",
11
+ "llm",
12
+ "workflow",
13
+ "context-management",
14
+ "agent-orchestration"
15
+ ],
16
+ "homepage": "https://github.com/chenhg5/agencycli",
17
+ "bugs": {
18
+ "url": "https://github.com/chenhg5/agencycli/issues"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/chenhg5/agencycli.git"
23
+ },
24
+ "license": "MIT",
25
+ "author": "chenhg5",
26
+ "type": "commonjs",
27
+ "main": "install.js",
28
+ "bin": {
29
+ "agencycli": "run.js"
30
+ },
31
+ "files": [
32
+ "install.js",
33
+ "run.js",
34
+ "README.md"
35
+ ],
36
+ "scripts": {
37
+ "postinstall": "node install.js"
38
+ },
39
+ "engines": {
40
+ "node": ">=16"
41
+ }
42
+ }
package/run.js ADDED
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env node
2
+
3
+ "use strict";
4
+
5
+ const { execFileSync, execSync } = require("child_process");
6
+ const path = require("path");
7
+ const fs = require("fs");
8
+
9
+ const PACKAGE = require("./package.json");
10
+ const EXPECTED_VER = PACKAGE.version;
11
+ const NAME = "agencycli";
12
+ const binDir = path.join(__dirname, "bin");
13
+ const ext = process.platform === "win32" ? ".exe" : "";
14
+ const binaryPath = path.join(binDir, NAME + ext);
15
+
16
+ // parseVersion splits "1.2.3-beta.1" into { nums: [1,2,3], pre: "beta.1" }
17
+ function parseVersion(v) {
18
+ v = v.replace(/^v/, "").trim();
19
+ const [base, ...rest] = v.split("-");
20
+ const nums = base.split(".").map(Number);
21
+ return { nums, pre: rest.join("-") };
22
+ }
23
+
24
+ function isNewerOrEqual(installed, expected) {
25
+ const a = parseVersion(installed);
26
+ const b = parseVersion(expected);
27
+ const len = Math.max(a.nums.length, b.nums.length);
28
+ for (let i = 0; i < len; i++) {
29
+ const av = a.nums[i] || 0;
30
+ const bv = b.nums[i] || 0;
31
+ if (av > bv) return true;
32
+ if (av < bv) return false;
33
+ }
34
+ if (!a.pre && b.pre) return true;
35
+ if (a.pre && !b.pre) return false;
36
+ return a.pre >= b.pre;
37
+ }
38
+
39
+ function needsReinstall() {
40
+ if (!fs.existsSync(binaryPath)) return true;
41
+ try {
42
+ const out = execFileSync(binaryPath, ["version"], { encoding: "utf8", timeout: 5000 });
43
+ if (out.includes(EXPECTED_VER)) return false;
44
+ const match = out.match(/(\d+\.\d+\.\d+[^\s]*)/);
45
+ if (match && isNewerOrEqual(match[1], EXPECTED_VER)) return false;
46
+ return true;
47
+ } catch {
48
+ return true;
49
+ }
50
+ }
51
+
52
+ if (needsReinstall()) {
53
+ console.log(`[agencycli] Binary missing or outdated, installing v${EXPECTED_VER}…`);
54
+ try {
55
+ execSync("node " + JSON.stringify(path.join(__dirname, "install.js")), {
56
+ stdio: "inherit",
57
+ cwd: __dirname,
58
+ });
59
+ } catch {
60
+ console.error(
61
+ "[agencycli] Auto-install failed.\n" +
62
+ " Run manually: npm uninstall -g agencycli && npm install -g agencycli"
63
+ );
64
+ process.exit(1);
65
+ }
66
+ }
67
+
68
+ try {
69
+ execFileSync(binaryPath, process.argv.slice(2), { stdio: "inherit" });
70
+ } catch (err) {
71
+ process.exit(err.status || 1);
72
+ }