-import { join } from 'path'
-import config = require('config')
-import { each, series } from 'async'
-import mkdirp = require('mkdirp')
-import passwordGenerator = require('password-generator')
-
-import { database as db } from './database'
-import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } 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 * as passwordGenerator from 'password-generator'
+import { UserRole } from '../../shared'
+import { logger } from '../helpers/logger'
+import { createApplicationActor, createUserAccountAndChannelAndPlaylist } from '../lib/user'
+import { UserModel } from '../models/account/user'
+import { ApplicationModel } from '../models/application/application'
+import { OAuthClientModel } from '../models/oauth/oauth-client'
+import { applicationExist, clientsExist, usersExist } from './checker-after-init'
+import { CACHE, CONFIG, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION } from './constants'
+import { sequelizeTypescript } from './database'
+import { remove, ensureDir } from 'fs-extra'
+
+async function installApplication () {
+ try {
+ await Promise.all([
+ // Database related
+ sequelizeTypescript.sync()
+ .then(() => {
+ return Promise.all([
+ createApplicationIfNotExist(),
+ createOAuthClientIfNotExist(),
+ createOAuthAdminIfNotExist()
+ ])
+ }),
+
+ // Directories
+ removeCacheAndTmpDirectories()
+ .then(() => createDirectoriesIfNotExist())
+ ])
+ } catch (err) {
+ logger.error('Cannot install application.', { err })
+ process.exit(-1)
+ }