@muwi/core 0.7.0 → 0.7.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.
- package/package.json +1 -1
- package/src/cli.js +10 -5
- package/src/update.js +134 -0
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import fs from 'node:fs';
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
import { ROOT, adoptSpawn, guideUpdate, handleHook, sessionInstructions } from './hook.js';
|
|
7
|
-
import { host, hostFailure, loadHost } from './host.js';
|
|
7
|
+
import { host, hostFailure, hostPath, loadHost } from './host.js';
|
|
8
8
|
import { Fail, dur, join, needPlan, out, parseArgs, resolveWork, str, SCREEN_COMMANDS } from './kit.js';
|
|
9
9
|
import { fromSubagent, markNode } from './node.js';
|
|
10
10
|
import { addNode, carryStatus, diffPlan, isEmptyDiff, parsePlan, removeNode, } from './plan.js';
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
SCHEMA, appendEvent, listWorks, nowIso, readConfig, readJson, readPlan, runsDir, withLock, workDir, writeConfig, writePlan,
|
|
18
18
|
|
|
19
19
|
} from './store.js';
|
|
20
|
+
import { afterCommand } from './update.js';
|
|
20
21
|
import { PAST_DAYS, loadBoard, loadView, } from './view.js';
|
|
21
22
|
|
|
22
23
|
const HELP = `사용법: muwi <명령>
|
|
@@ -729,7 +730,11 @@ async function run(cmd , a , dir ) {
|
|
|
729
730
|
}
|
|
730
731
|
}
|
|
731
732
|
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
})
|
|
733
|
+
const argv = process.argv.slice(2);
|
|
734
|
+
main(argv)
|
|
735
|
+
// 사람이 터미널에서 친 명령이면 npm에 새 판이 있을 때 알린다 (update.ts)
|
|
736
|
+
.then(() => afterCommand({ cmd: argv[0], current: version(), root: ROOT, hostFile: hostPath() }))
|
|
737
|
+
.catch(e => {
|
|
738
|
+
process.stderr.write((e instanceof Error ? e.message : String(e)) + '\n');
|
|
739
|
+
process.exit(e instanceof Fail ? e.code : 1);
|
|
740
|
+
});
|
package/src/update.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// 새 버전 알림: 사람이 터미널에서 친 명령이 끝나면, npm에 더 새 판이 있을 때 알린다.
|
|
2
|
+
// - 사람이 친 명령에서만: 표준 오류가 터미널(TTY)일 때다. AI 도구가 실행한 명령, 훅, 그래프 칸이 부르는 명령은 터미널이 아니라서 조용하다
|
|
3
|
+
// - 명령을 늦추지 않는다: 알릴지는 캐시만 보고 정한다. npm에 묻는 일은 하루 한 번 따로 띄운 프로세스가 하고, 결과는 다음 명령에서 쓴다
|
|
4
|
+
// - 같은 알림은 하루 한 번만 한다
|
|
5
|
+
// - npm으로 깐 판(.js)에서만 묻는다. 저장소에서 .ts로 돌릴 때는 하지 않는다
|
|
6
|
+
// - 끄기: MUWI_NO_UPDATE_CHECK=1 (NO_UPDATE_NOTIFIER나 CI가 있어도 끈다)
|
|
7
|
+
// npm에는 패키지 이름만 묻는다 (https://registry.npmjs.org/-/package/<이름>/dist-tags)
|
|
8
|
+
import { spawn } from 'node:child_process';
|
|
9
|
+
import fs from 'node:fs';
|
|
10
|
+
import os from 'node:os';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { olderThan } from './setup.js';
|
|
13
|
+
|
|
14
|
+
const DAY = 24 * 60 * 60 * 1000;
|
|
15
|
+
const REGISTRY = 'https://registry.npmjs.org';
|
|
16
|
+
const RELEASES = 'https://github.com/Ssoon-m/muwi/releases/tag';
|
|
17
|
+
|
|
18
|
+
/** 마지막으로 npm에 물어본 결과와 알린 때 */
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
export const cacheFile = (env ) =>
|
|
22
|
+
path.join(env.XDG_CACHE_HOME || path.join(env.HOME || os.homedir(), '.cache'), 'muwi', 'update.json');
|
|
23
|
+
|
|
24
|
+
function readCache(file ) {
|
|
25
|
+
try {
|
|
26
|
+
return JSON.parse(fs.readFileSync(file, 'utf8')) ;
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function writeCache(file , c ) {
|
|
33
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
34
|
+
fs.writeFileSync(file, `${JSON.stringify(c)}\n`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const nameAt = (dir ) => {
|
|
38
|
+
try {
|
|
39
|
+
return (JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8')) ).name;
|
|
40
|
+
} catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 사람이 npm으로 깐 패키지. 업데이트 명령에 이 이름을 쓴다.
|
|
47
|
+
* 화면(@muwi/herdr-app)을 쓰고 있으면 그 패키지, 아니면 코어(@muwi/core). npm으로 깐 판이 아니면 null
|
|
48
|
+
*/
|
|
49
|
+
export function installedPackage(root , hostFile ) {
|
|
50
|
+
if (!fs.existsSync(path.join(root, 'src', 'cli.js'))) return null;
|
|
51
|
+
// 화면은 <패키지>/src/host.js에서 불러온다
|
|
52
|
+
if (hostFile && nameAt(path.resolve(hostFile, '..', '..')) === '@muwi/herdr-app') return '@muwi/herdr-app';
|
|
53
|
+
// 화면을 아직 적어 두지 않았어도 @muwi/herdr-app 안에 따라 들어온 코어면 그 패키지다: <herdr-app>/node_modules/@muwi/core
|
|
54
|
+
if (path.basename(path.resolve(root, '..', '..')) === 'node_modules' && nameAt(path.resolve(root, '..', '..', '..')) === '@muwi/herdr-app') return '@muwi/herdr-app';
|
|
55
|
+
return nameAt(root) === '@muwi/core' ? '@muwi/core' : null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 캐시만 보고 정한다: 알릴 새 버전(tell)과, npm에 다시 물어볼지(refresh) */
|
|
59
|
+
export function decide(c , name , current , now ) {
|
|
60
|
+
const same = c?.name === name ? c : null;
|
|
61
|
+
const refresh = !same?.checkedAt || now - same.checkedAt >= DAY;
|
|
62
|
+
const latest = same?.latest;
|
|
63
|
+
const toldRecently = Boolean(same?.toldAt && now - same.toldAt < DAY);
|
|
64
|
+
return { tell: latest && olderThan(current, latest) && !toldRecently ? latest : undefined, refresh };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const notice = (name , current , latest ) =>
|
|
68
|
+
`\n새 버전이 나왔어요: ${current} → ${latest}\n npm i -g ${name}@latest\n 바뀐 것: ${RELEASES}/v${latest}\n`;
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
/** 명령이 끝난 뒤 부른다. 무엇이 잘못돼도 명령은 실패하지 않는다 */
|
|
84
|
+
export function afterCommand(o ) {
|
|
85
|
+
const env = o.env ?? process.env;
|
|
86
|
+
try {
|
|
87
|
+
if (!(o.tty ?? Boolean(process.stderr.isTTY))) return;
|
|
88
|
+
if (env.MUWI_NO_UPDATE_CHECK || env.NO_UPDATE_NOTIFIER || env.CI) return;
|
|
89
|
+
if (o.cmd === 'hook' || o.cmd === 'board') return;
|
|
90
|
+
const name = installedPackage(o.root, o.hostFile);
|
|
91
|
+
if (!name) return;
|
|
92
|
+
const file = cacheFile(env);
|
|
93
|
+
const now = o.now ?? Date.now();
|
|
94
|
+
const c = readCache(file);
|
|
95
|
+
const d = decide(c, name, o.current, now);
|
|
96
|
+
if (d.tell) {
|
|
97
|
+
(o.write ?? (t => process.stderr.write(t)))(notice(name, o.current, d.tell));
|
|
98
|
+
writeCache(file, { ...(c?.name === name ? c : { name }), toldAt: now });
|
|
99
|
+
}
|
|
100
|
+
if (d.refresh) (o.check ?? spawnCheck(o.root))(name, file);
|
|
101
|
+
} catch {
|
|
102
|
+
// 알림은 덤이다. 캐시를 못 읽거나 못 써도 넘어간다
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** npm에 묻는 일은 명령과 따로 돈다 (명령은 기다리지 않고 끝난다) */
|
|
107
|
+
const spawnCheck = (root ) => (name , file ) => {
|
|
108
|
+
const child = spawn(process.execPath, [path.join(root, 'src', 'update.js'), '--check', name, file], {
|
|
109
|
+
detached: true,
|
|
110
|
+
stdio: 'ignore',
|
|
111
|
+
windowsHide: true,
|
|
112
|
+
});
|
|
113
|
+
child.unref();
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/** npm의 latest 버전을 물어 캐시에 적는다. 실패해도(오프라인) 물어본 때는 적어서 하루 동안 다시 묻지 않는다 */
|
|
117
|
+
export async function checkLatest(name , file , registry = REGISTRY, now = Date.now()) {
|
|
118
|
+
let latest ;
|
|
119
|
+
try {
|
|
120
|
+
const res = await fetch(`${registry}/-/package/${name.replace('/', '%2f')}/dist-tags`, { signal: AbortSignal.timeout(5000) });
|
|
121
|
+
if (res.ok) latest = ((await res.json()) ).latest;
|
|
122
|
+
} catch {
|
|
123
|
+
// 오프라인이거나 npm이 답하지 않는다
|
|
124
|
+
}
|
|
125
|
+
const prev = readCache(file);
|
|
126
|
+
const same = prev?.name === name ? prev : null;
|
|
127
|
+
writeCache(file, { name, latest: latest ?? same?.latest, checkedAt: now, toldAt: same?.toldAt });
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (import.meta.main && process.argv[2] === '--check') {
|
|
131
|
+
const [name, file] = process.argv.slice(3);
|
|
132
|
+
// MUWI_UPDATE_REGISTRY: 시험에서 npm 대신 쓸 주소
|
|
133
|
+
if (name && file) await checkLatest(name, file, process.env.MUWI_UPDATE_REGISTRY || REGISTRY);
|
|
134
|
+
}
|