linting & cleaning
This commit is contained in:
144
server/cli.js
144
server/cli.js
@@ -3,9 +3,9 @@ process.env.NODE_ENV = 'production'
|
||||
|
||||
const fs = require('fs')
|
||||
const consola = require('consola')
|
||||
const sequelize = require('sequelize')
|
||||
const Sequelize = require('sequelize')
|
||||
const inquirer = require('inquirer')
|
||||
const package = require('../package.json')
|
||||
const pkg = require('../package.json')
|
||||
const firstrun = require('./firstrun')
|
||||
const path = require('path')
|
||||
const mkdirp = require('mkdirp')
|
||||
@@ -17,11 +17,10 @@ const cwd = process.cwd()
|
||||
process.chdir(path.resolve(__dirname, '..'))
|
||||
|
||||
function notEmpty (value) {
|
||||
return value.length>0
|
||||
return value.length > 0
|
||||
}
|
||||
|
||||
async function setupQuestionnaire(is_docker, db) {
|
||||
|
||||
async function setupQuestionnaire (is_docker, db) {
|
||||
const questions = []
|
||||
questions.push({
|
||||
name: 'title',
|
||||
@@ -31,11 +30,14 @@ async function setupQuestionnaire(is_docker, db) {
|
||||
})
|
||||
|
||||
questions.push({
|
||||
message: 'Specify a baseurl for this gancio installation! (eg. http://gancio.cisti.org)',
|
||||
message:
|
||||
'Specify a baseurl for this gancio installation! (eg. http://gancio.cisti.org)',
|
||||
name: 'baseurl',
|
||||
default: 'http://localhost:13120',
|
||||
validate: value => {
|
||||
if (!value) return false
|
||||
if (!value) {
|
||||
return false
|
||||
}
|
||||
return /^https?:\/\//.test(value)
|
||||
}
|
||||
})
|
||||
@@ -51,7 +53,7 @@ async function setupQuestionnaire(is_docker, db) {
|
||||
questions.push({
|
||||
name: 'server.port',
|
||||
message: 'port to listen to',
|
||||
default: 13120,
|
||||
default: 13120
|
||||
})
|
||||
|
||||
questions.push({
|
||||
@@ -67,7 +69,8 @@ async function setupQuestionnaire(is_docker, db) {
|
||||
default: './db.sqlite',
|
||||
filter: p => path.resolve(cwd, p),
|
||||
when: answers => answers.db.dialect === 'sqlite',
|
||||
validate: db_path => db_path.length>0 && fs.existsSync(path.dirname(db_path))
|
||||
validate: db_path =>
|
||||
db_path.length > 0 && fs.existsSync(path.dirname(db_path))
|
||||
})
|
||||
|
||||
questions.push({
|
||||
@@ -85,7 +88,7 @@ async function setupQuestionnaire(is_docker, db) {
|
||||
when: answers => answers.db.dialect === 'postgres',
|
||||
validate: notEmpty
|
||||
})
|
||||
|
||||
|
||||
questions.push({
|
||||
name: 'db.username',
|
||||
message: 'DB user',
|
||||
@@ -93,21 +96,26 @@ async function setupQuestionnaire(is_docker, db) {
|
||||
when: answers => answers.db.dialect === 'postgres',
|
||||
validate: notEmpty
|
||||
})
|
||||
|
||||
|
||||
questions.push({
|
||||
name: 'db.password',
|
||||
type: 'password',
|
||||
message: 'DB password',
|
||||
default: 'gancio',
|
||||
when: answers => answers.db.dialect === 'postgres',
|
||||
validate: async (password, options) => {
|
||||
validate: (password, options) => {
|
||||
try {
|
||||
const db = new sequelize({ ...options.db, dialect: 'postgres' , password, logging: false })
|
||||
return db.authenticate().then( () => {
|
||||
const db = new Sequelize({
|
||||
...options.db,
|
||||
dialect: 'postgres',
|
||||
password,
|
||||
logging: false
|
||||
})
|
||||
return db.authenticate().then(() => {
|
||||
db.close()
|
||||
return true
|
||||
})
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
consola.error(e)
|
||||
return false
|
||||
}
|
||||
@@ -119,13 +127,13 @@ async function setupQuestionnaire(is_docker, db) {
|
||||
message: 'Where gancio has to store media?',
|
||||
default: './uploads',
|
||||
filter: p => path.resolve(cwd, p),
|
||||
validate: async p => {
|
||||
let exists = fs.existsSync(p)
|
||||
validate: p => {
|
||||
const exists = fs.existsSync(p)
|
||||
if (!exists) {
|
||||
consola.warn(`"${p}" does not exists, trying to create it`)
|
||||
try {
|
||||
mkdirp.sync(p)
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
console.error(String(e))
|
||||
return false
|
||||
}
|
||||
@@ -138,11 +146,14 @@ async function setupQuestionnaire(is_docker, db) {
|
||||
name: 'admin.email',
|
||||
message: `Admin email (a first user with this username will be created)`,
|
||||
default: options => {
|
||||
return options.title.replace(' ', '').toLowerCase() + '@' + url.parse(options.baseurl, true).hostname
|
||||
const baseurl = new url.URL(options.baseurl, true)
|
||||
return (
|
||||
options.title.replace(' ', '').toLowerCase() + '@' + baseurl.hostname
|
||||
)
|
||||
},
|
||||
validate: notEmpty
|
||||
})
|
||||
|
||||
|
||||
questions.push({
|
||||
name: 'admin.password',
|
||||
message: 'Admin password',
|
||||
@@ -153,7 +164,7 @@ async function setupQuestionnaire(is_docker, db) {
|
||||
questions.push({
|
||||
name: 'smtp.host',
|
||||
message: 'SMTP Host',
|
||||
validate: notEmpty,
|
||||
validate: notEmpty
|
||||
})
|
||||
|
||||
questions.push({
|
||||
@@ -167,18 +178,23 @@ async function setupQuestionnaire(is_docker, db) {
|
||||
name: 'smtp.auth.pass',
|
||||
message: 'SMTP Password',
|
||||
type: 'password',
|
||||
validate: notEmpty,
|
||||
validate: notEmpty
|
||||
})
|
||||
|
||||
const answers = await inquirer.prompt(questions)
|
||||
if (is_docker) {
|
||||
answers.server = { host: '0.0.0.0', port: 13120 }
|
||||
answers.upload_path = '/opt/gancio/uploads'
|
||||
if(db === 'sqlite') {
|
||||
if (db === 'sqlite') {
|
||||
answers.db = { dialect: db, storage: '/opt/gancio/db.sqlite' }
|
||||
} else {
|
||||
answers.db = { dialect: db, host: 'db', database: 'gancio',
|
||||
username: 'gancio', password: 'gancio'}
|
||||
answers.db = {
|
||||
dialect: db,
|
||||
host: 'db',
|
||||
database: 'gancio',
|
||||
username: 'gancio',
|
||||
password: 'gancio'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,21 +211,26 @@ async function run_migrations (db_conf) {
|
||||
logging: consola.info,
|
||||
migrations: {
|
||||
wrap: fun => {
|
||||
return () => fun(db.queryInterface, Sequelize).catch(e => { consola.error(e); return false; })
|
||||
},
|
||||
path: path.resolve(__dirname, 'migrations')
|
||||
return () =>
|
||||
fun(db.queryInterface, Sequelize).catch(e => {
|
||||
consola.error(e)
|
||||
return false
|
||||
})
|
||||
},
|
||||
path: path.resolve(__dirname, 'migrations')
|
||||
}
|
||||
})
|
||||
await umzug.up()
|
||||
return await db.close()
|
||||
return db.close()
|
||||
}
|
||||
|
||||
|
||||
async function start (options) {
|
||||
// is first run?
|
||||
if (firstrun.check(options.config)) {
|
||||
if (options.docker) {
|
||||
consola.error('⚠ ️ Something goes wrong, did you run "docker-compose run --rm gancio gancio setup"')
|
||||
consola.error(
|
||||
'⚠ ️ Something goes wrong, did you run "docker-compose run --rm gancio gancio setup"'
|
||||
)
|
||||
process.exit(-1)
|
||||
}
|
||||
consola.error(` ⚠ Configuration file "${options.config}" not found! Use "--config <CONFIG_FILE.json>" to specify another path.
|
||||
@@ -226,43 +247,46 @@ async function setup (options) {
|
||||
const config = await setupQuestionnaire(options.docker, options.db)
|
||||
await run_migrations(config.db)
|
||||
const ret = await firstrun.setup(config, options.config)
|
||||
if (!ret) process.exit(-1)
|
||||
if (!ret) {
|
||||
process.exit(-1)
|
||||
}
|
||||
if (options.docker) {
|
||||
consola.info(`You can edit ./config.json to modify your configuration.`)
|
||||
consola.info(`Start the server with "docker-compose up"`)
|
||||
} else {
|
||||
consola.info(`You can edit '${options.config}' to modify your configuration. `)
|
||||
consola.info(
|
||||
`You can edit '${options.config}' to modify your configuration. `
|
||||
)
|
||||
consola.info(`Start the server with "gancio --config ${options.config}"`)
|
||||
}
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
consola.info(`${package.name} - v${package.version} - ${package.description}`)
|
||||
consola.info(`📅 ${pkg.name} - v${pkg.version} - ${pkg.description}`)
|
||||
|
||||
require('yargs')
|
||||
.usage('Usage $0 <command> [options]')
|
||||
.option('docker', {
|
||||
alias: 'd',
|
||||
describe: 'Inside docker',
|
||||
default: false,
|
||||
type: 'boolean'
|
||||
})
|
||||
.option('db', {
|
||||
describe: 'Specify db type',
|
||||
})
|
||||
.option('config', {
|
||||
alias: 'c',
|
||||
describe: 'Configuration file',
|
||||
default: '/opt/gancio/config.json',
|
||||
})
|
||||
.coerce('config', config_path => {
|
||||
const absolute_config_path = path.resolve(cwd, config_path)
|
||||
process.env.config_path = absolute_config_path
|
||||
return absolute_config_path
|
||||
})
|
||||
.command(['start', 'run', '$0'], 'Start gancio', {}, start)
|
||||
.command('setup', 'Setup a new instance', {}, setup)
|
||||
.help('h')
|
||||
.alias('h', 'help')
|
||||
.epilog('Made with ❤ by underscore hacklab - https://gancio.org')
|
||||
.argv
|
||||
.usage('Usage $0 <command> [options]')
|
||||
.option('docker', {
|
||||
alias: 'd',
|
||||
describe: 'Inside docker',
|
||||
default: false,
|
||||
type: 'boolean'
|
||||
})
|
||||
.option('db', {
|
||||
describe: 'Specify db type'
|
||||
})
|
||||
.option('config', {
|
||||
alias: 'c',
|
||||
describe: 'Configuration file',
|
||||
default: '/opt/gancio/config.json'
|
||||
})
|
||||
.coerce('config', config_path => {
|
||||
const absolute_config_path = path.resolve(cwd, config_path)
|
||||
process.env.config_path = absolute_config_path
|
||||
return absolute_config_path
|
||||
})
|
||||
.command(['start', 'run', '$0'], 'Start gancio', {}, start)
|
||||
.command('setup', 'Setup a new instance', {}, setup)
|
||||
.help('h')
|
||||
.alias('h', 'help')
|
||||
.epilog('Made with ❤ by underscore hacklab - https://gancio.org')
|
||||
|
||||
Reference in New Issue
Block a user