-import { join } from 'path'
-import config = require('config')
-import { each, series } from 'async'
-import mkdirp = require('mkdirp')
-import passwordGenerator = require('password-generator')
+import * as passwordGenerator from 'password-generator'
+import * as Promise from 'bluebird'
import { database as db } from './database'
-import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } from './constants'
+import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants'
import { clientsExist, usersExist } from './checker'
-import { logger, createCertsIfNotExist, root } from '../helpers'
-
-function installApplication (callback) {
- series([
- function createDatabase (callbackAsync) {
- db.sequelize.sync().asCallback(callbackAsync)
- // db.sequelize.sync({ force: true }).asCallback(callbackAsync)
- },
-
- function createDirectories (callbackAsync) {
- createDirectoriesIfNotExist(callbackAsync)
- },
-
- function createCertificates (callbackAsync) {
- createCertsIfNotExist(callbackAsync)
- },
-
- function createOAuthClient (callbackAsync) {
- createOAuthClientIfNotExist(callbackAsync)
- },
-
- function createOAuthUser (callbackAsync) {
- createOAuthAdminIfNotExist(callbackAsync)
- }
- ], callback)
+import { logger, createCertsIfNotExist, mkdirpPromise, rimrafPromise } from '../helpers'
+
+function installApplication () {
+ return db.sequelize.sync()
+ .then(() => removeCacheDirectories())
+ .then(() => createDirectoriesIfNotExist())
+ .then(() => createCertsIfNotExist())
+ .then(() => createOAuthClientIfNotExist())
+ .then(() => createOAuthAdminIfNotExist())
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
-function createDirectoriesIfNotExist (callback) {
- const storages = config.get('storage')
+function removeCacheDirectories () {
+ const cacheDirectories = CACHE.DIRECTORIES
+
+ const tasks = []
+
+ // Cache directories
+ Object.keys(cacheDirectories).forEach(key => {
+ const dir = cacheDirectories[key]
+ tasks.push(rimrafPromise(dir))
+ })
- each(Object.keys(storages), function (key, callbackEach) {
- const dir = storages[key]
- mkdirp(join(root(), dir), callbackEach)
- }, callback)
+ return Promise.all(tasks)
}
-function createOAuthClientIfNotExist (callback) {
- clientsExist(function (err, exist) {
- if (err) return callback(err)
+function createDirectoriesIfNotExist () {
+ const storage = CONFIG.STORAGE
+ const cacheDirectories = CACHE.DIRECTORIES
+ const tasks = []
+ Object.keys(storage).forEach(key => {
+ const dir = storage[key]
+ tasks.push(mkdirpPromise(dir))
+ })
+
+ // Cache directories
+ Object.keys(cacheDirectories).forEach(key => {
+ const dir = cacheDirectories[key]
+ tasks.push(mkdirpPromise(dir))
+ })
+
+ return Promise.all(tasks)
+}
+
+function createOAuthClientIfNotExist () {
+ return clientsExist(db.OAuthClient).then(exist => {
// Nothing to do, clients already exist
- if (exist === true) return callback(null)
+ if (exist === true) return undefined
logger.info('Creating a default OAuth Client.')
redirectUris: null
})
- client.save().asCallback(function (err, createdClient) {
- if (err) return callback(err)
-
+ return client.save().then(createdClient => {
logger.info('Client id: ' + createdClient.clientId)
logger.info('Client secret: ' + createdClient.clientSecret)
- return callback(null)
+ return undefined
})
})
}
-function createOAuthAdminIfNotExist (callback) {
- usersExist(function (err, exist) {
- if (err) return callback(err)
-
+function createOAuthAdminIfNotExist () {
+ return usersExist(db.User).then(exist => {
// Nothing to do, users already exist
- if (exist === true) return callback(null)
+ if (exist === true) return undefined
logger.info('Creating the administrator.')
username,
email,
password,
- role
+ role,
+ videoQuota: -1
}
- db.User.create(userData, createOptions).asCallback(function (err, createdUser) {
- if (err) return callback(err)
-
+ return db.User.create(userData, createOptions).then(createdUser => {
logger.info('Username: ' + username)
logger.info('User password: ' + password)
logger.info('Creating Application table.')
- db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION }).asCallback(callback)
+ return db.Application.create({ migrationVersion: LAST_MIGRATION_VERSION })
})
})
}