-import * as passwordGenerator from 'password-generator'
-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 { ensureDir, readdir, remove } from 'fs-extra'
+import passwordGenerator from 'password-generator'
+import { join } from 'path'
+import { isTestOrDevInstance } from '@server/helpers/core-utils'
+import { getNodeABIVersion } from '@server/helpers/version'
+import { UserRole } from '@shared/models'
+import { logger } from '../helpers/logger'
+import { buildUser, createApplicationActor, createUserAccountAndChannelAndPlaylist } from '../lib/user'
+import { ApplicationModel } from '../models/application/application'
+import { OAuthClientModel } from '../models/oauth/oauth-client'
+import { applicationExist, clientsExist, usersExist } from './checker-after-init'
+import { CONFIG } from './config'
+import { DIRECTORIES, FILES_CACHE, LAST_MIGRATION_VERSION } from './constants'
+import { sequelizeTypescript } from './database'
+
+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)
+ }