Files

65 lines
2.5 KiB
JavaScript

import { mkdir, rm, writeFile } from 'node:fs/promises';
import { join, resolve } from 'node:path';
import { lotsJournal } from '../../worker/fixtures/journal.ts';
import { ownedAuthSecret, ownedCredentials } from './auth-fixture.js';
import { categoryAllocationJournal } from './fixtures/category-allocation.js';
import { historyJournal } from './history-fixture.ts';
/**
* Materialize one fictional deployment, never an operator's config or data.
* The caller must supply an absent directory whose parent already exists.
* @param {string} directory
* @param {{ journal?: string, auth?: { secret: string, credentials: { username: string, password: string } } }} [options]
*/
export async function prepareBrowserFixture(
directory,
{ journal, auth = { secret: ownedAuthSecret, credentials: ownedCredentials } } = {}
) {
directory = resolve(directory);
// This must stay outside the cleanup block: an existing directory is not ours.
await mkdir(directory, { mode: 0o700 });
try {
for (const part of ['config', 'journal', 'journal/parts', 'data']) {
await mkdir(join(directory, part), { mode: 0o700 });
}
const sourceJournal = join(directory, 'journal', 'main.journal');
const configPath = join(directory, 'config', 'hledger-reports.yaml');
const files = [
[join(directory, 'journal', 'parts', 'base.journal'), lotsJournal],
[
join(directory, 'journal', 'parts', 'category-allocation.journal'),
categoryAllocationJournal
],
[join(directory, 'journal', 'parts', 'history.journal'), historyJournal],
[sourceJournal, 'include parts/*.journal\n'],
[
configPath,
JSON.stringify({
version: 1,
journal: journal ?? sourceJournal,
accounts: [
{ account: 'assets:bank:checking', label: 'Everyday checking', register: 'cash' },
{ account: 'assets:bank:history', label: 'History checking', register: 'cash' },
{ account: 'assets:investments:brokerage', label: 'Brokerage', register: 'investment' },
{ account: 'assets:investments:retirement', register: 'investment' }
],
url: { app: 'http://127.0.0.1:3000' },
auth
}) + '\n'
]
];
for (const [path, content] of files) {
await writeFile(path, content, { flag: 'wx', mode: 0o600 });
}
// Written last: permissions tooling accepts only a completed owned deployment.
await writeFile(join(directory, '.fixture'), 'hledger-browser-fixture\n', {
flag: 'wx',
mode: 0o600
});
return { directory, configPath };
} catch (error) {
await rm(directory, { recursive: true, force: true });
throw error;
}
}