-
Notifications
You must be signed in to change notification settings - Fork 2
/
instructions.ts
174 lines (152 loc) · 5.05 KB
/
instructions.ts
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
import * as sinkStatic from '@adonisjs/sink'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
import { join } from 'path'
type InstructionsState = {
permissionsTableName: string
rolesTableName: string
roleHasPermissionsTableName: string
rolesSchemaName: string
permissionsSchemaName: string
roleHasPermissionsSchemaName: string
}
function getStub(...paths: string[]) {
return join(__dirname, 'templates', ...paths)
}
function makePermissionsMigration(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
state: InstructionsState
) {
const migrationsDirectory = app.directoriesMap.get('migrations') || 'database'
const migrationPath = join(migrationsDirectory, `${Date.now()}_${state.permissionsTableName}.ts`)
const template = new sink.files.MustacheFile(
projectRoot,
migrationPath,
getStub('migrations/permissions.txt')
)
if (template.exists()) {
sink.logger.action('create').skipped(`${migrationPath} file already exists`)
return
}
template.apply(state).commit()
sink.logger.action('create').succeeded(migrationPath)
}
function makeRolesMigration(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
state: InstructionsState
) {
const migrationsDirectory = app.directoriesMap.get('migrations') || 'database'
const migrationPath = join(migrationsDirectory, `${Date.now()}_${state.rolesTableName}.ts`)
const template = new sink.files.MustacheFile(
projectRoot,
migrationPath,
getStub('migrations/roles.txt')
)
if (template.exists()) {
sink.logger.action('create').skipped(`${migrationPath} file already exists`)
return
}
template.apply(state).commit()
sink.logger.action('create').succeeded(migrationPath)
}
function makeRolesHasPermissionsMigration(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
state: InstructionsState
) {
const migrationsDirectory = app.directoriesMap.get('migrations') || 'database'
const migrationPath = join(
migrationsDirectory,
`${Date.now()}_${state.roleHasPermissionsTableName}.ts`
)
const template = new sink.files.MustacheFile(
projectRoot,
migrationPath,
getStub('migrations/roles_pivot.txt')
)
if (template.exists()) {
sink.logger.action('create').skipped(`${migrationPath} file already exists`)
return
}
template.apply(state).commit()
sink.logger.action('create').succeeded(migrationPath)
}
function makeConfig(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
state: InstructionsState
) {
const configDirectory = app.directoriesMap.get('config') || 'config'
const configPath = join(configDirectory, 'permissions.ts')
const template = new sink.files.MustacheFile(projectRoot, configPath, getStub('config.txt'))
template.overwrite = true
template.apply(state).commit()
sink.logger.action('create').succeeded(configPath)
}
async function getMigrationConsent(sink: typeof sinkStatic, tableName: string): Promise<boolean> {
return sink
.getPrompt()
.confirm(`Create migration for the ${sink.logger.colors.underline(tableName)} table?`)
}
async function getPermissionsTableName(sink: typeof sinkStatic): Promise<string> {
return sink.getPrompt().ask('Enter the permissions table name', {
default: 'permissions',
validate(value) {
return !!value.trim().length
},
})
}
async function getRolesTableName(sink: typeof sinkStatic): Promise<string> {
return sink.getPrompt().ask('Enter the roles table name', {
default: 'roles',
validate(value) {
return !!value.trim().length
},
})
}
async function getPermissionRolesPivotTable(sink: typeof sinkStatic): Promise<string> {
return sink.getPrompt().ask('Enter the roles and permissions pivot table name', {
default: 'role_has_permissions',
validate(value) {
return !!value.trim().length
},
})
}
export default async function instructions(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic
) {
const state: InstructionsState = {
rolesSchemaName: 'Roles',
permissionsSchemaName: 'Permissions',
roleHasPermissionsSchemaName: 'RoleHasPermissions',
rolesTableName: '',
permissionsTableName: '',
roleHasPermissionsTableName: '',
}
state.permissionsTableName = await getPermissionsTableName(sink)
state.rolesTableName = await getRolesTableName(sink)
state.roleHasPermissionsTableName = await getPermissionRolesPivotTable(sink)
const permissionsMigrationConsent = await getMigrationConsent(sink, state.permissionsTableName)
const rolesMigrationConsent = await getMigrationConsent(sink, state.rolesTableName)
const rolesHasPermissionsConsent = await getMigrationConsent(
sink,
state.roleHasPermissionsTableName
)
if (permissionsMigrationConsent) {
makePermissionsMigration(projectRoot, app, sink, state)
}
if (rolesMigrationConsent) {
makeRolesMigration(projectRoot, app, sink, state)
}
if (rolesHasPermissionsConsent) {
makeRolesHasPermissionsMigration(projectRoot, app, sink, state)
}
makeConfig(projectRoot, app, sink, state)
}