"use strict";
var path = require('path');
var fs = require('fs');
var fse = require('fs-extra');
/**
 * resolve
 *
 * transform a path relative to absolute, if relative
 * path start with `graphdoc/` return absolute path to
 * plugins directory
 */
var MODULE_BASEPATH = 'graphdoc/';
function resolve(relative) {
    if (relative.slice(0, MODULE_BASEPATH.length) === MODULE_BASEPATH)
        return path.resolve(__dirname, '../../', relative.slice(MODULE_BASEPATH.length));
    return path.resolve(relative);
}
exports.resolve = resolve;
;
/**
 * toPromise execution a Node function as Promise
 */
function toPromise(func, args) {
    function execution(resolve, reject) {
        execution.args
            .push(function (err, result) { return err ? reject(err) : resolve(result); });
        (_a = execution).func.apply(_a, args);
        var _a;
    }
    execution.args = args;
    execution.func = func;
    return new Promise(execution);
}
/**
 * Execute fs.read as Promise
 */
function readFile(filename, encoding) {
    if (encoding === void 0) { encoding = 'utf8'; }
    return toPromise(fs.readFile, [filename, encoding]);
}
exports.readFile = readFile;
/**
 * Execute fs.write as Promise
 */
function writeFile(filename, data) {
    return toPromise(fs.writeFile, [filename, data]);
}
exports.writeFile = writeFile;
/**
 * remove build directory
 */
function removeBuildDirectory(buildDirectory) {
    return toPromise(fse.remove, [buildDirectory]);
}
exports.removeBuildDirectory = removeBuildDirectory;
/**
 * Create build directory from a templete directory
 */
function createBuildDirectory(buildDirectory, templateDirectory, assets) {
    // read directory
    return toPromise(fs.readdir, [templateDirectory])
        .then(function (files) { return files.filter(function (file) { return path.extname(file) !== '.mustache'; }); })
        .then(function (files) {
        var copyAll = files.map(function (file) { return toPromise(fse.copy, [path.resolve(templateDirectory, file), path.resolve(buildDirectory, file)]); });
        return Promise.all(copyAll);
    })
        .then(function (files) {
        return toPromise(fs.mkdir, [path.resolve(buildDirectory, 'assets')]);
    })
        .then(function () {
        var copyAll = assets.map(function (asset) { return toPromise(fse.copy, [asset, path.resolve(buildDirectory, 'assets', path.basename(asset))]); });
        return Promise.all(copyAll);
    });
}
exports.createBuildDirectory = createBuildDirectory;
//# sourceMappingURL=fs.js.map