'use strict'
/**
 * Buildup script for server
 */


/**
 * Module dependencies
 */
const fs = require('fs');
const path = require('path');
const YAML = require('yaml-js');
const Log = require('./logger.js').Logger;
let logger;
let that = this;
/**
 * Server class
 * Creates a new server
 *
 * @param dir {String} Directory of build
 * @param options {Options} Options
 * @class
 */
function Server(dir, options) {
  logger = new Log('buildup');
  this.dir = path.resolve(dir);
  logger.info('New Server created.')
}

/**
 * Register the server's name
 * @param name {String} Name of server
 */
Server.prototype.nameServer = (name) => {
  this.name = name
  if (typeof process.env.DEBUG !== 'undefined' && ~process.env.DEBUG.indexOf('buildup')) {
    logger.debug('Server\'s name is %o', name)
  } else {
    logger.info(`Server is called ${name}`)
  }
};

/**
 * Load config
 * @param config {String} Config from a file
 * @param options {Object} Options
 */
Server.prototype.loadConfig = function (config, options) {
  logger.debug('Loading config...')
  // Is server named?
  if (typeof options !== 'undefined' && typeof options.file !== 'undefined' && options.file === true) {
    // file
    logger.debug('Loading config from file %o', config)
    if (path.resolve(config) !== path.normalize(config)) {
      config = path.join(this.dir, config);
      that.configFile = config;
    } else {
      that.configFile = config;
    }
    if (options.type === 'yml' || options.type === 'yaml' || options.type === 'YAML' || options.type === 'YML') {
      logger.debug('File type: YAML')
      that.config = YAML.load(fs.readFileSync(that.configFile));
      return that.config;
    } else if (options.type === 'json' || options.type === 'JSON') {
      logger.debug('File type: JSON')
      that.config = require(that.configFile);
      return that.config;
    }
  } else {
    // Not a file
    if (typeof config !== 'object') {
      const err = new TypeError('Config was not an object')
      logger.err(err, 'the config loader params.', 'Please supply an object instead of a string for the method Server.loadConfig()')
      return err;
    } else {
      that.config = config;
      return that.config;
    }
  }
};
/**
 * Load plugins
*/
Server.prototype.loadPlugins = () => {
  logger.debug('Loading plugins...');
  // Load core
  that.tasklist = {};
  logger.debug('Loading core plugins...');
  const returnval = require('./core/build.js')()
  if (typeof returnval.multiTask !== 'undefined') {
    if (typeof returnval.tasks === 'undefined') {
      logger.err(new SyntaxError(`No tasks from multi-task \'buildup-${returnval.name}\'`), `buildup`, `Please ask the author of \'buildup-${returnval.name}\' to add tasks`)
    } else if (typeof returnval.tasks[0] === 'undefined') {
      logger.err(new TypeError(`Tasks list was a ${typeof returnval.tasks} instead of an array in the plugin \'buildup-${returnval.name}\'`)
        , `buildup`
        , `Please ask the author of \'buildup-${returnval.name}\' to add tasks as an array`)
    } else {
      for (var i = 0; i < returnval.tasks.length; i++) {
        if (typeof that.tasklist[returnval.tasks[i].task] !== 'undefined') {
          logger.err(new Error(`Task \'${returnval.tasks[i].task}\' already exists!`)
            , `buildup`
            , `Please remove one of the plugins that conflict (\'buildup-${returnval.name}\' and \'buildup-${that.tasklist[returnval.tasks[i].task].from}\')`)
        }
        that.tasklist[returnval.tasks[i].task] = returnval.tasks[i]
        that.tasklist[returnval.tasks[i].task].from = `${returnval.name}`
      }
    }
    logger.debug('Loaded plugin %o.', returnval.name)
    logger.debug('Done loading core plugins.')
  }
  if (typeof that.config.plugins !== 'undefined') {
    logger.debug('Loading npm plugins...')
    for (var i = 0; i < that.config.plugins.length; i++) {
      // Vars
      const returnval = require(that.config.plugins[i])()
      const plugin = that.config.plugins[i];
      // Multi task?
      if (typeof returnval.multiTask !== 'undefined') {
        // Tasks defined?
        if (typeof returnval.tasks === 'undefined') {
          logger.err(new SyntaxError(`No tasks from multi-task \'${plugin}\'`)
            , `the plugin ${plugin}`, `Please ask the author of \'${plugin}\' to add tasks`)
        } else if (typeof returnval.tasks[0] === 'undefined') {
          logger.err(new TypeError(`Tasks list was a ${typeof returnval.tasks} instead of an array in the plugin \'${plugin}\'`)
            , `the plugin ${plugin}`
            , `Please ask the author of \'${plugin}\' to add tasks as an array`)
        } else {
          for (var i = 0; i < returnval.tasks.length; i++) {
            if (typeof that.tasklist[returnval.tasks[i].task] !== 'undefined') {
              logger.err(new Error(`Task \'${returnval.tasks[i].task}\' already exists!`)
                , `the plugin ${plugin}`
                , `Please remove one of the plugins that conflict (\'${plugin}\' and \'buildup-${that.tasklist[returnval.tasks[i].task].from}\')`)
            }
            that.tasklist[returnval.tasks[i].task] = returnval.tasks[i]
            that.tasklist[returnval.tasks[i].task].from = `${returnval.name}`
          }
        }
        logger.debug('Loaded plugin %o.', plugin)
        logger.debug('Done loading npm plugins.')
      }
    }
  } else {
    logger.debug('Done loading plugins.')
  }
};

/**
 * Run a task
 *
 * @param task {String Array} Task to run(s)
*/
Server.prototype.runTask = function (task) {
  // Check type
  logger.info(`Running task \'${task}\'`)
  if (typeof task !== 'array' && typeof task !== 'string') {
    logger.err(
      new Error(`Task param was not an array or string in function Server.runTask(), but a ${typeof task}!`),
      'the params supplied to Server.runTask() or buildup',
      'Please change the params, or file an issue at https://github.com/Gum-Joe/buildup'
    )
  } else {
    // Load task
    let taskconfig;
    if (that.config.tasks.hasOwnProperty(task)) {
      logger.debug('Found the correct task!')
      taskconfig = that.config.tasks[task]
    } else {
      logger.debug(`Finding task with { type: \'${task}\' }...`)
      for (var i in that.config.tasks) {
        if (that.config.tasks[i].hasOwnProperty('type') && that.config.tasks[i].type === task) {
          taskconfig = that.config.tasks[i];
        }
      }
    }
    // If no config, error
    if (!taskconfig) {
      logger.info(`Task ${task} was not found!`)
      logger.err(
        new ReferenceError(
          `Could not find task ${task}!`,
          `the task supplied`,
          `Please check that the task exists`
        )
      )
    }
  }
};

// test
var ser = new Server('./servertest', { quite: false })
ser.nameServer('buildtest')
ser.loadConfig(`buildup.json`, { file: true, type: 'json' })
ser.loadPlugins()
ser.runTask('buildt')
module.exports = {
    Server: Server
};

@@ -6899,17 +6899,16 @@
 k('build
-t
 ')%0Amodul

Fix testing