import fs from 'fs';
import {
  dataDir, initialFile,
  email, username, password } from './config';
import { User, UserGroup,
  File, ROOTID, FILETYPE } from './data/models';


function initialGroup() {
  return new Promise(async (resolve, reject) => {
    try {
      await UserGroup.create({ name: 'root' });
      await UserGroup.create({ name: 'user' });
      resolve(true);
    } catch (err) {
      reject(err);
    }
  });
}

function createRootUser() {
  return new Promise(async (resolve, reject) => {
    try {
      resolve(await User.create({
        email,
        username,
        password,
      }));
    } catch (err) {
      reject(err);
    }
  });
}

function shoudlInitial() {
  return !fs.existsSync(initialFile);
}

function createRoot(root) {
  return new Promise(async (resolve, reject) => {
    try {
      await File.create({
        id: ROOTID,
        name: 'root',
        type: FILETYPE.d,
        mode: '755',
        userId: root.id,
        linkTo: 'none',
        path: '/',
      });
      resolve();
    } catch (err) {
      reject(err);
    }
  });
}

export default async function () {
  try {
    if (!await shoudlInitial()) return;
    if (!fs.existsSync(dataDir)) {
      fs.mkdirSync(dataDir);
    }
    if (!fs.existsSync(initialFile)) {
      await initialGroup();
      await createRoot(await createRootUser());
      fs.closeSync(fs.openSync(initialFile, 'w'));
    }
  } catch (err) {
    console.error(err); // eslint-disable-line no-console
  }
}

@@ -169,16 +169,52 @@
 odels';%0A
+import %7B log %7D from './core/utils';%0A
 %0A%0Afuncti
@@ -275,32 +275,73 @@
 e, reject) =%3E %7B%0A
+    log('now is initial user group...');%0A
     try %7B%0A      
@@ -575,32 +575,72 @@
 e, reject) =%3E %7B%0A
+    log('now is initial root user...');%0A
     try %7B%0A      
@@ -925,24 +925,63 @@
 eject) =%3E %7B%0A
+    log('now is initial root folder');%0A
     try %7B%0A  
@@ -1242,16 +1242,161 @@
  %7D);%0A%7D%0A%0A
+function createUploadDataFolder() %7B%0A  log('now is initial upload folder...');%0A  if (!fs.existsSync(dataDir)) %7B%0A    fs.mkdirSync(dataDir);%0A  %7D%0A%7D%0A%0A
 export d
@@ -1478,73 +1478,33 @@
     
-if (!fs.existsSync(dataDir)) %7B%0A      fs.mkdirSync(dataDir);%0A    %7D
+createUploadDataFolder();
 %0A   

Split initial function