Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 54 additions & 54 deletions bin/lib/cli-utils.mjs → bin/lib/cli-utils.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
import fs from 'fs-extra'
import { red, cyan, bold } from 'colorette'
import { URL } from 'url'
import LDP from '../../lib/ldp.mjs'
import AccountManager from '../../lib/models/account-manager.mjs'
import SolidHost from '../../lib/models/solid-host.mjs'
export 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
})
}
export function loadConfig (program, options) {
let argv = {
...options,
version: program.version()
}
const configFile = argv.configFile || './config.json'
try {
const file = fs.readFileSync(configFile)
const config = JSON.parse(file)
argv = { ...config, ...argv }
} catch (err) {
if (typeof argv.configFile !== 'undefined') {
if (!fs.existsSync(configFile)) {
console.log(red(bold('ERR')), 'Config file ' + configFile + " doesn't exist.")
process.exit(1)
}
}
if (fs.existsSync(configFile)) {
console.log(red(bold('ERR')), 'config file ' + configFile + " couldn't be parsed: " + err)
process.exit(1)
}
console.log(cyan(bold('TIP')), 'create a config.json: `$ solid init`')
}
return argv
}
export 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))
}
export function loadUsernames ({ root, serverUri }) {
const hostname = new URL(serverUri).hostname
return loadAccounts({ root, hostname }).map(userDirectory => userDirectory.substr(0, userDirectory.length - hostname.length - 1))
}
import fs from 'fs-extra'
import { red, cyan, bold } from 'colorette'
import { URL } from 'url'
import LDP from '../../lib/ldp.js'
import AccountManager from '../../lib/models/account-manager.js'
import SolidHost from '../../lib/models/solid-host.js'

export 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
})
}

export function loadConfig (program, options) {
let argv = {
...options,
version: program.version()
}
const configFile = argv.configFile || './config.json'
try {
const file = fs.readFileSync(configFile)
const config = JSON.parse(file)
argv = { ...config, ...argv }
} catch (err) {
if (typeof argv.configFile !== 'undefined') {
if (!fs.existsSync(configFile)) {
console.log(red(bold('ERR')), 'Config file ' + configFile + " doesn't exist.")
process.exit(1)
}
}
if (fs.existsSync(configFile)) {
console.log(red(bold('ERR')), 'config file ' + configFile + " couldn't be parsed: " + err)
process.exit(1)
}
console.log(cyan(bold('TIP')), 'create a config.json: `$ solid init`')
}
return argv
}

export 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))
}

export function loadUsernames ({ root, serverUri }) {
const hostname = new URL(serverUri).hostname
return loadAccounts({ root, hostname }).map(userDirectory => userDirectory.substr(0, userDirectory.length - hostname.length - 1))
}
88 changes: 44 additions & 44 deletions bin/lib/cli.mjs → bin/lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
import { Command } from 'commander'
import loadInit from './init.mjs'
import loadStart from './start.mjs'
import loadInvalidUsernames from './invalidUsernames.mjs'
import loadMigrateLegacyResources from './migrateLegacyResources.mjs'
import loadUpdateIndex from './updateIndex.mjs'
import { spawnSync } from 'child_process'
import path from 'path'
import fs from 'fs'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
export default function startCli (server) {
const program = new Command()
program.version(getVersion())
loadInit(program)
loadStart(program, server)
loadInvalidUsernames(program)
loadMigrateLegacyResources(program)
loadUpdateIndex(program)
program.parse(process.argv)
if (program.args.length === 0) program.help()
}
function getVersion () {
try {
const options = { cwd: __dirname, encoding: 'utf8' }
const { stdout } = spawnSync('git', ['describe', '--tags'], options)
const { stdout: gitStatusStdout } = spawnSync('git', ['status'], options)
const version = stdout.trim()
if (version === '' || gitStatusStdout.match('Not currently on any branch')) {
throw new Error('No git version here')
}
return version
} catch (e) {
const pkgPath = path.join(__dirname, '../../package.json')
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
return pkg.version
}
}
import { Command } from 'commander'
import loadInit from './init.js'
import loadStart from './start.js'
import loadInvalidUsernames from './invalidUsernames.js'
import loadMigrateLegacyResources from './migrateLegacyResources.js'
import loadUpdateIndex from './updateIndex.js'
import { spawnSync } from 'child_process'
import path from 'path'
import fs from 'fs'
import { fileURLToPath } from 'url'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

export default function startCli (server) {
const program = new Command()
program.version(getVersion())

loadInit(program)
loadStart(program, server)
loadInvalidUsernames(program)
loadMigrateLegacyResources(program)
loadUpdateIndex(program)

program.parse(process.argv)
if (program.args.length === 0) program.help()
}

