From 77242a11913f13f47610b63f198e99583db40ddd Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 6 Jul 2021 12:10:10 +0200 Subject: [PATCH] feat: support options --- README.md | 30 ++++++++++++++++++++---------- src/index.ts | 46 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 52 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 17482fc..fe35dfd 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,22 @@ # 🥭 mongoz -> Ad hoc MongoDB server for Node.js +> Zero Config MongoDB Server for Node.js ## Why? [MongoDB](https://www.mongodb.com) is fantastic but setup for small projects can be lots of trouble. -This little package does everything necessary to download and start a fresh mongo server! +This little package does everything necessary to download and start a fresh MongoDB server! ## Usage **Note:** You need to have [Node.js](https://nodejs.org/en/) and [npm](https://docs.npmjs.com/cli/v7/commands/npm) already installed! -**Note:** Make sure there is not already a local mongodb server listening on default port (`2701`). If you do, either stop it or use `PORT` environment variable to change the port. +**Note:** Make sure there is not already a local mongodb server listening on default port (`2701`). If you do, either stop it or use `MONGO_PORT` environment variable to change the port. ### Standalone Server -Let's start a fresh mongoz shall we? +Let's start a fresh db shall we? ```sh npx mongoz @@ -24,9 +24,9 @@ npx mongoz It will take few seconds on first time of usage to install and extract mongo server. -### Programmatic Usage +### Programmatic usage -Do you need a MongoDB server? No problems! +Do you need a MongoDB server programmatically? No problems! ```js // CommonJS @@ -37,15 +37,25 @@ import { startMongo } from 'mongoz' // Install and start listening MongoDB on 127.0.0.1:27017 in background await startMongo() + +// Or with options +await startMongo({ port: 27018 }) ``` When closing server, mongo will also gracefully shutdown with [node-graceful-shutdown](https://www.npmjs.com/package/node-graceful-shutdown). -### In parallel with server +#### Options + +- `name`: Unique instance name. Default is `default` (env var: `MONGO_NAME`) +- `dir`: Data directory to store logs and data. Default is `${os.tmpDir}/mongo`. (env var: `MONGO_DIR`) +- `port`: Listening port. Default is `27017` (env var: `MONGO_PORT` or `PORT`) +- `platform`: OS to download binraries for. + +### In parallel with script -You can also use [concurrently](https://www.npmjs.com/package/concurrently) to start mongo alongside with server: +You can also use [concurrently](https://www.npmjs.com/package/concurrently) to start mongo alongside with server:. -`package.json`: +Via `package.json`: ```json { @@ -63,7 +73,7 @@ npx concurrently 'npx mongoz' 'node ./server.mjs' ## Supported platforms -Windows, Linux and Darwin (Mac) are supported. Check [formula](./src/formula) for details. +Windows, Linux and Darwin (Mac) are supported. Check [formula](./src/formula.ts) for details. ## Changing data dir diff --git a/src/index.ts b/src/index.ts index 042e4c3..6d043a3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,26 +4,45 @@ import fsExtra from 'fs-extra' import download from 'download' import consola from 'consola' import decompress from 'decompress' -import execa from 'execa' +import execa, { ExecaChildProcess } from 'execa' import ora from 'ora' import { onShutdown } from 'node-graceful-shutdown' import { mongoFormula as formula } from './formula' -export async function startMongo (opts = { args: null }) { - const platformName = process.platform - let platform = formula.platforms.find(p => p.name === platformName) - if (!platform) { - throw new Error(`Platform '${platformName}' is not available for '${formula.name}'`) +export interface MongoOptions { + name?: string + platform?: string + dir?: string + port?: string | number + args?: string[] +} + +export interface MongoService { + server: ExecaChildProcess + close: () => Promise +} + +export async function startMongo(opts: MongoOptions): MongoService { + // Apply defaults + opts = { + name: process.env.MONGO_NAME || 'default', + port: process.env.MONGO_PORT || process.env.PORT || formula.port, + platform: process.env.MONGO_PLATFORM || process.platform, + dir: process.env.MONGO_DIR || path.resolve(os.tmpdir(), 'mongo'), + ...opts } - const instanceName = 'default' - const appDir = process.env.MONGO_DIR || path.resolve(os.tmpdir(), formula.name) + // Find platform + const platform = formula.platforms.find(p => p.name === opts.platform) + if (!platform) { + throw new Error(`Platform '${opts.platform}' is not available for '${formula.name}'`) + } // Resolve paths - const dataDir = path.resolve(appDir, 'data', instanceName, formula.name) - const logsDir = path.resolve(appDir, 'logs', instanceName, formula.name) + const dataDir = path.resolve(opts.dir, 'data', opts.name, formula.name) + const logsDir = path.resolve(opts.dir, 'logs', opts.name, formula.name) const logFile = path.resolve(logsDir, 'logs.txt') - const sourceDir = path.resolve(appDir, 'source', formula.name, formula.version, platform.name) + const sourceDir = path.resolve(opts.dir, 'source', formula.name, formula.version, platform.name) const sourceFileName = `${formula.name}-${formula.version}-${platform.name}` + path.extname(platform.source) const sourceFile = path.resolve(sourceDir, sourceFileName) const extractDir = path.resolve(sourceDir, 'unpacked') @@ -60,14 +79,13 @@ export async function startMongo (opts = { args: null }) { consola.info(`Writing logs to: ${logFile}`) // Port and args - const port = process.env.PORT || formula.port - const execArgs = formula.execArgs.replace('{port}', port + '').replace('{data}', dataDir).split(' ') + const execArgs = formula.execArgs.replace('{port}', opts.port + '').replace('{data}', dataDir).split(' ') if (Array.isArray(opts.args)) { execArgs.push(...opts.args) } // Start app - spinner.info(`Starting ${formula.name} at port ${port}`) + spinner.info(`Starting ${formula.name} at port ${opts.port}`) const server = execa(execFile, execArgs, { stdout, stderr