128 lines
9.1 KiB
JavaScript
128 lines
9.1 KiB
JavaScript
"use strict";
|
|
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLm1lbW9yeS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0ZmlsZS5tZW1vcnkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLDBCQUF1QjtBQUV2QiwrQ0FBK0M7QUFDL0MsZ0VBQWdFO0FBQ2hFLElBQUksS0FBSyxHQUFHLE9BQU8sQ0FBQyxPQUFPLENBQUMsQ0FBQTtBQVE1QixJQUFJLFFBQVEsR0FBRyxPQUFPLENBQUMsUUFBUSxDQUFDLENBQUMsUUFBUSxDQUFBO0FBRXpDOzs7Ozs7R0FNRztBQUNRLFFBQUEsWUFBWSxHQUFHLFVBQVMsT0FBZ0QsRUFBQyxVQUFrQixHQUFHO0lBQ3JHLElBQUksU0FBUyxHQUFHLEVBQUUsQ0FBQTtJQUVsQixFQUFFLENBQUMsQ0FBQyxPQUFPLE9BQU8sS0FBSyxRQUFRLElBQUksS0FBSyxDQUFDLE9BQU8sQ0FBQyxPQUFPLENBQUMsQ0FBQyxDQUFDLENBQUM7UUFDeEQsU0FBUyxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsQ0FBQTtJQUMzQixDQUFDO0lBQUMsSUFBSSxDQUFDLEVBQUUsQ0FBQyxDQUFDLEtBQUssQ0FBQyxPQUFPLENBQUMsT0FBTyxDQUFDLENBQUMsQ0FBQyxDQUFDO1FBQ2hDLFNBQVMsR0FBRyxPQUFPLENBQUE7SUFDdkIsQ0FBQztJQUFDLElBQUksQ0FBQyxDQUFDO1FB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