This repository was archived by the owner on Jan 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathqnaconfig.js
More file actions
78 lines (66 loc) · 2.93 KB
/
Copy pathqnaconfig.js
File metadata and controls
78 lines (66 loc) · 2.93 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
const assert = require('assert');
const path = require('path')
const fs = require('fs-extra')
const chalk = require('chalk')
const Operations = require('./api/operations');
const Delay = require('delay');
const {cli} = require('cli-ux')
const config = {
buildConfig: function(flags, config) {
flags.kbId = (flags.kbId || config.kbId),
flags.subscriptionKey = (flags.subscriptionKey || config.subscriptionKey),
flags.endpointKey = (flags.endpointKey || config.endpointKey),
flags.hostname = (flags.hostname || config.hostname)
},
composeConfig: async function (args, configfile) {
const {QNAMAKER_SUBSCRIPTION_KEY, QNAMAKER_HOSTNAME, QNAMAKER_ENDPOINTKEY, QNAMAKER_KBID} = process.env;
const {subscriptionKey, hostname, endpointKey, kbId} = args;
let qnamakerrcJson = {};
let config = {}
try {
if (fs.existsSync(path.join(configfile, 'config.json'))) {
qnamakerrcJson = await fs.readJSON(path.join(configfile, 'config.json'))
}
} catch (e) {
// Do nothing
} finally {
config.subscriptionKey = (subscriptionKey || qnamakerrcJson.qnamaker__subscriptionKey || QNAMAKER_SUBSCRIPTION_KEY)
config.hostname = (hostname || qnamakerrcJson.qnamaker__hostname || QNAMAKER_HOSTNAME)
config.endpointKey = (endpointKey || qnamakerrcJson.qnamaker__endpointKey || QNAMAKER_ENDPOINTKEY)
config.kbId = (kbId || qnamakerrcJson.qnamaker__kbId || QNAMAKER_KBID)
}
return config;
},
validateConfig: function(config) {
// appId and versionId are not validated here since
// not all operations require these to be present.
// Validation of specific params are done in the
// ServiceBase.js
const {subscriptionKey} = config;
const messageTail = `is missing from the configuration.\n\nDid you run ${chalk.cyan.bold('bf qnamaker:init')} yet?`;
assert(typeof subscriptionKey === 'string', `The subscriptionKey ${messageTail}`);
},
waitForOperationSucceeded: async function(config, result) {
while (true) {
let opResult = await new Operations().getOperationDetails({
subscriptionKey: config.subscriptionKey,
operationId: result.operationId,
endpoint: config.endpoint
});
if (opResult.error)
throw new Error(JSON.stringify(opResult.data.error, null, 4));
result = opResult.data;
cli.action.start(result.operationState)
if (result.operationState === 'Failed'){
cli.action.stop()
throw new Error(JSON.stringify(result, null, 4));
}
if (result.operationState === 'Succeeded')
break;
await Delay(1000);
}
cli.action.stop()
return result;
}
}
module.exports = config;