-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall-open-design.js
More file actions
206 lines (179 loc) · 5.79 KB
/
Copy pathinstall-open-design.js
File metadata and controls
206 lines (179 loc) · 5.79 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env node
const fs = require('fs');
const os = require('os');
const path = require('path');
const { spawnSync } = require('child_process');
const DEFAULT_REPO_URL = 'https://github.com/nexu-io/open-design.git';
const DEFAULT_INSTALL_DIR = path.join(os.homedir(), '.tsp', 'open-design');
const DEFAULT_REF = 'main';
function parseArgs(argv) {
const parsed = {
installDir: process.env.TSP_OPEN_DESIGN_HOME || process.env.OPEN_DESIGN_HOME || DEFAULT_INSTALL_DIR,
repoUrl: process.env.TSP_OPEN_DESIGN_REPO || DEFAULT_REPO_URL,
ref: process.env.TSP_OPEN_DESIGN_REF || DEFAULT_REF,
skipDeps: process.env.TSP_OPEN_DESIGN_SKIP_DEPS === '1',
dryRun: false,
};
for (let index = 2; index < argv.length; index += 1) {
const arg = argv[index];
if (arg === '--dir') {
parsed.installDir = argv[index + 1] || parsed.installDir;
index += 1;
continue;
}
if (arg === '--repo') {
parsed.repoUrl = argv[index + 1] || parsed.repoUrl;
index += 1;
continue;
}
if (arg === '--ref') {
parsed.ref = argv[index + 1] || parsed.ref;
index += 1;
continue;
}
if (arg === '--skip-deps') {
parsed.skipDeps = true;
continue;
}
if (arg === '--dry-run') {
parsed.dryRun = true;
continue;
}
if (arg === '--help' || arg === '-h') {
parsed.help = true;
continue;
}
throw new Error(`Unknown argument: ${arg}`);
}
parsed.installDir = path.resolve(parsed.installDir);
return parsed;
}
function printHelp() {
console.log(`
Install or update Open Design for TSP.
Usage:
node scripts/install-open-design.js [--dir <path>] [--repo <url>] [--ref <git-ref>] [--skip-deps] [--dry-run]
Environment:
TSP_OPEN_DESIGN_HOME Override install directory. Default: ~/.tsp/open-design
TSP_OPEN_DESIGN_REPO Override upstream repository URL.
TSP_OPEN_DESIGN_REF Override git ref. Default: main
TSP_OPEN_DESIGN_SKIP_DEPS Set to 1 to skip pnpm dependency install.
`);
}
function commandExists(command) {
const result = spawnSync(command, ['--version'], {
encoding: 'utf8',
stdio: ['ignore', 'ignore', 'ignore'],
});
return result.status === 0;
}
function run(command, args, options = {}) {
const printable = [command, ...args].join(' ');
if (options.dryRun) {
console.log(`[dry-run] ${printable}`);
return;
}
const result = spawnSync(command, args, {
cwd: options.cwd || process.cwd(),
encoding: 'utf8',
stdio: options.quiet ? ['ignore', 'pipe', 'pipe'] : 'inherit',
});
if (result.status !== 0) {
const stderr = result.stderr ? `\n${result.stderr.trim()}` : '';
throw new Error(`Command failed: ${printable}${stderr}`);
}
}
function readGitRemote(installDir) {
const result = spawnSync('git', ['-C', installDir, 'remote', 'get-url', 'origin'], {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
});
return result.status === 0 ? result.stdout.trim() : null;
}
function isOpenDesignRemote(remoteUrl, expectedRepoUrl) {
const normalized = String(remoteUrl || '').replace(/\.git$/, '');
const expected = String(expectedRepoUrl || '').replace(/\.git$/, '');
return normalized === expected || normalized.endsWith('/nexu-io/open-design');
}
function ensureOpenDesignCheckout(options) {
const gitDir = path.join(options.installDir, '.git');
if (!fs.existsSync(options.installDir)) {
console.log(`Installing Open Design into ${options.installDir}`);
if (!options.dryRun) {
fs.mkdirSync(path.dirname(options.installDir), { recursive: true });
}
run('git', [
'clone',
'--depth',
'1',
'--branch',
options.ref,
options.repoUrl,
options.installDir,
], { dryRun: options.dryRun });
return;
}
if (!fs.existsSync(gitDir)) {
throw new Error(`Open Design install path exists but is not a git checkout: ${options.installDir}`);
}
const remoteUrl = readGitRemote(options.installDir);
if (!isOpenDesignRemote(remoteUrl, options.repoUrl)) {
throw new Error(
`Refusing to update ${options.installDir}; origin is ${remoteUrl || '(unknown)'}, expected ${options.repoUrl}`
);
}
console.log(`Updating Open Design at ${options.installDir}`);
run('git', ['-C', options.installDir, 'fetch', '--depth', '1', 'origin', options.ref], {
dryRun: options.dryRun,
});
run('git', ['-C', options.installDir, 'checkout', 'FETCH_HEAD'], {
dryRun: options.dryRun,
});
}
function installDependencies(options) {
if (options.skipDeps) {
console.log('Skipping Open Design dependency install because --skip-deps or TSP_OPEN_DESIGN_SKIP_DEPS=1 is set.');
return;
}
if (!commandExists('corepack')) {
console.warn(
'Open Design source is installed, but corepack/pnpm is not available. '
+ 'Install Node 24 with corepack, then run `corepack enable && pnpm install` in the Open Design directory.'
);
return;
}
run('corepack', ['enable'], {
cwd: options.installDir,
dryRun: options.dryRun,
});
if (!commandExists('pnpm')) {
console.warn(
'Open Design source is installed, but pnpm is still not available after `corepack enable`. '
+ 'Run `corepack prepare pnpm@10.33.2 --activate && pnpm install` in the Open Design directory.'
);
return;
}
run('pnpm', ['install'], {
cwd: options.installDir,
dryRun: options.dryRun,
});
}
function main() {
try {
const options = parseArgs(process.argv);
if (options.help) {
printHelp();
return;
}
if (!commandExists('git')) {
throw new Error('git is required to install Open Design');
}
ensureOpenDesignCheckout(options);
installDependencies(options);
console.log(`Open Design ready: ${options.installDir}`);
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}
}
main();