smartfile/dist/smartfile.memory.js

124 lines
8.9 KiB
JavaScript
Raw Normal View History

2016-04-02 21:03:18 +00:00
"use strict";
require("typings-global");
2016-07-17 15:34:15 +00:00
const plugins = require("./smartfile.plugins");
const SmartfileInterpreter = require("./smartfile.interpreter");
let Readable = require("stream").Readable;
2016-04-02 21:03:18 +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;
*/
2016-07-17 15:34:15 +00:00
exports.toGulpStream = function (fileArg, baseArg = "/") {
let fileArray = [];
2016-04-02 21:03:18 +00:00
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");
}
2016-07-17 15:34:15 +00:00
let vinylFileArray = []; //we want to have an array of vinylFiles
for (let fileIndexArg in fileArray) {
let file = fileArray[fileIndexArg];
2016-04-02 21:03:18 +00:00
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
}
;
2016-07-17 15:34:15 +00:00
let stream = new Readable({ objectMode: true });
for (let vinylFileIndexArg in vinylFileArray) {
let vinylFile = vinylFileArray[vinylFileIndexArg];
2016-04-02 21:03:18 +00:00
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 = "";
2016-07-17 15:34:15 +00:00
let vinylFile = new plugins.vinyl({
2016-04-02 21:03:18 +00:00
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) {
2016-07-17 15:34:15 +00:00
let vinylArray = [];
for (let stringIndexArg in arrayArg) {
let myString = arrayArg[stringIndexArg];
2016-04-02 21:03:18 +00:00
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
*/
2016-06-24 01:36:51 +00:00
exports.toFs = function (fileContentArg, filePathArg) {
2016-07-17 15:34:15 +00:00
let done = plugins.Q.defer();
2016-04-04 13:44:00 +00:00
//function checks to abort if needed
2016-06-24 01:36:51 +00:00
if (!fileContentArg || !filePathArg)
throw new Error("expected valid arguments");
// prepare actual write action
2016-07-17 15:34:15 +00:00
let fileString;
let filePath = filePathArg;
2016-06-24 01:36:51 +00:00
if (fileContentArg instanceof plugins.vinyl) {
fileString = exports.toStringSync(fileContentArg);
2016-04-04 13:44:00 +00:00
}
2016-06-24 01:36:51 +00:00
else if (typeof fileContentArg === "string") {
fileString = fileContentArg;
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-06-24 01:36:51 +00:00
exports.toFsSync = function (fileArg, filePathArg) {
2016-04-04 13:44:00 +00:00
//function checks to abort if needed
2016-06-24 01:36:51 +00:00
if (!fileArg || !filePathArg)
2016-04-04 13:44:00 +00:00
throw new Error("expected a valid arguments");
2016-06-24 01:36:51 +00:00
// prepare actual write action
2016-07-17 15:34:15 +00:00
let fileString;
let filePath = filePathArg;
2016-04-04 13:44:00 +00:00
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-07-17 15:34:15 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLm1lbW9yeS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0ZmlsZS5tZW1vcnkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sZ0JBQWdCLENBQUMsQ0FBQTtBQUV4QixNQUFPLE9BQU8sV0FBVyxxQkFBcUIsQ0FBQyxDQUFDO0FBQ2hELE1BQU8sb0JBQW9CLFdBQVcseUJBQXlCLENBQUMsQ0FBQztBQUVqRSxJQUFJLFFBQVEsR0FBRyxPQUFPLENBQUMsUUFBUSxDQUFDLENBQUMsUUFBUSxDQUFDO0FBQzFDOzs7Ozs7R0FNRztBQUNRLG9CQUFZLEdBQUcsVUFBUyxPQUFxRCxFQUFDLE9BQU8sR0FBVSxHQUFHO0lBQ3pHLElBQUksU0FBUyxHQUFHLEVBQUUsQ0FBQztJQUVuQixFQUFFLENBQUEsQ0FBQyxPQUFPLE9BQU8sS0FBSyxRQUFRLElBQUksT0FBTyxZQUFZLE9BQU8sQ0FBQyxLQUFLLENBQUMsQ0FBQSxDQUFDO1FBQ2hFLFNBQVMsQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDLENBQUM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