better config / install from cli / allow_registration

This commit is contained in:
lesion
2019-06-21 23:52:18 +02:00
parent 4c3c7ee324
commit cf81a73f2f
38 changed files with 530 additions and 272 deletions

View File

@@ -1,41 +1,43 @@
// check config.js existance
const fs = require('fs')
const path = require('path')
const argv = require('yargs').argv
const consola = require('consola')
const config_path = path.resolve(argv.config || './config.js')
module.exports = {
check (config_path) {
return !fs.existsSync(config_path)
},
if (!fs.existsSync(config_path)) {
console.error(`Configuration file not found at '${config_path}. Please copy 'config.example.js' and modify it.`)
process.exit(1)
}
async setup (config, config_path) {
// generate a random salt
consola.info('Generate random salt')
config.secret = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
const config = require(config_path)
if (!config.secret) {
console.error(`Please specify a random 'secret' in '${config_path}'!`)
process.exit(1)
}
consola.info(`Save configuration into ${config_path}`)
fs.writeFileSync(config_path, JSON.stringify(config, null, 2))
const Sequelize = require('sequelize')
let db
try {
db = new Sequelize(config.db)
} catch (e) {
console.error(`DB Error: check '${config.env}' configuration.\n (sequelize error -> ${e})`)
process.exit(1)
}
// return db existence
module.exports = db.authenticate()
.then(() => {
require('./api/models')
if (config.env === 'development') {
console.error('DB Force sync')
return db.sync({ force: true })
// sync db (TODO, check if there's something in db and ask to backup)
const db = require('./api/models')
try {
consola.info(`Create tables..`)
await db.sequelize.sync({force: true})
} catch(e) {
consola.error('Error creating tables', e)
return -1
}
})
.catch(e => {
console.error(e)
console.error(`DB Error: check '${config.env}' configuration\n (sequelize error -> ${e})`)
process.exit(1)
})
// create admin user
consola.info('Create admin user')
await db.user.create({
email: config.admin.email,
password: config.admin.password,
is_admin: true,
is_active: true
})
const settings = require('./api/controller/settings')
settings.set('enable_registration', true)
settings.set('allow_anon_event', true)
settings.set('allow_mastodon_association', true)
}
}