This repository has been archived by the owner on Jul 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup.js
68 lines (59 loc) · 1.98 KB
/
setup.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
const keytar = require('keytar');
const inquirer = require('inquirer');
const israeliBankScrapers = require('israeli-bank-scrapers');
const CONFIG = require('../config');
const CONSTS = require('./consts');
function validateNonEmpty(field, input) {
if (input) {
return true;
}
return `${field} must be non empty`;
}
function verifyServiceExists(service) {
if (Object.keys(israeliBankScrapers.SCRAPERS).indexOf(service.companyId) === -1) {
console.error(`Service ${service.companyId} (${service.niceName}) configured wrong`);
return false;
}
return true;
}
function setKeytarCredentials(service, credentialsResult) {
keytar.setPassword(
CONSTS.KEYTAR_SERVICE_NAME,
service.credentialsIdentifier,
JSON.stringify(credentialsResult),
);
}
async function setup() {
console.log('Setting up accounts. Credentials will be saved encrypted by your OS credentials storage');
if (CONFIG.SERVICES.length === 0) {
console.error('No services found in config.js. Configure some and try again');
return;
}
if (!CONFIG.SERVICES.every(verifyServiceExists)) {
console.error('Please fix configuration and try again');
return;
}
const serviceResult = await inquirer.prompt([{
type: 'list',
name: 'service',
message: 'Which service would you like to save credentials for?',
choices: CONFIG.SERVICES.map((service) => {
return {
name: service.niceName,
value: service,
};
}),
}]);
const { loginFields } = israeliBankScrapers.SCRAPERS[serviceResult.service.companyId];
const questions = loginFields.map((field) => {
return {
type: field === CONSTS.SCRAPER_PASSWORD_FIELD_NAME ? CONSTS.SCRAPER_PASSWORD_FIELD_NAME : 'input',
name: field,
message: `Enter value for ${field}:`,
validate: input => validateNonEmpty(field, input),
};
});
const credentialsResult = await inquirer.prompt(questions);
setKeytarCredentials(serviceResult.service, credentialsResult);
}
module.exports = setup;