-import * as Promise from 'bluebird'
-
-import { database as db } from './database'
-import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } from './constants'
-import { clientsExist, usersExist } from './checker'
-import { logger, createCertsIfNotExist, root, mkdirpPromise } from '../helpers'
-
-function installApplication () {
- return db.sequelize.sync()
- .then(() => createDirectoriesIfNotExist())
- .then(() => createCertsIfNotExist())
- .then(() => createOAuthClientIfNotExist())
- .then(() => createOAuthAdminIfNotExist())
+import { UserRole } from '../../shared'
+import { mkdirpPromise, rimrafPromise } from '../helpers/core-utils'
+import { logger } from '../helpers/logger'
+import { createApplicationActor, createUserAccountAndChannel } 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'
+import { CACHE, CONFIG, LAST_MIGRATION_VERSION } from './constants'
+import { sequelizeTypescript } from './database'
+
+async function installApplication () {
+ try {
+ await sequelizeTypescript.sync()
+ await removeCacheDirectories()
+ await createDirectoriesIfNotExist()
+ await createApplicationIfNotExist()
+ await createOAuthClientIfNotExist()
+ await createOAuthAdminIfNotExist()
+ } catch (err) {
+ logger.error('Cannot install application.', { err })
+ process.exit(-1)
+ }