smartfile/dist/smartfile.memory.js

129 lines
9.1 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("typings-global");
const plugins = require("./smartfile.plugins");
const SmartfileInterpreter = require("./smartfile.interpreter");
let vinyl = require('vinyl');
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' || vinyl.isVinyl(fileArg)) {
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 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 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.vinylToStringSync = 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 (vinyl.isVinyl(fileContentArg)) {
let fileContentArg2 = fileContentArg;
fileString = exports.vinylToStringSync(fileContentArg2);
}
else if (typeof fileContentArg === 'string') {
fileString = fileContentArg;
}
plugins.fsExtra.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 (typeof fileArg !== 'string') {
fileString = exports.vinylToStringSync(fileArg);
}
else if (typeof fileArg === 'string') {
fileString = fileArg;
}
plugins.fsExtra.writeFileSync(filePath, fileString, 'utf8');
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLm1lbW9yeS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0ZmlsZS5tZW1vcnkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSwwQkFBdUI7QUFFdkIsK0NBQStDO0FBQy9DLGdFQUFnRTtBQUNoRSxJQUFJLEtBQUssR0FBRyxPQUFPLENBQUMsT0FBTyxDQUFDLENBQUE7QUFRNUIsSUFBSSxRQUFRLEdBQ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