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

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.6",
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,199 @@
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 https = require("https");
8
+ const { spawnSync } = require("child_process");
9
+
10
+ const {
11
+ detectPlatform,
12
+ loadPackageJson,
13
+ buildUrl,
14
+ downloadText,
15
+ downloadBinary,
16
+ computeChecksum,
17
+ } = require("./install.js");
18
+
19
+ const packageRoot = path.join(__dirname, "..");
20
+ const binDir = path.join(packageRoot, "bin");
21
+ const binaryPath = path.join(binDir, "opencodereview");
22
+ const stateDir = path.join(os.homedir(), ".open-code-review");
23
+ const tsFile = path.join(stateDir, "last-update-check");
24
+ const lockFile = path.join(stateDir, "update.lock");
25
+
26
+ const GITHUB_API_URL =
27
+ "https://api.github.com/repos/alibaba/open-code-review/releases/latest";
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() {
82
+ return new Promise((resolve) => {
83
+ const options = {
84
+ headers: {
85
+ "User-Agent": "ocr-updater",
86
+ Accept: "application/vnd.github.v3+json",
87
+ },
88
+ timeout: 15000,
89
+ };
90
+ const req = https
91
+ .get(GITHUB_API_URL, options, (res) => {
92
+ if (res.statusCode !== 200) {
93
+ res.resume();
94
+ resolve(null);
95
+ return;
96
+ }
97
+ let data = "";
98
+ res.on("data", (chunk) => (data += chunk));
99
+ res.on("end", () => {
100
+ try {
101
+ const json = JSON.parse(data);
102
+ const tag = json.tag_name || "";
103
+ const version = tag.startsWith("v") ? tag.slice(1) : tag;
104
+ resolve(version || null);
105
+ } catch (_) {
106
+ resolve(null);
107
+ }
108
+ });
109
+ res.on("error", () => resolve(null));
110
+ })
111
+ .on("error", () => resolve(null));
112
+ req.on("timeout", () => {
113
+ req.destroy();
114
+ resolve(null);
115
+ });
116
+ });
117
+ }
118
+
119
+ function semverGt(a, b) {
120
+ const pa = a.split(".").map(Number);
121
+ const pb = b.split(".").map(Number);
122
+ for (let i = 0; i < 3; i++) {
123
+ if ((pa[i] || 0) > (pb[i] || 0)) return true;
124
+ if ((pa[i] || 0) < (pb[i] || 0)) return false;
125
+ }
126
+ return false;
127
+ }
128
+
129
+ function cleanupTemp() {
130
+ try {
131
+ const files = fs.readdirSync(binDir);
132
+ for (const f of files) {
133
+ if (f.startsWith(".opencodereview.tmp.")) {
134
+ fs.unlinkSync(path.join(binDir, f));
135
+ }
136
+ }
137
+ } catch (_) {}
138
+ }
139
+
140
+ async function main() {
141
+ touchTimestamp();
142
+
143
+ if (!acquireLock()) return;
144
+
145
+ cleanupTemp();
146
+
147
+ try {
148
+ const installedVersion = getInstalledVersion();
149
+ if (!installedVersion) return;
150
+
151
+ const latestVersion = await fetchLatestVersion();
152
+ if (!latestVersion) return;
153
+
154
+ if (!semverGt(latestVersion, installedVersion)) return;
155
+
156
+ const { os: platform, arch } = detectPlatform();
157
+ const pkg = loadPackageJson();
158
+ const config = pkg.ocrConfig;
159
+
160
+ const vars = { version: latestVersion, os: platform, arch };
161
+ const downloadUrl = buildUrl(config.urlPattern, vars);
162
+
163
+ const tempPath = path.join(binDir, `.opencodereview.tmp.${process.pid}`);
164
+ await downloadBinary(downloadUrl, tempPath);
165
+ fs.chmodSync(tempPath, 0o755);
166
+
167
+ if (config.checksumPattern) {
168
+ try {
169
+ const checksumUrl = buildUrl(config.checksumPattern, vars);
170
+ const shaContent = await downloadText(checksumUrl);
171
+ const actualSha = await computeChecksum(tempPath);
172
+
173
+ let verified = false;
174
+ for (const line of shaContent.split("\n")) {
175
+ const trimmed = line.trim();
176
+ if (trimmed.includes(`-${platform}-${arch}`)) {
177
+ const expectedSha = trimmed.split(/\s+/)[0].toLowerCase();
178
+ if (expectedSha && actualSha !== expectedSha) {
179
+ fs.unlinkSync(tempPath);
180
+ return;
181
+ }
182
+ verified = true;
183
+ break;
184
+ }
185
+ }
186
+ } catch (_) {
187
+ // checksum fetch failed, continue with the download
188
+ }
189
+ }
190
+
191
+ fs.renameSync(tempPath, binaryPath);
192
+ } catch (_) {
193
+ cleanupTemp();
194
+ } finally {
195
+ releaseLock();
196
+ }
197
+ }
198
+
199
+ main().catch(() => {});