gancio-upstream/server/firstrun.js

62 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-06-06 23:54:32 +02:00
// check config.js existance
const fs = require('fs')
const consola = require('consola')
2019-06-06 23:54:32 +02:00
module.exports = {
check (config_path) {
return !fs.existsSync(config_path)
},
2019-06-06 23:54:32 +02:00
async setup (config, config_path) {
// generate a random salt
2021-06-20 23:50:22 +02:00
// consola.info('Generate random salt')
// config.secret = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
2019-06-06 23:54:32 +02:00
2019-07-03 16:58:24 +02:00
// do not save admin's password in config file
2019-06-26 14:44:21 +02:00
const admin = { email: config.admin.email, password: config.admin.password }
delete config.admin
2019-07-03 16:58:24 +02:00
if (config.smtp_type === 'sendmail') {
config.smtp = {
sendmail: true,
newline: 'unix',
path: config.smtp.path
}
}
delete config.smtp_type
delete config.smtp_need_auth
2019-06-26 14:44:21 +02:00
config.admin_email = admin.email
2019-07-03 16:58:24 +02:00
config.db.logging = false
2021-05-27 00:04:10 +02:00
config.log_level = 'debug'
2019-07-03 16:58:24 +02:00
consola.info(`Save configuration to ${config_path}`)
try {
fs.writeFileSync(config_path, JSON.stringify(config, null, 2))
2019-10-28 17:33:20 +01:00
} catch (e) {
consola.warn(` ⚠️ ${e}. You can specify configuration path using '--config'`)
}
2019-06-06 23:54:32 +02:00
2019-06-26 16:15:25 +02:00
// sync db
2020-08-27 22:12:36 +02:00
const db = require('./api/models/index')
const User = require('./api/models/user')
const users = await User.findAll()
2019-10-25 18:43:49 +02:00
if (users.length) {
2020-01-15 23:51:09 +01:00
consola.warn(' ⚠ Non empty db! Please move your current db elsewhere than retry.')
2019-08-06 01:12:05 +02:00
return false
2019-10-25 18:43:49 +02:00
}
// create admin user
2020-02-15 15:42:41 +01:00
consola.info(`Create admin with email: ${admin.email}`)
2020-08-27 22:12:36 +02:00
await User.create({
2019-07-29 22:40:27 +02:00
email: admin.email,
password: admin.password,
is_admin: true,
is_active: true
})
2019-07-03 16:58:24 +02:00
// close db connection
2020-08-27 22:12:36 +02:00
await db.close()
return true
}
2019-08-03 15:46:42 +02:00
}