#!/usr/bin/env node
'use strict';

const path = require('path');
const promise = require('es6-promise');
const promisify = require('es6-promisify');
const promisifyAll = require('es6-promisify-all');
const fs = promisifyAll(require('fs-extra'));
const archiver = require('archiver');

const pkg = require('../package.json');
const version = pkg.version;
const name = pkg.name;

const from = path.resolve(__dirname, '../dist');
const tmp = path.resolve(__dirname, '../_dist');
const to = path.join(path.resolve(__dirname, '../dist'), name, version);
const distName = path.join(path.resolve(__dirname, '..'), name + '-' + version);

const createArchive = (archive, src, dst) =>
{
	return new Promise((resolve, reject) =>
	{
		const output = fs.createWriteStream(dst);

		output.on('close', () =>
		{
			resolve();
		});

		archive.on('error', (err) =>
		{
			reject(err);
		});

		archive.pipe(output);

		archive
			.bulk([
				{ expand: true, cwd: src, src: ['**/*.*'] }
			])
			.finalize();
	})
};

const createDistTar = () =>
{
	const archive = archiver('tar', {
		gzip: true,
		gzipOptions: {
			level: 1
		}
	});

	return createArchive(archive, to, distName + '.tar.gz');
};

const createDistZip = () =>
{
	const archive = archiver('zip', {});
	return createArchive(archive, to, distName + '.zip');
};

const createEs6Zip = () =>
{
	const archive = archiver('zip', {});
	return createArchive(archive, path.join(to, 'es6'), path.join(to, name + '-es6.zip'));
};

Promise.resolve()
	.then(() => fs.moveAsync(from, tmp))
	.then(() => fs.moveAsync(tmp, to))
	.then(createDistTar)
	.then(createDistZip)
	.then(createEs6Zip)
	.then(() => fs.removeAsync(path.join(to, 'es6')))
	.catch(err =>
	{
		console.log(err);
		process.exit(1);
	});

@@ -68,133 +68,36 @@
 nst 
-promise = require('es6-promise');%0Aconst promisify = require('es6-promisify');%0Aconst promisifyAll = require('es6-promisify-all
+archiver = require('archiver
 ');%0A
@@ -111,21 +111,8 @@
 s = 
-promisifyAll(
 requ
@@ -122,257 +122,124 @@
 ('fs
--extra')
+'
 );%0A
+%0A
 const 
-archiver = require('archiver');%0A%0Aconst pkg = require('../package.json');%0Aconst version = pkg.version;%0Aconst name = pkg.name;%0A%0Aconst from = path.resolve(__dirname, '../dist');%0Aconst tmp = path.resolve(__dirname, '../_dist');%0Aconst to
+%7B name, version %7D = require('../package.json');%0Aconst src = path.resolve(__dirname, '../dist');%0Aconst dst
  = p
@@ -278,111 +278,32 @@
  '..
-/dist
 '), 
+%60$%7B
 name
-, version);%0Aconst distName = path.join(path.resolve(__dirname, '..'), name + '-' + 
+%7D-$%7B
 version
+%7D%60
 );%0A%0A
@@ -343,26 +343,28 @@
 rc, 
-dst) =%3E%0A%7B%0A%09return 
+compressedFile) =%3E%0A%09
 new 
@@ -391,18 +391,17 @@
 ject) =%3E
-%0A%09
+ 
 %7B%0A%09%09cons
@@ -436,15 +436,25 @@
 eam(
-dst
+compressedFile
 );%0A
-%0A
 %09%09ou
@@ -475,24 +475,17 @@
 ', () =%3E
-%0A%09%09%7B%0A%09%09%09
+ 
 resolve(
@@ -485,21 +485,16 @@
 esolve()
-;%0A%09%09%7D
 );%0A%0A%09%09ar
@@ -523,16 +523,9 @@
 ) =%3E
-%0A%09%09%7B%0A%09%09%09
+ 
 reje
@@ -535,17 +535,11 @@
 err)
-;%0A%09%09%7D
 );%0A
-%0A
 %09%09ar
@@ -558,17 +558,16 @@
 utput);%0A
-%0A
 %09%09archiv
@@ -571,77 +571,38 @@
 hive
-%0A%09%09%09.bulk(%5B%0A%09%09%09%09%7B expand: true, cwd: src, src: %5B'**/*.*'%5D %7D%0A%09%09%09%5D)%0A%09%09%09
+.directory(src, '');%0A%09%09archive
 .fin
@@ -613,18 +613,16 @@
 e();%0A%09%7D)
-%0A%7D
 ;%0A%0Aconst
@@ -643,36 +643,33 @@
  = () =%3E
-%0A%7B%0A%09const a
+ createA
 rchive
- = 
+(%0A%09
 archiver
@@ -677,19 +677,17 @@
 'tar', %7B
-%0A%09%09
+ 
 gzip: tr
@@ -689,19 +689,17 @@
 p: true,
-%0A%09%09
+ 
 gzipOpti
@@ -708,90 +708,48 @@
 s: %7B
-%0A%09%09%09
+ 
 level: 
-1%0A%09%09%7D%0A%09%7D);%0A%0A%09return createArchive(archive, to, distName + '
+9 %7D %7D),%0A%09src,%0A%09%60$%7Bdst%7D
 .tar.gz
-');%0A%7D
+%60%0A)
 ;%0A%0Ac
@@ -778,473 +778,140 @@
 ) =%3E
-%0A%7B%0A%09const archive = archiver('zip', %7B%7D);%0A%09return createArchive(archive, to, distName + '.zip');%0A%7D;%0A%0Aconst createEs6Zip = () =%3E%0A%7B%0A%09const archive = archiver('zip', %7B%7D);%0A%09return createArchive(archive, path.join(to, 'es6'), path.join(to, name + '-es6.zip'));%0A%7D;%0A%0APromise.resolve()%0A%09.then(() =%3E fs.moveAsync(from, tmp))%0A%09.then(() =%3E fs.moveAsync(tmp, to))%0A%09.then(createDistTar)%0A%09.then(createDistZip)%0A%09.then(createEs6Zip)%0A%09.then(() =%3E fs.removeAsync(path.join(to, 'es6'))
+ createArchive(%0A%09archiver('zip', %7B level: 9 %7D),%0A%09src,%0A%09%60$%7Bdst%7D.zip%60%0A);%0A%0APromise.resolve()%0A%09.then(createDistTar)%0A%09.then(createDistZip
 )%0A%09.

Refactor package-dist to make use of archiver 2.x functionality