diff --git a/bin/elasticsearch-migrate b/bin/elasticsearch-migrate new file mode 100755 index 0000000..d98bff5 --- /dev/null +++ b/bin/elasticsearch-migrate @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +require('../dist/cli.js') diff --git a/package.json b/package.json index 803ac40..025ea9e 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "node": ">=8" }, "bin": { - "elasticsearch-migrate": "dist/cli.js" + "elasticsearch-migrate": "bin/elasticsearch-migrate" }, "scripts": { "build": "tsc", diff --git a/src/cli.helper.ts b/src/cli.helper.ts new file mode 100644 index 0000000..206d934 --- /dev/null +++ b/src/cli.helper.ts @@ -0,0 +1,38 @@ +import {Client} from "@elastic/elasticsearch"; +import {join} from "path"; +import Storage from "./storage"; +import Runner from "./runner"; +import {Migration} from "./migration"; + +export interface Configuration { + root: string, + migrations: string[] + elasticsearch: string + index: string +} + +export function getClient(argv: Configuration) { + if(typeof argv.elasticsearch === 'string') { + if(argv.elasticsearch.match(/http/)) { + return new Client({node: argv.elasticsearch}); + } + else { + return require(join(argv.root, argv.elasticsearch)) + } + } + return new Client(argv.elasticsearch); +} + +export function getRunner(argv: Configuration) { + const client = getClient(argv); + const storage = new Storage(client, argv.index); + const runner = new Runner(client, storage); + runner.on('up', (migration: Migration) => { + process.stdout.write(`\u2713 ${migration.title}\n`) + }); + runner.on('down', (migration: Migration) => { + process.stdout.write(`\u2713 ${migration.title}\n`) + }) + + return runner; +} diff --git a/src/cli.ts b/src/cli.ts index e0d5c71..0060b6a 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,94 +1,15 @@ -import {Client} from "@elastic/elasticsearch"; -import Runner from './runner'; -import Storage from './storage'; -import Loader from "./loader"; -import {Migration} from "./migration"; import {join} from 'path' - import yargs from 'yargs' -interface Configuration { - root: string, - migrations: string[] - elasticsearch: string - index: string -} - yargs.options({ r: {alias: 'root', type: 'string', default: process.cwd()}, m: {alias: 'migrations', type: 'array', default: ['./migrations/*.js']}, e: {alias: 'elasticsearch', type: 'string', default: 'http://localhost:9200'}, i: {alias: 'index', type: 'string', default: 'migrations'}, }); -yargs.pkgConf('elasticsearch-migrate'); - -yargs.command({ - command: 'list', - describe: 'List migrations', - handler: async function(argv: Configuration) { - const loader = new Loader(argv.root, argv.migrations); - const client = getClient(argv); - const storage = new Storage(client); - const migrations = await loader.load(); - const last = await storage.getLastRevision(); - process.stdout.write('Migrations:\n-----------\n') - migrations.forEach(function (migration) { - process.stdout.write(`${migration.title === last ? '->' : ' '} ${migration.title}\n`) - }) - await client.close(); - }, -}) - -yargs.command({ - command: 'up', - describe: 'Execute migrations in order', - handler: async function(argv: Configuration) { - const loader = new Loader(argv.root, argv.migrations); - const runner = getRunner(argv); - process.stdout.write('Running migrations...\n---------------------\n'); - await runner.up(await loader.load()); - await runner.client.close(); - } -}) - -yargs.command({ - command: 'down', - describe: 'Rollback migrations', - handler: async function(argv: Configuration) { - const loader = new Loader(argv.root, argv.migrations); - const runner = getRunner(argv); - process.stdout.write('Running downward migrations...\n------------------------------\n'); - await runner.down(await loader.load()); - await runner.client.close(); - } -}) - -yargs.help() +yargs.pkgConf('elasticsearch-migrate') + .commandDir(join(__dirname, 'commands')) + .help() .demandCommand() .argv -function getClient(argv: Configuration) { - if(typeof argv.elasticsearch === 'string') { - if(argv.elasticsearch.match(/http/)) { - return new Client({node: argv.elasticsearch}); - } - else { - return require(join(argv.root, argv.elasticsearch)) - } - } - return new Client(argv.elasticsearch); -} - -function getRunner(argv: Configuration) { - const client = getClient(argv); - const storage = new Storage(client, argv.index); - const runner = new Runner(client, storage); - runner.on('up', (migration: Migration) => { - process.stdout.write(`\u2713 ${migration.title}\n`) - }); - runner.on('down', (migration: Migration) => { - process.stdout.write(`\u2713 ${migration.title}\n`) - }) - - return runner; -} diff --git a/src/commands/down.ts b/src/commands/down.ts new file mode 100644 index 0000000..4355885 --- /dev/null +++ b/src/commands/down.ts @@ -0,0 +1,12 @@ +import Loader from "../loader"; +import {Configuration, getRunner} from "../cli.helper"; + +export const command = 'down'; +export const describe = 'Rollback migrations'; +export const handler = async function(argv: Configuration) { + const loader = new Loader(argv.root, argv.migrations); + const runner = getRunner(argv); + process.stdout.write('Running downward migrations...\n------------------------------\n'); + await runner.down(await loader.load()); + await runner.client.close(); +} diff --git a/src/commands/list.ts b/src/commands/list.ts new file mode 100644 index 0000000..a2de96d --- /dev/null +++ b/src/commands/list.ts @@ -0,0 +1,18 @@ +import {Configuration, getClient} from "../cli.helper"; +import Loader from "../loader"; +import Storage from "../storage"; + +export const command = 'list' +export const describe = 'List migrations' +export const handler = async function(argv: Configuration) { + const loader = new Loader(argv.root, argv.migrations); + const client = getClient(argv); + const storage = new Storage(client); + const migrations = await loader.load(); + const last = await storage.getLastRevision(); + process.stdout.write('Migrations:\n-----------\n') + migrations.forEach(function (migration) { + process.stdout.write(`${migration.title === last ? '->' : ' '} ${migration.title}\n`) + }) + await client.close(); +}; diff --git a/src/commands/up.ts b/src/commands/up.ts new file mode 100644 index 0000000..d531fe1 --- /dev/null +++ b/src/commands/up.ts @@ -0,0 +1,12 @@ +import Loader from "../loader"; +import {Configuration, getRunner} from "../cli.helper"; + +export const command = 'up'; +export const describe = 'Execute migrations in order'; +export const handler = async function(argv: Configuration) { + const loader = new Loader(argv.root, argv.migrations); + const runner = getRunner(argv); + process.stdout.write('Running migrations...\n---------------------\n'); + await runner.up(await loader.load()); + await runner.client.close(); +} diff --git a/tsconfig.json b/tsconfig.json index 8c7fd95..3a9db0a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,7 @@ "strictPropertyInitialization": false, "emitDecoratorMetadata": true, "experimentalDecorators": true, - }, - "include": ["src/**/*"] + "include": ["src/**/*"], + "exclude": ["src/**/*.test.ts"] }