vit 0.0.1 → 0.1.3

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.
@@ -0,0 +1,143 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 sol pbc
3
+
4
+ import { requireDid, loadConfig } from '../lib/config.js';
5
+ import { restoreAgent } from '../lib/oauth.js';
6
+ import { readFollowing, writeFollowing } from '../lib/vit-dir.js';
7
+ import { mark } from '../lib/brand.js';
8
+ import { jsonOk, jsonError } from '../lib/json-output.js';
9
+
10
+ export default function register(program) {
11
+ program
12
+ .command('follow')
13
+ .argument('<handle>', 'Handle to follow (e.g. alice.bsky.social)')
14
+ .description('Add an account to this project\'s following list')
15
+ .option('-v, --verbose', 'Show step-by-step details')
16
+ .option('--json', 'Output as JSON')
17
+ .option('--did <did>', 'DID to use for handle resolution')
18
+ .action(async (handle, opts) => {
19
+ try {
20
+ const { verbose } = opts;
21
+ const vlog = opts.json ? (...a) => console.error(...a) : console.log;
22
+ handle = handle.replace(/^@/, '');
23
+
24
+ if (opts.json && !(opts.did || loadConfig().did)) {
25
+ jsonError('no DID configured', "run 'vit login <handle>' first");
26
+ return;
27
+ }
28
+ const did = requireDid(opts);
29
+ if (!did) return;
30
+ if (verbose) vlog(`[verbose] DID: ${did}`);
31
+
32
+ const list = readFollowing();
33
+ if (list.some(e => e.handle === handle)) {
34
+ if (opts.json) {
35
+ jsonError(`already following ${handle}`);
36
+ return;
37
+ }
38
+ console.error(`already following ${handle}`);
39
+ process.exitCode = 1;
40
+ return;
41
+ }
42
+
43
+ const { agent } = await restoreAgent(did);
44
+ if (verbose) vlog('[verbose] session restored');
45
+
46
+ const resolved = await agent.resolveHandle({ handle });
47
+ const targetDid = resolved.data.did;
48
+ if (verbose) vlog(`[verbose] resolved ${handle} to ${targetDid}`);
49
+
50
+ list.push({ handle, did: targetDid, followedAt: new Date().toISOString() });
51
+ writeFollowing(list);
52
+ if (opts.json) {
53
+ jsonOk({ handle, did: targetDid, followedAt: list[list.length - 1].followedAt });
54
+ return;
55
+ }
56
+ console.log(`${mark} following ${handle} (${targetDid})`);
57
+ } catch (err) {
58
+ const msg = err instanceof Error ? err.message : String(err);
59
+ if (opts.json) {
60
+ jsonError(msg);
61
+ return;
62
+ }
63
+ console.error(msg);
64
+ process.exitCode = 1;
65
+ }
66
+ });
67
+
68
+ program
69
+ .command('unfollow')
70
+ .argument('<handle>', 'Handle to unfollow (e.g. alice.bsky.social)')
71
+ .description('Remove an account from this project\'s following list')
72
+ .option('-v, --verbose', 'Show step-by-step details')
73
+ .option('--json', 'Output as JSON')
74
+ .action(async (handle, opts) => {
75
+ try {
76
+ const { verbose } = opts;
77
+ const vlog = opts.json ? (...a) => console.error(...a) : console.log;
78
+ handle = handle.replace(/^@/, '');
79
+
80
+ const list = readFollowing();
81
+ const filtered = list.filter(e => e.handle !== handle);
82
+ if (filtered.length === list.length) {
83
+ if (opts.json) {
84
+ jsonError(`not following ${handle}`);
85
+ return;
86
+ }
87
+ console.error(`not following ${handle}`);
88
+ process.exitCode = 1;
89
+ return;
90
+ }
91
+
92
+ writeFollowing(filtered);
93
+ if (verbose) vlog(`[verbose] removed ${handle} from following list`);
94
+ if (opts.json) {
95
+ jsonOk({ handle });
96
+ return;
97
+ }
98
+ console.log(`${mark} unfollowed ${handle}`);
99
+ } catch (err) {
100
+ const msg = err instanceof Error ? err.message : String(err);
101
+ if (opts.json) {
102
+ jsonError(msg);
103
+ return;
104
+ }
105
+ console.error(msg);
106
+ process.exitCode = 1;
107
+ }
108
+ });
109
+
110
+ program
111
+ .command('following')
112
+ .description('List accounts in this project\'s following list')
113
+ .option('-v, --verbose', 'Show step-by-step details')
114
+ .option('--json', 'Output as JSON')
115
+ .action(async (opts) => {
116
+ try {
117
+ const list = readFollowing();
118
+ if (list.length === 0) {
119
+ if (opts.json) {
120
+ jsonOk({ following: [] });
121
+ return;
122
+ }
123
+ console.log('no followings');
124
+ return;
125
+ }
126
+ if (opts.json) {
127
+ jsonOk({ following: list });
128
+ return;
129
+ }
130
+ for (const e of list) {
131
+ console.log(`${e.handle} (${e.did})`);
132
+ }
133
+ } catch (err) {
134
+ const msg = err instanceof Error ? err.message : String(err);
135
+ if (opts.json) {
136
+ jsonError(msg);
137
+ return;
138
+ }
139
+ console.error(msg);
140
+ process.exitCode = 1;
141
+ }
142
+ });
143
+ }
@@ -0,0 +1,83 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 sol pbc
3
+
4
+ import { execFileSync } from 'node:child_process';
5
+ import { existsSync, readFileSync, writeFileSync } from 'node:fs';
6
+ import { join, resolve } from 'node:path';
7
+ import { which } from '../lib/compat.js';
8
+ import { mark, name } from '../lib/brand.js';
9
+
10
+ export default function register(program) {
11
+ program
12
+ .command('hack')
13
+ .description('Fork and install vit from source')
14
+ .option('--from <repo>', 'Clone an existing fork instead of creating a new one (e.g. user/vit)')
15
+ .action(async (opts) => {
16
+ try {
17
+ const ghPath = which('gh');
18
+ if (!opts.from && !ghPath) {
19
+ console.error(`${name} hack requires gh (GitHub CLI). Install it from https://cli.github.com`);
20
+ process.exitCode = 1;
21
+ return;
22
+ }
23
+
24
+ const gitPath = which('git');
25
+ if (opts.from && !gitPath) {
26
+ console.error('missing required tool: git');
27
+ process.exitCode = 1;
28
+ return;
29
+ }
30
+
31
+ const repo = opts.from || 'solpbc/vit';
32
+ const dirName = repo.split('/').pop();
33
+ const dirPath = resolve(process.cwd(), dirName);
34
+ const cloned = !existsSync(dirPath);
35
+
36
+ if (!cloned) {
37
+ console.log(`${mark} ${dirName}/ already exists, skipping clone`);
38
+ } else if (opts.from) {
39
+ execFileSync('git', ['clone', 'https://github.com/' + opts.from + '.git', dirName], { stdio: 'inherit' });
40
+ } else {
41
+ execFileSync('gh', ['repo', 'fork', 'solpbc/vit', '--clone'], { stdio: 'inherit' });
42
+ }
43
+
44
+ process.chdir(dirName);
45
+
46
+ const bunPath = which('bun');
47
+ if (!bunPath) {
48
+ console.error('missing required tool: bun');
49
+ process.exitCode = 1;
50
+ return;
51
+ }
52
+
53
+ execFileSync('bun', ['install'], { stdio: 'inherit' });
54
+ execFileSync('node', [join(process.cwd(), 'bin', 'vit.js'), 'link'], { stdio: 'inherit' });
55
+
56
+ if (!opts.from) {
57
+ const remote = execFileSync(
58
+ 'gh',
59
+ ['repo', 'view', '--json', 'nameWithOwner', '-q', '.nameWithOwner'],
60
+ { encoding: 'utf-8' }
61
+ ).trim();
62
+ const hackPath = join(process.cwd(), 'hack');
63
+ if (existsSync(hackPath)) {
64
+ const current = readFileSync(hackPath, 'utf-8');
65
+ writeFileSync(hackPath, current.replace('SELF="solpbc/vit"', `SELF="${remote}"`));
66
+ }
67
+ }
68
+
69
+ if (opts.from && cloned) {
70
+ try {
71
+ execFileSync('git', ['remote', 'add', 'upstream', 'https://github.com/solpbc/vit.git'], { stdio: 'inherit' });
72
+ } catch {}
73
+ }
74
+
75
+ console.log('');
76
+ console.log(`${mark} ${name} installed from source`);
77
+ console.log(`run: cd ${dirName}`);
78
+ } catch (err) {
79
+ console.error(err instanceof Error ? err.message : String(err));
80
+ process.exitCode = 1;
81
+ }
82
+ });
83
+ }
@@ -0,0 +1,242 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 sol pbc
3
+
4
+ import { existsSync, writeFileSync } from 'node:fs';
5
+ import { execSync } from 'node:child_process';
6
+ import { join } from 'node:path';
7
+ import { toBeacon } from '../lib/beacon.js';
8
+ import { vitDir, readProjectConfig, writeProjectConfig } from '../lib/vit-dir.js';
9
+ import { requireAgent } from '../lib/agent.js';
10
+ import { mark, name, DOT_VIT_README } from '../lib/brand.js';
11
+ import { jsonOk, jsonError } from '../lib/json-output.js';
12
+
13
+ export default function register(program) {
14
+ program
15
+ .command('init')
16
+ .description('Initialize .vit directory and set project beacon. Use the most official upstream or well-known git URL so all contributors converge on the same beacon.')
17
+ .option('--beacon <url>', 'Git URL (or "." to read from git remote upstream/origin) to derive the beacon URI')
18
+ .option('--secondary <url>', 'Secondary beacon URL for upstream cap discovery')
19
+ .option('--json', 'Output as JSON')
20
+ .option('-v, --verbose', 'Show step-by-step details')
21
+ .action(async (opts) => {
22
+ try {
23
+ const gate = requireAgent();
24
+ if (!gate.ok) {
25
+ if (opts.json) {
26
+ jsonError('agent required', 'run vit init from a coding agent');
27
+ return;
28
+ }
29
+ console.error(`${name} init should be run by a coding agent (e.g. claude code, gemini cli).`);
30
+ console.error(`open your agent and ask it to run '${name} init' for you.`);
31
+ process.exitCode = 1;
32
+ return;
33
+ }
34
+
35
+ const { verbose } = opts;
36
+ const vlog = opts.json ? (...a) => console.error(...a) : console.log;
37
+ const dir = vitDir();
38
+ if (verbose) vlog(`[verbose] .vit dir: ${dir}`);
39
+
40
+ if (!opts.beacon && !opts.secondary) {
41
+ const config = readProjectConfig();
42
+ if (config.beacon) {
43
+ if (opts.json) {
44
+ const out = { beacon: config.beacon };
45
+ if (config.secondaryBeacon) out.secondaryBeacon = config.secondaryBeacon;
46
+ jsonOk(out);
47
+ return;
48
+ }
49
+ console.log(`${mark} beacon: ${config.beacon}`);
50
+ if (config.secondaryBeacon) {
51
+ console.log(`${mark} secondary beacon: ${config.secondaryBeacon}`);
52
+ }
53
+ console.log(`hint: to change the beacon, run: ${name} init --beacon <git-url>`);
54
+ return;
55
+ }
56
+
57
+ let isGitRepo = false;
58
+ try {
59
+ execSync('git rev-parse --is-inside-work-tree', {
60
+ encoding: 'utf-8',
61
+ stdio: ['pipe', 'pipe', 'pipe'],
62
+ });
63
+ isGitRepo = true;
64
+ } catch {}
65
+ if (verbose) vlog(`[verbose] in git repo: ${isGitRepo ? 'yes' : 'no'}`);
66
+
67
+ const hasVitDir = existsSync(dir);
68
+ if (!isGitRepo) {
69
+ let remotes = [];
70
+ if (opts.json) {
71
+ jsonOk({
72
+ status: hasVitDir ? 'no beacon' : 'not initialized',
73
+ git: false,
74
+ remotes,
75
+ });
76
+ return;
77
+ }
78
+ console.log(hasVitDir ? 'status: no beacon' : 'status: not initialized');
79
+ console.log('git: false');
80
+ if (hasVitDir) {
81
+ console.log(`hint: run: ${name} init --beacon <canonical-git-url>`);
82
+ } else {
83
+ console.log(`hint: run ${name} init from inside a git repository.`);
84
+ }
85
+ return;
86
+ }
87
+
88
+ console.log(hasVitDir ? 'status: no beacon' : 'status: not initialized');
89
+ console.log('git: true');
90
+
91
+ let remoteNames = [];
92
+ try {
93
+ remoteNames = execSync('git remote', {
94
+ encoding: 'utf-8',
95
+ stdio: ['pipe', 'pipe', 'pipe'],
96
+ })
97
+ .trim()
98
+ .split('\n')
99
+ .filter(Boolean);
100
+ } catch {
101
+ remoteNames = [];
102
+ }
103
+ if (verbose) vlog(`[verbose] remotes detected: ${remoteNames.length > 0 ? remoteNames.join(', ') : 'none'}`);
104
+
105
+ const remotes = [];
106
+ for (const name of remoteNames) {
107
+ try {
108
+ const url = execSync(`git config --get remote.${name}.url`, {
109
+ encoding: 'utf-8',
110
+ stdio: ['pipe', 'pipe', 'pipe'],
111
+ }).trim();
112
+ if (url) remotes.push({ name, url });
113
+ } catch {}
114
+ }
115
+ if (verbose && remotes.length > 0) {
116
+ vlog(`[verbose] remote urls: ${remotes.map(r => `${r.name}=${r.url}`).join(' ')}`);
117
+ }
118
+
119
+ if (opts.json) {
120
+ jsonOk({
121
+ status: hasVitDir ? 'no beacon' : 'not initialized',
122
+ git: true,
123
+ remotes: remotes.map(r => ({ name: r.name, url: r.url })),
124
+ });
125
+ return;
126
+ }
127
+
128
+ const remotesDisplay = remotes.length > 0
129
+ ? remotes.map(remote => `${remote.name}=${remote.url}`).join(' ')
130
+ : 'none';
131
+ console.log(`remotes: ${remotesDisplay}`);
132
+
133
+ const upstream = remotes.find(remote => remote.name === 'upstream');
134
+ const origin = remotes.find(remote => remote.name === 'origin');
135
+ if (upstream) {
136
+ console.log('hint: detected upstream remote. upstream points to the canonical repo.');
137
+ console.log(`hint: run: ${name} init --beacon ${upstream.url}`);
138
+ } else if (origin) {
139
+ console.log(`hint: run: ${name} init --beacon ${origin.url}`);
140
+ } else {
141
+ console.log(`hint: no git remotes found. run: ${name} init --beacon <canonical-git-url>`);
142
+ }
143
+ return;
144
+ }
145
+
146
+ if (opts.secondary && !opts.beacon) {
147
+ const config = readProjectConfig();
148
+ if (!config.beacon) {
149
+ if (opts.json) {
150
+ jsonError("no primary beacon set — run 'vit init --beacon <url>' first");
151
+ return;
152
+ }
153
+ console.error("no primary beacon set — run 'vit init --beacon <url>' first");
154
+ process.exitCode = 1;
155
+ return;
156
+ }
157
+
158
+ const secondary = 'vit:' + toBeacon(opts.secondary);
159
+ const merged = { ...config, secondaryBeacon: secondary };
160
+ writeProjectConfig(merged);
161
+ if (opts.json) {
162
+ jsonOk({ beacon: merged.beacon, secondaryBeacon: merged.secondaryBeacon });
163
+ return;
164
+ }
165
+ console.log(`${mark} beacon: ${merged.beacon}`);
166
+ console.log(`${mark} secondary beacon: ${merged.secondaryBeacon}`);
167
+ return;
168
+ }
169
+
170
+ let gitUrl = opts.beacon;
171
+ if (gitUrl === '.') {
172
+ if (verbose) vlog('[verbose] resolving --beacon . via remote.upstream.url then remote.origin.url');
173
+ let usedRemote = '';
174
+ try {
175
+ gitUrl = execSync('git config --get remote.upstream.url', {
176
+ encoding: 'utf-8',
177
+ stdio: ['pipe', 'pipe', 'pipe'],
178
+ }).trim();
179
+ if (gitUrl) usedRemote = 'upstream';
180
+ } catch {
181
+ gitUrl = '';
182
+ }
183
+
184
+ if (!gitUrl) {
185
+ try {
186
+ gitUrl = execSync('git config --get remote.origin.url', {
187
+ encoding: 'utf-8',
188
+ stdio: ['pipe', 'pipe', 'pipe'],
189
+ }).trim();
190
+ if (gitUrl) usedRemote = 'origin';
191
+ } catch {
192
+ gitUrl = '';
193
+ }
194
+ }
195
+
196
+ if (!gitUrl) {
197
+ if (opts.json) {
198
+ jsonError('no git remote found', 'set a remote or provide a git URL directly');
199
+ return;
200
+ }
201
+ console.error('No git remote found. Set a remote or provide a git URL directly.');
202
+ process.exitCode = 1;
203
+ return;
204
+ }
205
+ if (verbose) vlog(`[verbose] Read git remote ${usedRemote}: ${gitUrl}`);
206
+ }
207
+
208
+ const beacon = 'vit:' + toBeacon(gitUrl);
209
+ if (verbose) vlog(`[verbose] Computed beacon: ${beacon}`);
210
+ const existing = readProjectConfig();
211
+ const merged = { ...existing, beacon };
212
+ if (opts.secondary) {
213
+ merged.secondaryBeacon = 'vit:' + toBeacon(opts.secondary);
214
+ }
215
+ writeProjectConfig(merged);
216
+ if (verbose) vlog(`[verbose] Wrote config.json`);
217
+ const readmePath = join(vitDir(), 'README.md');
218
+ if (!existsSync(readmePath)) {
219
+ writeFileSync(readmePath, DOT_VIT_README);
220
+ if (verbose) vlog(`[verbose] Wrote .vit/README.md`);
221
+ }
222
+ if (opts.json) {
223
+ const out = { beacon: merged.beacon };
224
+ if (merged.secondaryBeacon) out.secondaryBeacon = merged.secondaryBeacon;
225
+ jsonOk(out);
226
+ return;
227
+ }
228
+ console.log(`${mark} beacon: ${beacon}`);
229
+ if (merged.secondaryBeacon) {
230
+ console.log(`${mark} secondary beacon: ${merged.secondaryBeacon}`);
231
+ }
232
+ } catch (err) {
233
+ const msg = err instanceof Error ? err.message : String(err);
234
+ if (opts.json) {
235
+ jsonError(msg);
236
+ return;
237
+ }
238
+ console.error(msg);
239
+ process.exitCode = 1;
240
+ }
241
+ });
242
+ }