directus-template-cli 0.5.0-beta.7 → 0.5.0-beta.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/apply.d.ts +16 -15
- package/dist/commands/apply.js +41 -12
- package/dist/commands/extract.js +4 -4
- package/dist/lib/constants.d.ts +3 -0
- package/dist/lib/constants.js +6 -0
- package/dist/lib/load/load-access.js +2 -4
- package/dist/lib/load/load-collections.js +43 -17
- package/dist/lib/load/load-dashboards.js +3 -5
- package/dist/lib/load/load-data.js +45 -9
- package/dist/lib/load/load-extensions.js +49 -46
- package/dist/lib/load/load-files.js +2 -5
- package/dist/lib/load/load-flows.d.ts +1 -1
- package/dist/lib/load/load-flows.js +51 -42
- package/dist/lib/load/load-folders.js +3 -5
- package/dist/lib/load/load-permissions.js +2 -6
- package/dist/lib/load/load-policies.js +3 -3
- package/dist/lib/load/load-presets.js +3 -4
- package/dist/lib/load/load-relations.js +2 -3
- package/dist/lib/load/load-roles.js +2 -4
- package/dist/lib/load/load-settings.js +39 -2
- package/dist/lib/load/load-translations.js +3 -5
- package/dist/lib/load/load-users.js +14 -7
- package/dist/lib/utils/auth.js +1 -1
- package/dist/lib/utils/catch-error.js +2 -2
- package/dist/lib/utils/get-template.d.ts +1 -0
- package/dist/lib/utils/get-template.js +42 -1
- package/dist/lib/utils/read-templates.js +4 -2
- package/oclif.manifest.json +1 -1
- package/package.json +1 -1
package/dist/commands/apply.d.ts
CHANGED
|
@@ -3,26 +3,27 @@ export default class ApplyCommand extends Command {
|
|
|
3
3
|
static description: string;
|
|
4
4
|
static examples: string[];
|
|
5
5
|
static flags: {
|
|
6
|
-
content: import("@oclif/core/lib/interfaces
|
|
7
|
-
dashboards: import("@oclif/core/lib/interfaces
|
|
8
|
-
directusToken: import("@oclif/core/lib/interfaces
|
|
9
|
-
directusUrl: import("@oclif/core/lib/interfaces
|
|
10
|
-
extensions: import("@oclif/core/lib/interfaces
|
|
11
|
-
files: import("@oclif/core/lib/interfaces
|
|
12
|
-
flows: import("@oclif/core/lib/interfaces
|
|
13
|
-
partial: import("@oclif/core/lib/interfaces
|
|
14
|
-
permissions: import("@oclif/core/lib/interfaces
|
|
15
|
-
programmatic: import("@oclif/core/lib/interfaces
|
|
16
|
-
schema: import("@oclif/core/lib/interfaces
|
|
17
|
-
settings: import("@oclif/core/lib/interfaces
|
|
18
|
-
templateLocation: import("@oclif/core/lib/interfaces
|
|
19
|
-
templateType: import("@oclif/core/lib/interfaces
|
|
20
|
-
users: import("@oclif/core/lib/interfaces
|
|
6
|
+
content: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
7
|
+
dashboards: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
8
|
+
directusToken: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
9
|
+
directusUrl: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
10
|
+
extensions: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
11
|
+
files: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
12
|
+
flows: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
13
|
+
partial: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
14
|
+
permissions: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
15
|
+
programmatic: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
16
|
+
schema: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
17
|
+
settings: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
18
|
+
templateLocation: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
19
|
+
templateType: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
20
|
+
users: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
21
21
|
};
|
|
22
22
|
run(): Promise<void>;
|
|
23
23
|
private initializeDirectusApi;
|
|
24
24
|
private redirectToDirectusPlus;
|
|
25
25
|
private runInteractive;
|
|
26
26
|
private runProgrammatic;
|
|
27
|
+
private selectLocalTemplate;
|
|
27
28
|
private validateFlags;
|
|
28
29
|
}
|
package/dist/commands/apply.js
CHANGED
|
@@ -4,13 +4,14 @@ const tslib_1 = require("tslib");
|
|
|
4
4
|
const sdk_1 = require("@directus/sdk");
|
|
5
5
|
const core_1 = require("@oclif/core");
|
|
6
6
|
const inquirer = tslib_1.__importStar(require("inquirer"));
|
|
7
|
+
const constants_1 = require("../lib/constants");
|
|
7
8
|
const index_js_1 = tslib_1.__importDefault(require("../lib/load/index.js"));
|
|
8
9
|
const sdk_2 = require("../lib/sdk");
|
|
9
10
|
const auth_1 = require("../lib/utils/auth");
|
|
10
11
|
const catch_error_1 = tslib_1.__importDefault(require("../lib/utils/catch-error"));
|
|
11
12
|
const get_template_1 = require("../lib/utils/get-template");
|
|
13
|
+
const logger_1 = require("../lib/utils/logger");
|
|
12
14
|
const open_url_1 = tslib_1.__importDefault(require("../lib/utils/open-url"));
|
|
13
|
-
const separator = '------------------';
|
|
14
15
|
class ApplyCommand extends core_1.Command {
|
|
15
16
|
// MAIN FUNCTION
|
|
16
17
|
async run() {
|
|
@@ -23,10 +24,10 @@ class ApplyCommand extends core_1.Command {
|
|
|
23
24
|
try {
|
|
24
25
|
sdk_2.api.setAuthToken(flags.directusToken);
|
|
25
26
|
const response = await sdk_2.api.client.request((0, sdk_1.readMe)());
|
|
26
|
-
core_1.ux.log(
|
|
27
|
+
core_1.ux.log(`-- Logged in as ${response.first_name} ${response.last_name}`);
|
|
27
28
|
}
|
|
28
29
|
catch {
|
|
29
|
-
(0, catch_error_1.default)('Invalid Directus token. Please check your credentials.', {
|
|
30
|
+
(0, catch_error_1.default)('-- Invalid Directus token. Please check your credentials.', {
|
|
30
31
|
fatal: true,
|
|
31
32
|
});
|
|
32
33
|
}
|
|
@@ -38,6 +39,7 @@ class ApplyCommand extends core_1.Command {
|
|
|
38
39
|
}
|
|
39
40
|
async runInteractive(flags) {
|
|
40
41
|
const validatedFlags = this.validateFlags(flags);
|
|
42
|
+
core_1.ux.styledHeader(core_1.ux.colorize(constants_1.DIRECTUS_PURPLE, 'Welcome to the Directus Template CLI'));
|
|
41
43
|
const templateType = await inquirer.prompt([
|
|
42
44
|
{
|
|
43
45
|
choices: [
|
|
@@ -68,7 +70,7 @@ class ApplyCommand extends core_1.Command {
|
|
|
68
70
|
}
|
|
69
71
|
case 'local': {
|
|
70
72
|
const localTemplateDir = await core_1.ux.prompt('What is the local template directory?');
|
|
71
|
-
template = await
|
|
73
|
+
template = await this.selectLocalTemplate(localTemplateDir);
|
|
72
74
|
break;
|
|
73
75
|
}
|
|
74
76
|
case 'github': {
|
|
@@ -80,19 +82,19 @@ class ApplyCommand extends core_1.Command {
|
|
|
80
82
|
this.redirectToDirectusPlus();
|
|
81
83
|
}
|
|
82
84
|
}
|
|
83
|
-
core_1.ux.log(`You selected ${template.templateName}`);
|
|
84
|
-
core_1.ux.log(
|
|
85
|
+
core_1.ux.log(`You selected ${core_1.ux.colorize(constants_1.DIRECTUS_PINK, template.templateName)}`);
|
|
86
|
+
core_1.ux.log(constants_1.SEPARATOR);
|
|
85
87
|
// Get Directus URL and token
|
|
86
88
|
const directusUrl = await (0, auth_1.getDirectusUrl)();
|
|
87
89
|
const directusToken = await (0, auth_1.getDirectusToken)(directusUrl);
|
|
88
90
|
flags.directusUrl = directusUrl;
|
|
89
91
|
flags.directusToken = directusToken;
|
|
90
92
|
if (template) {
|
|
91
|
-
core_1.ux.
|
|
93
|
+
core_1.ux.styledHeader(core_1.ux.colorize(constants_1.DIRECTUS_PURPLE, `Applying template - ${template.templateName} to ${directusUrl}`));
|
|
92
94
|
await (0, index_js_1.default)(template.directoryPath, validatedFlags);
|
|
93
95
|
core_1.ux.action.stop();
|
|
94
|
-
core_1.ux.log(
|
|
95
|
-
core_1.ux.
|
|
96
|
+
core_1.ux.log(constants_1.SEPARATOR);
|
|
97
|
+
core_1.ux.info('Template applied successfully.');
|
|
96
98
|
core_1.ux.exit(0);
|
|
97
99
|
}
|
|
98
100
|
}
|
|
@@ -120,13 +122,40 @@ class ApplyCommand extends core_1.Command {
|
|
|
120
122
|
}
|
|
121
123
|
}
|
|
122
124
|
await this.initializeDirectusApi(validatedFlags);
|
|
123
|
-
|
|
125
|
+
const logMessage = `Applying template - ${template.templateName} to ${validatedFlags.directusUrl}`;
|
|
126
|
+
core_1.ux.styledHeader(logMessage);
|
|
127
|
+
logger_1.logger.log('info', logMessage);
|
|
124
128
|
await (0, index_js_1.default)(template.directoryPath, validatedFlags);
|
|
125
129
|
core_1.ux.action.stop();
|
|
126
|
-
core_1.ux.log(
|
|
127
|
-
core_1.ux.
|
|
130
|
+
core_1.ux.log(constants_1.SEPARATOR);
|
|
131
|
+
core_1.ux.info('Template applied successfully.');
|
|
128
132
|
core_1.ux.exit(0);
|
|
129
133
|
}
|
|
134
|
+
async selectLocalTemplate(localTemplateDir) {
|
|
135
|
+
try {
|
|
136
|
+
const templates = await (0, get_template_1.getInteractiveLocalTemplate)(localTemplateDir);
|
|
137
|
+
if (templates.length === 1) {
|
|
138
|
+
return templates[0];
|
|
139
|
+
}
|
|
140
|
+
const { selectedTemplate } = await inquirer.prompt([
|
|
141
|
+
{
|
|
142
|
+
choices: templates.map(t => ({ name: `${t.templateName} (${core_1.ux.colorize('dim', t.directoryPath)})`, value: t })),
|
|
143
|
+
message: 'Multiple templates found. Please select one:',
|
|
144
|
+
name: 'selectedTemplate',
|
|
145
|
+
type: 'list',
|
|
146
|
+
},
|
|
147
|
+
]);
|
|
148
|
+
return selectedTemplate;
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
if (error instanceof Error) {
|
|
152
|
+
core_1.ux.error(error.message);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
core_1.ux.error('An unknown error occurred while getting the local template.');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
130
159
|
validateFlags(flags) {
|
|
131
160
|
if (flags.programmatic) {
|
|
132
161
|
if (!flags.directusUrl || !flags.directusToken) {
|
package/dist/commands/extract.js
CHANGED
|
@@ -6,12 +6,12 @@ const core_1 = require("@oclif/core");
|
|
|
6
6
|
const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
|
|
7
7
|
const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
8
8
|
const slugify_1 = tslib_1.__importDefault(require("slugify"));
|
|
9
|
+
const constants_1 = require("../lib/constants");
|
|
9
10
|
const extract_1 = tslib_1.__importDefault(require("../lib/extract/"));
|
|
10
11
|
const sdk_2 = require("../lib/sdk");
|
|
11
12
|
const auth_1 = require("../lib/utils/auth");
|
|
12
13
|
const catch_error_1 = tslib_1.__importDefault(require("../lib/utils/catch-error"));
|
|
13
14
|
const template_defaults_1 = require("../lib/utils/template-defaults");
|
|
14
|
-
const separator = '------------------';
|
|
15
15
|
class ExtractCommand extends core_1.Command {
|
|
16
16
|
async run() {
|
|
17
17
|
const { flags } = await this.parse(ExtractCommand);
|
|
@@ -33,11 +33,11 @@ class ExtractCommand extends core_1.Command {
|
|
|
33
33
|
catch (error) {
|
|
34
34
|
core_1.ux.error(`Failed to create directory or write files: ${error.message}`);
|
|
35
35
|
}
|
|
36
|
-
core_1.ux.log(
|
|
36
|
+
core_1.ux.log(constants_1.SEPARATOR);
|
|
37
37
|
core_1.ux.action.start(`Extracting template - from ${flags.directusUrl} to ${directory}`);
|
|
38
38
|
await (0, extract_1.default)(directory);
|
|
39
39
|
core_1.ux.action.stop();
|
|
40
|
-
core_1.ux.log(
|
|
40
|
+
core_1.ux.log(constants_1.SEPARATOR);
|
|
41
41
|
core_1.ux.log('Template extracted successfully.');
|
|
42
42
|
this.exit(0);
|
|
43
43
|
}
|
|
@@ -46,7 +46,7 @@ class ExtractCommand extends core_1.Command {
|
|
|
46
46
|
try {
|
|
47
47
|
sdk_2.api.setAuthToken(flags.directusToken);
|
|
48
48
|
const response = await sdk_2.api.client.request((0, sdk_1.readMe)());
|
|
49
|
-
core_1.ux.log(
|
|
49
|
+
core_1.ux.log(`-- Logged in as ${response.first_name} ${response.last_name}`);
|
|
50
50
|
}
|
|
51
51
|
catch {
|
|
52
52
|
(0, catch_error_1.default)('Invalid Directus token. Please check your credentials.', {
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SEPARATOR = exports.DIRECTUS_PINK = exports.DIRECTUS_PURPLE = void 0;
|
|
4
|
+
exports.DIRECTUS_PURPLE = '#6644ff';
|
|
5
|
+
exports.DIRECTUS_PINK = '#FF99DD';
|
|
6
|
+
exports.SEPARATOR = '------------------';
|
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
const core_1 = require("@oclif/core");
|
|
5
|
+
const constants_1 = require("../constants");
|
|
5
6
|
const sdk_1 = require("../sdk");
|
|
6
7
|
const catch_error_1 = tslib_1.__importDefault(require("../utils/catch-error"));
|
|
7
8
|
const read_file_1 = tslib_1.__importDefault(require("../utils/read-file"));
|
|
8
9
|
async function loadAccess(dir) {
|
|
9
10
|
const access = (0, read_file_1.default)('access', dir);
|
|
10
|
-
core_1.ux.action.start(`Loading ${access.length} accesses`);
|
|
11
|
+
core_1.ux.action.start(core_1.ux.colorize(constants_1.DIRECTUS_PINK, `Loading ${access.length} accesses`));
|
|
11
12
|
// Fetch existing accesses
|
|
12
13
|
const existingAccesses = await sdk_1.api.client.request(() => ({
|
|
13
14
|
method: 'GET',
|
|
@@ -21,12 +22,10 @@ async function loadAccess(dir) {
|
|
|
21
22
|
for await (const acc of access) {
|
|
22
23
|
try {
|
|
23
24
|
if (existingAccessById.has(acc.id)) {
|
|
24
|
-
core_1.ux.log(`Skipping existing access with ID: ${acc.id}`);
|
|
25
25
|
continue;
|
|
26
26
|
}
|
|
27
27
|
const compositeKey = getCompositeKey(acc);
|
|
28
28
|
if (existingAccessByCompositeKey.has(compositeKey)) {
|
|
29
|
-
core_1.ux.log(`Skipping existing access with composite key: ${compositeKey}`);
|
|
30
29
|
continue;
|
|
31
30
|
}
|
|
32
31
|
// If the role is null, delete the role key to avoid errors
|
|
@@ -52,7 +51,6 @@ async function loadAccess(dir) {
|
|
|
52
51
|
}
|
|
53
52
|
}
|
|
54
53
|
core_1.ux.action.stop();
|
|
55
|
-
core_1.ux.log('Loaded Accesses');
|
|
56
54
|
}
|
|
57
55
|
exports.default = loadAccess;
|
|
58
56
|
// Helper function to generate a composite key for each access
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
const sdk_1 = require("@directus/sdk");
|
|
5
5
|
const core_1 = require("@oclif/core");
|
|
6
|
+
const constants_1 = require("../constants");
|
|
6
7
|
const sdk_2 = require("../sdk");
|
|
7
8
|
const catch_error_1 = tslib_1.__importDefault(require("../utils/catch-error"));
|
|
8
9
|
const read_file_1 = tslib_1.__importDefault(require("../utils/read-file"));
|
|
@@ -10,32 +11,53 @@ const read_file_1 = tslib_1.__importDefault(require("../utils/read-file"));
|
|
|
10
11
|
* Load collections into the Directus instance
|
|
11
12
|
*/
|
|
12
13
|
async function loadCollections(dir) {
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
core_1.ux.action.start(`Loading ${
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return col;
|
|
20
|
-
});
|
|
21
|
-
await addCollections(removedGroupKey, fields);
|
|
22
|
-
await updateCollections(collections);
|
|
23
|
-
await addCustomFieldsOnSystemCollections(fields);
|
|
14
|
+
const collectionsToAdd = (0, read_file_1.default)('collections', dir);
|
|
15
|
+
const fieldsToAdd = (0, read_file_1.default)('fields', dir);
|
|
16
|
+
core_1.ux.action.start(core_1.ux.colorize(constants_1.DIRECTUS_PINK, `Loading ${collectionsToAdd.length} collections and ${fieldsToAdd.length} fields`));
|
|
17
|
+
await processCollections(collectionsToAdd, fieldsToAdd);
|
|
18
|
+
await updateCollections(collectionsToAdd);
|
|
19
|
+
await addCustomFieldsOnSystemCollections(fieldsToAdd);
|
|
24
20
|
core_1.ux.action.stop();
|
|
25
|
-
core_1.ux.log('Loaded collections and fields.');
|
|
26
21
|
}
|
|
27
22
|
exports.default = loadCollections;
|
|
28
|
-
async function
|
|
29
|
-
|
|
23
|
+
async function processCollections(collectionsToAdd, fieldsToAdd) {
|
|
24
|
+
const existingCollections = await sdk_2.api.client.request((0, sdk_1.readCollections)());
|
|
25
|
+
const existingFields = await sdk_2.api.client.request((0, sdk_1.readFields)());
|
|
26
|
+
for await (const collection of collectionsToAdd) {
|
|
30
27
|
try {
|
|
31
|
-
|
|
32
|
-
await
|
|
28
|
+
const existingCollection = existingCollections.find((c) => c.collection === collection.collection);
|
|
29
|
+
await (existingCollection ? addNewFieldsToExistingCollection(collection.collection, fieldsToAdd, existingFields) : addNewCollectionWithFields(collection, fieldsToAdd));
|
|
33
30
|
}
|
|
34
31
|
catch (error) {
|
|
35
32
|
(0, catch_error_1.default)(error);
|
|
36
33
|
}
|
|
37
34
|
}
|
|
38
35
|
}
|
|
36
|
+
async function addNewCollectionWithFields(collection, allFields) {
|
|
37
|
+
const collectionFields = allFields.filter(field => field.collection === collection.collection);
|
|
38
|
+
const collectionWithoutGroup = {
|
|
39
|
+
...collection,
|
|
40
|
+
fields: collectionFields,
|
|
41
|
+
meta: { ...collection.meta },
|
|
42
|
+
};
|
|
43
|
+
delete collectionWithoutGroup.meta.group;
|
|
44
|
+
await sdk_2.api.client.request((0, sdk_1.createCollection)(collectionWithoutGroup));
|
|
45
|
+
}
|
|
46
|
+
async function addNewFieldsToExistingCollection(collectionName, fieldsToAdd, existingFields) {
|
|
47
|
+
const collectionFieldsToAdd = fieldsToAdd.filter(field => field.collection === collectionName);
|
|
48
|
+
const existingCollectionFields = existingFields.filter((field) => field.collection === collectionName);
|
|
49
|
+
for await (const field of collectionFieldsToAdd) {
|
|
50
|
+
if (!existingCollectionFields.some((existingField) => existingField.field === field.field)) {
|
|
51
|
+
try {
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
await sdk_2.api.client.request((0, sdk_1.createField)(collectionName, field));
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
(0, catch_error_1.default)(error);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
39
61
|
async function updateCollections(collections) {
|
|
40
62
|
for await (const collection of collections) {
|
|
41
63
|
try {
|
|
@@ -55,9 +77,13 @@ async function updateCollections(collections) {
|
|
|
55
77
|
}
|
|
56
78
|
async function addCustomFieldsOnSystemCollections(fields) {
|
|
57
79
|
const customFields = fields.filter((field) => field.collection.startsWith('directus_'));
|
|
80
|
+
const existingFields = await sdk_2.api.client.request((0, sdk_1.readFields)());
|
|
58
81
|
for await (const field of customFields) {
|
|
59
82
|
try {
|
|
60
|
-
|
|
83
|
+
const fieldExists = existingFields.some((existingField) => existingField.collection === field.collection && existingField.field === field.field);
|
|
84
|
+
if (!fieldExists) {
|
|
85
|
+
await sdk_2.api.client.request((0, sdk_1.createField)(field.collection, field));
|
|
86
|
+
}
|
|
61
87
|
}
|
|
62
88
|
catch (error) {
|
|
63
89
|
(0, catch_error_1.default)(error);
|
|
@@ -4,12 +4,13 @@ exports.loadPanels = void 0;
|
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const sdk_1 = require("@directus/sdk");
|
|
6
6
|
const core_1 = require("@oclif/core");
|
|
7
|
+
const constants_1 = require("../constants");
|
|
7
8
|
const sdk_2 = require("../sdk");
|
|
8
9
|
const catch_error_1 = tslib_1.__importDefault(require("../utils/catch-error"));
|
|
9
10
|
const read_file_1 = tslib_1.__importDefault(require("../utils/read-file"));
|
|
10
11
|
async function loadDashboards(dir) {
|
|
11
12
|
const dashboards = (0, read_file_1.default)('dashboards', dir);
|
|
12
|
-
core_1.ux.action.start(`Loading ${dashboards.length} dashboards`);
|
|
13
|
+
core_1.ux.action.start(core_1.ux.colorize(constants_1.DIRECTUS_PINK, `Loading ${dashboards.length} dashboards`));
|
|
13
14
|
// Fetch existing dashboards
|
|
14
15
|
const existingDashboards = await sdk_2.api.client.request((0, sdk_1.readDashboards)({
|
|
15
16
|
limit: -1,
|
|
@@ -17,7 +18,6 @@ async function loadDashboards(dir) {
|
|
|
17
18
|
const existingDashboardIds = new Set(existingDashboards.map(dashboard => dashboard.id));
|
|
18
19
|
const filteredDashboards = dashboards.filter(dashboard => {
|
|
19
20
|
if (existingDashboardIds.has(dashboard.id)) {
|
|
20
|
-
core_1.ux.log(`Skipping existing dashboard: ${dashboard.name}`);
|
|
21
21
|
return false;
|
|
22
22
|
}
|
|
23
23
|
return true;
|
|
@@ -36,12 +36,11 @@ async function loadDashboards(dir) {
|
|
|
36
36
|
}));
|
|
37
37
|
await loadPanels(dir);
|
|
38
38
|
core_1.ux.action.stop();
|
|
39
|
-
core_1.ux.log('Loaded dashboards');
|
|
40
39
|
}
|
|
41
40
|
exports.default = loadDashboards;
|
|
42
41
|
async function loadPanels(dir) {
|
|
43
42
|
const panels = (0, read_file_1.default)('panels', dir);
|
|
44
|
-
core_1.ux.
|
|
43
|
+
core_1.ux.action.status = `Loading ${panels.length} panels`;
|
|
45
44
|
// Fetch existing panels
|
|
46
45
|
const existingPanels = await sdk_2.api.client.request((0, sdk_1.readPanels)({
|
|
47
46
|
limit: -1,
|
|
@@ -49,7 +48,6 @@ async function loadPanels(dir) {
|
|
|
49
48
|
const existingPanelIds = new Set(existingPanels.map(panel => panel.id));
|
|
50
49
|
const filteredPanels = panels.filter(panel => {
|
|
51
50
|
if (existingPanelIds.has(panel.id)) {
|
|
52
|
-
core_1.ux.log(`Skipping existing panel: ${panel.id}`);
|
|
53
51
|
return false;
|
|
54
52
|
}
|
|
55
53
|
return true;
|
|
@@ -4,6 +4,7 @@ const tslib_1 = require("tslib");
|
|
|
4
4
|
const sdk_1 = require("@directus/sdk");
|
|
5
5
|
const core_1 = require("@oclif/core");
|
|
6
6
|
const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
7
|
+
const constants_1 = require("../constants");
|
|
7
8
|
const sdk_2 = require("../sdk");
|
|
8
9
|
const catch_error_1 = tslib_1.__importDefault(require("../utils/catch-error"));
|
|
9
10
|
const chunk_array_1 = require("../utils/chunk-array");
|
|
@@ -11,16 +12,15 @@ const read_file_1 = tslib_1.__importDefault(require("../utils/read-file"));
|
|
|
11
12
|
const BATCH_SIZE = 50;
|
|
12
13
|
async function loadData(dir) {
|
|
13
14
|
const collections = (0, read_file_1.default)('collections', dir);
|
|
14
|
-
core_1.ux.action.start(`Loading data for ${collections.length} collections`);
|
|
15
|
+
core_1.ux.action.start(core_1.ux.colorize(constants_1.DIRECTUS_PINK, `Loading data for ${collections.length} collections`));
|
|
15
16
|
await loadSkeletonRecords(dir);
|
|
16
17
|
await loadFullData(dir);
|
|
17
18
|
await loadSingletons(dir);
|
|
18
19
|
core_1.ux.action.stop();
|
|
19
|
-
core_1.ux.log('Loaded data.');
|
|
20
20
|
}
|
|
21
21
|
exports.default = loadData;
|
|
22
22
|
async function loadSkeletonRecords(dir) {
|
|
23
|
-
core_1.ux.
|
|
23
|
+
core_1.ux.action.status = 'Loading skeleton records';
|
|
24
24
|
const collections = (0, read_file_1.default)('collections', dir);
|
|
25
25
|
const primaryKeyMap = await getCollectionPrimaryKeys(dir);
|
|
26
26
|
const userCollections = collections
|
|
@@ -32,10 +32,46 @@ async function loadSkeletonRecords(dir) {
|
|
|
32
32
|
const primaryKeyField = getPrimaryKey(primaryKeyMap, name);
|
|
33
33
|
const sourceDir = node_path_1.default.resolve(dir, 'content');
|
|
34
34
|
const data = (0, read_file_1.default)(name, sourceDir);
|
|
35
|
-
|
|
35
|
+
// Fetch existing primary keys
|
|
36
|
+
const existingPrimaryKeys = await getExistingPrimaryKeys(name, primaryKeyField);
|
|
37
|
+
// Filter out existing records
|
|
38
|
+
const newData = data.filter(entry => !existingPrimaryKeys.has(entry[primaryKeyField]));
|
|
39
|
+
if (newData.length === 0) {
|
|
40
|
+
core_1.ux.log(`${core_1.ux.colorize('dim', '--')} Skipping ${name}: No new records to add`);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const batches = (0, chunk_array_1.chunkArray)(newData, BATCH_SIZE).map(batch => batch.map(entry => ({ [primaryKeyField]: entry[primaryKeyField] })));
|
|
36
44
|
await Promise.all(batches.map(batch => uploadBatch(name, batch, sdk_1.createItems)));
|
|
45
|
+
core_1.ux.log(`${core_1.ux.colorize('dim', '--')} Added ${newData.length} new skeleton records to ${name}`);
|
|
37
46
|
}));
|
|
38
|
-
core_1.ux.
|
|
47
|
+
core_1.ux.action.status = 'Loaded skeleton records';
|
|
48
|
+
}
|
|
49
|
+
async function getExistingPrimaryKeys(collection, primaryKeyField) {
|
|
50
|
+
const existingKeys = new Set();
|
|
51
|
+
let page = 1;
|
|
52
|
+
const limit = 1000; // Adjust based on your needs and API limits
|
|
53
|
+
while (true) {
|
|
54
|
+
try {
|
|
55
|
+
// @ts-expect-error string
|
|
56
|
+
const response = await sdk_2.api.client.request((0, sdk_1.readItems)(collection, {
|
|
57
|
+
fields: [primaryKeyField],
|
|
58
|
+
limit,
|
|
59
|
+
page,
|
|
60
|
+
}));
|
|
61
|
+
if (response.length === 0)
|
|
62
|
+
break;
|
|
63
|
+
for (const item of response)
|
|
64
|
+
existingKeys.add(item[primaryKeyField]);
|
|
65
|
+
if (response.length < limit)
|
|
66
|
+
break;
|
|
67
|
+
page++;
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
(0, catch_error_1.default)(error);
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return existingKeys;
|
|
39
75
|
}
|
|
40
76
|
async function uploadBatch(collection, batch, method) {
|
|
41
77
|
try {
|
|
@@ -46,7 +82,7 @@ async function uploadBatch(collection, batch, method) {
|
|
|
46
82
|
}
|
|
47
83
|
}
|
|
48
84
|
async function loadFullData(dir) {
|
|
49
|
-
core_1.ux.
|
|
85
|
+
core_1.ux.action.status = 'Updating records with full data';
|
|
50
86
|
const collections = (0, read_file_1.default)('collections', dir);
|
|
51
87
|
const userCollections = collections
|
|
52
88
|
.filter(item => !item.collection.startsWith('directus_', 0))
|
|
@@ -59,10 +95,10 @@ async function loadFullData(dir) {
|
|
|
59
95
|
const batches = (0, chunk_array_1.chunkArray)(data, BATCH_SIZE).map(batch => batch.map(({ user_created, user_updated, ...cleanedRow }) => cleanedRow));
|
|
60
96
|
await Promise.all(batches.map(batch => uploadBatch(name, batch, sdk_1.updateItemsBatch)));
|
|
61
97
|
}));
|
|
62
|
-
core_1.ux.
|
|
98
|
+
core_1.ux.action.status = 'Updated records with full data';
|
|
63
99
|
}
|
|
64
100
|
async function loadSingletons(dir) {
|
|
65
|
-
core_1.ux.
|
|
101
|
+
core_1.ux.action.status = 'Loading data for singleton collections';
|
|
66
102
|
const collections = (0, read_file_1.default)('collections', dir);
|
|
67
103
|
const singletonCollections = collections
|
|
68
104
|
.filter(item => !item.collection.startsWith('directus_', 0))
|
|
@@ -80,7 +116,7 @@ async function loadSingletons(dir) {
|
|
|
80
116
|
(0, catch_error_1.default)(error);
|
|
81
117
|
}
|
|
82
118
|
}));
|
|
83
|
-
core_1.ux.
|
|
119
|
+
core_1.ux.action.status = 'Loaded data for singleton collections';
|
|
84
120
|
}
|
|
85
121
|
async function getCollectionPrimaryKeys(dir) {
|
|
86
122
|
var _a;
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
const sdk_1 = require("@directus/sdk");
|
|
5
5
|
const core_1 = require("@oclif/core");
|
|
6
|
+
const constants_1 = require("../constants");
|
|
6
7
|
const sdk_2 = require("../sdk");
|
|
7
8
|
const catch_error_1 = tslib_1.__importDefault(require("../utils/catch-error"));
|
|
8
9
|
const read_file_1 = tslib_1.__importDefault(require("../utils/read-file"));
|
|
@@ -17,56 +18,58 @@ async function installExtension(extension) {
|
|
|
17
18
|
}));
|
|
18
19
|
}
|
|
19
20
|
async function loadExtensions(dir) {
|
|
20
|
-
core_1.ux.action.start('Loading extensions');
|
|
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
|
-
|
|
21
|
+
core_1.ux.action.start(core_1.ux.colorize(constants_1.DIRECTUS_PINK, 'Loading extensions'));
|
|
22
|
+
try {
|
|
23
|
+
const extensions = (0, read_file_1.default)('extensions', dir);
|
|
24
|
+
if (extensions && extensions.length > 0) {
|
|
25
|
+
const installedExtensions = await sdk_2.api.client.request((0, sdk_1.readExtensions)());
|
|
26
|
+
const registryExtensions = extensions.filter(ext => { var _a; return ((_a = ext.meta) === null || _a === void 0 ? void 0 : _a.source) === 'registry' && !ext.bundle; });
|
|
27
|
+
const bundles = [...new Set(extensions.filter(ext => ext.bundle).map(ext => ext.bundle))];
|
|
28
|
+
const localExtensions = extensions.filter(ext => { var _a; return ((_a = ext.meta) === null || _a === void 0 ? void 0 : _a.source) === 'local'; });
|
|
29
|
+
const extensionsToInstall = extensions.filter(ext => {
|
|
30
|
+
var _a;
|
|
31
|
+
return ((_a = ext.meta) === null || _a === void 0 ? void 0 : _a.source) === 'registry'
|
|
32
|
+
&& !ext.bundle
|
|
33
|
+
// @ts-expect-error
|
|
34
|
+
&& !installedExtensions.some(installed => installed.id === ext.id);
|
|
35
|
+
});
|
|
36
|
+
core_1.ux.log(`Found ${extensions.length} extensions total: ${registryExtensions.length} registry extensions (including ${bundles.length} bundles), and ${localExtensions.length} local extensions`);
|
|
37
|
+
if (extensionsToInstall.length > 0) {
|
|
38
|
+
core_1.ux.action.start(core_1.ux.colorize(constants_1.DIRECTUS_PINK, `Installing ${extensionsToInstall.length} extensions`));
|
|
39
|
+
const results = await Promise.allSettled(extensionsToInstall.map(async (ext) => {
|
|
40
|
+
var _a, _b, _c;
|
|
41
|
+
try {
|
|
42
|
+
await installExtension({
|
|
43
|
+
id: ext.id,
|
|
44
|
+
// The extension version UUID is the folder name
|
|
45
|
+
version: (_a = ext.meta) === null || _a === void 0 ? void 0 : _a.folder,
|
|
46
|
+
});
|
|
47
|
+
return `-- Installed ${(_b = ext.schema) === null || _b === void 0 ? void 0 : _b.name}`;
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
(0, catch_error_1.default)(error);
|
|
51
|
+
return `-- Failed to install ${(_c = ext.schema) === null || _c === void 0 ? void 0 : _c.name}`;
|
|
52
|
+
}
|
|
53
|
+
}));
|
|
54
|
+
for (const result of results) {
|
|
55
|
+
if (result.status === 'fulfilled') {
|
|
56
|
+
core_1.ux.log(result.value);
|
|
57
|
+
}
|
|
55
58
|
}
|
|
59
|
+
core_1.ux.action.stop();
|
|
60
|
+
core_1.ux.log('Finished installing extensions');
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
// All extensions are already installed
|
|
64
|
+
core_1.ux.log('All extensions are already installed');
|
|
65
|
+
}
|
|
66
|
+
if (localExtensions.length > 0) {
|
|
67
|
+
core_1.ux.log(`Note: ${localExtensions.length} local extensions need to be installed manually.`);
|
|
56
68
|
}
|
|
57
|
-
core_1.ux.action.stop();
|
|
58
|
-
core_1.ux.log('Finished installing extensions');
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
// All extensions are already installed
|
|
62
|
-
core_1.ux.log('All extensions are already installed');
|
|
63
|
-
}
|
|
64
|
-
if (localExtensions.length > 0) {
|
|
65
|
-
core_1.ux.log(`Note: ${localExtensions.length} local extensions need to be installed manually.`);
|
|
66
69
|
}
|
|
67
70
|
}
|
|
68
|
-
|
|
69
|
-
core_1.ux.log('No extensions found or extensions file is empty. Skipping extension installation
|
|
71
|
+
catch {
|
|
72
|
+
core_1.ux.log(`${core_1.ux.colorize('dim', '--')} No extensions found or extensions file is empty. Skipping extension installation.`);
|
|
70
73
|
}
|
|
71
74
|
core_1.ux.action.stop();
|
|
72
75
|
}
|
|
@@ -6,12 +6,13 @@ const core_1 = require("@oclif/core");
|
|
|
6
6
|
const formdata_node_1 = require("formdata-node");
|
|
7
7
|
const node_fs_1 = require("node:fs");
|
|
8
8
|
const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
9
|
+
const constants_1 = require("../constants");
|
|
9
10
|
const sdk_2 = require("../sdk");
|
|
10
11
|
const catch_error_1 = tslib_1.__importDefault(require("../utils/catch-error"));
|
|
11
12
|
const read_file_1 = tslib_1.__importDefault(require("../utils/read-file"));
|
|
12
13
|
async function loadFiles(dir) {
|
|
13
14
|
const files = (0, read_file_1.default)('files', dir);
|
|
14
|
-
core_1.ux.action.start(`Loading ${files.length} files`);
|
|
15
|
+
core_1.ux.action.start(core_1.ux.colorize(constants_1.DIRECTUS_PINK, `Loading ${files.length} files`));
|
|
15
16
|
try {
|
|
16
17
|
const fileIds = files.map(file => file.id);
|
|
17
18
|
// Fetch only the files we're interested in
|
|
@@ -28,11 +29,9 @@ async function loadFiles(dir) {
|
|
|
28
29
|
const existingFileNames = new Set(existingFiles.map(file => file.filename_disk));
|
|
29
30
|
const filesToUpload = files.filter(file => {
|
|
30
31
|
if (existingFileIds.has(file.id)) {
|
|
31
|
-
core_1.ux.log(`Skipping existing file with ID: ${file.id}`);
|
|
32
32
|
return false;
|
|
33
33
|
}
|
|
34
34
|
if (existingFileNames.has(file.filename_disk)) {
|
|
35
|
-
core_1.ux.log(`Skipping existing file with name: ${file.filename_disk}`);
|
|
36
35
|
return false;
|
|
37
36
|
}
|
|
38
37
|
return true;
|
|
@@ -57,12 +56,10 @@ async function loadFiles(dir) {
|
|
|
57
56
|
(0, catch_error_1.default)(error);
|
|
58
57
|
}
|
|
59
58
|
}));
|
|
60
|
-
core_1.ux.log(`Uploaded ${filesToUpload.length} new files`);
|
|
61
59
|
}
|
|
62
60
|
catch (error) {
|
|
63
61
|
(0, catch_error_1.default)(error);
|
|
64
62
|
}
|
|
65
63
|
core_1.ux.action.stop();
|
|
66
|
-
core_1.ux.log('Finished loading files');
|
|
67
64
|
}
|
|
68
65
|
exports.default = loadFiles;
|