-import * as Promise from 'bluebird'
-
-import { database as db } from './database'
-import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION, CACHE } from './constants'
-import { clientsExist, usersExist } from './checker'
-import { logger, createCertsIfNotExist, mkdirpPromise, rimrafPromise } from '../helpers'
-
-function installApplication () {
- return db.sequelize.sync()
- .then(() => removeCacheDirectories())
- .then(() => createDirectoriesIfNotExist())
- .then(() => createCertsIfNotExist())
- .then(() => createOAuthClientIfNotExist())
- .then(() => createOAuthAdminIfNotExist())
+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)
+ }