diff --git a/README.md b/README.md index de4e3bf0..704a4423 100644 --- a/README.md +++ b/README.md @@ -318,9 +318,9 @@ $ sudo systemctl status station ## API -### `new Core({ cacheRoot?: String, stateRoot?: String }?)` +### `Core.create({ cacheRoot?: String, stateRoot?: String }?)` -Creates a new instance of `Core`. +Returns `Promise`. ### `Core#logs.get(module?: String)` diff --git a/bin/station.js b/bin/station.js index d1a37660..68021f85 100755 --- a/bin/station.js +++ b/bin/station.js @@ -3,7 +3,7 @@ import * as commands from '../commands/index.js' import fs from 'node:fs/promises' import { join } from 'node:path' -import { repoRoot, getDefaultRootDirs } from '../lib/paths.js' +import { repoRoot } from '../lib/paths.js' import * as Sentry from '@sentry/node' import yargs from 'yargs/yargs' import { hideBin } from 'yargs/helpers' @@ -19,24 +19,7 @@ Sentry.init({ ignoreErrors: [/EACCES/, /EPERM/, /ENOSPC/, /EPIPE/] }) -const core = new Core(getDefaultRootDirs()) -const modules = [ - 'zinnia', - 'saturn-L2-node', - 'bacalhau' -] - -await fs.mkdir(core.paths.moduleLogs, { recursive: true }) -await fs.mkdir(core.paths.metrics, { recursive: true }) -await core.activity.maybeCreateActivityFile() -await core.metrics.maybeCreateMetricsFile() -await core.logs.maybeCreateLogFile() -for (const module of modules) { - await fs.mkdir(join(core.paths.moduleCache, module), { recursive: true }) - await fs.mkdir(join(core.paths.moduleState, module), { recursive: true }) - await core.metrics.maybeCreateMetricsFile(module) - await core.logs.maybeCreateLogFile(module) -} +const core = await Core.create() yargs(hideBin(process.argv)) .usage('Usage: $0 [options]') @@ -77,7 +60,7 @@ yargs(hideBin(process.argv)) () => {}, args => commands.logs({ ...args, core }) ) - .choices('module', modules) + .choices('module', core.modules) .version(`${pkg.name}: ${pkg.version}`) .alias('v', 'version') .alias('h', 'help') diff --git a/index.js b/index.js index 15bcb15f..3eb1fb0d 100644 --- a/index.js +++ b/index.js @@ -2,17 +2,21 @@ import { Activity } from './lib/activity.js' import { Logs } from './lib/log.js' import { Metrics } from './lib/metrics.js' import { getPaths, getDefaultRootDirs } from './lib/paths.js' +import fs from 'node:fs/promises' +import { join } from 'node:path' export { ActivityEvent } from './lib/activity.js' export { MetricsEvent } from './lib/metrics.js' export class Core { - /** - * @param {Object} [options] - * @param {String} [options.cacheRoot] - * @param {String} [options.stateRoot] - */ - constructor ({ cacheRoot, stateRoot } = getDefaultRootDirs()) { + modules = [ + 'zinnia', + 'saturn-L2-node', + 'bacalhau' + ] + + /** @private */ + constructor ({ cacheRoot, stateRoot }) { this.paths = getPaths({ cacheRoot, stateRoot }) this.logs = new Logs(this.paths.moduleLogs, this.paths.allLogs) this.activity = new Activity(this.paths.activity, this.logs) @@ -22,4 +26,30 @@ export class Core { this.logs ) } + + async #setup () { + await fs.mkdir(this.paths.moduleLogs, { recursive: true }) + await fs.mkdir(this.paths.metrics, { recursive: true }) + await this.activity.maybeCreateActivityFile() + await this.metrics.maybeCreateMetricsFile() + await this.logs.maybeCreateLogFile() + for (const module of this.modules) { + await fs.mkdir(join(this.paths.moduleCache, module), { recursive: true }) + await fs.mkdir(join(this.paths.moduleState, module), { recursive: true }) + await this.metrics.maybeCreateMetricsFile(module) + await this.logs.maybeCreateLogFile(module) + } + } + + /** + * @param {Object} [options] + * @param {String} [options.cacheRoot] + * @param {String} [options.stateRoot] + * @returns {Promise} + */ + static async create ({ cacheRoot, stateRoot } = getDefaultRootDirs()) { + const core = new Core({ cacheRoot, stateRoot }) + await core.#setup() + return core + } } diff --git a/test/api.js b/test/api.js index cec14b8f..cbeef1c4 100644 --- a/test/api.js +++ b/test/api.js @@ -3,34 +3,36 @@ import assert from 'node:assert' import { getDefaultRootDirs } from '../lib/paths.js' describe('API', () => { - describe('Core()', () => { + describe('Core', () => { it('can be imported', () => { assert(Core) }) - it('can be constructed', () => { - assert(new Core()) + }) + describe('Core.create', () => { + it('can be constructed', async () => { + assert(await Core.create()) }) - it('can be constructed with custom paths', () => { - assert(new Core(getDefaultRootDirs())) + it('can be constructed with custom paths', async () => { + assert(await Core.create(getDefaultRootDirs())) }) }) describe('core.logs', () => { - it('exports methods', () => { - const core = new Core() + it('exports methods', async () => { + const core = await Core.create() assert(core.logs.get) assert(core.logs.follow) }) }) describe('core.activity', () => { - it('exports methods', () => { - const core = new Core() + it('exports methods', async () => { + const core = await Core.create() assert(core.activity.get) assert(core.activity.follow) }) }) describe('core.metrics', () => { - it('exports methods', () => { - const core = new Core() + it('exports methods', async () => { + const core = await Core.create() assert(core.metrics.getLatest) assert(core.metrics.follow) })