wikijs-fork/server/core/system.js

91 lines
2.3 KiB
JavaScript
Raw Normal View History

const _ = require('lodash')
const cfgHelper = require('../helpers/config')
const Promise = require('bluebird')
2019-01-26 23:35:56 +00:00
const fs = require('fs-extra')
const path = require('path')
/* global WIKI */
2017-02-13 05:59:52 +00:00
module.exports = {
2019-01-13 20:37:45 +00:00
updates: {
channel: 'BETA',
version: WIKI.version,
releaseDate: WIKI.releaseDate,
minimumVersionRequired: '2.0.0-beta.0',
minimumNodeRequired: '10.12.0'
},
init() {
// Listen for updates events
WIKI.events.on('updates', (infoRaw) => {
try {
this.updates = JSON.parse(infoRaw)
} catch (err) {
WIKI.logger.warn('Failed to parse updates info.')
}
})
2019-01-26 23:35:56 +00:00
// Clear content cache
fs.emptyDir(path.join(WIKI.ROOTPATH, 'data/cache'))
2019-01-13 20:37:45 +00:00
return this
},
2017-02-14 04:30:42 +00:00
/**
* Upgrade from WIKI.js 1.x - MongoDB database
2017-02-14 04:30:42 +00:00
*
* @param {Object} opts Options object
2017-02-14 04:30:42 +00:00
*/
async upgradeFromMongo (opts) {
WIKI.logger.info('Upgrading from MongoDB...')
2017-12-17 04:41:16 +00:00
let mongo = require('mongodb').MongoClient
let parsedMongoConStr = cfgHelper.parseConfigValue(opts.mongoCnStr)
2017-02-13 05:59:52 +00:00
return new Promise((resolve, reject) => {
// Connect to MongoDB
mongo.connect(parsedMongoConStr, {
autoReconnect: false,
reconnectTries: 2,
reconnectInterval: 1000,
connectTimeoutMS: 5000,
socketTimeoutMS: 5000
}, async (err, db) => {
try {
if (err !== null) { throw err }
let users = db.collection('users')
// Check if users table is populated
let userCount = await users.count()
if (userCount < 2) {
throw new Error('MongoDB Upgrade: Users table is empty!')
2017-02-13 05:59:52 +00:00
}
// Import all users
let userData = await users.find({
email: {
$not: 'guest'
}
}).toArray()
await WIKI.models.User.bulkCreate(_.map(userData, usr => {
return {
email: usr.email,
name: usr.name || 'Imported User',
password: usr.password || '',
provider: usr.provider || 'local',
providerId: usr.providerId || '',
role: 'user',
createdAt: usr.createdAt
}
}))
2017-02-14 04:30:42 +00:00
resolve(true)
} catch (err) {
reject(err)
}
db.close()
})
2017-02-13 05:59:52 +00:00
})
}
}