Skip to content

Commit

Permalink
Split commands out of cli.js
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayliss committed Dec 31, 2019
1 parent 56ace64 commit a06b90e
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 85 deletions.
3 changes: 3 additions & 0 deletions bin/elasticsearch-migrate
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../dist/cli.js')
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"node": ">=8"
},
"bin": {
"elasticsearch-migrate": "dist/cli.js"
"elasticsearch-migrate": "bin/elasticsearch-migrate"
},
"scripts": {
"build": "tsc",
Expand Down
38 changes: 38 additions & 0 deletions src/cli.helper.ts
Original file line number Diff line number Diff line change
@@ -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;
}
85 changes: 3 additions & 82 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions src/commands/down.ts
Original file line number Diff line number Diff line change
@@ -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();
}
18 changes: 18 additions & 0 deletions src/commands/list.ts
Original file line number Diff line number Diff line change
@@ -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();
};
12 changes: 12 additions & 0 deletions src/commands/up.ts
Original file line number Diff line number Diff line change
@@ -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();
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"strictPropertyInitialization": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,

},
"include": ["src/**/*"]
"include": ["src/**/*"],
"exclude": ["src/**/*.test.ts"]
}

0 comments on commit a06b90e

Please sign in to comment.