-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.ts
132 lines (115 loc) · 3.89 KB
/
configure.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
/*
|--------------------------------------------------------------------------
| Configure hook
|--------------------------------------------------------------------------
|
| The configure hook is called when someone runs "node ace configure <package>"
| command. You are free to perform any operations inside this function to
| configure the package.
|
| To make things easier, you have access to the underlying "ConfigureCommand"
| instance and you can use codemods to modify the source files.
|
*/
import type ConfigureCommand from '@adonisjs/core/commands/configure';
import { stubsRoot } from './stubs/main.js';
const configure = async function (command: ConfigureCommand): Promise<void> {
const { defaults, migrations, middlewares } = command.parsedFlags as {
defaults: boolean | undefined;
migrations: boolean | undefined;
middlewares: boolean | undefined;
};
let shouldCreateMigrations = migrations;
let shouldInstallMiddlewares = middlewares;
const defaultConfig = {
rolesTable: 'roles',
permissionsTable: 'permissions',
roleHasPermissionsTable: 'role_has_permissions',
};
if (defaults === undefined || !defaults) {
defaultConfig.rolesTable = await command.prompt.ask('Enter the roles table name:', {
default: 'roles',
validate: (value) => {
return Boolean(value.trim());
},
});
defaultConfig.permissionsTable = await command.prompt.ask('Enter the permissions table name:', {
default: 'permissions',
validate: (value) => {
return Boolean(value.trim());
},
});
defaultConfig.roleHasPermissionsTable = await command.prompt.ask(
'Enter the roles and permissions pivot table name',
{
default: 'role_has_permissions',
validate: (value) => {
return Boolean(value.trim());
},
},
);
}
if (shouldCreateMigrations === undefined) {
shouldCreateMigrations = await command.prompt.confirm(
'Do you want to create migrations files?',
{
name: 'migrations',
},
);
}
if (shouldInstallMiddlewares === undefined) {
shouldInstallMiddlewares = await command.prompt.confirm(
'Do you want to register middlewares?',
{
name: 'middlewares',
},
);
}
const codemods = await command.createCodemods();
/** Publish provider */
await codemods.updateRcFile((rcFile) => {
rcFile.addCommand('adonis-lucid-permission/commands');
rcFile.addProvider('adonis-lucid-permission/permissions_provider');
});
/** Publish stubs */
await codemods.makeUsingStub(stubsRoot, 'config.stub', { config: defaultConfig });
if (shouldCreateMigrations) {
await codemods.makeUsingStub(stubsRoot, 'migrations/permissions.stub', {
migration: {
tableName: defaultConfig.permissionsTable,
fileName: `${Date.now()}_create_${defaultConfig.permissionsTable}_table.ts`,
},
});
await codemods.makeUsingStub(stubsRoot, 'migrations/roles.stub', {
migration: {
tableName: defaultConfig.rolesTable,
fileName: `${Date.now()}_create_${defaultConfig.rolesTable}_table.ts`,
},
});
await codemods.makeUsingStub(stubsRoot, 'migrations/roles_pivot.stub', {
migration: {
tableName: defaultConfig.roleHasPermissionsTable,
fileName: `${Date.now()}_create_${defaultConfig.roleHasPermissionsTable}_table.ts`,
},
permissionsTable: defaultConfig.permissionsTable,
rolesTable: defaultConfig.rolesTable,
});
}
if (shouldInstallMiddlewares) {
await codemods.registerMiddleware('named', [
{
name: 'role',
path: 'adonis-lucid-permission/role_middleware',
},
{
name: 'permission',
path: 'adonis-lucid-permission/permission_middleware',
},
{
name: 'roleOrPermission',
path: 'adonis-lucid-permission/role_or_permission_middleware',
},
]);
}
};
export { configure };