diff --git a/README.md b/README.md index d916dc3..53cf6d4 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,12 @@ $ npm install -g @ethersphere/bee-factory ## Usage ```shell -# This spin up the cluster and exits +# This spins up the cluster for specific Bee version and exits $ bee-factory start --detach 1.5.1 +# The spins up the cluster using Bee version configured in external places. See bellow for options where to place it. +$ bee-factory start --detach + # This attaches to the Queen container and displays its logs $ bee-factory logs queen --follow @@ -48,6 +51,13 @@ $ bee-factory start 1.5.1 For more details see the `--help` page of the CLI and its commands. +### External Bee version configuration + +You can omit the Bee version argument when running `bee-factory start` command if you specify it in one of the expected places: + + - `package.json` placed in current working directory (cwd) under the `engines.bee` property. + - `.beefactory.json` placed in current working directory (cwd) with property `version`. + ### Docker Images Bee Factory as the NPM package that you can install, like mentioned above, works in a way that it orchestrates launching Bee Factory Docker images diff --git a/package.json b/package.json index a1fdc85..c5d549b 100644 --- a/package.json +++ b/package.json @@ -73,6 +73,7 @@ }, "engines": { "node": ">=12.0.0", - "npm": ">=6.0.0" + "npm": ">=6.0.0", + "bee": "1.5.1" } } diff --git a/src/command/start.ts b/src/command/start.ts index cc07100..a94a769 100644 --- a/src/command/start.ts +++ b/src/command/start.ts @@ -11,6 +11,7 @@ import { import { waitForBlockchain, waitForQueen, waitForWorkers } from '../utils/wait' import ora from 'ora' import { VerbosityLevel } from './root-command/logging' +import { findBeeVersion } from '../utils/config-sources' const DEFAULT_REPO = 'ethersphere' @@ -72,12 +73,18 @@ export class Start extends RootCommand implements LeafCommand { }) public envPrefix!: string - @Argument({ key: 'bee-version', description: 'Bee image version', required: true }) + @Argument({ key: 'bee-version', description: 'Bee image version', required: false }) public beeVersion!: string public async run(): Promise { await super.init() + if (!this.beeVersion) { + this.beeVersion = await findBeeVersion() + this.console.log('Bee version not specified. Found it configured externally.') + this.console.log(`Spinning up cluster with Bee version ${this.beeVersion}.`) + } + const dockerOptions = await this.buildDockerOptions() const docker = new Docker(this.console, this.envPrefix, this.imagePrefix, this.repo) const status = await docker.getAllStatus() diff --git a/src/utils/config-sources.ts b/src/utils/config-sources.ts new file mode 100644 index 0000000..d1dfefb --- /dev/null +++ b/src/utils/config-sources.ts @@ -0,0 +1,32 @@ +import { readFile } from 'fs/promises' +import * as path from 'path' + +async function searchPackageJson(): Promise { + const expectedPath = path.join(process.cwd(), 'package.json') + const pkgJson = JSON.parse(await readFile(expectedPath, { encoding: 'utf8' })) + + return pkgJson?.engines?.bee +} + +async function searchBeeFactory(): Promise { + const expectedPath = path.join(process.cwd(), '.beefactory.json') + const pkgJson = JSON.parse(await readFile(expectedPath, { encoding: 'utf8' })) + + return pkgJson?.version +} + +export async function findBeeVersion(): Promise { + const packageJson = await searchPackageJson() + + if (packageJson) { + return packageJson + } + + const beeFactory = await searchBeeFactory() + + if (beeFactory) { + return beeFactory + } + + throw new Error('Bee Version was not specified nor it is present in expected external places!') +} diff --git a/test/integration/start.spec.ts b/test/integration/start.spec.ts index e1b618b..adbf607 100644 --- a/test/integration/start.spec.ts +++ b/test/integration/start.spec.ts @@ -8,8 +8,6 @@ import { Bee, BeeDebug, Reference } from '@ethersphere/bee-js' import { DockerError } from '../../src/utils/docker' import { findContainer, waitForUsablePostageStamp } from '../utils/docker' -const BEE_VERSION = '1.5.1' - let testFailed = false function wrapper(fn: () => Promise): () => Promise { @@ -53,7 +51,7 @@ describe('start command', () => { 'should start cluster', wrapper(async () => { // As spinning the cluster with --detach the command will exit once the cluster is up and running - await run(['start', '--detach', BEE_VERSION]) + await run(['start', '--detach']) await expect(findContainer(docker, 'queen')).resolves.toBeDefined() await expect(findContainer(docker, 'blockchain')).resolves.toBeDefined() @@ -83,7 +81,7 @@ describe('start command', () => { it( '', wrapper(async () => { - await run(['start', '--detach', BEE_VERSION]) + await run(['start', '--detach']) expect(docker.getNetwork(`${envPrefix}-network`)).toBeDefined() }), @@ -95,7 +93,7 @@ describe('start command', () => { beforeAll(async () => { console.log('(before) Starting up Bee Factory') - await run(['start', '--detach', BEE_VERSION]) + await run(['start', '--detach']) console.log('(before) Creating postage stamp ') const postage = await beeDebug.createPostageBatch('10', 18) @@ -116,7 +114,7 @@ describe('start command', () => { '', wrapper(async () => { console.log('(test) Starting the Bee Factory') - await run(['start', '--fresh', '--detach', BEE_VERSION]) + await run(['start', '--fresh', '--detach']) console.log('(test) Trying to fetch the data') await expect(bee.downloadData(reference)).rejects.toHaveProperty('status', 404) diff --git a/test/integration/stop.spec.ts b/test/integration/stop.spec.ts index 7127e9f..134037d 100644 --- a/test/integration/stop.spec.ts +++ b/test/integration/stop.spec.ts @@ -6,8 +6,6 @@ import { run } from '../utils/run' import { ENV_ENV_PREFIX_KEY } from '../../src/command/start' import { findContainer } from '../utils/docker' -const BEE_VERSION = '1.5.1' - describe('stop command', () => { let docker: Dockerode const envPrefix = `bee-factory-test-${crypto.randomBytes(4).toString('hex')}` @@ -26,7 +24,7 @@ describe('stop command', () => { describe('should stop cluster', () => { beforeAll(async () => { // As spinning the cluster with --detach the command will exit once the cluster is up and running - await run(['start', '--detach', BEE_VERSION]) + await run(['start', '--detach']) }) it('', async () => { @@ -51,7 +49,7 @@ describe('stop command', () => { describe('should stop cluster and remove containers', () => { beforeAll(async () => { // As spinning the cluster with --detach the command will exit once the cluster is up and running - await run(['start', '--detach', BEE_VERSION]) + await run(['start', '--detach']) }) it('', async () => {