smartfile/dist/smartfile.memory.js

129 lines
16 KiB
JavaScript
Raw Normal View History

2016-04-02 21:03:18 +00:00
"use strict";
require("typings-global");
2016-04-02 21:03:18 +00:00
var plugins = require("./smartfile.plugins");
var SmartfileInterpreter = require("./smartfile.interpreter");
2016-04-02 21:03:18 +00:00
var Readable = require("stream").Readable;
/**
* 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-09 20:51:30 +00:00
* converts file to Object
* @param fileStringArg
* @param fileTypeArg
* @returns {any|any}
*/
exports.toObject = function (fileStringArg, fileTypeArg) {
2016-06-23 16:31:55 +00:00
return SmartfileInterpreter.objectFile(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-06-23 16:31:55 +00:00
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNtYXJ0ZmlsZS5tZW1vcnkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sZ0JBQWdCLENBQUMsQ0FBQTtBQUV4QixJQUFPLE9BQU8sV0FBVyxxQkFBcUIsQ0FBQyxDQUFDO0FBQ2hELElBQU8sb0JBQW9CLFdBQVcseUJBQXlCLENBQUMsQ0FBQztBQUNqRSxJQUFJLFFBQVEsR0FBRyxPQUFPLENBQUMsUUFBUSxDQUFDLENBQUMsUUFBUSxDQUFDO0FBQzFDOzs7Ozs7R0FNRztBQUNRLG9CQUFZLEdBQUcsVUFBUyxPQUFxRCxFQUFDLE9BQW9CO0lBQXBCLHVCQUFvQixHQUFwQixhQUFvQjtJQUN6RyxJQUFJLFNBQVMsR0FBRyxFQUFFLENBQUM7SUFFbkIsRUFBRSxDQUFBLENBQUMsT0FBTyxPQUFPLEtBQUssUUFBUSxJQUFJLE9BQU8sWUFBWSxPQUFPLENBQUMsS0FBSyxDQUFDLENBQUEsQ0FBQztRQUNoRSxTQUFTLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxDQUFDO0lBQzVCLENBQUM7SUFBQyxJQUFJLENBQUMsRUFBRSxDQUFDLENBQUMsS0FBSyxDQUFDLE9BQU8sQ0FBQyxPQUFPLENBQUMsQ0FBQyxDQUFBLENBQUM7UUFDL0IsU0FBUyxHQUFHLE9BQU8sQ0FBQztJQUN4QixDQUFDO0lBQUMsSUFBSSxDQUFDLENBQUM7UUFDSixNQUFNLElBQUksS0FBSyxDQUFDLDRCQUE0QixDQUFDLENBQUM7SUFDbEQsQ0FBQztJQUVELElBQ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