cli.js
6.35 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env node
/* eslint no-console:0, no-var:0 */
var Liftoff = require('liftoff');
var Promise = require('bluebird');
var interpret = require('interpret');
var path = require('path');
var chalk = require('chalk');
var tildify = require('tildify');
var commander = require('commander');
var argv = require('minimist')(process.argv.slice(2));
var fs = Promise.promisifyAll(require('fs'));
var cliPkg = require('../package');
function exit(text) {
if (text instanceof Error) {
chalk.red(console.error(text.stack));
} else {
chalk.red(console.error(text));
}
process.exit(1);
}
function success(text) {
console.log(text);
process.exit(0);
}
function checkLocalModule(env) {
if (!env.modulePath) {
console.log(chalk.red('No local knex install found in:'), chalk.magenta(tildify(env.cwd)));
exit('Try running: npm install knex.');
}
}
function initKnex(env) {
checkLocalModule(env);
if (!env.configPath) {
exit('No knexfile found in this directory. Specify a path with --knexfile');
}
if (process.cwd() !== env.cwd) {
process.chdir(env.cwd);
console.log('Working directory changed to', chalk.magenta(tildify(env.cwd)));
}
var environment = commander.env || process.env.NODE_ENV;
var defaultEnv = 'development';
var config = require(env.configPath);
if (!environment && typeof config[defaultEnv] === 'object') {
environment = defaultEnv;
}
if (environment) {
console.log('Using environment:', chalk.magenta(environment));
config = config[environment] || config;
}
if (!config) {
console.log(chalk.red('Warning: unable to read knexfile config'));
process.exit(1);
}
if (argv.debug !== undefined)
config.debug = argv.debug;
var knex = require(env.modulePath);
return knex(config);
}
function invoke(env) {
var filetypes = ['js', 'coffee', 'ts', 'eg', 'ls'];
var pending = null;
commander
.version(
chalk.blue('Knex CLI version: ', chalk.green(cliPkg.version)) + '\n' +
chalk.blue('Local Knex version: ', chalk.green(env.modulePackage.version)) + '\n'
)
.option('--debug', 'Run with debugging.')
.option('--knexfile [path]', 'Specify the knexfile path.')
.option('--cwd [path]', 'Specify the working directory.')
.option('--env [name]', 'environment, default: process.env.NODE_ENV || development');
commander
.command('init')
.description(' Create a fresh knexfile.')
.option(`-x [${filetypes.join('|')}]`, 'Specify the knexfile extension (default js)')
.action(function() {
var type = (argv.x || 'js').toLowerCase();
if (filetypes.indexOf(type) === -1) {
exit(`Invalid filetype specified: ${type}`);
}
if (env.configPath) {
exit(`Error: ${env.configPath} already exists`);
}
checkLocalModule(env);
var stubPath = `./knexfile.${type}`;
pending = fs.readFileAsync(
path.dirname(env.modulePath) +
'/lib/migrate/stub/knexfile-' +
type + '.stub'
).then(function(code) { return fs.writeFileAsync(stubPath, code) }).then(function() {
success(chalk.green(`Created ${stubPath}`));
}).catch(exit);
});
commander
.command('migrate:make <name>')
.description(' Create a named migration file.')
.option(`-x [${filetypes.join('|')}]`, 'Specify the stub extension (default js)')
.action(function(name) {
var instance = initKnex(env);
var ext = (argv.x || env.configPath.split('.').pop()).toLowerCase();
pending = instance.migrate.make(name, {extension: ext}).then(function(name) {
success(chalk.green(`Created Migration: ${name}`));
}).catch(exit);
});
commander
.command('migrate:latest')
.description(' Run all migrations that have not yet been run.')
.action(function() {
pending = initKnex(env).migrate.latest().spread(function(batchNo, log) {
if (log.length === 0) {
success(chalk.cyan('Already up to date'));
}
success(
chalk.green(`Batch ${batchNo} run: ${log.length} migrations \n`) +
chalk.cyan(log.join('\n'))
);
}).catch(exit);
});
commander
.command('migrate:rollback')
.description(' Rollback the last set of migrations performed.')
.action(function() {
pending = initKnex(env).migrate.rollback().spread(function(batchNo, log) {
if (log.length === 0) {
success(chalk.cyan('Already at the base migration'));
}
success(
chalk.green(`Batch ${batchNo} rolled back: ${log.length} migrations \n`) +
chalk.cyan(log.join('\n'))
);
}).catch(exit);
});
commander
.command('migrate:currentVersion')
.description(' View the current version for the migration.')
.action(function () {
pending = initKnex(env).migrate.currentVersion().then(function(version) {
success(chalk.green('Current Version: ') + chalk.blue(version));
}).catch(exit);
});
commander
.command('seed:make <name>')
.description(' Create a named seed file.')
.option(`-x [${filetypes.join('|')}]`, 'Specify the stub extension (default js)')
.action(function(name) {
var instance = initKnex(env);
var ext = (argv.x || env.configPath.split('.').pop()).toLowerCase();
pending = instance.seed.make(name, {extension: ext}).then(function(name) {
success(chalk.green(`Created seed file: ${name}`));
}).catch(exit);
});
commander
.command('seed:run')
.description(' Run seed files.')
.action(function() {
pending = initKnex(env).seed.run().spread(function(log) {
if (log.length === 0) {
success(chalk.cyan('No seed files exist'));
}
success(chalk.green(`Ran ${log.length} seed files \n${chalk.cyan(log.join('\n'))}`));
}).catch(exit);
});
commander.parse(process.argv);
Promise.resolve(pending).then(function() {
commander.help();
});
}
var cli = new Liftoff({
name: 'knex',
extensions: interpret.jsVariants,
v8flags: require('v8flags')
});
cli.on('require', function(name) {
console.log('Requiring external module', chalk.magenta(name));
});
cli.on('requireFail', function(name) {
console.log(chalk.red('Failed to load external module'), chalk.magenta(name));
});
cli.launch({
cwd: argv.cwd,
configPath: argv.knexfile,
require: argv.require,
completion: argv.completion
}, invoke);