-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli
executable file
·92 lines (78 loc) · 1.89 KB
/
cli
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
#! /usr/bin/env node
var path = require('path')
var migrations = require('./lib').default
var chalk = require('chalk')
var args = process.argv.slice(2)
var command = args[0]
var database = args[1]
var migrationsPath
if (args[2]) {
migrationsPath = path.normalize(args[2])
if (!path.isAbsolute(migrationsPath)) {
migrationsPath = path.normalize(path.join(process.cwd(), migrationsPath))
}
}
var schema = args[3]
function exitSuccess(result) {
printResult(result)
process.exit()
}
function logAndExitFailure(err) {
console.error(err)
return process.exit(1)
}
var handlers = {
run: function () {
migrations(database, { schema })
.fromDirectory(migrationsPath)
.run()
.then(exitSuccess)
.catch(logAndExitFailure)
},
test: function () {
migrations(database, { schema })
.fromDirectory(migrationsPath)
.test()
.then(exitSuccess)
.catch(logAndExitFailure)
},
info: function () {
migrations(database, { schema })
.fromDirectory(migrationsPath)
.info()
.then((info) => {
printInfo(info)
process.exit()
})
.catch(logAndExitFailure)
},
}
function printResult(result) {
result.applied.map(function (migration) {
if (migration.results) {
console.log(chalk.white(JSON.stringify(migration.results)))
}
})
}
function printInfo(info) {
console.log(
chalk.magenta('Applied') + ' | ' + chalk.magenta('Name')
)
info.applied.map(function (migration) {
console.log(
chalk.green(migration.applied.toISOString()) +
' | ' +
chalk.yellow(migration.name)
)
})
}
var handler = handlers[command]
if (!handler || (command === 'run' && !migrationsPath)) {
console.log(
'Usage: sfm [' +
Object.keys(handlers).join('|') +
'] [database url] [migrations path] [optional schema]'
)
process.exit(1)
}
handler(database)