-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexport-config-handler.ts
More file actions
86 lines (76 loc) · 3.17 KB
/
export-config-handler.ts
File metadata and controls
86 lines (76 loc) · 3.17 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
import merge from 'merge';
import * as path from 'path';
import { configHandler, isAuthenticated, FlagInput, cliux, sanitizePath } from '@contentstack/cli-utilities';
import defaultConfig from '../config';
import { readFile } from './file-helper';
import { askExportDir, askAPIKey } from './interactive';
import login from './basic-login';
import { filter, includes } from 'lodash';
import { ExportConfig } from '../types';
const setupConfig = async (exportCmdFlags: any): Promise<ExportConfig> => {
let config = merge({}, defaultConfig);
// setup the config
if (exportCmdFlags['config']) {
const externalConfig = await readFile(exportCmdFlags['config']);
config = merge.recursive(config, externalConfig);
}
config.exportDir = sanitizePath(exportCmdFlags['data'] || exportCmdFlags['data-dir'] || config.data || (await askExportDir()));
const pattern = /[*$%#<>{}!&?]/g;
if (pattern.test(config.exportDir)) {
cliux.print(`\nPlease add a directory path without any of the special characters: (*,&,{,},[,],$,%,<,>,?,!)`, {
color: 'yellow',
});
config.exportDir = await askExportDir();
}
config.exportDir = config.exportDir.replace(/['"]/g, '');
config.exportDir = path.resolve(config.exportDir);
//Note to support the old key
config.data = config.exportDir;
const managementTokenAlias = exportCmdFlags['management-token-alias'] || exportCmdFlags['alias'];
if (managementTokenAlias) {
const { token, apiKey } = configHandler.get(`tokens.${managementTokenAlias}`) || {};
config.management_token = token;
config.apiKey = apiKey;
if (!config.management_token) {
throw new Error(`No management token found on given alias ${managementTokenAlias}`);
}
}
if (!config.management_token) {
if (!isAuthenticated()) {
if (config.username && config.password) {
await login(config);
} else {
throw new Error('Please login or provide an alias for the management token');
}
} else {
config.apiKey =
exportCmdFlags['stack-uid'] || exportCmdFlags['stack-api-key'] || config.source_stack || (await askAPIKey());
if (typeof config.apiKey !== 'string') {
throw new Error('Invalid API key received');
}
}
}
// Note support old config
config.source_stack = config.apiKey;
config.forceStopMarketplaceAppsPrompt = exportCmdFlags.yes;
config.auth_token = configHandler.get('authtoken'); // TBD handle auth token in httpClient & sdk
config.isAuthenticated = isAuthenticated();
if (exportCmdFlags['branch']) {
config.branchName = exportCmdFlags['branch'];
}
if (exportCmdFlags['module']) {
config.moduleName = exportCmdFlags['module'];
config.singleModuleExport = true;
}
if (exportCmdFlags['secured-assets']) {
config.securedAssets = true;
}
if (Array.isArray(exportCmdFlags['content-types']) && exportCmdFlags['content-types'].length > 0) {
config.contentTypes = exportCmdFlags['content-types'];
}
if (Array.isArray(config.filteredModules) && config.filteredModules.length > 0) {
config.modules.types = filter(defaultConfig.modules.types, (module) => includes(config.filteredModules, module));
}
return config;
};
export default setupConfig;