MCPcopy
hub / github.com/renatoasse/opensquad / copyCanonicalSources

Function copyCanonicalSources

src/init.js:286–341  ·  view source on GitHub ↗
(targetDir, { overwrite = false, backupFn = null, protectedFn = null } = {})

Source from the content-addressed store, hash-verified

284}
285
286export async function copyCanonicalSources(targetDir, { overwrite = false, backupFn = null, protectedFn = null } = {}) {
287 let count = 0;
288
289 for (const { src, dest } of CANONICAL_SOURCES) {
290 const isDashboard = dest === 'dashboard';
291 let entries;
292 try {
293 entries = await getTemplateEntries(src);
294 } catch {
295 continue; // source dir doesn't exist (e.g., running from a partial install)
296 }
297
298 for (const entry of entries) {
299 const relativeToSrc = entry.slice(src.length + 1);
300 const normalizedRel = relativeToSrc.replace(/\\/g, '/');
301
302 // Skip dashboard-local artifacts
303 if (isDashboard && DASHBOARD_EXCLUDES.some(ex => normalizedRel === ex || normalizedRel.startsWith(ex + '/'))) {
304 continue;
305 }
306
307 const relativePath = join(dest, relativeToSrc);
308 const normalizedPath = relativePath.replace(/\\/g, '/');
309
310 // Skip protected paths (update mode)
311 if (protectedFn && protectedFn(normalizedPath)) continue;
312
313 const destPath = join(targetDir, relativePath);
314 await mkdir(dirname(destPath), { recursive: true });
315
316 if (!overwrite) {
317 // Init mode: skip existing files
318 try {
319 await stat(destPath);
320 continue;
321 } catch {
322 // does not exist — copy it
323 }
324 await cp(entry, destPath);
325 console.log(` ${t('createdFile', { path: normalizedPath })}`);
326 } else {
327 // Update mode: backup then overwrite
328 const backed = backupFn ? await backupFn(destPath) : false;
329 await cp(entry, destPath);
330 if (backed) {
331 console.log(` ${t('updatedFile', { path: normalizedPath })} (backup: ${normalizedPath}.bak)`);
332 } else {
333 console.log(` ${t('updatedFile', { path: normalizedPath })}`);
334 }
335 }
336 count++;
337 }
338 }
339
340 return count;
341}

Callers 2

updateFunction · 0.90
initFunction · 0.85

Calls 2

tFunction · 0.90
getTemplateEntriesFunction · 0.85

Tested by

no test coverage detected