directus-template-cli 0.5.0-beta.2 → 0.5.0-beta.20

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.
Files changed (66) hide show
  1. package/README.md +176 -15
  2. package/dist/commands/apply.d.ts +41 -18
  3. package/dist/commands/apply.js +113 -122
  4. package/dist/commands/extract.d.ts +29 -1
  5. package/dist/commands/extract.js +76 -51
  6. package/dist/flags/common.d.ts +7 -0
  7. package/dist/flags/common.js +41 -0
  8. package/dist/lib/constants.d.ts +3 -0
  9. package/dist/lib/constants.js +6 -0
  10. package/dist/lib/extract/extract-access.js +5 -3
  11. package/dist/lib/extract/extract-assets.d.ts +0 -414
  12. package/dist/lib/extract/extract-assets.js +29 -25
  13. package/dist/lib/extract/extract-collections.js +5 -4
  14. package/dist/lib/extract/extract-content.d.ts +0 -2
  15. package/dist/lib/extract/extract-content.js +16 -15
  16. package/dist/lib/extract/extract-dashboards.js +8 -6
  17. package/dist/lib/extract/extract-extensions.js +5 -3
  18. package/dist/lib/extract/extract-fields.js +5 -4
  19. package/dist/lib/extract/extract-files.js +5 -3
  20. package/dist/lib/extract/extract-flows.js +8 -6
  21. package/dist/lib/extract/extract-folders.js +5 -3
  22. package/dist/lib/extract/extract-permissions.js +5 -3
  23. package/dist/lib/extract/extract-policies.js +5 -3
  24. package/dist/lib/extract/extract-presets.js +5 -3
  25. package/dist/lib/extract/extract-relations.js +5 -3
  26. package/dist/lib/extract/extract-roles.js +5 -3
  27. package/dist/lib/extract/extract-schema.js +6 -8
  28. package/dist/lib/extract/extract-settings.js +5 -3
  29. package/dist/lib/extract/extract-translations.js +6 -6
  30. package/dist/lib/extract/extract-users.js +6 -6
  31. package/dist/lib/load/apply-flags.d.ts +22 -0
  32. package/dist/lib/load/apply-flags.js +67 -0
  33. package/dist/lib/load/index.js +9 -5
  34. package/dist/lib/load/load-access.js +47 -41
  35. package/dist/lib/load/load-collections.js +61 -17
  36. package/dist/lib/load/load-dashboards.js +30 -30
  37. package/dist/lib/load/load-data.js +47 -11
  38. package/dist/lib/load/load-extensions.js +49 -43
  39. package/dist/lib/load/load-files.js +44 -51
  40. package/dist/lib/load/load-flows.d.ts +1 -1
  41. package/dist/lib/load/load-flows.js +44 -38
  42. package/dist/lib/load/load-folders.js +34 -35
  43. package/dist/lib/load/load-permissions.js +15 -17
  44. package/dist/lib/load/load-policies.js +23 -21
  45. package/dist/lib/load/load-presets.js +27 -26
  46. package/dist/lib/load/load-relations.js +19 -18
  47. package/dist/lib/load/load-roles.js +45 -45
  48. package/dist/lib/load/load-settings.js +39 -2
  49. package/dist/lib/load/load-translations.js +24 -24
  50. package/dist/lib/load/load-users.js +44 -34
  51. package/dist/lib/load/update-required-fields.d.ts +1 -0
  52. package/dist/lib/load/update-required-fields.js +24 -0
  53. package/dist/lib/sdk.d.ts +20 -2
  54. package/dist/lib/sdk.js +124 -9
  55. package/dist/lib/utils/auth.d.ts +26 -0
  56. package/dist/lib/utils/auth.js +48 -4
  57. package/dist/lib/utils/catch-error.d.ts +15 -2
  58. package/dist/lib/utils/catch-error.js +31 -25
  59. package/dist/lib/utils/get-template.d.ts +1 -0
  60. package/dist/lib/utils/get-template.js +42 -1
  61. package/dist/lib/utils/read-file.js +2 -1
  62. package/dist/lib/utils/read-templates.js +4 -2
  63. package/oclif.manifest.json +74 -28
  64. package/package.json +2 -2
  65. package/dist/lib/interfaces.d.ts +0 -8
  66. package/dist/lib/interfaces.js +0 -2
