-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathglobal-fields.js
More file actions
123 lines (112 loc) · 5.21 KB
/
Copy pathglobal-fields.js
File metadata and controls
123 lines (112 loc) · 5.21 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
/*!
* Contentstack Import
* Copyright (c) 2026 Contentstack LLC
* MIT Licensed
*/
let fs = require('fs');
let path = require('path');
let chalk = require('chalk');
let mkdirp = require('mkdirp');
let Promise = require('bluebird');
const { isEmpty, merge } = require('lodash');
let { default: config } = require('../../config');
const { fileHelper, log, formatError, lookupExtension, removeReferenceFields } = require('../../utils');
global._globalField_pending = [];
module.exports = class ImportGlobalFields {
fails = [];
success = [];
snipUidMapper = {};
installedExtensions = [];
reqConcurrency = config.concurrency || config.fetchConcurrency || 1;
constructor(importConfig, stackAPIClient) {
this.config = merge(config, importConfig);
this.stackAPIClient = stackAPIClient;
}
async start() {
log(this.config, chalk.white('Migrating global-fields'), 'success');
let self = this;
let globalfieldsConfig = config.modules.globalfields;
let globalfieldsFolderPath = path.resolve(this.config.data, globalfieldsConfig.dirName);
let globalfieldsMapperPath = path.resolve(this.config.data, 'mapper', 'global_fields');
let globalfieldsUidMapperPath = path.resolve(this.config.data, 'mapper', 'global_fields', 'uid-mapping.json');
let globalfieldsSuccessPath = path.resolve(this.config.data, 'mapper', 'global_fields', 'success.json');
let globalFieldsPending = path.resolve(this.config.data, 'mapper', 'global_fields', 'pending_global_fields.js');
let globalfieldsFailsPath = path.resolve(this.config.data, 'mapper', 'global_fields', 'fails.json');
self.globalfields = fileHelper.readFileSync(path.resolve(globalfieldsFolderPath, globalfieldsConfig.fileName));
const appMapperPath = path.join(this.config.data, 'mapper', 'marketplace_apps', 'uid-mapping.json');
this.installedExtensions = ((await fileHelper.readFileSync(appMapperPath)) || { extension_uid: {} }).extension_uid;
if (fs.existsSync(globalfieldsUidMapperPath)) {
self.snipUidMapper = fileHelper.readFileSync(globalfieldsUidMapperPath);
self.snipUidMapper = self.snipUidMapper || {};
}
if (!fs.existsSync(globalfieldsMapperPath)) {
mkdirp.sync(globalfieldsMapperPath);
}
return new Promise(function (resolve, reject) {
if (self.globalfields === undefined || isEmpty(self.globalfields)) {
log(self.config, chalk.white('No globalfields Found'), 'success');
fileHelper.writeFileSync(globalFieldsPending, _globalField_pending);
return resolve({ empty: true });
}
let snipUids = Object.keys(self.globalfields);
return Promise.map(
snipUids,
async function (snipUid) {
let flag = { supressed: false };
let snip = self.globalfields[snipUid];
lookupExtension(self.config, snip.schema, self.config.preserveStackVersion, self.installedExtensions);
await removeReferenceFields(snip.schema, flag, self.stackAPIClient);
if (flag.supressed) {
// eslint-disable-next-line no-undef
_globalField_pending.push(snip.uid);
}
if (!self.snipUidMapper.hasOwnProperty(snipUid)) {
let requestOption = { global_field: snip };
return self.stackAPIClient
.globalField()
.create(requestOption)
.then((globalField) => {
self.success.push(globalField.items);
let global_field_uid = globalField.uid;
self.snipUidMapper[snipUid] = globalField.items;
fileHelper.writeFileSync(globalfieldsUidMapperPath, self.snipUidMapper);
log(self.config, chalk.green('Global field ' + global_field_uid + ' created successfully'), 'success');
})
.catch(function (err) {
let error = JSON.parse(err.message);
if (error.errors.title) {
// eslint-disable-next-line no-undef
log(self.config, `Globalfield '${snip.uid} already exists'`, 'error');
} else {
log(self.config, chalk.red(`Globalfield '${snip.title}' failed to import. ${formatError(error)}`), 'error');
}
self.fails.push(snip);
});
} else {
// globalfields has already been created
log(
self.config,
chalk.white('The globalfields already exists. Skipping it to avoid duplicates!'),
'success',
);
}
// import 2 globalfields at a time
},
{ concurrency: self.reqConcurrency },
)
.then(function () {
// globalfields have imported successfully
fileHelper.writeFileSync(globalfieldsSuccessPath, self.success);
fileHelper.writeFileSync(globalFieldsPending, _globalField_pending);
log(self.config, chalk.green('globalfields have been imported successfully!'), 'success');
return resolve();
})
.catch(function (err) {
let error = JSON.parse(err);
fileHelper.writeFileSync(globalfieldsFailsPath, self.fails);
log(self.config, `Globalfields import failed. ${formatError(err)}`, 'error');
return reject(error);
});
});
}
};