Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"harnesses/**/*",
"workers/**/*",
"public/**/*",
"templates/**/*",
"node_modules/**/*"
],
"asarUnpack": [
Expand Down
18 changes: 17 additions & 1 deletion projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion public/settings-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {});
}

Expand Down
20 changes: 20 additions & 0 deletions test/projects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => [] });
Expand Down