-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathstack.js
More file actions
99 lines (88 loc) · 2.94 KB
/
stack.js
File metadata and controls
99 lines (88 loc) · 2.94 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
/*!
* Contentstack Export
* Copyright (c) 2026 Contentstack LLC
* MIT Licensed
*/
const path = require('path');
const mkdirp = require('mkdirp');
const merge = require('lodash/merge');
const { default: config } = require('../../config');
const { managementSDKClient, isAuthenticated } = require('@contentstack/cli-utilities');
const { log, fileHelper, formatError } = require('../../utils');
class ExportStack {
stackConfig = config.modules.stack;
constructor(exportConfig, stackAPIClient) {
this.config = merge(config, exportConfig);
this.stackAPIClient = stackAPIClient;
this.requestOption = {
uri: this.config.host + this.config.apis.stacks,
headers: this.config.headers,
json: true,
};
}
async start() {
const self = this;
if (isAuthenticated()) {
const tempAPIClient = await managementSDKClient({ host: config.host });
const tempStackData = await tempAPIClient
.stack({ api_key: self.config.source_stack })
.fetch()
.catch((error) => {
});
if (tempStackData && tempStackData.org_uid) {
self.config.org_uid = tempStackData.org_uid;
self.config.sourceStackName = tempStackData.name;
}
}
if (!self.config.preserveStackVersion && !self.config.hasOwnProperty('master_locale')) {
const apiDetails = {
limit: 100,
skip: 0,
include_count: true,
};
return self.getLocales(apiDetails);
} else if (self.config.preserveStackVersion) {
log(self.config, 'Exporting stack details', 'success');
let stackFolderPath = path.resolve(self.config.data, this.stackConfig.dirName);
let stackContentsFile = path.resolve(stackFolderPath, this.stackConfig.fileName);
mkdirp.sync(stackFolderPath);
return new Promise((resolve, reject) => {
return self.stackAPIClient
.fetch()
.then((response) => {
fileHelper.writeFile(stackContentsFile, response);
log(self.config, 'Exported stack details successfully!', 'success');
return resolve(response);
})
.catch(reject);
});
}
}
getLocales(apiDetails) {
const self = this;
return new Promise((resolve, reject) => {
const result = self.stackAPIClient.locale().query(apiDetails);
result
.find()
.then((response) => {
const masterLocalObj = response.items.find((obj) => {
if (obj.fallback_locale === null) {
return obj;
}
});
apiDetails.skip += apiDetails.limit;
if (masterLocalObj) {
return resolve(masterLocalObj);
} else if (apiDetails.skip <= response.count) {
return resolve(self.getLocales(apiDetails));
} else {
return reject('Master locale not found');
}
})
.catch((error) => {
return reject(error);
});
});
}
}
module.exports = ExportStack;