@alibaba-group/open-code-review 1.0.0 → 1.0.7

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.
package/bin/ocr.js CHANGED
@@ -1,11 +1,36 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
3
 
4
- const { spawnSync } = require("child_process");
4
+ const { spawnSync, spawn } = require("child_process");
5
5
  const path = require("path");
6
+ const fs = require("fs");
7
+ const os = require("os");
6
8
 
7
9
  const binaryPath = path.join(__dirname, "opencodereview");
8
10
 
11
+ if (!process.env.OCR_NO_UPDATE) {
12
+ const stateDir = path.join(os.homedir(), ".open-code-review");
13
+ const tsFile = path.join(stateDir, "last-update-check");
14
+ const cooldownMs =
15
+ (parseInt(process.env.OCR_UPDATE_INTERVAL, 10) || 60) * 60 * 1000;
16
+
17
+ let shouldCheck = true;
18
+ try {
19
+ const mt = fs.statSync(tsFile).mtimeMs;
20
+ if (Date.now() - mt < cooldownMs) shouldCheck = false;
21
+ } catch (_) {}
22
+
23
+ if (shouldCheck) {
24
+ const updateScript = path.join(__dirname, "..", "scripts", "update.js");
25
+ const child = spawn(process.execPath, [updateScript], {
26
+ detached: true,
27
+ stdio: "ignore",
28
+ env: Object.assign({}, process.env, { OCR_NO_UPDATE: "1" }),
29
+ });
30
+ child.unref();
31
+ }
32
+ }
33
+
9
34
  const result = spawnSync(binaryPath, process.argv.slice(2), {
10
35
  stdio: "inherit",
11
36
  env: process.env,
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alibaba-group/open-code-review",
3
- "version": "1.0.0",
3
+ "version": "1.0.7",
4
4
  "description": "OpenCodeReview CLI — AI-powered code review tool",
5
5
  "bin": {
6
6
  "ocr": "bin/ocr.js"
@@ -218,7 +218,19 @@ async function main() {
218
218
  info(" ocr review Start a code review");
219
219
  }
220
220
 
221
- main().catch((err) => {
222
- error(err.message);
223
- process.exit(1);
224
- });
221
+ if (require.main === module) {
222
+ main().catch((err) => {
223
+ error(err.message);
224
+ process.exit(1);
225
+ });
226
+ } else {
227
+ module.exports = {
228
+ detectPlatform,
229
+ loadPackageJson,
230
+ buildUrl,
231
+ download,
232
+ downloadText,
233
+ downloadBinary,
234
+ computeChecksum,
235
+ };
236
+ }
@@ -0,0 +1,201 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ const fs = require("fs");
5
+ const path = require("path");
6
+ const os = require("os");
7
+ const http = require("http");
8
+ const https = require("https");
9
+ const { spawnSync } = require("child_process");
10
+
11
+ const {
12
+ detectPlatform,
13
+ loadPackageJson,
14
+ buildUrl,
15
+ downloadText,
16
+ downloadBinary,
17
+ computeChecksum,
18
+ } = require("./install.js");
19
+
20
+ const packageRoot = path.join(__dirname, "..");
21
+ const binDir = path.join(packageRoot, "bin");
22
+ const binaryPath = path.join(binDir, "opencodereview");
23
+ const stateDir = path.join(os.homedir(), ".open-code-review");
24
+ const tsFile = path.join(stateDir, "last-update-check");
25
+ const lockFile = path.join(stateDir, "update.lock");
26
+
27
+ const DEFAULT_REGISTRY = "https://registry.npmjs.org";
28
+
29
+ function touchTimestamp() {
30
+ fs.mkdirSync(stateDir, { recursive: true });
31
+ const now = new Date();
32
+ try {
33
+ fs.utimesSync(tsFile, now, now);
34
+ } catch (_) {
35
+ fs.writeFileSync(tsFile, now.toISOString());
36
+ }
37
+ }
38
+
39
+ function acquireLock() {
40
+ fs.mkdirSync(stateDir, { recursive: true });
41
+ try {
42
+ fs.writeFileSync(lockFile, String(process.pid), { flag: "wx" });
43
+ return true;
44
+ } catch (e) {
45
+ if (e.code !== "EEXIST") return false;
46
+ try {
47
+ const pid = parseInt(fs.readFileSync(lockFile, "utf8").trim(), 10);
48
+ process.kill(pid, 0);
49
+ return false;
50
+ } catch (_) {
51
+ try {
52
+ fs.unlinkSync(lockFile);
53
+ fs.writeFileSync(lockFile, String(process.pid), { flag: "wx" });
54
+ return true;
55
+ } catch (_2) {
56
+ return false;
57
+ }
58
+ }
59
+ }
60
+ }
61
+
62
+ function releaseLock() {
63
+ try {
64
+ fs.unlinkSync(lockFile);
65
+ } catch (_) {}
66
+ }
67
+
68
+ function getInstalledVersion() {
69
+ try {
70
+ const result = spawnSync(binaryPath, ["version"], {
71
+ encoding: "utf8",
72
+ timeout: 3000,
73
+ });
74
+ const match = (result.stdout || "").match(/v(\d+\.\d+(?:\.\d+)?)/);
75
+ return match ? match[1] : null;
76
+ } catch (_) {
77
+ return null;
78
+ }
79
+ }
80
+
81
+ function fetchLatestVersion(pkg) {
82
+ const registry = (pkg.publishConfig && pkg.publishConfig.registry) || DEFAULT_REGISTRY;
83
+ const pkgName = pkg.name;
84
+ if (!pkgName) return Promise.resolve(null);
85
+ const encodedName = pkgName.replace(/\//g, "%2F");
86
+ const url = `${registry.replace(/\/$/, "")}/${encodedName}/latest`;
87
+ const client = url.startsWith("https") ? https : http;
88
+
89
+ return new Promise((resolve) => {
90
+ const options = {
91
+ headers: { "User-Agent": "ocr-updater", Accept: "application/json" },
92
+ timeout: 15000,
93
+ };
94
+ const req = client
95
+ .get(url, options, (res) => {
96
+ if (res.statusCode !== 200) {
97
+ res.resume();
98
+ resolve(null);
99
+ return;
100
+ }
101
+ let data = "";
102
+ res.on("data", (chunk) => (data += chunk));
103
+ res.on("end", () => {
104
+ try {
105
+ const json = JSON.parse(data);
106
+ resolve(json.version || null);
107
+ } catch (_) {
108
+ resolve(null);
109
+ }
110
+ });
111
+ res.on("error", () => resolve(null));
112
+ })
113
+ .on("error", () => resolve(null));
114
+ req.on("timeout", () => {
115
+ req.destroy();
116
+ resolve(null);
117
+ });
118
+ });
119
+ }
120
+
121
+ function semverGt(a, b) {
122
+ const pa = a.split(".").map(Number);
123
+ const pb = b.split(".").map(Number);
124
+ for (let i = 0; i < 3; i++) {
125
+ if ((pa[i] || 0) > (pb[i] || 0)) return true;
126
+ if ((pa[i] || 0) < (pb[i] || 0)) return false;
127
+ }
128
+ return false;
129
+ }
130
+
131
+ function cleanupTemp() {
132
+ try {
133
+ const files = fs.readdirSync(binDir);
134
+ for (const f of files) {
135
+ if (f.startsWith(".opencodereview.tmp.")) {
136
+ fs.unlinkSync(path.join(binDir, f));
137
+ }
138
+ }
139
+ } catch (_) {}
140
+ }
141
+
142
+ async function main() {
143
+ touchTimestamp();
144
+
145
+ if (!acquireLock()) return;
146
+
147
+ cleanupTemp();
148
+
149
+ try {
150
+ const installedVersion = getInstalledVersion();
151
+ if (!installedVersion) return;
152
+
153
+ const pkg = loadPackageJson();
154
+ const latestVersion = await fetchLatestVersion(pkg);
155
+ if (!latestVersion) return;
156
+
157
+ if (!semverGt(latestVersion, installedVersion)) return;
158
+
159
+ const { os: platform, arch } = detectPlatform();
160
+ const config = pkg.ocrConfig;
161
+
162
+ const vars = { version: latestVersion, os: platform, arch };
163
+ const downloadUrl = buildUrl(config.urlPattern, vars);
164
+
165
+ const tempPath = path.join(binDir, `.opencodereview.tmp.${process.pid}`);
166
+ await downloadBinary(downloadUrl, tempPath);
167
+ fs.chmodSync(tempPath, 0o755);
168
+
169
+ if (config.checksumPattern) {
170
+ try {
171
+ const checksumUrl = buildUrl(config.checksumPattern, vars);
172
+ const shaContent = await downloadText(checksumUrl);
173
+ const actualSha = await computeChecksum(tempPath);
174
+
175
+ let verified = false;
176
+ for (const line of shaContent.split("\n")) {
177
+ const trimmed = line.trim();
178
+ if (trimmed.includes(`-${platform}-${arch}`)) {
179
+ const expectedSha = trimmed.split(/\s+/)[0].toLowerCase();
180
+ if (expectedSha && actualSha !== expectedSha) {
181
+ fs.unlinkSync(tempPath);
182
+ return;
183
+ }
184
+ verified = true;
185
+ break;
186
+ }
187
+ }
188
+ } catch (_) {
189
+ // checksum fetch failed, continue with the download
190
+ }
191
+ }
192
+
193
+ fs.renameSync(tempPath, binaryPath);
194
+ } catch (_) {
195
+ cleanupTemp();
196
+ } finally {
197
+ releaseLock();
198
+ }
199
+ }
200
+
201
+ main().catch(() => {});