1 | const {
|
2 | findConfig,
|
3 | getConfig,
|
4 | getSampleConfig,
|
5 | } = require('./config')
|
6 | const {
|
7 | getSampleMigration,
|
8 | runPendingMigrations,
|
9 | } = require('./migrations')
|
10 | const {
|
11 | mkdir,
|
12 | writeFile,
|
13 | } = require('./utils/fs')
|
14 | const path = require('path')
|
15 | const slugify = require('slugify')
|
16 |
|
17 | exports.getConfig = getConfig
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | exports.init = async (targetPath) => {
|
23 | const existingConfigPath = await findConfig()
|
24 | .catch(err => {
|
25 | if (err.code === 'NOCONFIG') return false
|
26 | throw err
|
27 | })
|
28 | const sampleConfig = await getSampleConfig()
|
29 | if (existingConfigPath) {
|
30 | const err = new Error(`A configuration already exists in "${existingConfigPath}"`)
|
31 | err.code = 'INITEXISTS'
|
32 | throw err
|
33 | }
|
34 | return writeFile(targetPath, sampleConfig)
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | exports.create = async (name) => {
|
41 | const config = await getConfig()
|
42 |
|
43 |
|
44 | const targetDir = config.migrationsDirectory
|
45 | await mkdir(targetDir, { recursive: true })
|
46 |
|
47 | const targetName = `${Date.now()}-${slugify(name, { lower: true })}.js`
|
48 | const targetPath = path.join(targetDir, targetName)
|
49 | const template = await getSampleMigration()
|
50 | await writeFile(targetPath, template, 'utf8')
|
51 | return targetPath
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | exports.migrate = async () => {
|
58 |
|
59 | const config = await getConfig()
|
60 |
|
61 |
|
62 | const context = config.context ? await config.context() : {}
|
63 |
|
64 | const ranMigrations = await runPendingMigrations()
|
65 |
|
66 | const state = await config.fetchState(context)
|
67 | return { state, ranMigrations }
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | exports.run = this.migrate
|
75 |
|
76 | exports.rollback = async () => {}
|
77 |
|
78 | exports.up = async () => {}
|
79 |
|
80 | exports.down = async () => {}
|