-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig-handler.ts
More file actions
56 lines (51 loc) · 2.1 KB
/
config-handler.ts
File metadata and controls
56 lines (51 loc) · 2.1 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
import * as path from 'path';
import { QueryExportConfig } from '../types';
import { sanitizePath, pathValidator, configHandler, isAuthenticated } from '@contentstack/cli-utilities';
import config from '../config';
import { askAPIKey } from './common-helper';
export async function setupQueryExportConfig(flags: any): Promise<QueryExportConfig> {
const exportDir = sanitizePath(flags['data-dir'] || pathValidator('export'));
const exportQueryConfig: QueryExportConfig = {
...config,
exportDir,
stackApiKey: flags['stack-api-key'] || '',
managementToken: flags.alias ? configHandler.get(`tokens.${flags.alias}`)?.token : undefined,
query: flags.query,
skipReferences: flags['skip-references'] || false,
skipDependencies: flags['skip-dependencies'] || false,
branchName: flags.branch,
securedAssets: flags['secured-assets'] || false,
isQueryBasedExport: true,
logsPath: exportDir,
dataPath: exportDir,
// Todo: accept the path of the config file from the user
externalConfigPath: path.join(__dirname, '../config/export-config.json'),
};
if (flags['branch-alias']) {
exportQueryConfig.branchAlias = flags['branch-alias'];
}
// override the external config path if the user provides a config file
if (flags.config) {
exportQueryConfig.externalConfigPath = sanitizePath(flags['config']);
}
// Handle authentication
if (flags.alias) {
const { token, apiKey } = configHandler.get(`tokens.${flags.alias}`) || {};
exportQueryConfig.managementToken = token;
exportQueryConfig.stackApiKey = apiKey;
if (!exportQueryConfig.managementToken) {
throw new Error(`No management token found on given alias ${flags.alias}`);
}
}
if (!exportQueryConfig.managementToken) {
if (!isAuthenticated()) {
throw new Error('Please login or provide an alias for the management token');
} else {
exportQueryConfig.stackApiKey = flags['stack-api-key'] || (await askAPIKey());
if (typeof exportQueryConfig.stackApiKey !== 'string') {
throw new Error('Invalid API key received');
}
}
}
return exportQueryConfig;
}