Skip to content
Merged
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
10 changes: 5 additions & 5 deletions apps/daemon/src/daemon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ describe('createDaemon', () => {

test('serves while plugin is enabling', async () => {
const data = dataDirectory(path.join(root, 'data'));
const folder = path.join(data.plugins, 'a', 'daemon');
const folder = path.join(data.plugins, '@test/a', 'daemon');
await fs.mkdir(folder, { recursive: true });
await fs.writeFile(
path.join(data.root, 'recorder.mjs'),
'export const events = [];\nexport const gate = Promise.withResolvers();\n',
);
await fs.writeFile(
path.join(data.plugins, 'a', 'manifest.json'),
JSON.stringify({ id: 'a', name: 'a', version: '1.0.0', elsewise: '*', frontend: {}, daemon: {} }),
path.join(data.plugins, '@test/a', 'manifest.json'),
JSON.stringify({ id: '@test/a', name: '@test/a', version: '1.0.0', elsewise: '*', frontend: {}, daemon: {} }),
);
await fs.writeFile(
path.join(folder, 'main.js'),
`
import { events, gate } from '../../../recorder.mjs';
import { events, gate } from '../../../../recorder.mjs';
export default () => ({
async onEnable() {
events.push('enable started');
Expand All @@ -58,7 +58,7 @@ export default () => ({
});
`,
);
await fs.writeFile(data.pluginsManifest, JSON.stringify([{ id: 'a', enabled: true }]));
await fs.writeFile(data.pluginsManifest, JSON.stringify([{ id: '@test/a', enabled: true }]));
const { events, gate } = await import(pathToFileURL(path.join(data.root, 'recorder.mjs')).href);

const daemon = await createDaemon({ dataDirectory: data.root, codeDirectory: path.join(root, 'code') });
Expand Down
2 changes: 1 addition & 1 deletion apps/daemon/src/directories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface DataDirectory {
database: string;

/**
* The folder with one folder per installed plugin.
* The folder with the installed plugins.
*/
plugins: string;

Expand Down
133 changes: 71 additions & 62 deletions apps/daemon/src/plugins/plugin-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { PluginStore } from './plugin-store';
// A main module that records what happens to it, with `enabled` for an enable, through a module it and the test share.
function tracked(enabled = 'enable'): string {
return `
import { events } from '../../../recorder.mjs';
import { events } from '../../../../recorder.mjs';
export default ({ id }) => ({
onEnable() {
events.push(\`${enabled} \${id}\`);
Expand Down Expand Up @@ -93,61 +93,61 @@ function status(id: string) {

describe('loadAll', () => {
test('reads flags', async () => {
await install('a');
await install('b', { enabled: false });
await install('c');
await install('@test/a');
await install('@test/b', { enabled: false });
await install('@test/c');
await fs.rm(data.pluginsManifest);
await flag('a', true);
await flag('b', false);
await flag('@test/a', true);
await flag('@test/b', false);

await store.loadAll();

expect(store.plugins.map((plugin) => [plugin.id, plugin.status])).toEqual([
['a', 'enabled'],
['b', 'disabled'],
['c', 'disabled'],
['@test/a', 'enabled'],
['@test/b', 'disabled'],
['@test/c', 'disabled'],
]);
});

test('reports folder without manifest', async () => {
await fs.mkdir(path.join(data.plugins, 'a'), { recursive: true });
await fs.mkdir(path.join(data.plugins, '@test/a'), { recursive: true });

await store.loadAll();

expect(store.plugins).toMatchObject([
{ id: 'a', status: 'invalid', error: expect.stringContaining('does not exist') },
{ id: '@test/a', status: 'invalid', error: expect.stringContaining('does not exist') },
]);
});

test('reports malformed manifest', async () => {
await install('a');
await fs.writeFile(path.join(data.plugins, 'a', 'manifest.json'), '{');
await install('@test/a');
await fs.writeFile(path.join(data.plugins, '@test/a', 'manifest.json'), '{');

await store.loadAll();

expect(store.plugins).toMatchObject([
{ id: 'a', status: 'invalid', error: expect.stringContaining('is not JSON') },
{ id: '@test/a', status: 'invalid', error: expect.stringContaining('is not JSON') },
]);
});

test('reports manifest whose id is not its folder', async () => {
await install('a');
await fs.rename(path.join(data.plugins, 'a'), path.join(data.plugins, 'b'));
await install('@test/a');
await fs.rename(path.join(data.plugins, '@test/a'), path.join(data.plugins, '@test/b'));

await store.loadAll();

expect(store.plugins).toMatchObject([
{ id: 'b', status: 'invalid', error: expect.stringContaining('declares the id a') },
{ id: '@test/b', status: 'invalid', error: expect.stringContaining('declares the id @test/a') },
]);
});

test('ignores files beside plugins', async () => {
await install('a');
await install('@test/a');
await fs.writeFile(path.join(data.plugins, '.DS_Store'), '');

await store.loadAll();

expect(store.plugins.map((plugin) => plugin.id)).toEqual(['a']);
expect(store.plugins.map((plugin) => plugin.id)).toEqual(['@test/a']);
});

test('loads nothing without plugins folder', async () => {
Expand All @@ -165,11 +165,11 @@ describe('loadAll', () => {

describe('load', () => {
test('drops uninstalled plugin', async () => {
await install('a');
await install('@test/a');
await store.loadAll();
await fs.rm(path.join(data.plugins, 'a'), { recursive: true });
await fs.rm(path.join(data.plugins, '@test/a'), { recursive: true });

await store.load('a');
await store.load('@test/a');

expect(store.plugins).toEqual([]);
});
Expand All @@ -179,154 +179,163 @@ describe('load', () => {
});

test('runs reloaded code', async () => {
await install('a', { code: tracked('one') });
await install('@test/a', { code: tracked('one') });
await store.loadAll();
await store.enableAll({});
await fs.writeFile(path.join(data.plugins, 'a', 'daemon', 'main.js'), tracked('two'));
await fs.writeFile(path.join(data.plugins, '@test/a', 'daemon', 'main.js'), tracked('two'));

await store.disable('a');
await store.load('a');
await store.enable('a', {});
await store.disable('@test/a');
await store.load('@test/a');
await store.enable('@test/a', {});

expect(events).toEqual(['one a', 'disable a', 'dispose a', 'two a']);
expect(events).toEqual(['one @test/a', 'disable @test/a', 'dispose @test/a', 'two @test/a']);
});
});

describe('enableAll', () => {
test('enables dependencies first', async () => {
await install('b', { dependencies: { a: '^1.0.0' } });
await install('a');
await install('@test/b', { dependencies: { '@test/a': '^1.0.0' } });
await install('@test/a');
await store.loadAll();

await store.enableAll({});

expect(events).toEqual(['enable a', 'enable b']);
expect(events).toEqual(['enable @test/a', 'enable @test/b']);
});

test('skips disabled plugin', async () => {
await install('a', { enabled: false });
await install('@test/a', { enabled: false });
await store.loadAll();

await store.enableAll({});

expect(events).toEqual([]);
expect(status('a')).toBe('disabled');
expect(status('@test/a')).toBe('disabled');
});

test('marks plugin whose range excludes version incompatible', async () => {
await install('a', { elsewise: '^2.0.0' });
await install('@test/a', { elsewise: '^2.0.0' });
await store.loadAll();

await store.enableAll({});

expect(store.plugins).toMatchObject([{ id: 'a', status: 'incompatible', unsatisfied: { elsewise: '^2.0.0' } }]);
expect(store.plugins).toMatchObject([
{ id: '@test/a', status: 'incompatible', unsatisfied: { elsewise: '^2.0.0' } },
]);
expect(events).toEqual([]);
});

test('marks plugin whose targets exclude platform incompatible', async () => {
await install('a', { targets: [TARGET === 'win32-arm64' ? 'win32-x64' : 'win32-arm64'] });
await install('@test/a', { targets: [TARGET === 'win32-arm64' ? 'win32-x64' : 'win32-arm64'] });
await store.loadAll();

await store.enableAll({});

expect(store.plugins).toMatchObject([{ id: 'a', status: 'incompatible', unsatisfied: { target: TARGET } }]);
expect(store.plugins).toMatchObject([{ id: '@test/a', status: 'incompatible', unsatisfied: { target: TARGET } }]);
expect(events).toEqual([]);
});

test('enables plugin whose targets include platform', async () => {
await install('a', { targets: [TARGET] });
await install('@test/a', { targets: [TARGET] });
await store.loadAll();

await store.enableAll({});

expect(status('a')).toBe('enabled');
expect(events).toEqual(['enable a']);
expect(status('@test/a')).toBe('enabled');
expect(events).toEqual(['enable @test/a']);
});

test('marks plugin whose dependency is missing incompatible', async () => {
await install('b', { dependencies: { a: '^1.0.0' } });
await install('@test/b', { dependencies: { '@test/a': '^1.0.0' } });
await store.loadAll();

await store.enableAll({});

expect(store.plugins).toMatchObject([
{ id: 'b', status: 'incompatible', unsatisfied: { dependency: 'a', cause: 'missing' } },
{ id: '@test/b', status: 'incompatible', unsatisfied: { dependency: '@test/a', cause: 'missing' } },
]);
});

test('marks plugin whose dependency is wrong version incompatible', async () => {
await install('a', { version: '2.0.0' });
await install('b', { dependencies: { a: '^1.0.0' } });
await install('@test/a', { version: '2.0.0' });
await install('@test/b', { dependencies: { '@test/a': '^1.0.0' } });
await store.loadAll();

await store.enableAll({});

expect(status('b')).toBe('incompatible');
expect(events).toEqual(['enable a']);
expect(status('@test/b')).toBe('incompatible');
expect(events).toEqual(['enable @test/a']);
});

test('marks plugins in cycle incompatible', async () => {
await install('a', { dependencies: { b: '*' } });
await install('b', { dependencies: { a: '*' } });
await install('@test/a', { dependencies: { '@test/b': '*' } });
await install('@test/b', { dependencies: { '@test/a': '*' } });
await store.loadAll();

await store.enableAll({});

expect(store.plugins).toMatchObject([
{ id: 'a', status: 'incompatible', unsatisfied: { cycle: ['a', 'b'] } },
{ id: 'b', status: 'incompatible', unsatisfied: { cycle: ['a', 'b'] } },
{ id: '@test/a', status: 'incompatible', unsatisfied: { cycle: ['@test/a', '@test/b'] } },
{ id: '@test/b', status: 'incompatible', unsatisfied: { cycle: ['@test/a', '@test/b'] } },
]);
});

test('marks plugin without default function failed', async () => {
await install('a', { code: 'export const x = 1;' });
await install('@test/a', { code: 'export const x = 1;' });
await store.loadAll();

await store.enableAll({});

expect(status('a')).toBe('failed');
expect(status('@test/a')).toBe('failed');
});

test('marks plugin that throws failed', async () => {
await install('a', { code: 'export default () => ({ onEnable() { throw new Error("no"); } });' });
await install('@test/a', { code: 'export default () => ({ onEnable() { throw new Error("no"); } });' });
await store.loadAll();

await store.enableAll({});

expect(status('a')).toBe('failed');
expect(status('@test/a')).toBe('failed');
});

test('marks plugin whose module is missing failed', async () => {
await install('a');
await fs.rm(path.join(data.plugins, 'a', 'daemon', 'main.js'));
await install('@test/a');
await fs.rm(path.join(data.plugins, '@test/a', 'daemon', 'main.js'));
await store.loadAll();

await store.enableAll({});

expect(status('a')).toBe('failed');
expect(status('@test/a')).toBe('failed');
});

test('enables plugin without daemon part', async () => {
await install('a', { code: null });
await install('@test/a', { code: null });
await store.loadAll();

await store.enableAll({});

expect(status('a')).toBe('enabled');
expect(status('@test/a')).toBe('enabled');
expect(events).toEqual([]);
});
});

describe('disableAll', () => {
test('disables dependents first', async () => {
await install('a');
await install('b', { dependencies: { a: '^1.0.0' } });
await install('@test/a');
await install('@test/b', { dependencies: { '@test/a': '^1.0.0' } });
await store.loadAll();
await store.enableAll({});

await store.disableAll();

expect(events).toEqual(['enable a', 'enable b', 'disable b', 'dispose b', 'disable a', 'dispose a']);
expect(events).toEqual([
'enable @test/a',
'enable @test/b',
'disable @test/b',
'dispose @test/b',
'disable @test/a',
'dispose @test/a',
]);
expect(store.plugins.map((plugin) => plugin.status)).toEqual(['disabled', 'disabled']);
});
});
2 changes: 1 addition & 1 deletion apps/daemon/src/plugins/plugin-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { TARGET, VERSION } from '../global';
/**
* The installed plugins, loaded from the data directory, whose daemon parts run in the daemon.
*
* A load returns each folder in `plugins/` with its `manifest.json`, and whether `plugins.json` enables it. It
* A load returns each plugin's folder in `plugins/` with its `manifest.json`, and whether `plugins.json` enables it. It
* rejects a malformed `plugins.json` rather than repair it.
*/
export class PluginStore extends PluginResolver<InstalledPlugin, Daemon, Plugin, EnabledSnapshot> {
Expand Down
6 changes: 5 additions & 1 deletion apps/desktop/scripts/plugin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if (command !== 'add' && !(command === 'remove' && argument !== undefined)) {
const root = command === 'add' ? path.resolve(argument ?? '.') : undefined;
const manifest = command === 'add' ? JSON.parse(fs.readFileSync(path.join(root, 'manifest.json'), 'utf8')) : undefined;
const id = command === 'add' ? manifest.id : argument;
if (!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(id ?? '')) {
if (!/^@[a-z0-9][a-z0-9-]*\/[a-z0-9][a-z0-9-]*$/.test(id ?? '')) {
console.error(`[plugin] ${JSON.stringify(id)} is not a plugin id`);
process.exit(1);
}
Expand Down Expand Up @@ -79,6 +79,10 @@ if (command === 'add') {
for (const part of Object.keys(sides)) {
// Removes the plugin's settings too, which sit in its folder.
fs.rmSync(path.join(sides[part], 'plugins', id), { recursive: true, force: true });
const scope = path.dirname(path.join(sides[part], 'plugins', id));
if (fs.existsSync(scope) && fs.readdirSync(scope).length === 0) {
fs.rmdirSync(scope);
}
flag(part, false);
}
}
Expand Down
Loading
Loading