-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
66 lines (59 loc) · 2.33 KB
/
config.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
const keytar = require('keytar')
const readline = require('readline');
const Writable = require('stream').Writable;
module.exports = {
async retriveConfiguration(argv, keychainService) {
const token = await userToken(argv.token, keychainService)
return {
all: !!argv.all,
path: argv.path || process.cwd(),
token: token
}
}
}
async function userToken(passedToken, keychainService) {
const username = require("os").userInfo().username
// If the user hasn't passed the token check if already exists
if (passedToken == null) {
const token = await keytar.getPassword(keychainService, username)
// If the token isn't in the system keychain
if (token == null) {
// Ask the user to insert a token
var mutableStdout = new Writable({ write: function(chunk, encoding, callback) {
if (!this.muted)
process.stdout.write(chunk, encoding)
callback()
}
});
mutableStdout.muted = false
var readLine = readline.createInterface({
input: process.stdin,
output: mutableStdout,
terminal: true
})
return await new Promise((resolve, error) => {
readLine.question('Insert token: ', function(token) {
console.log(`\nSaved token is token ${token}. Add --token TOKEN to rewrite it.`);
keytar.setPassword(keychainService, username, token)
readLine.close();
resolve(token)
})
// Mute the stdoutput to make the token insertion not visible
mutableStdout.muted = true
})
} else {
// There was already a token saved
console.log('Fetched token from the keychain')
return await new Promise((resolve, error) => {
resolve(token)
})
}
} else {
// Set the token being passed as argument as new token
console.log('The new token is being saved')
await keytar.setPassword(keychainService, username, passedToken)
return await new Promise((resolve, error) => {
resolve(passedToken)
})
}
}