@@ -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,70 @@ 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 collections = (0, read_file_1.default)('collections', dir);
14
- const fields = (0, read_file_1.default)('fields', dir);
15
- core_1.ux.action.start(`Loading ${collections.length} collections and ${fields.length} fields.`);
16
- // Remove the group so that we can create the collections
17
- const removedGroupKey = structuredClone(collections).map(col => {
18
- delete col.meta.group;
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 addCollections(collections, fields) {
29
- for await (const collection of collections) {
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
- collection.fields = fields.filter((field) => field.collection === collection.collection);
32
- await sdk_2.api.client.request((0, sdk_1.createCollection)(collection));
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
+ const removeRequiredorIsNullable = (field) => {
37
+ var _a, _b, _c;
38
+ if (((_a = field.meta) === null || _a === void 0 ? void 0 : _a.required) === true) {
39
+ field.meta.required = false;
40
+ }
41
+ if (((_b = field.schema) === null || _b === void 0 ? void 0 : _b.is_nullable) === false) {
42
+ // eslint-disable-next-line camelcase
43
+ field.schema.is_nullable = true;
44
+ }
45
+ if (((_c = field.schema) === null || _c === void 0 ? void 0 : _c.is_unique) === true) {
46
+ // eslint-disable-next-line camelcase
47
+ field.schema.is_unique = false;
48
+ }
49
+ return field;
50
+ };
51
+ async function addNewCollectionWithFields(collection, allFields) {
52
+ const collectionFields = allFields.filter(field => field.collection === collection.collection)
53
+ .map(field => removeRequiredorIsNullable(field));
54
+ const collectionWithoutGroup = {
55
+ ...collection,
56
+ fields: collectionFields,
57
+ meta: { ...collection.meta },
58
+ };
59
+ delete collectionWithoutGroup.meta.group;
60
+ await sdk_2.api.client.request((0, sdk_1.createCollection)(collectionWithoutGroup));
61
+ }
62
+ async function addNewFieldsToExistingCollection(collectionName, fieldsToAdd, existingFields) {
63
+ const collectionFieldsToAdd = fieldsToAdd.filter(field => field.collection === collectionName)
64
+ .map(field => removeRequiredorIsNullable(field));
65
+ const existingCollectionFields = existingFields.filter((field) => field.collection === collectionName);
66
+ for await (const field of collectionFieldsToAdd) {
67
+ if (!existingCollectionFields.some((existingField) => existingField.field === field.field)) {
68
+ try {
69
+ // @ts-ignore
70
+ await sdk_2.api.client.request((0, sdk_1.createField)(collectionName, field));
71
+ }
72
+ catch (error) {
73
+ (0, catch_error_1.default)(error);
74
+ }
75
+ }
76
+ }
77
+ }
39
78
  async function updateCollections(collections) {
40
79
  for await (const collection of collections) {
41
80
  try {
@@ -55,9 +94,14 @@ async function updateCollections(collections) {
55
94
  }
56
95
  async function addCustomFieldsOnSystemCollections(fields) {
57
96
  const customFields = fields.filter((field) => field.collection.startsWith('directus_'));
97
+ const existingFields = await sdk_2.api.client.request((0, sdk_1.readFields)());
58
98
  for await (const field of customFields) {
59
99
  try {
60
- await sdk_2.api.client.request((0, sdk_1.createField)(field.collection, field));
100
+ const fieldExists = existingFields.some((existingField) => existingField.collection === field.collection && existingField.field === field.field);
101
+ if (!fieldExists) {
102
+ // @ts-expect-error string
103
+ await sdk_2.api.client.request((0, sdk_1.createField)(field.collection, field));
104
+ }
61
105
  }
62
106
  catch (error) {
63
107
  (0, catch_error_1.default)(error);
@@ -4,44 +4,45 @@ 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
- // Fetch existing dashboards
14
- const existingDashboards = await sdk_2.api.client.request((0, sdk_1.readDashboards)({
15
- limit: -1,
16
- }));
17
- const existingDashboardIds = new Set(existingDashboards.map(dashboard => dashboard.id));
18
- const filteredDashboards = dashboards.filter(dashboard => {
19
- if (existingDashboardIds.has(dashboard.id)) {
20
- core_1.ux.log(`Skipping existing dashboard: ${dashboard.name}`);
21
- return false;
22
- }
23
- return true;
24
- }).map(dash => {
25
- const newDash = { ...dash };
26
- delete newDash.panels;
27
- return newDash;
28
- });
29
- await Promise.all(filteredDashboards.map(async (dashboard) => {
30
- try {
31
- await sdk_2.api.client.request((0, sdk_1.createDashboard)(dashboard));
32
- }
33
- catch (error) {
34
- (0, catch_error_1.default)(error);
35
- }
36
- }));
37
- await loadPanels(dir);
38
- core_1.ux.action.stop();
39
- core_1.ux.log('Loaded dashboards');
13
+ core_1.ux.action.start(core_1.ux.colorize(constants_1.DIRECTUS_PINK, `Loading ${dashboards.length} dashboards`));
14
+ if (dashboards && dashboards.length > 0) {
15
+ // Fetch existing dashboards
16
+ const existingDashboards = await sdk_2.api.client.request((0, sdk_1.readDashboards)({
17
+ limit: -1,
18
+ }));
19
+ const existingDashboardIds = new Set(existingDashboards.map(dashboard => dashboard.id));
20
+ const filteredDashboards = dashboards.filter(dashboard => {
21
+ if (existingDashboardIds.has(dashboard.id)) {
22
+ return false;
23
+ }
24
+ return true;
25
+ }).map(dash => {
26
+ const newDash = { ...dash };
27
+ delete newDash.panels;
28
+ return newDash;
29
+ });
30
+ await Promise.all(filteredDashboards.map(async (dashboard) => {
31
+ try {
32
+ await sdk_2.api.client.request((0, sdk_1.createDashboard)(dashboard));
33
+ }
34
+ catch (error) {
35
+ (0, catch_error_1.default)(error);
36
+ }
37
+ }));
38
+ await loadPanels(dir);
39
+ core_1.ux.action.stop();
40
+ }
40
41
  }
41
42
  exports.default = loadDashboards;
42
43
  async function loadPanels(dir) {
43
44
  const panels = (0, read_file_1.default)('panels', dir);
44
- core_1.ux.log(`Loading ${panels.length} panels`);
45
+ core_1.ux.action.status = `Loading ${panels.length} panels`;
45
46
  // Fetch existing panels
46
47
  const existingPanels = await sdk_2.api.client.request((0, sdk_1.readPanels)({
47
48
  limit: -1,
@@ -49,7 +50,6 @@ async function loadPanels(dir) {
49
50
  const existingPanelIds = new Set(existingPanels.map(panel => panel.id));
50
51
  const filteredPanels = panels.filter(panel => {
51
52
  if (existingPanelIds.has(panel.id)) {
52
- core_1.ux.log(`Skipping existing panel: ${panel.id}`);
53
53
  return false;
54
54
  }
55
55
  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.log('Loading skeleton records');
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
- const batches = (0, chunk_array_1.chunkArray)(data, BATCH_SIZE).map(batch => batch.map(entry => ({ [primaryKeyField]: entry[primaryKeyField] })));
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
+ // ux.log(`${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
+ // ux.log(`${ux.colorize('dim', '--')} Added ${newData.length} new skeleton records to ${name}`)
37
46
  }));
38
- core_1.ux.log('Loaded skeleton records');
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.log('Updating records with full data');
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.log('Updated records with full data');
98
+ core_1.ux.action.status = 'Updated records with full data';
63
99
  }
64
100
  async function loadSingletons(dir) {
65
- core_1.ux.log('Loading data for singleton collections');
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))
@@ -72,15 +108,15 @@ async function loadSingletons(dir) {
72
108
  const sourceDir = node_path_1.default.resolve(dir, 'content');
73
109
  const data = (0, read_file_1.default)(name, sourceDir);
74
110
  try {
75
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
76
111
  const { user_created, user_updated, ...cleanedData } = data;
112
+ // @ts-expect-error
77
113
  await sdk_2.api.client.request((0, sdk_1.updateSingleton)(name, cleanedData));
78
114
  }
79
115
  catch (error) {
80
116
  (0, catch_error_1.default)(error);
81
117
  }
82
118
  }));
83
- core_1.ux.log('Loaded data for singleton collections');
119
+ core_1.ux.action.status = 'Loaded data for singleton collections';
84
120
  }
85
121
  async function getCollectionPrimaryKeys(dir) {
86
122
  var _a;
@@ -95,7 +131,7 @@ async function getCollectionPrimaryKeys(dir) {
95
131
  }
96
132
  function getPrimaryKey(collectionsMap, collection) {
97
133
  if (!collectionsMap[collection]) {
98
- throw new Error(`Collection ${collection} not found in collections map`);
134
+ (0, catch_error_1.default)(`Collection ${collection} not found in collections map`);
99
135
  }
100
136
  return collectionsMap[collection];
101
137
  }
@@ -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,54 +18,59 @@ async function installExtension(extension) {
17
18
  }));
18
19
  }
19
20
  async function loadExtensions(dir) {
20
- const extensions = (0, read_file_1.default)('extensions', dir);
21
- if (!extensions || extensions.length === 0) {
22
- core_1.ux.log('No extensions found');
23
- return;
24
- }
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(`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}`;
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
+ }
58
+ }
59
+ core_1.ux.action.stop();
60
+ core_1.ux.log('Finished installing extensions');
48
61
  }
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}`;
62
+ else {
63
+ // All extensions are already installed
64
+ core_1.ux.log('All extensions are already installed');
52
65
  }
53
- }));
54
- for (const result of results) {
55
- if (result.status === 'fulfilled') {
56
- core_1.ux.log(result.value);
66
+ if (localExtensions.length > 0) {
67
+ core_1.ux.log(`Note: ${localExtensions.length} local extensions need to be installed manually.`);
57
68
  }
58
69
  }
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
70
  }
66
- if (localExtensions.length > 0) {
67
- core_1.ux.log(`Note: ${localExtensions.length} local extensions need to be installed manually.`);
71
+ catch {
72
+ core_1.ux.log(`${core_1.ux.colorize('dim', '--')} No extensions found or extensions file is empty. Skipping extension installation.`);
68
73
  }
74
+ core_1.ux.action.stop();
69
75
  }
70
76
  exports.default = loadExtensions;
@@ -6,63 +6,56 @@ 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
- try {
16
- const fileIds = files.map(file => file.id);
17
- // Fetch only the files we're interested in
18
- const existingFiles = await sdk_2.api.client.request((0, sdk_1.readFiles)({
19
- fields: ['id', 'filename_disk'],
20
- filter: {
21
- id: {
22
- _in: fileIds,
23
- },
24
- },
25
- limit: -1,
26
- }));
27
- const existingFileIds = new Set(existingFiles.map(file => file.id));
28
- const existingFileNames = new Set(existingFiles.map(file => file.filename_disk));
29
- const filesToUpload = files.filter(file => {
30
- if (existingFileIds.has(file.id)) {
31
- core_1.ux.log(`Skipping existing file with ID: ${file.id}`);
32
- return false;
33
- }
34
- if (existingFileNames.has(file.filename_disk)) {
35
- core_1.ux.log(`Skipping existing file with name: ${file.filename_disk}`);
36
- return false;
37
- }
38
- return true;
39
- });
40
- await Promise.all(filesToUpload.map(async (asset) => {
41
- const fileName = asset.filename_disk;
42
- const assetPath = node_path_1.default.resolve(dir, 'assets', fileName);
43
- const fileStream = new Blob([(0, node_fs_1.readFileSync)(assetPath)], { type: asset.type });
44
- const form = new formdata_node_1.FormData();
45
- form.append('id', asset.id);
46
- if (asset.title)
47
- form.append('title', asset.title);
48
- if (asset.description)
49
- form.append('description', asset.description);
50
- if (asset.folder)
51
- form.append('folder', asset.folder);
52
- form.append('file', fileStream, fileName);
53
- try {
54
- await sdk_2.api.client.request((0, sdk_1.uploadFiles)(form));
55
- }
56
- catch (error) {
57
- (0, catch_error_1.default)(error);
58
- }
59
- }));
60
- core_1.ux.log(`Uploaded ${filesToUpload.length} new files`);
61
- }
62
- catch (error) {
63
- (0, catch_error_1.default)(error);
15
+ core_1.ux.action.start(core_1.ux.colorize(constants_1.DIRECTUS_PINK, `Loading ${files.length} files`));
16
+ if (files && files.length > 0) {
17
+ try {
18
+ // Fetch only the files we're interested in
19
+ const existingFiles = await sdk_2.api.client.request((0, sdk_1.readFiles)({
20
+ fields: ['id', 'filename_disk'],
21
+ limit: -1,
22
+ }));
23
+ const existingFileIds = new Set(existingFiles.map(file => file.id));
24
+ const existingFileNames = new Set(existingFiles.map(file => file.filename_disk));
25
+ const filesToUpload = files.filter(file => {
26
+ if (existingFileIds.has(file.id)) {
27
+ return false;
28
+ }
29
+ if (existingFileNames.has(file.filename_disk)) {
30
+ return false;
31
+ }
32
+ return true;
33
+ });
34
+ await Promise.all(filesToUpload.map(async (asset) => {
35
+ const fileName = asset.filename_disk;
36
+ const assetPath = node_path_1.default.resolve(dir, 'assets', fileName);
37
+ const fileStream = new Blob([(0, node_fs_1.readFileSync)(assetPath)], { type: asset.type });
38
+ const form = new formdata_node_1.FormData();
39
+ form.append('id', asset.id);
40
+ if (asset.title)
41
+ form.append('title', asset.title);
42
+ if (asset.description)
43
+ form.append('description', asset.description);
44
+ if (asset.folder)
45
+ form.append('folder', asset.folder);
46
+ form.append('file', fileStream, fileName);
47
+ try {
48
+ await sdk_2.api.client.request((0, sdk_1.uploadFiles)(form));
49
+ }
50
+ catch (error) {
51
+ (0, catch_error_1.default)(error);
52
+ }
53
+ }));
54
+ }
55
+ catch (error) {
56
+ (0, catch_error_1.default)(error);
57
+ }
64
58
  }
65
59
  core_1.ux.action.stop();
66
- core_1.ux.log('Finished loading files');
67
60
  }
68
61
  exports.default = loadFiles;
@@ -1,2 +1,2 @@
1
1
  export default function loadFlows(dir: string): Promise<void>;
2
- export declare function loadOperations(dir: string): Promise<void>;
2
+ export declare function loadOperations(operations: any[]): Promise<void>;