-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathstripes-config-middleware.js
More file actions
61 lines (52 loc) · 2.03 KB
/
stripes-config-middleware.js
File metadata and controls
61 lines (52 loc) · 2.03 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
import path from 'path';
import { stdinJsonMiddleware } from './stdin-middleware.js';
import StripesCliError from './stripes-cli-error.js';
import getLogger from './logger.js';
const logger = getLogger('stripesConfigMiddleware');
function loadStripesConfig(stripesConfigFile) {
logger.log('loading stripes config file...', stripesConfigFile);
let config;
try {
config = require(path.resolve(stripesConfigFile)); // eslint-disable-line
} catch (err) {
console.error(err);
throw new StripesCliError(`Unable to load ${stripesConfigFile}`);
}
return config;
}
async function readStripesConfigStdin() {
const stdin = await stdinJsonMiddleware('config')({});
if (stdin.config) {
logger.log('read stripes config from stdin');
}
return stdin.config || undefined;
}
// Given a "--configFile" has been provided, this middleware will load the stripes configuration from disk
// and make the stripes configuration object available to the command as "argv.stripesConfig"
// If no configFile has been provided, stdin is checked for JSON input
export function stripesConfigMiddleware(fileOnly) {
return async function middleware(argv) {
logger.log('initializing...');
let stripesConfig;
if (argv.configFile) {
stripesConfig = loadStripesConfig(argv.configFile);
} else if (!fileOnly) {
stripesConfig = await readStripesConfigStdin();
}
if (stripesConfig) {
argv.stripesConfig = stripesConfig;
// Assign okapi values to argv if not already present
if (stripesConfig.okapi) {
if (stripesConfig.okapi.url && !argv.okapi) {
logger.log(`setting argv.okapi with "${stripesConfig.okapi.url}" from ${argv.configFile || 'stdin'}`);
argv.okapi = stripesConfig.okapi.url;
}
if (stripesConfig.okapi.tenant && !argv.tenant) {
logger.log(`setting argv.tenant with "${stripesConfig.okapi.tenant}" from ${argv.configFile || 'stdin'}`);
argv.tenant = stripesConfig.okapi.tenant;
}
}
}
return Promise.resolve(argv);
};
}