Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>`.

### `Core#logs.get(module?: String)`

Expand Down
23 changes: 3 additions & 20 deletions bin/station.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 <command> [options]')
Expand Down Expand Up @@ -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')
Expand Down
42 changes: 36 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<Core>}
*/
static async create ({ cacheRoot, stateRoot } = getDefaultRootDirs()) {
const core = new Core({ cacheRoot, stateRoot })
await core.#setup()
return core
}
}
24 changes: 13 additions & 11 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down