-import config = require('config')
-import { each, series } from 'async'
-import mkdirp = require('mkdirp')
-import passwordGenerator = require('password-generator')
-
-const db = require('./database')
-import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } from './constants'
-import { clientsExist, usersExist } from './checker'
-import { logger, createCertsIfNotExist } 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 { 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)
+ }