2016-04-02 21:03:18 +00:00
|
|
|
/// <reference path="./typings/main.d.ts" />
|
|
|
|
"use strict";
|
|
|
|
var plugins = require("./smartfile.plugins");
|
2016-04-07 21:38:24 +00:00
|
|
|
var SmartfileInterpreter = require("./smartfile.interpreter");
|
2016-04-02 21:03:18 +00:00
|
|
|
var Readable = require("stream").Readable;
|
|
|
|
/**
|
2016-04-07 21:38:24 +00:00
|
|
|
* allows you to create a gulp stream
|
|
|
|
* from String, from an Array of Strings, from Vinyl File, from an Array of VinylFiles
|
2016-04-02 21:03:18 +00:00
|
|
|
* @param fileArg
|
|
|
|
* @returns stream.Readable
|
|
|
|
* @TODO: make it async;
|
|
|
|
*/
|
|
|
|
exports.toGulpStream = function (fileArg, baseArg) {
|
2016-04-04 13:44:00 +00:00
|
|
|
if (baseArg === void 0) { baseArg = "/"; }
|
2016-04-02 21:03:18 +00:00
|
|
|
var fileArray = [];
|
|
|
|
if (typeof fileArg === "string" || fileArg instanceof plugins.vinyl) {
|
|
|
|
fileArray.push(fileArg);
|
|
|
|
}
|
|
|
|
else if (Array.isArray(fileArg)) {
|
|
|
|
fileArray = fileArg;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error("fileArg has unknown format");
|
|
|
|
}
|
|
|
|
var vinylFileArray = []; //we want to have an array of vinylFiles
|
|
|
|
for (var fileIndexArg in fileArray) {
|
|
|
|
var file = fileArray[fileIndexArg];
|
|
|
|
file instanceof plugins.vinyl ?
|
|
|
|
vinylFileArray.push(file) :
|
2016-04-04 13:44:00 +00:00
|
|
|
vinylFileArray.push(exports.toVinylFileSync(file, { filename: fileIndexArg, base: baseArg }));
|
2016-04-02 21:03:18 +00:00
|
|
|
}
|
|
|
|
;
|
|
|
|
var stream = new Readable({ objectMode: true });
|
|
|
|
for (var vinylFileIndexArg in vinylFileArray) {
|
|
|
|
var vinylFile = vinylFileArray[vinylFileIndexArg];
|
|
|
|
stream.push(vinylFile);
|
|
|
|
}
|
|
|
|
;
|
|
|
|
stream.push(null); //signal end of stream;
|
|
|
|
return stream;
|
|
|
|
};
|
2016-04-07 21:38:24 +00:00
|
|
|
/**
|
2016-04-09 20:51:30 +00:00
|
|
|
* converts file to Object
|
2016-04-07 21:38:24 +00:00
|
|
|
* @param fileStringArg
|
|
|
|
* @param fileTypeArg
|
|
|
|
* @returns {any|any}
|
|
|
|
*/
|
|
|
|
exports.toObject = function (fileStringArg, fileTypeArg) {
|
|
|
|
return SmartfileInterpreter(fileStringArg, fileTypeArg);
|
|
|
|
};
|
2016-04-02 21:03:18 +00:00
|
|
|
/**
|
|
|
|
* takes a string and converts it to vinyl file
|
|
|
|
* @param fileArg
|
|
|
|
* @param optionsArg
|
|
|
|
*/
|
|
|
|
exports.toVinylFileSync = function (fileArg, optionsArg) {
|
|
|
|
optionsArg ? void (0) : optionsArg = { filename: "vinylfile", base: "/" };
|
|
|
|
optionsArg.filename ? void (0) : optionsArg.filename = "vinylfile";
|
|
|
|
optionsArg.base ? void (0) : optionsArg.base = "/";
|
|
|
|
optionsArg.relPath ? void ("0") : optionsArg.relPath = "";
|
|
|
|
var vinylFile = new plugins.vinyl({
|
|
|
|
base: optionsArg.base,
|
|
|
|
path: plugins.path.join(optionsArg.base, optionsArg.relPath, optionsArg.filename),
|
|
|
|
contents: new Buffer(fileArg)
|
|
|
|
});
|
|
|
|
return vinylFile;
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* takes a string array and some options and returns a vinylfile array
|
|
|
|
* @param arrayArg
|
|
|
|
* @param optionsArg
|
|
|
|
*/
|
|
|
|
exports.toVinylArraySync = function (arrayArg, optionsArg) {
|
|
|
|
var vinylArray = [];
|
|
|
|
for (var stringIndexArg in arrayArg) {
|
|
|
|
var myString = arrayArg[stringIndexArg];
|
|
|
|
vinylArray.push(exports.toVinylFileSync(myString, optionsArg));
|
|
|
|
}
|
|
|
|
return vinylArray;
|
|
|
|
};
|
2016-04-04 13:44:00 +00:00
|
|
|
/**
|
|
|
|
* takes a vinylFile object and converts it to String
|
|
|
|
*/
|
|
|
|
exports.toStringSync = function (fileArg) {
|
|
|
|
return fileArg.contents.toString("utf8");
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* writes string or vinyl file to disk.
|
|
|
|
* @param fileArg
|
|
|
|
* @param fileNameArg
|
|
|
|
* @param fileBaseArg
|
|
|
|
*/
|
|
|
|
exports.toFs = function (fileArg, optionsArg) {
|
|
|
|
var done = plugins.q.defer();
|
|
|
|
//function checks to abort if needed
|
|
|
|
if (!fileArg || !optionsArg || !(typeof optionsArg.fileName === "string"))
|
|
|
|
throw new Error("expected a valid arguments");
|
|
|
|
if (!(typeof optionsArg.filePath === "string"))
|
|
|
|
optionsArg.filePath = "/";
|
|
|
|
var filePath = plugins.path.join(optionsArg.filePath, optionsArg.fileName);
|
|
|
|
var fileString;
|
2016-04-02 21:03:18 +00:00
|
|
|
if (fileArg instanceof plugins.vinyl) {
|
2016-04-04 13:44:00 +00:00
|
|
|
fileString = exports.toStringSync(fileArg);
|
|
|
|
}
|
|
|
|
else if (typeof fileArg === "string") {
|
|
|
|
fileString = fileArg;
|
2016-04-02 21:03:18 +00:00
|
|
|
}
|
2016-04-04 13:44:00 +00:00
|
|
|
plugins.fs.writeFile(filePath, fileString, "utf8", done.resolve);
|
|
|
|
return done.promise;
|
2016-04-02 21:03:18 +00:00
|
|
|
};
|
2016-04-04 13:44:00 +00:00
|
|
|
exports.toFsSync = function (fileArg, optionsArg) {
|
|
|
|
//function checks to abort if needed
|
|
|
|
if (!fileArg || !optionsArg || !(typeof optionsArg.fileName === "string"))
|
|
|
|
throw new Error("expected a valid arguments");
|
|
|
|
if (!(typeof optionsArg.filePath === "string"))
|
|
|
|
optionsArg.filePath = "/";
|
|
|
|
var filePath = plugins.path.join(optionsArg.filePath, optionsArg.fileName);
|
|
|
|
var fileString;
|
|
|
|
if (fileArg instanceof plugins.vinyl) {
|
|
|
|
fileString = exports.toStringSync(fileArg);
|
|
|
|
}
|
|
|
|
else if (typeof fileArg === "string") {
|
|
|
|
fileString = fileArg;
|
|
|
|
}
|
|
|
|
plugins.fs.writeFileSync(filePath, fileString, "utf8");
|
2016-04-02 21:03:18 +00:00
|
|
|
};
|
|
|
|
|
2016-05-01 21:19:54 +00:00
|
|
|
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNtYXJ0ZmlsZS5tZW1vcnkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsNENBQTRDOztBQUU1QyxJQUFPLE9BQU8sV0FBVyxxQkFBcUIsQ0FBQyxDQUFDO0FBQ2hELElBQU8sb0JBQW9CLFdBQVcseUJBQXlCLENBQUMsQ0FBQztBQUNqRSxJQUFJLFFBQVEsR0FBRyxPQUFPLENBQUMsUUFBUSxDQUFDLENBQUMsUUFBUSxDQUFDO0FBQzFDOzs7Ozs7R0FNRztBQUNRLG9CQUFZLEdBQUcsVUFBUyxPQUFxRCxFQUFDLE9BQW9CO0lBQXBCLHVCQUFvQixHQUFwQixhQUFvQjtJQUN6RyxJQUFJLFNBQVMsR0FBRyxFQUFFLENBQUM7SUFFbkIsRUFBRSxDQUFBLENBQUMsT0FBTyxPQUFPLEtBQUssUUFBUSxJQUFJLE9BQU8sWUFBWSxPQUFPLENBQUMsS0FBSyxDQUFDLENBQUEsQ0FBQztRQUNoRSxTQUFTLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxDQUFDO0lBQzVCLENBQUM7SUFBQyxJQUFJLENBQUMsRUFBRSxDQUFDLENBQUMsS0FBSyxDQUFDLE9BQU8sQ0FBQyxPQUFPLENBQUMsQ0FBQyxDQUFBLENBQUM7UUFDL0IsU0FBUyxHQUFHLE9BQU8sQ0FBQztJQUN4QixDQUFDO0lBQUMsSUFBSSxDQUFDLENBQUM7UUFDSixNQUFNLElBQUksS0FBSyxDQUFDLDRCQUE0QixDQUFDLENBQUM7SUFDbEQsQ0FBQztJQUVELElBQUksY0FBYyxHQUFtQixFQUFFLENBQ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