From 47c3b8d961aae03907db6762732d3592cf40664e Mon Sep 17 00:00:00 2001 From: Giovanni Manzoni Date: Sat, 28 Nov 2026 18:44:55 +0100 Subject: [PATCH] Ship the project templates in the packaged app In an installed build, Settings > Project Templates > Open Folder did nothing and no template existed: - build.files left out templates/, so there was nothing to seed from. - fs.cpSync cannot read from app.asar, so adding the files alone would still fail with ENOENT. Copy by reading and writing each file instead. - The button ignored the error from open-path. Show it. Co-Authored-By: Claude Sonnet 5.5 --- package.json | 1 + projects.js | 18 +++++++++++++++++- public/settings-panel.js | 5 ++++- test/projects.test.js | 20 ++++++++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 3fe49dccc..0959b47cd 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ "harnesses/**/*", "workers/**/*", "public/**/*", + "templates/**/*", "node_modules/**/*" ], "asarUnpack": [ diff --git a/projects.js b/projects.js index 66e87709b..1f8847624 100644 --- a/projects.js +++ b/projects.js @@ -203,13 +203,29 @@ function templatesRoot() { return templatesDir || path.join(dataDir(), 'templates'); } +/** + * Recursive copy for files that ship inside the app. In a packaged build they + * are in app.asar, which fs.cpSync cannot read; these calls can. Reading and + * writing also gives the copies normal permissions, which copyFileSync does not + * out of the archive. + */ +function copyBundledTree(from, to) { + fs.mkdirSync(to, { recursive: true }); + for (const entry of fs.readdirSync(from, { withFileTypes: true })) { + const source = path.join(from, entry.name); + const target = path.join(to, entry.name); + if (entry.isDirectory()) copyBundledTree(source, target); + else fs.writeFileSync(target, fs.readFileSync(source)); + } +} + /** Copy the bundled templates in when the folder does not exist yet. Never overwrites. */ function seedTemplates() { const dir = templatesRoot(); if (fs.existsSync(dir)) return false; if (!fs.existsSync(BUNDLED_TEMPLATES_DIR)) return false; try { - fs.cpSync(BUNDLED_TEMPLATES_DIR, dir, { recursive: true }); + copyBundledTree(BUNDLED_TEMPLATES_DIR, dir); return true; } catch (err) { log.error?.(`[projects] could not seed templates into ${dir}`, err); diff --git a/public/settings-panel.js b/public/settings-panel.js index 9845b0111..75185d6b5 100644 --- a/public/settings-panel.js +++ b/public/settings-panel.js @@ -405,7 +405,10 @@ window.api.listTemplates().then(result => { const dirEl = settingsViewerBody.querySelector('#sv-templates-dir'); if (dirEl && result?.dir) dirEl.textContent = result.dir; - openTemplates.onclick = () => { if (result?.dir) window.api.openPath(result.dir); }; + openTemplates.onclick = async () => { + const opened = result?.dir ? await window.api.openPath(result.dir) : result; + if (opened?.error) alert(`Could not open the templates folder: ${opened.error}`); + }; }).catch(() => {}); } diff --git a/test/projects.test.js b/test/projects.test.js index 9df117c97..af055e3fe 100644 --- a/test/projects.test.js +++ b/test/projects.test.js @@ -891,6 +891,26 @@ test('templates are seeded once into a missing folder and never overwritten', () } }); +test('the packaged app ships the bundled templates', () => { + assert.ok(require('../package.json').build.files.includes('templates/**/*')); +}); + +test('templates are seeded without fs.cpSync, which cannot read the app archive', () => { + const dir = path.join(tmpDir('switchboard-tpl-'), 'templates'); + const bundled = path.join(__dirname, '..', 'templates'); + const cpSync = fs.cpSync; + try { + fs.cpSync = () => { throw Object.assign(new Error('not found in app.asar'), { code: 'ENOENT' }); }; + projects.init({ db: makeFakeDb(), log: { info() {}, error() {} }, buildProjectsFromCache: () => [], templatesDir: dir }); + assert.equal(projects.listTemplates().templates.length, 3); + const tree = root => fs.readdirSync(root, { recursive: true }).sort(); + assert.deepEqual(tree(dir), tree(bundled), 'nested folders and files too'); + } finally { + fs.cpSync = cpSync; + rm(path.dirname(dir)); + } +}); + test('projectsRoot expands ~ and falls back to ~/Switchboard', () => { const home = os.homedir(); projects.init({ db: { getSetting: () => ({ projectsRoot: '~/Work/projects' }) }, buildProjectsFromCache: () => [] });