forked from redwoodjs/redwood
/
install.js
99 lines (86 loc) · 2.26 KB
/
install.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
import path from 'path'
import execa from 'execa'
import fs from 'fs-extra'
import { Listr } from 'listr2'
import terminalLink from 'terminal-link'
import { errorTelemetry } from '@redwoodjs/telemetry'
import { getPaths } from '../../lib'
import c from '../../lib/colors'
const MODEL = `model RW_DataMigration {
version String @id
name String
startedAt DateTime
finishedAt DateTime
}`
const POST_INSTALL_INSTRUCTIONS = `${c.warning(
"Don't forget to apply your migration when ready:"
)}
${c.bold('yarn rw prisma migrate dev')}
`
// Creates dataMigrations directory
const createPath = () => {
return fs.outputFileSync(
path.join(getPaths().api.dataMigrations, '.keep'),
''
)
}
// Appends RW_DataMigration model to schema.prisma
const appendModel = () => {
const schemaPath = getPaths().api.dbSchema
const schema = fs.readFileSync(schemaPath).toString()
const newSchema = `${schema}\n${MODEL}\n`
return fs.writeFileSync(schemaPath, newSchema)
}
// Create a new migration
const save = async () => {
return await execa(
'yarn rw',
['prisma migrate dev', '--name create_data_migrations', '--create-only'],
{
cwd: getPaths().api.base,
shell: true,
}
)
}
export const command = 'install'
export const description = 'Add the RW_DataMigration model to your schema'
export const builder = (yargs) => {
yargs.epilogue(
`Also see the ${terminalLink(
'Redwood CLI Reference',
'https://redwoodjs.com/docs/cli-commands#install'
)}`
)
}
export const handler = async () => {
const tasks = new Listr(
[
{
title: `Creating dataMigrations directory...`,
task: createPath,
},
{
title: 'Adding RW_DataMigration model to schema.prisma...',
task: await appendModel,
},
{
title: 'Create db migration...',
task: await save,
},
{
title: 'One more thing...',
task: (_ctx, task) => {
task.title = `Next steps:\n ${POST_INSTALL_INSTRUCTIONS}`
},
},
],
{ rendererOptions: { collapse: false }, exitOnError: true }
)
try {
await tasks.run()
} catch (e) {
errorTelemetry(process.argv, e.message)
console.error(c.error(e.message))
process.exit(e?.exitCode || 1)
}
}