This repository has been archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
77 lines (67 loc) · 2.23 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Created by austin on 11/10/15.
*/
"use strict";
var constants = require("./constants");
var _scriptMethods = constants.scriptMethods
, _dialects = constants.dialects
, _dumpFormats = constants.dumpFormats;
// set up the command line interface
var argv = require("yargs")
.usage("Usage: $0 $1 --config [string] --dialect [string] --format [string] --out [relative path] --schema --bdr --dry")
.demand([1])
.describe(1, _scriptMethods.join("|"))
.describe("config", "config: specify config path")
.describe("dialect", "migration: choose desired knexjs dialect - " + _dialects.join("|"))
.describe("schema", "migrate: perform migration from dump.json file - true|false")
.describe("dry", "migrate: perform dry run - true|false")
.describe("format", "dump: choose dump format - " + _dumpFormats.join("|"))
.describe("out", "dump: choose dump output file name - ./relative/path/to/dump/file")
.describe("bdr", "bdr, use BDR compliant update statements - true|false")
.default(constants.runDefaults)
.check(function (args) {
if (_scriptMethods.indexOf(args._[0]) < 0) {
throw new Error("Invalid '-1' option '" + args._[0] + "' supplied!");
}
if (_dialects.indexOf(args.dialect) < 0) {
throw new Error("Invalid dialect '" + args.dialect + "' supplied!");
}
if (_dumpFormats.indexOf(args.format) < 0) {
throw new Error("Invalid format '" + args.format + "' supplied!");
}
return true;
})
.argv;
// imports
var path = require("path")
, _ = require("lodash");
// load config
var config = require(path.resolve(__dirname, argv.config));
// determine client path and instantiate connection
var clientPath = "";
switch (argv.dialect) {
case "pg":
clientPath = "lib/clients/postgres.js";
break;
case "mysql":
clientPath = "lib/clients/mysql.js"
break;
}
var client = require(path.resolve(__dirname, clientPath))(config);
// build _argv
var _argv = _.extend({}, argv, {
command: argv._[0],
client: client
});
// run it
var runner = require("./index");
runner.commandLine(_argv, function (err) {
if (!err) {
console.log("Migration task complete!");
}
else {
console.log("Error in migrate task!", err);
console.log(err.stack);
}
process.exit(err ? 1 : 0);
});