smartfile/dist/smartfile.memory.js

129 lines
9.1 KiB
JavaScript
Raw Normal View History

2016-04-02 21:03:18 +00:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("typings-global");
2016-07-17 15:34:15 +00:00
const plugins = require("./smartfile.plugins");
const SmartfileInterpreter = require("./smartfile.interpreter");
let vinyl = require('vinyl');
2016-09-20 15:56:49 +00:00
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-09-20 15:56:49 +00:00
exports.toGulpStream = function (fileArg, baseArg = '/') {
2016-07-17 15:34:15 +00:00
let fileArray = [];
2017-01-20 23:47:48 +00:00
if (typeof fileArg === 'string' || vinyl.isVinyl(fileArg)) {
2016-04-02 21:03:18 +00:00
fileArray.push(fileArg);
}
else if (Array.isArray(fileArg)) {
fileArray = fileArg;
}
else {
2016-09-20 15:56:49 +00:00
throw new Error('fileArg has unknown format');
2016-04-02 21:03:18 +00:00
}
2016-09-20 15:56:49 +00:00
let vinylFileArray = []; // we want to have an array of vinylFiles
2016-07-17 15:34:15 +00:00
for (let fileIndexArg in fileArray) {
let file = fileArray[fileIndexArg];
file instanceof vinyl ?
2016-04-02 21:03:18 +00:00
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);
}
;
2016-09-20 15:56:49 +00:00
stream.push(null); // signal end of stream;
2016-04-02 21:03:18 +00:00
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) {
2016-09-20 15:56:49 +00:00
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 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.vinylToStringSync = function (fileArg) {
2016-09-20 15:56:49 +00:00
return fileArg.contents.toString('utf8');
2016-04-04 13:44:00 +00:00
};
/**
* 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-09-17 21:11:44 +00:00
let done = plugins.q.defer();
2016-09-20 15:56:49 +00:00
// function checks to abort if needed
if (!fileContentArg || !filePathArg) {
throw new Error('expected 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;
2017-01-20 23:47:48 +00:00
if (vinyl.isVinyl(fileContentArg)) {
let fileContentArg2 = fileContentArg;
fileString = exports.vinylToStringSync(fileContentArg2);
2016-04-04 13:44:00 +00:00
}
2016-09-20 15:56:49 +00:00
else if (typeof fileContentArg === 'string') {
2016-06-24 01:36:51 +00:00
fileString = fileContentArg;
2016-04-02 21:03:18 +00:00
}
2016-09-20 15:56:49 +00:00
plugins.fsExtra.writeFile(filePath, fileString, 'utf8', done.resolve);
2016-04-04 13:44:00 +00:00
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-09-20 15:56:49 +00:00
// function checks to abort if needed
if (!fileArg || !filePathArg) {
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;
if (typeof fileArg !== 'string') {
fileString = exports.vinylToStringSync(fileArg);
2016-04-04 13:44:00 +00:00
}
2016-09-20 15:56:49 +00:00
else if (typeof fileArg === 'string') {
2016-04-04 13:44:00 +00:00
fileString = fileArg;
}
2016-09-20 15:56:49 +00:00
plugins.fsExtra.writeFileSync(filePath, fileString, 'utf8');
2016-04-02 21:03:18 +00:00
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLm1lbW9yeS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0ZmlsZS5tZW1vcnkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSwwQkFBdUI7QUFFdkIsK0NBQStDO0FBQy9DLGdFQUFnRTtBQUNoRSxJQUFJLEtBQUssR0FBRyxPQUFPLENBQUMsT0FBTyxDQUFDLENBQUE7QUFRNUIsSUFBSSxRQUFRLEdBQUcsT0FBTyxDQUFDLFFBQVEsQ0FBQyxDQUFDLFFBQVEsQ0FBQTtBQUV6Qzs7Ozs7O0dBTUc7QUFD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