{{ message }}
Repository navigation
Expand file tree
/
Copy pathauto-update.js
More file actions
361 lines (302 loc) · 9.55 KB
/
Copy pathauto-update.js
File metadata and controls
361 lines (302 loc) · 9.55 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env node
const fs = require('fs');
const os = require('os');
const path = require('path');
const { spawnSync } = require('child_process');
const { discoverInstalledStates } = require('./lib/install-lifecycle');
const { SUPPORTED_INSTALL_TARGETS } = require('./lib/install-manifests');
function showHelp(exitCode = 0) {
console.log(`
Usage: node scripts/auto-update.js [--target <${SUPPORTED_INSTALL_TARGETS.join('|')}>] [--repo-root <path>] [--dry-run] [--json]
Pull the latest ECC repo changes and reinstall the current context's managed targets
using the original install-state request.
`);
process.exit(exitCode);
}
function parseArgs(argv) {
const args = argv.slice(2);
const parsed = {
targets: [],
repoRoot: null,
dryRun: false,
json: false,
help: false,
};
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (arg === '--target') {
parsed.targets.push(args[index + 1] || null);
index += 1;
} else if (arg === '--repo-root') {
parsed.repoRoot = args[index + 1] || null;
index += 1;
} else if (arg === '--dry-run') {
parsed.dryRun = true;
} else if (arg === '--json') {
parsed.json = true;
} else if (arg === '--help' || arg === '-h') {
parsed.help = true;
} else {
throw new Error(`Unknown argument: ${arg}`);
}
}
return parsed;
}
function deriveRepoRootFromState(state) {
const operations = Array.isArray(state && state.operations) ? state.operations : [];
for (const operation of operations) {
if (typeof operation.sourcePath !== 'string' || !operation.sourcePath.trim()) {
continue;
}
if (typeof operation.sourceRelativePath !== 'string' || !operation.sourceRelativePath.trim()) {
continue;
}
const relativeParts = operation.sourceRelativePath
.split(/[\\/]+/)
.filter(Boolean);
if (relativeParts.length === 0) {
continue;
}
let repoRoot = path.resolve(operation.sourcePath);
for (let index = 0; index < relativeParts.length; index += 1) {
repoRoot = path.dirname(repoRoot);
}
return repoRoot;
}
throw new Error('Unable to infer ECC repo root from install-state operations');
}
function buildInstallApplyArgs(record) {
const state = record.state;
const target = state.target.target || record.adapter.target;
const request = state.request || {};
const args = [];
if (target) {
args.push('--target', target);
}
if (request.profile) {
args.push('--profile', request.profile);
}
if (Array.isArray(request.modules) && request.modules.length > 0) {
args.push('--modules', request.modules.join(','));
}
for (const componentId of Array.isArray(request.includeComponents) ? request.includeComponents : []) {
args.push('--with', componentId);
}
for (const componentId of Array.isArray(request.excludeComponents) ? request.excludeComponents : []) {
args.push('--without', componentId);
}
for (const language of Array.isArray(request.legacyLanguages) ? request.legacyLanguages : []) {
args.push(language);
}
return args;
}
function determineInstallCwd(record, repoRoot) {
if (record.adapter.kind === 'project') {
return path.dirname(record.state.target.root);
}
return repoRoot;
}
function validateRepoRoot(repoRoot) {
const normalized = path.resolve(repoRoot);
const packageJsonPath = path.join(normalized, 'package.json');
const installApplyPath = path.join(normalized, 'scripts', 'install-apply.js');
if (!fs.existsSync(packageJsonPath)) {
throw new Error(`Invalid ECC repo root: missing package.json at ${packageJsonPath}`);
}
if (!fs.existsSync(installApplyPath)) {
throw new Error(`Invalid ECC repo root: missing install script at ${installApplyPath}`);
}
return normalized;
}
function runExternalCommand(command, args, options = {}) {
const result = spawnSync(command, args, {
cwd: options.cwd,
env: options.env || process.env,
encoding: 'utf8',
maxBuffer: 10 * 1024 * 1024,
});
if (result.error) {
throw result.error;
}
if (typeof result.status === 'number' && result.status !== 0) {
const errorOutput = (result.stderr || result.stdout || '').trim();
throw new Error(`${command} ${args.join(' ')} failed${errorOutput ? `: ${errorOutput}` : ''}`);
}
return result;
}
function runAutoUpdate(options = {}, dependencies = {}) {
const discover = dependencies.discoverInstalledStates || discoverInstalledStates;
const execute = dependencies.runExternalCommand || runExternalCommand;
const homeDir = options.homeDir || process.env.HOME || os.homedir();
const projectRoot = options.projectRoot || process.cwd();
const requestedRepoRoot = options.repoRoot ? validateRepoRoot(options.repoRoot) : null;
const records = discover({
homeDir,
projectRoot,
targets: options.targets,
}).filter(record => record.exists);
const results = [];
if (records.length === 0) {
return {
dryRun: Boolean(options.dryRun),
repoRoot: requestedRepoRoot,
results,
summary: {
checkedCount: 0,
updatedCount: 0,
errorCount: 0,
},
};
}
const validRecords = [];
const inferredRepoRoots = [];
for (const record of records) {
if (record.error || !record.state) {
results.push({
adapter: record.adapter,
installStatePath: record.installStatePath,
status: 'error',
error: record.error || 'No valid install-state available',
});
continue;
}
const recordRepoRoot = requestedRepoRoot || validateRepoRoot(deriveRepoRootFromState(record.state));
inferredRepoRoots.push(recordRepoRoot);
validRecords.push({
record,
repoRoot: recordRepoRoot,
});
}
if (!requestedRepoRoot) {
const uniqueRepoRoots = [...new Set(inferredRepoRoots)];
if (uniqueRepoRoots.length > 1) {
throw new Error(`Multiple ECC repo roots detected: ${uniqueRepoRoots.join(', ')}`);
}
}
const repoRoot = requestedRepoRoot || inferredRepoRoots[0] || null;
if (!repoRoot) {
return {
dryRun: Boolean(options.dryRun),
repoRoot,
results,
summary: {
checkedCount: results.length,
updatedCount: 0,
errorCount: results.length,
},
};
}
const env = {
...process.env,
HOME: homeDir,
USERPROFILE: homeDir,
};
if (!options.dryRun) {
execute('git', ['fetch', '--all', '--prune'], { cwd: repoRoot, env });
execute('git', ['pull', '--ff-only'], { cwd: repoRoot, env });
}
for (const entry of validRecords) {
const installArgs = buildInstallApplyArgs(entry.record);
const args = [
path.join(repoRoot, 'scripts', 'install-apply.js'),
...installArgs,
'--json',
];
if (options.dryRun) {
args.push('--dry-run');
}
try {
const commandResult = execute(process.execPath, args, {
cwd: determineInstallCwd(entry.record, repoRoot),
env,
});
let payload = null;
if (commandResult.stdout && commandResult.stdout.trim()) {
payload = JSON.parse(commandResult.stdout);
}
results.push({
adapter: entry.record.adapter,
installStatePath: entry.record.installStatePath,
repoRoot,
cwd: determineInstallCwd(entry.record, repoRoot),
installArgs,
status: options.dryRun ? 'planned' : 'updated',
payload,
});
} catch (error) {
results.push({
adapter: entry.record.adapter,
installStatePath: entry.record.installStatePath,
repoRoot,
installArgs,
status: 'error',
error: error.message,
});
}
}
return {
dryRun: Boolean(options.dryRun),
repoRoot,
results,
summary: {
checkedCount: results.length,
updatedCount: results.filter(result => result.status === 'updated' || result.status === 'planned').length,
errorCount: results.filter(result => result.status === 'error').length,
},
};
}
function printHuman(result) {
if (result.results.length === 0) {
console.log('No ECC install-state files found for the current home/project context.');
return;
}
console.log(`${result.dryRun ? 'Auto-update dry run' : 'Auto-update summary'}:\n`);
if (result.repoRoot) {
console.log(`Repo root: ${result.repoRoot}\n`);
}
for (const entry of result.results) {
console.log(`- ${entry.adapter.id}`);
console.log(` Status: ${entry.status.toUpperCase()}`);
console.log(` Install-state: ${entry.installStatePath}`);
if (entry.error) {
console.log(` Error: ${entry.error}`);
continue;
}
console.log(` Reinstall args: ${entry.installArgs.join(' ') || '(none)'}`);
}
console.log(`\nSummary: checked=${result.summary.checkedCount}, ${result.dryRun ? 'planned' : 'updated'}=${result.summary.updatedCount}, errors=${result.summary.errorCount}`);
}
function main() {
try {
const options = parseArgs(process.argv);
if (options.help) {
showHelp(0);
}
const result = runAutoUpdate({
homeDir: process.env.HOME || os.homedir(),
projectRoot: process.cwd(),
targets: options.targets,
repoRoot: options.repoRoot,
dryRun: options.dryRun,
});
if (options.json) {
console.log(JSON.stringify(result, null, 2));
} else {
printHuman(result);
}
process.exitCode = result.summary.errorCount > 0 ? 1 : 0;
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}
}
if (require.main === module) {
main();
}
module.exports = {
parseArgs,
deriveRepoRootFromState,
buildInstallApplyArgs,
determineInstallCwd,
runAutoUpdate,
};
You can’t perform that action at this time.
