'use strict';

// Load requirements
const path = require('path'),
      fs = require('fs');

// Load modules
const settings = __require('libs/settings'),
      job = __require('libs/job');

// Export promise for use
module.exports = function(file) {
  return new Promise((resolve, reject) => {

    // Variables
    let promises = [],
        tasks = [];

    // Resolve to an absolute path
    if ( ! path.isAbsolute(file) ) {
      file = path.resolve(path.join(settings.directory, file));
    }

    // Check the file exists
    if ( ! fs.existsSync(file) ) {
      return reject('Provided file "' + file + '" does not exist.');
    }

    // Load the file
    try {
      tasks = require(file);
    } catch(e) {
      return reject('Invalid JSON data');
    }

    // Check the task data is compliant
    let valid = this.validate.file(tasks);

    // If not, throw exception
    if ( ! valid.status ) {
      return reject(valid.errors.map((err) => { return err.path + '\n  ' + err.message; }));
    }

    // Loop the available tasks and pass them to task creation
    tasks.tasks.forEach((task) => {
      promises.push(this.create(task));
    });

    // Resolve with all tasks
    Promise.all(promises).then((result) => {
      return resolve(result);
    }).catch((err) => {
      return reject(err);
    });
  });
};

@@ -1120,16 +1120,31 @@
 es.push(
+() =%3E %7B return 
 this.cre
@@ -1152,16 +1152,19 @@
 te(task)
+; %7D
 );%0A    %7D
@@ -1205,29 +1205,59 @@
     
-P
+p
 romise
-.all(promises)
+s.reduce((prev, next) =%3E %7B%0A      return prev
 .the
@@ -1282,49 +1282,152 @@
     
+  
 return 
-resolve(
+next().then(Array.prototype.concat.bind(result)).catch((err) =%3E %7B return 
 result
+; %7D
 );%0A    
+ 
+ 
 %7D)
-.catch((err
+;%0A    %7D, Promise.resolve(%5B%5D)).then((result
 ) =%3E
@@ -1448,16 +1448,20 @@
 n re
-ject(err
+solve(result
 );%0A 

Make task loading sequential