@mingxy/opencode-mascot 0.3.7 → 0.3.8
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/package.json +1 -1
- package/src/core/logger.ts +22 -0
- package/src/core/updater.ts +21 -3
package/package.json
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { appendFileSync, mkdirSync, existsSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
|
|
5
|
+
const LOG_DIR = join(homedir(), ".cache", "opencode", "logs");
|
|
6
|
+
const LOG_FILE = join(LOG_DIR, "mascot.log");
|
|
7
|
+
|
|
8
|
+
function ensureDir(): void {
|
|
9
|
+
if (!existsSync(LOG_DIR)) {
|
|
10
|
+
try { mkdirSync(LOG_DIR, { recursive: true }); } catch {}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function log(level: string, message: string): void {
|
|
15
|
+
ensureDir();
|
|
16
|
+
const now = new Date();
|
|
17
|
+
const pad = (n: number) => String(n).padStart(2, "0");
|
|
18
|
+
const ts = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
|
|
19
|
+
try {
|
|
20
|
+
appendFileSync(LOG_FILE, `${ts} [${level}] ${message}\n`);
|
|
21
|
+
} catch {}
|
|
22
|
+
}
|
package/src/core/updater.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { createRequire } from "node:module";
|
|
|
4
4
|
import { join, dirname } from "node:path";
|
|
5
5
|
import { homedir, tmpdir } from "node:os";
|
|
6
6
|
import { openSync, closeSync, unlinkSync, statSync, writeSync, mkdtempSync, readdirSync, rmSync, readFileSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { log } from "./logger.js";
|
|
7
8
|
|
|
8
9
|
const execFileAsync = promisify(execFile);
|
|
9
10
|
const require = createRequire(import.meta.url);
|
|
@@ -106,19 +107,36 @@ export async function checkAndUpdate(
|
|
|
106
107
|
currentVersion: string,
|
|
107
108
|
onSuccess: (newVersion: string) => void,
|
|
108
109
|
): Promise<void> {
|
|
110
|
+
log("INFO", `checking: current=${currentVersion}`);
|
|
109
111
|
const latest = await getLatestVersion();
|
|
110
|
-
if (!latest)
|
|
112
|
+
if (!latest) {
|
|
113
|
+
log("ERROR", "failed to fetch latest version");
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
log("INFO", `latest=${latest}`);
|
|
111
117
|
|
|
112
|
-
if (compareVersions(latest, currentVersion) <= 0)
|
|
118
|
+
if (compareVersions(latest, currentVersion) <= 0) {
|
|
119
|
+
log("DEBUG", "already up to date");
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
113
122
|
|
|
114
|
-
if (!acquireLock())
|
|
123
|
+
if (!acquireLock()) {
|
|
124
|
+
log("WARN", "lock acquisition failed");
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
115
127
|
|
|
116
128
|
try {
|
|
117
129
|
const targetDir = getInstallDir();
|
|
130
|
+
log("INFO", `installing to ${targetDir}`);
|
|
118
131
|
const success = await installUpdate(targetDir, latest);
|
|
119
132
|
if (success) {
|
|
133
|
+
log("INFO", `SUCCESS: ${currentVersion} -> ${latest}`);
|
|
120
134
|
onSuccess(latest);
|
|
135
|
+
} else {
|
|
136
|
+
log("ERROR", "install FAILED");
|
|
121
137
|
}
|
|
138
|
+
} catch (err) {
|
|
139
|
+
log("ERROR", `exception: ${err}`);
|
|
122
140
|
} finally {
|
|
123
141
|
releaseLock();
|
|
124
142
|
}
|