'use strict';

const isStream = require('is-stream');
const Promise  = require('pinkie-promise');
const globby   = require('globby');
const tar      = require('tar-fs');
const fs       = require('fs');
const _        = require('lodash');
const dirGlob  = require('dir-glob');

/**
 * Creates a tar archive containing all files matched by the given input glob(s). The directory structure is preserved.
 * @param {string|string[]} globs
 * @param {string} [output]
 * @returns {Promise.<stream.Readable>|Promise.<string[]>} A readable stream of the tar archive if no output argument is provided,
 *      or a promise for writing the tar archive to the given output destination.
 */
const nectar = function (globs, output) {
    let writeStream = null;
    if (_.isString(output))
        writeStream = fs.createWriteStream(output);
    else if (isStream.writable(output))
        writeStream = output;
    return dirGlob(_.isArray(globs) ? globs : [globs])
        .then(globs => globby(globs, {
            nodir: true
        }))
        .then(entries => {
            const matches = entries.slice();
            let pack      = tar.pack(process.cwd(), {entries: entries});
            if (writeStream !== null) {
                pack.pipe(writeStream);
                return new Promise(resolve => pack.on('end', () => resolve(matches)));
            } else
                return pack;
        });
};

module.exports = nectar;
@@ -268,16 +268,383 @@
 -glob');
+%0Aconst path     = require('path');%0A%0A/**%0A * Make the given glob absolute based on the given working directory%0A * Takes into account the possibility of a leading %22!%22%0A * @param %7Bstring%7D glob%0A * @param %7Bstring%7D wd%0A * @returns %7Bstring%7D%0A */%0Aconst makeGlobAbsolute = (glob, wd) =%3E%0A    _.startsWith(glob, '!') ? ('!' + path.join(wd, glob.substring(1))) : path.join(wd, glob);
 %0A%0A/**%0A *
@@ -816,18 +816,98 @@
 ring
-%7D %5Boutput%5D
+%7Cstream.Writable%7D %5Boutput%5D%0A * @param %7Bstring%7D %5Bwd=process.cwd()%5D The working the directory
 %0A * 
@@ -1158,20 +1158,25 @@
 , output
+, wd
 ) %7B%0A
+%0A
     let 
@@ -1199,35 +1199,515 @@
 ll;%0A
-    if (_.isString(output))
+%0A    // Use process.cwd() as a default for the working directory if the argument was not provided%0A    if (!wd) wd = process.cwd();%0A%0A    // Check if wd is an absolute path%0A    else if (!path.isAbsolute(wd)) throw new Error('Working directory argument needs to be an absolute path');%0A%0A    // Make all globs absolute%0A    globs = _.map(globs, glob =%3E path.isAbsolute(glob) ? glob : makeGlobAbsolute(glob, wd));%0A%0A    if (_.isString(output)) %7B%0A        if (!path.isAbsolute(output)) output = path.join(wd, output);
 %0A   
@@ -1754,24 +1754,26 @@
 output);%0A   
+ %7D
  else if (is
@@ -1827,16 +1827,17 @@
 output;%0A
+%0A
     retu
@@ -1883,16 +1883,17 @@
 globs%5D)%0A
+%0A
         
@@ -1922,29 +1922,16 @@
 globs, %7B
-%0A            
 nodir: t
@@ -1937,21 +1937,13 @@
 true
-%0A        
 %7D))%0A
+%0A
     
@@ -1981,41 +1981,80 @@
     
-const matches = entries.slice(
+let relativePaths = _.map(entries, entry =%3E path.relative(wd, entry)
 );%0A
+%0A
     
@@ -2070,21 +2070,16 @@
 et pack 
-     
 = tar.pa
@@ -2110,19 +2110,34 @@
 es: 
-entries
+relativePaths.slice()
 %7D);%0A
+%0A
     
@@ -2287,22 +2287,28 @@
 resolve(
-matche
+relativePath
 s)));%0A  

Allow specification of a "working directory" parameter (fixes #11)