Something went wrong. Try again.
A reusable desktop shell: your sidecar, your index, safe APIs, installers for every platform
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134import { test } from 'node:test';import assert from 'node:assert/strict';import { existsSync } from 'node:fs';import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises';import { tmpdir } from 'node:os';import { join } from 'node:path';import { builderConfig, compact, productionModules, stageApp } from '../lib/builder.js';import { loadConfig } from '../shell/config.js';
const example = new URL('../examples/hello/', import.meta.url).pathname;
test('builderConfig: from the example config', async () => { const { config } = await loadConfig(example); const cfg = builderConfig({ config, stage: '/s', outDir: '/o', sign: false, electronVersion: '40.0.0', hostPlatform: 'mac', dist: '/dist', env: {} }); assert.equal(cfg.appId, 'com.berjon.mollusc-hello'); assert.equal(cfg.productName, 'Hello'); assert.deepEqual(cfg.files, ['package.json', 'shell/**/*']); assert.equal(cfg.extraResources, undefined); assert.equal(typeof cfg.afterPack, 'function'); assert.deepEqual(cfg.fileAssociations, [{ ext: ['txt'], name: 'Text file', role: 'Viewer' }]); assert.deepEqual(cfg.protocols, [{ name: 'Hello', schemes: ['mollusc-hello'] }]); assert.equal(cfg.mac.identity, null); assert.ok(!('notarize' in cfg.mac)); assert.ok(!('icon' in cfg.mac)); assert.equal(cfg.electronDist({ platformName: 'darwin' }), '/dist'); assert.equal(cfg.electronDist({ platformName: 'win32' }), null); assert.equal(cfg.electronDist({ platformName: 'darwin', arch: process.arch }), '/dist'); assert.equal(cfg.electronDist({ platformName: 'darwin', arch: process.arch === 'arm64' ? 'x64' : 'arm64' }), null); assert.equal(cfg.directories.output, '/o'); assert.equal(cfg.directories.buildResources, '/s/build');});
test('builderConfig: signing, icons, platform passthrough', () => { const config = { id: 'com.x.y', name: 'y', icon: 'icons/y.png', fileTypes: [], urlSchemes: [], build: { mac: { category: 'public.app-category.graphics-design', target: ['dmg'] }, linux: { category: 'Graphics' }, win: { target: 'nsis' } }, }; const unsigned = builderConfig({ config, stage: '/s', outDir: '/o', sign: false, electronVersion: '40.0.0', hostPlatform: 'linux', env: {} }); assert.equal(unsigned.mac.icon, 'mollusc-app/icons/y.png'); assert.equal(unsigned.win.icon, 'mollusc-app/icons/y.png'); assert.equal(unsigned.linux.category, 'Graphics'); assert.equal(unsigned.mac.category, 'public.app-category.graphics-design'); assert.deepEqual(unsigned.mac.target, ['dmg']); assert.equal(unsigned.win.target, 'nsis'); assert.equal(unsigned.protocols, undefined); const pdf = builderConfig({ config: { ...config, fileTypes: [{ ext: ['pdf'], name: 'PDF', role: 'Viewer', rank: 'Alternate', mimeType: 'application/pdf' }] }, stage: '/s', outDir: '/o', sign: false, electronVersion: '40.0.0', hostPlatform: 'mac', env: {} }); assert.deepEqual(pdf.fileAssociations, [{ ext: ['pdf'], name: 'PDF', role: 'Viewer', rank: 'Alternate', mimeType: 'application/pdf' }]); assert.equal(unsigned.electronDist({ platformName: 'linux' }).endsWith('/node_modules/electron/dist'), true); const signed = builderConfig({ config, stage: '/s', outDir: '/o', sign: true, electronVersion: '40.0.0', hostPlatform: 'mac', env: {} }); assert.ok(!('identity' in signed.mac)); assert.equal(signed.mac.notarize, false); const creds = { APPLE_ID: 'a', APPLE_APP_SPECIFIC_PASSWORD: 'b', APPLE_TEAM_ID: 'c' }; assert.equal(builderConfig({ config, stage: '/s', outDir: '/o', sign: true, electronVersion: '40.0.0', hostPlatform: 'mac', env: creds }).mac.notarize, true); const key = { APPLE_API_KEY: '/k.p8', APPLE_API_KEY_ID: 'K', APPLE_API_ISSUER: 'I' }; assert.equal(builderConfig({ config, stage: '/s', outDir: '/o', sign: true, electronVersion: '40.0.0', hostPlatform: 'mac', env: key }).mac.notarize, true); assert.equal(builderConfig({ config, stage: '/s', outDir: '/o', sign: true, electronVersion: '40.0.0', hostPlatform: 'mac', env: { APPLE_KEYCHAIN_PROFILE: 'p' } }).mac.notarize, true); assert.equal(builderConfig({ config, stage: '/s', outDir: '/o', sign: true, electronVersion: '40.0.0', hostPlatform: 'mac', env: { APPLE_ID: 'a' } }).mac.notarize, false);});
test('compact drops undefined, keeps functions and null', () => { const fn = () => 1; assert.deepEqual(compact({ a: undefined, b: null, c: fn, d: [{ e: undefined, f: 1 }] }), { b: null, c: fn, d: [{ f: 1 }] });});
test('stageApp: shell, package.json, listed files, icons', async () => { const stage = await mkdtemp(join(tmpdir(), 'mollusc-stage-')); try { const { config } = await loadConfig(example); const { files, modules } = await stageApp({ appDir: example, config, stage }); assert.ok(files.includes('server/main.js')); assert.ok(files.includes('web/index.html')); assert.ok(files.includes('mollusc.json')); assert.deepEqual(modules, []); assert.ok(!files.some((f) => f.startsWith('dist/'))); assert.ok(existsSync(join(stage, 'shell', 'main.js'))); assert.ok(existsSync(join(stage, 'shell', 'preload.cjs'))); assert.ok(existsSync(join(stage, 'mollusc-app','web', 'index.html'))); assert.ok(existsSync(join(stage, 'mollusc-app','mollusc.json'))); const pkg = JSON.parse(await readFile(join(stage, 'package.json'), 'utf8')); assert.equal(pkg.main, 'shell/main.js'); assert.equal(pkg.type, 'module'); assert.equal(pkg.productName, 'Hello'); assert.equal(pkg.name, 'hello'); } finally { await rm(stage, { recursive: true, force: true }); }});
test('productionModules and stageApp: production tree only, nested packages, no devDependencies', async () => { const dir = await mkdtemp(join(tmpdir(), 'mollusc-app-')); const stage = await mkdtemp(join(tmpdir(), 'mollusc-stage-')); try { const pkg = (name, extra = {}) => JSON.stringify({ name, version: '1.0.0', ...extra }); await writeFile(join(dir, 'package.json'), pkg('fake', { dependencies: { 'dep-a': '1.0.0' }, devDependencies: { 'dev-x': '1.0.0' } })); await writeFile(join(dir, 'mollusc.json'), JSON.stringify({ id: 'com.x.fake', name: 'fake', version: '1.0.0', window: { url: 'index.html' }, sidecar: { node: true, args: ['server.js'] } })); await writeFile(join(dir, 'index.html'), '<title>fake</title>'); for (const [p, body] of [ ['node_modules/dep-a/package.json', pkg('dep-a', { dependencies: { 'dep-b': '1.0.0' } })], ['node_modules/dep-a/index.js', 'a'], ['node_modules/dep-a/node_modules/dep-b/package.json', pkg('dep-b')], ['node_modules/dep-a/node_modules/dep-b/index.js', 'b'], ['node_modules/dev-x/package.json', pkg('dev-x')], ['node_modules/dev-x/index.js', 'x'], ]) { await mkdir(join(dir, p, '..'), { recursive: true }); await writeFile(join(dir, p), body); } const mods = await productionModules(dir); assert.deepEqual(mods.map((m) => m.to).sort(), ['node_modules/dep-a', 'node_modules/dep-a/node_modules/dep-b']);
const { config } = await loadConfig(dir); const { modules } = await stageApp({ appDir: dir, config, stage }); assert.equal(modules.length, 2); assert.ok(existsSync(join(stage, 'mollusc-app','index.html'))); assert.ok(existsSync(join(stage, 'mollusc-app','node_modules', 'dep-a', 'index.js'))); assert.ok(existsSync(join(stage, 'mollusc-app','node_modules', 'dep-a', 'node_modules', 'dep-b', 'index.js'))); assert.ok(!existsSync(join(stage, 'mollusc-app', 'node_modules', 'dev-x')));
// A sidecar that needs none of them says so. await writeFile(join(dir, 'mollusc.json'), JSON.stringify({ id: 'com.x.fake', name: 'fake', version: '1.0.0', window: { url: 'index.html' }, sidecar: { node: true, args: ['server.js'] }, build: { modules: false } })); const { config: bare } = await loadConfig(dir); assert.deepEqual((await stageApp({ appDir: dir, config: bare, stage })).modules, []);
// Without a Node sidecar there is nothing to run them. await writeFile(join(dir, 'mollusc.json'), JSON.stringify({ id: 'com.x.fake', name: 'fake', version: '1.0.0', window: { url: 'index.html' } })); const { config: pageOnly } = await loadConfig(dir); assert.deepEqual((await stageApp({ appDir: dir, config: pageOnly, stage })).modules, []); assert.ok(!existsSync(join(stage, 'mollusc-app', 'node_modules'))); } finally { await rm(dir, { recursive: true, force: true }); await rm(stage, { recursive: true, force: true }); }});