function getVersion () {
try {
const options = { cwd: __dirname, encoding: 'utf8' }
const { stdout } = spawnSync('git', ['describe', '--tags'], options)
const { stdout: gitStatusStdout } = spawnSync('git', ['status'], options)
const version = stdout.trim()
if (version === '' || gitStatusStdout.match('Not currently on any branch')) {
throw new Error('No git version here')
}
return version
} catch (e) {
const pkgPath = path.join(__dirname, '../../package.json')
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
return pkg.version
}
}
186 changes: 93 additions & 93 deletions bin/lib/init.mjs → bin/lib/init.js
Original file line number Diff line number Diff line change
@@ -1,93 +1,93 @@
import inquirer from 'inquirer'
import fs from 'fs'
import options from './options.mjs'
import camelize from 'camelize'
const questions = options
.map((option) => {
if (!option.type) {
if (option.flag) {
option.type = 'confirm'
} else {
option.type = 'input'
}
}
option.message = option.question || option.help
return option
})
export default function (program) {
program
.command('init')
.option('--advanced', 'Ask for all the settings')
.description('create solid server configurations')
.action((opts) => {
// Filter out advanced commands
let filtered = questions
if (!opts.advanced) {
filtered = filtered.filter((option) => option.prompt)
}
// Prompt to the user
inquirer.prompt(filtered)
.then((answers) => {
manipulateEmailSection(answers)
manipulateServerSection(answers)
cleanupAnswers(answers)
// write config file
const config = JSON.stringify(camelize(answers), null, ' ')
const configPath = process.cwd() + '/config.json'
fs.writeFile(configPath, config, (err) => {
if (err) {
return console.log('failed to write config.json')
}
console.log('config created on', configPath)
})
})
.catch((err) => {
console.log('Error:', err)
})
})
}
function cleanupAnswers (answers) {
Object.keys(answers).forEach((answer) => {
if (answer.startsWith('use')) {
delete answers[answer]
}
})
}
function manipulateEmailSection (answers) {
if (answers.useEmail) {
answers.email = {
host: answers['email-host'],
port: answers['email-port'],
secure: true,
auth: {
user: answers['email-auth-user'],
pass: answers['email-auth-pass']
}
}
delete answers['email-host']
delete answers['email-port']
delete answers['email-auth-user']
delete answers['email-auth-pass']
}
}
function manipulateServerSection (answers) {
answers.server = {
name: answers['server-info-name'],
description: answers['server-info-description'],
logo: answers['server-info-logo']
}
Object.keys(answers).forEach((answer) => {
if (answer.startsWith('server-info-')) {
delete answers[answer]
}
})
}
import inquirer from 'inquirer'
import fs from 'fs'
import options from './options.js'
import camelize from 'camelize'

const questions = options
.map((option) => {
if (!option.type) {
if (option.flag) {
option.type = 'confirm'
} else {
option.type = 'input'
}
}

option.message = option.question || option.help
return option
})

export default function (program) {
program
.command('init')
.option('--advanced', 'Ask for all the settings')
.description('create solid server configurations')
.action((opts) => {
// Filter out advanced commands
let filtered = questions
if (!opts.advanced) {
filtered = filtered.filter((option) => option.prompt)
}

// Prompt to the user
inquirer.prompt(filtered)
.then((answers) => {
manipulateEmailSection(answers)
manipulateServerSection(answers)
cleanupAnswers(answers)

// write config file
const config = JSON.stringify(camelize(answers), null, ' ')
const configPath = process.cwd() + '/config.json'

fs.writeFile(configPath, config, (err) => {
if (err) {
return console.log('failed to write config.json')
}
console.log('config created on', configPath)
})
})
.catch((err) => {
console.log('Error:', err)
})
})
}

function cleanupAnswers (answers) {
Object.keys(answers).forEach((answer) => {
if (answer.startsWith('use')) {
delete answers[answer]
}
})
}

function manipulateEmailSection (answers) {
if (answers.useEmail) {
answers.email = {
host: answers['email-host'],
port: answers['email-port'],
secure: true,
auth: {
user: answers['email-auth-user'],
pass: answers['email-auth-pass']
}
}
delete answers['email-host']
delete answers['email-port']
delete answers['email-auth-user']
delete answers['email-auth-pass']
}
}

function manipulateServerSection (answers) {
answers.server = {
name: answers['server-info-name'],
description: answers['server-info-description'],
logo: answers['server-info-logo']
}
Object.keys(answers).forEach((answer) => {
if (answer.startsWith('server-info-')) {
delete answers[answer]
}
})
}
Loading