-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathprogram.ts
More file actions
323 lines (296 loc) · 10.2 KB
/
program.ts
File metadata and controls
323 lines (296 loc) · 10.2 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { Command, createCommand, Option } from 'commander';
import { resolve } from 'path';
import { readJSONSync } from 'fs-extra';
import {
DefaultConfig,
DefaultConfigPaths,
disposeContext,
initContext,
runDiff,
runPull,
runPush,
runRemovePermissionDuplicates,
runUntrack,
runWaitServerReady,
} from './index';
import { runSeedPush, runSeedDiff } from './commands/seed';
/**
* Remove some default values from the program options that overrides the config file
*/
function cleanProgramOptions(programOptions: Record<string, unknown>) {
return programOptions;
}
/**
* Remove some default values from the command options that overrides the config file
*/
function cleanCommandOptions(commandOptions: Record<string, unknown>) {
if (commandOptions.collections === true) {
delete commandOptions.collections;
}
if (commandOptions.snapshot === true) {
delete commandOptions.snapshot;
}
if (commandOptions.split === true) {
delete commandOptions.split;
}
if (commandOptions.specs === true) {
delete commandOptions.specs;
}
if (commandOptions.syncPolicyRoles === true) {
delete commandOptions.syncPolicyRoles;
}
return commandOptions;
}
function wrapAction(program: Command, action: () => Promise<void>) {
return (commandOpts: Record<string, unknown>) => {
return initContext(
cleanProgramOptions(program.opts()),
cleanCommandOptions(commandOpts),
)
.then(action)
.then(disposeContext);
};
}
function getVersion(): string {
try {
const { version } = readJSONSync(
resolve(__dirname, '..', '..', 'package.json'),
) as { version?: string };
return version ?? 'undefined';
} catch (e) {
return (e as Error).message ?? 'error';
}
}
/**
* Split a comma separated list
*/
function commaSeparatedList(value: string) {
return value.split(',').map((v) => v.trim());
}
/**
* Split a comma separated list unless the value is "all" or "*"
*/
function commaSeparatedListOrAll(value: string) {
const v = value.trim();
if (v === '*' || v === 'all') {
return v;
}
return commaSeparatedList(value);
}
export function createProgram() {
const program = createCommand();
// Global options
const debugOption = new Option(
'-d, --debug',
`display more logging (default "${DefaultConfig.debug}")`,
);
const directusUrlOption = new Option(
'-u, --directus-url <directusUrl>',
'Directus URL',
).env('DIRECTUS_URL');
const directusTokenOption = new Option(
'-t, --directus-token <directusToken>',
'Directus access token',
).env('DIRECTUS_TOKEN');
const directusEmailOption = new Option(
'-e, --directus-email <directusEmail>',
'Directus user email',
).env('DIRECTUS_ADMIN_EMAIL');
const directusPasswordOption = new Option(
'-p, --directus-password <directusPassword>',
'Directus user password',
).env('DIRECTUS_ADMIN_PASSWORD');
const configPathOption = new Option(
'-c, --config-path <configPath>',
`the path to the config file. Required for extended options (default paths: ${DefaultConfigPaths.join(
', ',
)})`,
);
const sortJsonOption = new Option(
'--sort-json',
`sort JSON keys when saving files (default "${DefaultConfig.sortJson}")`,
);
// Shared options
const dumpPathOption = new Option(
'--dump-path <dumpPath>',
`the base path for the dump (default "${DefaultConfig.dumpPath}")`,
);
const maxPushRetriesOption = new Option(
'--max-push-retries <maxPushRetries>',
`the number of retries for the push operation (default "${DefaultConfig.maxPushRetries}")`,
);
const collectionsPathOption = new Option(
'--collections-path <collectionPath>',
`the path for the collections dump, relative to the dump path (default "${DefaultConfig.collectionsPath}")`,
);
const excludeCollectionsOption = new Option(
'-x, --exclude-collections <excludeCollections>',
`comma separated list of collections to exclude from the process (default to none)`,
).argParser(commaSeparatedList);
const onlyCollectionsOption = new Option(
'-o, --only-collections <onlyCollections>',
`comma separated list of collections to include in the process (default to all)`,
).argParser(commaSeparatedList);
const noCollectionsOption = new Option(
'--no-collections',
`should pull and push the collections (default "${DefaultConfig.collections}")`,
);
const noSyncPolicyRolesOption = new Option(
'--no-sync-policy-roles',
`should sync the role ↔ policy attachments (directus_access entries linking roles and policies). Disable to leave existing role-policy assignments on the target untouched (default "${DefaultConfig.syncPolicyRoles}")`,
);
const preserveIdsOption = new Option(
'--preserve-ids <preserveIds>',
`comma separated list of collections that preserve their original ids (default to none). Use "*" or "all" to preserve all ids, if applicable.`,
).argParser(commaSeparatedListOrAll);
const snapshotPathOption = new Option(
'--snapshot-path <snapshotPath>',
`the path for the schema snapshot dump, relative to the dump path (default "${DefaultConfig.snapshotPath}")`,
);
const noSnapshotOption = new Option(
'--no-snapshot',
`should pull and push the Directus schema (default "${DefaultConfig.snapshot}")`,
);
const noSplitOption = new Option(
'--no-split',
`should split the schema snapshot into multiple files (default "${DefaultConfig.split}")`,
);
const specificationsPathOption = new Option(
'--specs-path <specsPath>',
`the path for the specifications dump (GraphQL & OpenAPI), relative to the dump path (default "${DefaultConfig.specsPath}")`,
);
const noSpecificationsOption = new Option(
'--no-specs',
`should dump the GraphQL & OpenAPI specifications (default "${DefaultConfig.specs}")`,
);
const forceOption = new Option(
'-f, --force',
`force the diff of schema, even if the Directus version is different (default "${DefaultConfig.force}")`,
);
const prettyDiffOption = new Option(
'--pretty-diff',
`display a human readable schema diff instead of the raw JSON (default "${DefaultConfig.prettyDiff}")`,
);
program
.version(getVersion())
.addOption(debugOption)
.addOption(sortJsonOption)
.addOption(directusUrlOption)
.addOption(directusTokenOption)
.addOption(directusEmailOption)
.addOption(directusPasswordOption)
.addOption(configPathOption);
program
.command('pull')
.description('get the schema and collections and store them locally')
.addOption(dumpPathOption)
.addOption(collectionsPathOption)
.addOption(excludeCollectionsOption)
.addOption(onlyCollectionsOption)
.addOption(noCollectionsOption)
.addOption(noSyncPolicyRolesOption)
.addOption(preserveIdsOption)
.addOption(snapshotPathOption)
.addOption(noSnapshotOption)
.addOption(noSplitOption)
.addOption(specificationsPathOption)
.addOption(noSpecificationsOption)
.action(wrapAction(program, runPull));
program
.command('diff')
.description(
'describe the schema and collections diff. Does not modify the database.',
)
.addOption(dumpPathOption)
.addOption(collectionsPathOption)
.addOption(excludeCollectionsOption)
.addOption(onlyCollectionsOption)
.addOption(noCollectionsOption)
.addOption(noSyncPolicyRolesOption)
.addOption(snapshotPathOption)
.addOption(noSnapshotOption)
.addOption(noSplitOption)
.addOption(forceOption)
.addOption(prettyDiffOption)
.action(wrapAction(program, runDiff));
program
.command('push')
.description('push the schema and collections')
.addOption(dumpPathOption)
.addOption(collectionsPathOption)
.addOption(excludeCollectionsOption)
.addOption(onlyCollectionsOption)
.addOption(noCollectionsOption)
.addOption(noSyncPolicyRolesOption)
.addOption(preserveIdsOption)
.addOption(snapshotPathOption)
.addOption(noSnapshotOption)
.addOption(noSplitOption)
.addOption(forceOption)
.addOption(maxPushRetriesOption)
.action(wrapAction(program, runPush));
// ---------------------------------------------------------------------------------
// Seed
const defaultSeedPath = Array.isArray(DefaultConfig.seedPath)
? DefaultConfig.seedPath.join(', ')
: DefaultConfig.seedPath;
const seedPathOption = new Option(
'--seed-path <seedPath...>',
`the base path(s) for the seed (default "${defaultSeedPath}")`,
);
const seed = program
.command('seed')
.description('seed the custom collections with data');
seed
.command('push')
.description('push the seed data')
.addOption(seedPathOption)
.addOption(maxPushRetriesOption)
.action(wrapAction(program, runSeedPush));
seed
.command('diff')
.description('describe the seed data diff')
.addOption(seedPathOption)
.action(wrapAction(program, runSeedDiff));
// ---------------------------------------------------------------------------------
// Helpers
const helpers = program
.command('helpers')
.description('a set of helper utilities');
helpers
.command('untrack')
.description('stop tracking of an element')
.requiredOption(
'--collection <collection>',
'the collection of the element',
)
.requiredOption('--id <id>', 'the id of the element to untrack')
.action(wrapAction(program, runUntrack));
helpers
.command('remove-permission-duplicates')
.description(
'remove conflicts in permissions when there are duplicated groups "policy + collection + action".',
)
.option(
'--keep <keep>',
`the permission to keep in case of conflict: "first" or "last" (default "${DefaultConfig.keep}")`,
)
.action(wrapAction(program, runRemovePermissionDuplicates));
helpers
.command('wait-server-ready')
.description('wait until the Directus server is ready (health endpoint)')
.option(
'--interval <interval>',
`seconds between checks (default "${DefaultConfig.interval}")`,
)
.option(
'--timeout <timeout>',
`timeout in seconds (default "${DefaultConfig.timeout}")`,
)
.option(
'--successes <successes>',
`number of consecutive successes required (default "${DefaultConfig.successes}")`,
)
.action(wrapAction(program, runWaitServerReady));
return program;
}