-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathcli-utils.js
More file actions
85 lines (75 loc) · 2.59 KB
/
cli-utils.js
File metadata and controls
85 lines (75 loc) · 2.59 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
79
80
81
82
83
84
85
const fs = require('fs-extra')
const { red, cyan, bold } = require('colorette')
const { URL } = require('url')
const LDP = require('../../lib/ldp')
const AccountManager = require('../../lib/models/account-manager')
const SolidHost = require('../../lib/models/solid-host')
module.exports.getAccountManager = getAccountManager
module.exports.loadAccounts = loadAccounts
module.exports.loadConfig = loadConfig
module.exports.loadUsernames = loadUsernames
/**
* Returns an instance of AccountManager
*
* @param {Object} config
* @param {Object} [options]
* @returns {AccountManager}
*/
function getAccountManager (config, options = {}) {
const ldp = options.ldp || new LDP(config)
const host = options.host || SolidHost.from({ port: config.port, serverUri: config.serverUri })
return AccountManager.from({
host,
store: ldp,
multiuser: config.multiuser
})
}
function loadConfig (program, options) {
let argv = {
...options,
version: program.version()
}
const configFile = argv.configFile || './config.json'
try {
const file = fs.readFileSync(configFile)
// Use flags with priority over config file
const config = JSON.parse(file)
argv = { ...config, ...argv }
} catch (err) {
// If config file was specified, but it doesn't exist, stop with error message
if (typeof argv.configFile !== 'undefined') {
if (!fs.existsSync(configFile)) {
console.log(red(bold('ERR')), 'Config file ' + configFile + ' doesn\'t exist.')
process.exit(1)
}
}
// If the file exists, but parsing failed, stop with error message
if (fs.existsSync(configFile)) {
console.log(red(bold('ERR')), 'config file ' + configFile + ' couldn\'t be parsed: ' + err)
process.exit(1)
}
// Legacy behavior - if config file does not exist, start with default
// values, but an info message to create a config file.
console.log(cyan(bold('TIP')), 'create a config.json: `$ solid init`')
}
return argv
}
/**
*
* @param root
* @param [serverUri] If not set, hostname must be set
* @param [hostname] If not set, serverUri must be set
* @returns {*}
*/
function loadAccounts ({ root, serverUri, hostname }) {
const files = fs.readdirSync(root)
hostname = hostname || new URL(serverUri).hostname
const isUserDirectory = new RegExp(`.${hostname}$`)
return files
.filter(file => isUserDirectory.test(file))
}
function loadUsernames ({ root, serverUri }) {
const hostname = new URL(serverUri).hostname
return loadAccounts({ root, hostname })
.map(userDirectory => userDirectory.substr(0, userDirectory.length - hostname.length - 1))
}