improve installation/documentation
This commit is contained in:
134
server/cli.js
134
server/cli.js
@@ -1,56 +1,19 @@
|
||||
#!/usr/bin/env node
|
||||
process.env.NODE_ENV = "production"
|
||||
process.env.NODE_ENV = 'production'
|
||||
|
||||
const fs = require('fs')
|
||||
const consola = require('consola')
|
||||
const sequelize = require('sequelize')
|
||||
const inquirer = require('inquirer')
|
||||
const package = require('../package.json')
|
||||
const firstrun = require('./firstrun')
|
||||
const path = require('path')
|
||||
|
||||
const cwd = process.cwd()
|
||||
|
||||
// needed by nuxt
|
||||
process.chdir(path.resolve(__dirname, '..'))
|
||||
|
||||
const arg = require('arg')
|
||||
const inquirer = require('inquirer')
|
||||
const package = require('../package.json')
|
||||
const consola = require('consola')
|
||||
const firstrun = require('./firstrun')
|
||||
const fs = require('fs')
|
||||
const sequelize = require('sequelize')
|
||||
|
||||
/**
|
||||
* initial setup:
|
||||
* - check first run
|
||||
* - ask title, description, baseurl
|
||||
* - ask and create upload path and thumb dir
|
||||
* - ask and inizialite db
|
||||
* - create first admin account
|
||||
* - enable open registration?
|
||||
* - enable anon event?
|
||||
* - enable email export?
|
||||
* - enable email notification?
|
||||
* - enable notifier?
|
||||
* - enable pm2
|
||||
*
|
||||
* start gancio:
|
||||
*
|
||||
* update gancio:
|
||||
* - yarn/npm global update...
|
||||
* - sequelize migrate !
|
||||
*/
|
||||
|
||||
function parseArguments(rawArgs) {
|
||||
const args = arg({
|
||||
'--config': String,
|
||||
'--install': Boolean,
|
||||
'--upgrade': Boolean
|
||||
}, {
|
||||
argv: rawArgs.slice(2),
|
||||
});
|
||||
|
||||
return {
|
||||
config: path.resolve(cwd, args['--config'] || '/etc/gancio_config.json') ,
|
||||
install: args['--install'] || false,
|
||||
upgrade: args['--upgrade'] || false
|
||||
};
|
||||
}
|
||||
|
||||
function notEmpty (value) {
|
||||
return value.length>0
|
||||
}
|
||||
@@ -58,10 +21,17 @@ function notEmpty (value) {
|
||||
async function setupQuestionnaire() {
|
||||
|
||||
const questions = []
|
||||
questions.push({
|
||||
name: 'title',
|
||||
message: 'Name of your instance',
|
||||
default: 'Gancio',
|
||||
validate: notEmpty
|
||||
})
|
||||
|
||||
questions.push({
|
||||
message: 'Specify a baseurl for this gancio installation! (eg. http://gancio.cisti.org)',
|
||||
name: 'baseurl',
|
||||
default: 'http://localhost:3000',
|
||||
default: 'http://localhost:13120',
|
||||
validate: notEmpty
|
||||
})
|
||||
|
||||
@@ -88,7 +58,7 @@ async function setupQuestionnaire() {
|
||||
questions.push({
|
||||
name: 'db.storage',
|
||||
message: 'sqlite db path',
|
||||
default: '/var/gancio/db.sqlite',
|
||||
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))
|
||||
@@ -128,7 +98,7 @@ async function setupQuestionnaire() {
|
||||
try {
|
||||
const db = new sequelize({ ...options.db, dialect: 'postgres' , password, logging: false })
|
||||
return db.authenticate().then( () => {
|
||||
// consola.info(`DB connected`)
|
||||
db.close()
|
||||
return true
|
||||
})
|
||||
} catch(e) {
|
||||
@@ -141,7 +111,7 @@ async function setupQuestionnaire() {
|
||||
questions.push({
|
||||
name: 'upload_path',
|
||||
message: 'Where gancio has to store media?',
|
||||
default: '/var/gancio/',
|
||||
default: './uploads',
|
||||
filter: p => path.resolve(cwd, p),
|
||||
validate: p => {
|
||||
const exists = fs.existsSync(p)
|
||||
@@ -187,38 +157,48 @@ async function setupQuestionnaire() {
|
||||
return answers
|
||||
}
|
||||
|
||||
async function cli(args) {
|
||||
|
||||
const options = parseArguments(args)
|
||||
consola.info(`${package.name} - v${package.version} - ${package.description}`)
|
||||
|
||||
// install flag specified?
|
||||
if (options.install) {
|
||||
consola.info(`Cool! You're going to setup gancio on this machine.`)
|
||||
const config = await setupQuestionnaire()
|
||||
await firstrun.setup(config, options.config)
|
||||
consola.info(`You can edit '${options.config}' to modify your configuration. `)
|
||||
consola.info(`- Run "gancio --config ${options.config}"`)
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
// upgrade gancio / TODO npm/yarn global upgrade gancio ?
|
||||
if (options.upgrade) {
|
||||
consola.warn('Not implemented yet but should be an easy task! PR welcome!')
|
||||
process.exit(-1)
|
||||
}
|
||||
|
||||
async function start (options) {
|
||||
// is first run?
|
||||
if (firstrun.check(options.config)) {
|
||||
consola.error(`Configuration file "${options.config}" not found!
|
||||
This is your first run? You could create it using --install flag`)
|
||||
|
||||
process.exit(-1)
|
||||
} else {
|
||||
process.env.config_path = options.config
|
||||
require('./index')
|
||||
}
|
||||
|
||||
require('./index')
|
||||
}
|
||||
|
||||
cli(process.argv)
|
||||
async function setup (options) {
|
||||
consola.info(`You're going to setup gancio on this machine.`)
|
||||
const config = await setupQuestionnaire()
|
||||
await firstrun.setup(config, options.config)
|
||||
consola.info(`You can edit '${options.config}' to modify your configuration. `)
|
||||
consola.info(`Run "gancio --config ${options.config}"`)
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
async function upgrade (options) {
|
||||
consola.warn('Not implemented yet but should be an easy task! PR welcome!')
|
||||
process.exit(-1)
|
||||
}
|
||||
|
||||
consola.info(`${package.name} - v${package.version} - ${package.description}`)
|
||||
|
||||
require('yargs')
|
||||
.usage('Usage $0 <command> [options]')
|
||||
.option('config', {
|
||||
alias: 'c',
|
||||
describe: 'Configuration file',
|
||||
default: './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)
|
||||
.command('upgrade', 'Upgrade gancio to a new release (interactive)', {}, upgrade)
|
||||
.help('h')
|
||||
.alias('h', 'help')
|
||||
.epilog('Made with ❤ by underscore hacklab - https://autistici.org/underscore')
|
||||
.argv
|
||||
|
||||
Reference in New Issue
Block a user