Skip to content

Commit

Permalink
feat: add a mysql-test CLI (#80)
Browse files Browse the repository at this point in the history
There was already a CLI, but it wasn't very complete and wasn't actually exposed under the "bin" field
  • Loading branch information
ForbesLindesay authored May 3, 2020
1 parent 5d23486 commit 626453e
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 49 deletions.
34 changes: 29 additions & 5 deletions docs/mysql-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,41 @@ If you need to run migrations before your tests run, e.g. to create database tab
},
"mysql": {
"test": {
"migrationsScript": [
"yarn",
"run",
"migrations:test"
]
"migrationsScript": "yarn run migrations:test"
}
}
```

Your migrations script will run with the `DATABASE_URL` set to the same value as for your tests.

## CLI

To install as a CLI:

```
npm i -g @databases/mysql-test
```

To start a local MySQL database on a free port, and apply any migrations you have configured (see Jest), you can run:

```
mysql-test start
```

When you're done with your database, you can dispose of it via:

```
mysql-test stop
```

If you have a script (e.g. a node.js server) that you need a MySQL database for, and you're happy for that MySQL database to be disposed of as soon as your script exits, you can do that via:

```
mysql-test run -- node my-server.js
```

The `--` is optional, but can be used to clarify where the `mysql-test` parameters end and your script begins.

## Circle CI

If the `DATABASE_URL` environment is already set, `mysql-test` does nothing. This means you can use CircleCI's native support for running tests with an acompanying database to run your tests. In your `.circleci/config.yml`:
Expand Down
12 changes: 10 additions & 2 deletions packages/mysql-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
"description": "",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"bin": {
"mysql-test": "./lib/cli.js"
},
"dependencies": {
"@databases/mysql-config": "^0.0.0",
"@databases/with-container": "^0.0.0",
"modern-spawn": "^1.0.0",
"ms": "^2.1.2",
"mysql2": "^1.6.4",
"yargs": "^12.0.5"
"parameter-reducers": "^1.0.1",
"type-assertions": "^1.1.0"
},
"scripts": {},
"repository": "https://github.com/ForbesLindesay/atdatabases/tree/master/packages/mysql-test",
Expand All @@ -25,5 +30,8 @@
"jest/globalSetup.d.ts",
"jest/globalTeardown.js",
"jest/globalTeardown.d.ts"
]
],
"devDependencies": {
"@types/ms": "^0.7.31"
}
}
94 changes: 58 additions & 36 deletions packages/mysql-test/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,62 @@
#! /usr/bin/env nod
#! /usr/bin/env node

import getDatabase, {killDatabase} from '.';
import * as commands from './commands';
const command = process.argv[2];
const args = process.argv.slice(3);

// tslint:disable-next-line:no-unused-expression
require('yargs')
.command(
'start',
'start the database',
(yargs: any) => {
// TODO: take options as CLI parameters
},
async (argv: any) => {
try {
const {databaseURL} = await getDatabase({detached: true});
console.info(databaseURL);
} catch (ex) {
console.error(ex.stack || ex);
process.exit(1);
}
},
)
.command(
'kill',
'kill the database',
(yargs: any) => {
// TODO: take options as CLI parameters
const hasHelpFlag = args.includes('--help') || args.includes('-h');
if (hasHelpFlag) {
commands.help();
process.exit(0);
}

switch (command) {
case 'start':
if (hasHelpFlag) {
commands.help('start');
} else {
handle(commands.start(args));
}
break;
case 'run':
if (hasHelpFlag) {
commands.help('run');
} else {
handle(commands.run(args));
}
break;
case 'stop':
if (hasHelpFlag) {
commands.help('stop');
} else {
handle(commands.stop(args));
}
break;
case 'help':
commands.help(args[0]);
break;
default:
commands.help();
if (!hasHelpFlag) {
console.error(
`Unexpected command ${command}, expected one of "start" or "help"`,
);
process.exit(1);
}
break;
}

function handle(v: Promise<number>) {
if (!v) {
process.exit(0);
}
v.then(
(value) => {
process.exit(value);
},
async (argv: any) => {
try {
await killDatabase();
} catch (ex) {
console.error(ex.stack || ex);
process.exit(1);
}
(ex) => {
console.error(ex.stack || ex);
process.exit(1);
},
)
.strict()
.demandCommand()
.help().argv;
);
}
Loading

0 comments on commit 626453e

Please sign in to comment.