Repository navigation
Expand file tree
/
Copy pathcode-agent-setup.cjs
More file actions
executable file
·143 lines (129 loc) · 5.72 KB
/
Copy pathcode-agent-setup.cjs
File metadata and controls
executable file
·143 lines (129 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env node
'use strict';
// One cross-platform setup script for the code-agent launcher — replaces the
// per-OS bash/PowerShell sprawl. Node runs everywhere, so this single file
// handles macOS, Linux and Windows by branching on process.platform.
//
// node code-agent-setup.cjs prereqs install nvm + pinned node (+ go-task)
// node code-agent-setup.cjs build npm install + compile (uses pinned node)
// node code-agent-setup.cjs install put `code-agent` on the user PATH
// node code-agent-setup.cjs uninstall remove it from PATH
// node code-agent-setup.cjs setup prereqs -> build -> install
// node code-agent-setup.cjs status build + launcher readiness
//
// The Taskfile just calls this; you can also run it directly with plain node
// (no `task` needed) — handy as the zero-tools bootstrap.
const { spawnSync } = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
const REPO = __dirname;
const isWin = process.platform === 'win32';
const HOME = os.homedir();
const NODE_VERSION = fs.readFileSync(path.join(REPO, '.nvmrc'), 'utf8').trim();
// ---- shell helpers -----------------------------------------------------
function run(cmd, { cwd = REPO, check = true } = {}) {
console.log(`\n$ ${cmd}`);
const r = spawnSync(cmd, { cwd, stdio: 'inherit', shell: true });
if (check && r.status !== 0) {
console.error(`\n✗ failed (exit ${r.status}): ${cmd}`);
process.exit(r.status || 1);
}
return r.status;
}
function has(bin) {
const r = spawnSync(isWin ? 'where' : 'which', [bin], { stdio: 'ignore', shell: false });
return r.status === 0;
}
// Run a command through a login bash that has nvm sourced (POSIX only).
function bashNvm(inner) {
const prelude = 'export NVM_DIR="$HOME/.nvm"; [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh";';
return run(`bash -lc '${prelude} ${inner.replace(/'/g, `'\\''`)}'`);
}
// ---- steps -------------------------------------------------------------
function prereqs() {
if (isWin) {
if (!has('nvm')) {
if (has('winget')) {
run('winget install --id CoreyButler.NVMforWindows -e --accept-source-agreements --accept-package-agreements', { check: false });
} else if (has('choco')) {
run('choco install nvm -y', { check: false });
} else {
console.error('Install nvm-windows (https://github.com/coreybutler/nvm-windows) then re-run.');
process.exit(1);
}
}
run(`nvm install ${NODE_VERSION}`);
run(`nvm use ${NODE_VERSION}`, { check: false });
if (!has('task') && has('winget')) {
run('winget install --id Task.Task -e --accept-source-agreements --accept-package-agreements', { check: false });
}
} else {
if (!fs.existsSync(path.join(HOME, '.nvm', 'nvm.sh'))) {
console.log('installing nvm...');
run('curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash');
}
bashNvm(`nvm install ${NODE_VERSION}`);
if (!has('task')) {
if (has('brew')) {
run('brew install go-task/tap/go-task', { check: false });
} else {
run(`sh -c "$(curl -fsSL https://taskfile.dev/install.sh)" -- -d -b "$HOME/.local/bin"`, { check: false });
}
}
}
console.log(`\n✓ prereqs ready (node ${NODE_VERSION})`);
}
function build() {
if (isWin) {
run(`nvm use ${NODE_VERSION} && npm install && npm run compile`);
} else {
bashNvm(`nvm use ${NODE_VERSION} >/dev/null && npm install && npm run compile`);
}
console.log('\n✓ build complete');
}
function install() {
if (isWin) {
const bin = path.join(HOME, 'bin');
fs.mkdirSync(bin, { recursive: true });
const shim = path.join(bin, 'code-agent.cmd');
fs.writeFileSync(shim, `@echo off\r\ncall "${path.join(REPO, 'code-agent.cmd')}" %*\r\n`);
// add %USERPROFILE%\bin to the user PATH if missing (no truncation, unlike setx)
run(`powershell -NoProfile -Command "$b='${bin}'; $u=[Environment]::GetEnvironmentVariable('Path','User'); if($u -notlike ('*'+$b+'*')){[Environment]::SetEnvironmentVariable('Path', ($u.TrimEnd(';')+';'+$b),'User'); Write-Host ('added '+$b+' to user PATH (restart your shell)')}"`, { check: false });
console.log(`\n✓ installed shim: ${shim} -> ${path.join(REPO, 'code-agent.cmd')}`);
} else {
const bin = path.join(HOME, '.local', 'bin');
fs.mkdirSync(bin, { recursive: true });
const link = path.join(bin, 'code-agent');
try { fs.unlinkSync(link); } catch { /* not there */ }
fs.symlinkSync(path.join(REPO, 'code-agent'), link);
console.log(`\n✓ installed: ${link} -> ${path.join(REPO, 'code-agent')} (ensure ~/.local/bin is on PATH)`);
}
}
function uninstall() {
const target = isWin ? path.join(HOME, 'bin', 'code-agent.cmd') : path.join(HOME, '.local', 'bin', 'code-agent');
try { fs.unlinkSync(target); console.log(`removed ${target}`); }
catch { console.log(`nothing to remove at ${target}`); }
}
function status() {
const ok = p => fs.existsSync(path.join(REPO, p)) ? 'present' : 'MISSING (run: setup)';
console.log(`repo: ${REPO}`);
console.log(`platform: ${process.platform}`);
console.log(`pinned node: ${NODE_VERSION}`);
console.log(`node_modules: ${ok('node_modules')}`);
console.log(`out/: ${ok('out')}`);
console.log(`launcher: ${isWin ? 'code-agent.cmd' : 'code-agent'} on PATH: ${has('code-agent') ? 'yes' : 'no (run: install)'}`);
}
// ---- dispatch ----------------------------------------------------------
const cmd = process.argv[2] || 'help';
switch (cmd) {
case 'prereqs': prereqs(); break;
case 'build': build(); break;
case 'install': install(); break;
case 'uninstall': uninstall(); break;
case 'status': status(); break;
case 'setup': prereqs(); build(); install(); break;
default:
console.log('usage: node code-agent-setup.cjs <prereqs|build|install|uninstall|setup|status>');
process.exit(cmd === 'help' ? 0 : 2);
}