|
| 1 | +import fs from 'fs-extra' |
| 2 | +import { red, cyan, bold } from 'colorette' |
| 3 | +import { URL } from 'url' |
| 4 | +import LDP from '../../lib/ldp.mjs' |
| 5 | +import AccountManager from '../../lib/models/account-manager.mjs' |
| 6 | +import SolidHost from '../../lib/models/solid-host.mjs' |
| 7 | + |
| 8 | +export function getAccountManager (config, options = {}) { |
| 9 | + const ldp = options.ldp || new LDP(config) |
| 10 | + const host = options.host || SolidHost.from({ port: config.port, serverUri: config.serverUri }) |
| 11 | + return AccountManager.from({ |
| 12 | + host, |
| 13 | + store: ldp, |
| 14 | + multiuser: config.multiuser |
| 15 | + }) |
| 16 | +} |
| 17 | + |
| 18 | +export function loadConfig (program, options) { |
| 19 | + let argv = { |
| 20 | + ...options, |
| 21 | + version: program.version() |
| 22 | + } |
| 23 | + const configFile = argv.configFile || './config.json' |
| 24 | + try { |
| 25 | + const file = fs.readFileSync(configFile) |
| 26 | + const config = JSON.parse(file) |
| 27 | + argv = { ...config, ...argv } |
| 28 | + } catch (err) { |
| 29 | + if (typeof argv.configFile !== 'undefined') { |
| 30 | + if (!fs.existsSync(configFile)) { |
| 31 | + console.log(red(bold('ERR')), 'Config file ' + configFile + " doesn't exist.") |
| 32 | + process.exit(1) |
| 33 | + } |
| 34 | + } |
| 35 | + if (fs.existsSync(configFile)) { |
| 36 | + console.log(red(bold('ERR')), 'config file ' + configFile + " couldn't be parsed: " + err) |
| 37 | + process.exit(1) |
| 38 | + } |
| 39 | + console.log(cyan(bold('TIP')), 'create a config.json: `$ solid init`') |
| 40 | + } |
| 41 | + return argv |
| 42 | +} |
| 43 | + |
| 44 | +export function loadAccounts ({ root, serverUri, hostname }) { |
| 45 | + const files = fs.readdirSync(root) |
| 46 | + hostname = hostname || new URL(serverUri).hostname |
| 47 | + const isUserDirectory = new RegExp(`.${hostname}$`) |
| 48 | + return files.filter(file => isUserDirectory.test(file)) |
| 49 | +} |
| 50 | + |
| 51 | +export function loadUsernames ({ root, serverUri }) { |
| 52 | + const hostname = new URL(serverUri).hostname |
| 53 | + return loadAccounts({ root, hostname }).map(userDirectory => userDirectory.substr(0, userDirectory.length - hostname.length - 1)) |
| 54 | +} |
0 commit comments