-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcli.js
196 lines (174 loc) · 5.11 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
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
/* eslint-disable no-console, global-require, import/no-dynamic-require */
import Liftoff from 'liftoff'
import v8flags from 'v8flags'
import { jsVariants } from 'interpret'
import commander from 'commander'
import chalk from 'chalk'
import tildify from 'tildify'
import minimist from 'minimist'
import { join } from 'path'
import cliPkg from '../package.json'
import create from './create'
import drop from './drop'
import dump from './dump'
import load from './load'
import truncate from './truncate'
import checkStructure from './checkStructure'
const argv = minimist(process.argv.slice(2))
function exit(text) {
if (text instanceof Error) {
console.error(chalk.red(text.stack))
} else {
console.error(chalk.red(text))
}
process.exit(1)
}
const cli = new Liftoff({
name: 'knex',
extensions: jsVariants,
v8flags,
})
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.')
}
}
async function initConfig(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)))
}
const defaultEnv = 'development'
const rawConfig = await import(env.configPath)
let config = rawConfig.default || rawConfig
let environment = commander.env || process.env.NODE_ENV
if (!environment && typeof config[defaultEnv] === 'object') {
environment = defaultEnv
}
const knexScriptsConfig = config.knexScripts || {}
if (environment) {
console.log('Using environment:', chalk.magenta(environment))
config = { ...config, ...config[environment] }
}
if (!config) {
console.log(chalk.red('Warning: unable to read knexfile config'))
process.exit(1)
}
if (argv.debug !== undefined) config.debug = argv.debug
const knex = require(env.modulePath)
return {
getKnex: () => knex(config),
knexConfig: config,
env: environment,
structurePath: knexScriptsConfig.structurePath || 'db/structure.sql',
migrationsPath:
config.migrations && config.migrations.directory
? config.migrations.directory
: join(process.cwd(), 'migrations'),
docker:
(commander.docker !== undefined
? commander.docker
: knexScriptsConfig.docker) || false,
dockerService:
(commander.dockerService !== undefined
? commander.dockerService
: knexScriptsConfig.dockerService) || 'postgres',
}
}
function invoke(env) {
commander
.version(
chalk`{blue Knex Scripts version: {green ${cliPkg.version}}}\n` +
chalk`{blue Knex version: {green ${env.modulePackage.version}}}\n`,
)
.option('--docker', 'Use docker.')
.option(
'--docker-service [service]',
'Docker service name, default: "postgres".',
)
.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('create')
.description('Create database.')
.action(async () => {
const config = await initConfig(env)
return create(config)
.then(() => console.log(chalk.green('Database created.')))
.catch(exit)
})
commander
.command('drop')
.description('Drop database.')
.action(async () => {
const config = await initConfig(env)
return drop(config)
.then(() => console.log(chalk.green('Database dropped.')))
.catch(exit)
})
commander
.command('dump')
.description('Dump database.')
.action(async () => {
const config = await initConfig(env)
return dump(config)
.then(() => console.log(chalk.green('Dump created.')))
.catch(exit)
})
commander
.command('load')
.description('Load database.')
.action(async () => {
const config = await initConfig(env)
return load(config)
.then(() => console.log(chalk.green('Database loaded.')))
.catch(exit)
})
commander
.command('check-structure')
.description('Check structure.')
.action(async () => {
const config = await initConfig(env)
return checkStructure(config)
.then((upToDate) => {
if (upToDate) {
console.log(chalk.green('Structure is up to date.'))
} else {
console.log(chalk.red('Structure is not up to date.'))
process.exit(1)
}
})
.catch(exit)
})
commander
.command('truncate')
.description('Truncate all tables.')
.action(async () => {
const config = await initConfig(env)
return truncate(config)
.then(() => console.log(chalk.green(`Database truncated.`)))
.catch(exit)
})
commander.parse(process.argv)
}
cli.launch(
{
cwd: argv.cwd,
configPath: argv.knexfile,
require: argv.require,
completion: argv.completion,
},
invoke,
)