-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (83 loc) · 2.46 KB
/
index.js
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
100
101
102
103
104
105
106
107
108
109
110
// index.js - CNS Onboarding
// Copyright 2024 Padi, Inc. All Rights Reserved.
'use strict';
// Imports
const env = require('dotenv').config();
const fs = require('fs');
const pack = require('./package.json');
// Errors
const E_CODE = 'no code';
const E_ABORTED = 'aborted';
// Defaults
const defaults = {
CNS_BROKER: 'padi',
CNS_CODE: '',
CNS_CONTEXT: '',
CNS_TOKEN: ''
};
// Config
const config = {
CNS_BROKER: process.env.CNS_BROKER || defaults.CNS_BROKER,
CNS_CODE: process.env.CNS_CODE || defaults.CNS_CODE,
CNS_CONTEXT: process.env.CNS_CONTEXT || defaults.CNS_CONTEXT,
CNS_TOKEN: process.env.CNS_TOKEN || defaults.CNS_TOKEN
};
// Display wait text
function wait(text, promise) {
const frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
var index = 0;
const timer = setInterval(() => {
process.stdout.write('\r' + frames[index] + ' ' + text);
index = (index + 1) % frames.length;
}, 250);
return promise.then((result) => {
clearInterval(timer);
process.stdout.write('\r\x1b[K');
return result;
});
}
// Start application
async function start() {
console.log('CNS Onboarding', pack.version);
// Check for context
if (config.CNS_CONTEXT !== '' && config.CNS_TOKEN !== '') {
console.log('✓ CNS context:', config.CNS_CONTEXT);
process.exit(0);
}
// Must have code
if (config.CNS_CODE === '')
throw new Error(E_CODE);
// Wait for signal
console.log('✓ CNS code:', config.CNS_CODE);
// Bind the broker
const broker = require('./src/brokers/' + config.CNS_BROKER + '.js');
// Ask broker for context
return wait('Looking for context...', broker.getContext(config.CNS_CODE))
// Success
.then((result) => {
console.log('✓ CNS context:', result.context);
// Create new env data
const data = {};
for (const name in env.parsed)
data[name] = env.parsed[name];
data.CNS_CONTEXT = result.context;
data.CNS_TOKEN = result.token;
// Construct env file
var text = '// Generated by ' + pack.name + ' on ' + new Date().toISOString() + '\n';
for (const name in data)
text += name + '=' + data[name] + '\n';
// Write env file
fs.writeFileSync('.env', text, 'utf8');
process.exit(0);
});
}
// Catch terminate
process.on('SIGINT', () => {
console.error('\r\x1b[K𐄂 Error:', E_ABORTED);
process.exit(1);
});
// Start application
start().catch((e) => {
console.error('\r\x1b[K𐄂 Error:', e.message);
process.exit(1);
});