1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | const path = require('path')
|
6 | const mygreat = require('../index')
|
7 | const program = require('commander')
|
8 | const ConflictError = require('../errors/conflict')
|
9 | const environment = process.env.NODE_ENV || 'development'
|
10 | const fallback = (value, defaultValue) => value || defaultValue
|
11 |
|
12 | const configFactory = (dir) => {
|
13 | return Object.assign(
|
14 | { shutdown: () => { } },
|
15 | require(path.resolve(process.cwd(), dir))(environment)
|
16 | )
|
17 | }
|
18 |
|
19 | const errorHandler = (err) => {
|
20 | console.log('│')
|
21 | console.log('│')
|
22 |
|
23 | if (err instanceof ConflictError) {
|
24 | console.error('│ ┌')
|
25 | console.error('│ │ [ error: conflict ]')
|
26 | console.error('│ │ unable to proceed due possible integrity break')
|
27 | console.error('│ │')
|
28 | console.error('│ │ any of the following scenarios could couse this error:')
|
29 | console.error('├──┤ - a synced migration file has been renamed;')
|
30 | console.error('│ │ - a new migration has been named wrongly - all new')
|
31 | console.error('│ │ migrations should be placed as last, considering')
|
32 | console.error('│ │ an ascending order;')
|
33 | console.error('│ │ - a synced migration file has been deleted before')
|
34 | console.error('│ │ been migrated-down;')
|
35 | console.error('│ └')
|
36 | } else {
|
37 | console.error('│ ┌')
|
38 | console.error(`├──┤ [ error: ${err.name} ]`)
|
39 | console.error('│ │', err.message)
|
40 | console.error('│ └')
|
41 | }
|
42 |
|
43 | console.log('│')
|
44 | console.log('│')
|
45 | console.log('└─ [ » exit 1 ]')
|
46 | process.exit(1)
|
47 | }
|
48 |
|
49 | const action = async (file, direction) => {
|
50 | console.log([' ╔═════════', (direction === 'up') ? '═══╗' : '═════╗'].join(''))
|
51 | console.log(['┌─║ migrate ', (direction === 'up') ? 'up ║' : 'down ║'].join(''))
|
52 | console.log(['│ ╚═════════', (direction === 'up') ? '═══╝' : '═════╝'].join(''))
|
53 | console.log('│')
|
54 | console.log('├─ setting up ...')
|
55 | const config = configFactory(file)
|
56 | const migrate = mygreat.from(config.local, config.remote)
|
57 | const setup = await config.setup()
|
58 | const fn = migrate[direction]
|
59 | console.log('├─ migrating ...')
|
60 | const result = await fn(setup)
|
61 |
|
62 | if (!result || result.content.length === 0) {
|
63 | console.log('│ └─ nothing to migrate')
|
64 | console.log('│')
|
65 | console.log('└─ [ » exit 0 ]')
|
66 | config.shutdown()
|
67 | return
|
68 | }
|
69 | console.log(`│ └─ migrated ${direction}`)
|
70 | console.log('│')
|
71 | console.log('│')
|
72 | console.log('│ ╔══════════════════════════════════════════════════════════════╗')
|
73 | console.log(`└─║ migration execution id #${result.name} ║─┐`)
|
74 | console.log(' ╚══════════════════════════════════════════════════════════════╝ │')
|
75 | console.log(' ┌──────────────────────────────────────────────────┐ │')
|
76 | console.log('┌───────┤ the following migration files have been executed │───────┘')
|
77 | console.log('│ └──────────────────────────────────────────────────┘')
|
78 | console.log('│')
|
79 | result.content.forEach(file => { console.log(`├─ file "${file}" / method "${direction}"`) })
|
80 | console.log('│')
|
81 | console.log('└─ [ » exit 0 ]')
|
82 |
|
83 | return config.shutdown()
|
84 | }
|
85 |
|
86 | ;(async () => {
|
87 | program
|
88 | .version(process.env.npm_package_version)
|
89 | .option('-f, --file <dir>', 'path to mygreat configuration file', fallback, './.mygreat.js')
|
90 |
|
91 | program
|
92 | .command('up')
|
93 | .action(options => action(options.parent.file, 'up').catch(errorHandler))
|
94 |
|
95 | program
|
96 | .command('down')
|
97 | .action(options => action(options.parent.file, 'down').catch(errorHandler))
|
98 |
|
99 | program.parse(process.argv)
|
100 | })()
|
101 |
|