smartfile/dist/smartfile.memory.js

124 lines
8.9 KiB
JavaScript

"use strict";
require("typings-global");
const plugins = require("./smartfile.plugins");
const SmartfileInterpreter = require("./smartfile.interpreter");
let 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
* @param fileArg
* @returns stream.Readable
* @TODO: make it async;
*/
exports.toGulpStream = function (fileArg, baseArg = "/") {
let 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");
}
let vinylFileArray = []; //we want to have an array of vinylFiles
for (let fileIndexArg in fileArray) {
let file = fileArray[fileIndexArg];
file instanceof plugins.vinyl ?
vinylFileArray.push(file) :
vinylFileArray.push(exports.toVinylFileSync(file, { filename: fileIndexArg, base: baseArg }));
}
;
let stream = new Readable({ objectMode: true });
for (let vinylFileIndexArg in vinylFileArray) {
let vinylFile = vinylFileArray[vinylFileIndexArg];
stream.push(vinylFile);
}
;
stream.push(null); //signal end of stream;
return stream;
};
/**
* converts file to Object
* @param fileStringArg
* @param fileTypeArg
* @returns {any|any}
*/
exports.toObject = function (fileStringArg, fileTypeArg) {
return SmartfileInterpreter.objectFile(fileStringArg, fileTypeArg);
};
/**
* 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 = "";
let 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) {
let vinylArray = [];
for (let stringIndexArg in arrayArg) {
let myString = arrayArg[stringIndexArg];
vinylArray.push(exports.toVinylFileSync(myString, optionsArg));
}
return vinylArray;
};
/**
* 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 (fileContentArg, filePathArg) {
let done = plugins.Q.defer();
//function checks to abort if needed
if (!fileContentArg || !filePathArg)
throw new Error("expected valid arguments");
// prepare actual write action
let fileString;
let filePath = filePathArg;
if (fileContentArg instanceof plugins.vinyl) {
fileString = exports.toStringSync(fileContentArg);
}
else if (typeof fileContentArg === "string") {
fileString = fileContentArg;
}
plugins.fs.writeFile(filePath, fileString, "utf8", done.resolve);
return done.promise;
};
exports.toFsSync = function (fileArg, filePathArg) {
//function checks to abort if needed
if (!fileArg || !filePathArg)
throw new Error("expected a valid arguments");
// prepare actual write action
let fileString;
let filePath = filePathArg;
if (fileArg instanceof plugins.vinyl) {
fileString = exports.toStringSync(fileArg);
}
else if (typeof fileArg === "string") {
fileString = fileArg;
}
plugins.fs.writeFileSync(filePath, fileString, "utf8");
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLm1lbW9yeS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0ZmlsZS5tZW1vcnkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sZ0JBQWdCLENBQUMsQ0FBQTtBQUV4QixNQUFPLE9BQU8sV0FBVyxxQkFBcUIsQ0FBQyxDQUFDO0FBQ2hELE1BQU8sb0JBQW9CLFdBQVcseUJBQXlCLENBQUMsQ0FBQztBQUVqRSxJQUFJLFFBQVEsR0FBRyxPQUFPLENBQUMsUUFBUSxDQUFDLENBQUMsUUFBUSxDQ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