2016-04-02 21:03:18 +00:00
|
|
|
"use strict";
|
2016-05-23 06:15:47 +00:00
|
|
|
require("typings-global");
|
2016-07-17 15:34:15 +00:00
|
|
|
const plugins = require("./smartfile.plugins");
|
|
|
|
const SmartfileInterpreter = require("./smartfile.interpreter");
|
2016-09-20 15:56:49 +00:00
|
|
|
let Readable = require('stream').Readable;
|
2016-04-02 21:03:18 +00:00
|
|
|
/**
|
2016-04-07 21:38:24 +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 = [];
|
2016-09-20 15:56:49 +00:00
|
|
|
if (typeof fileArg === 'string' || fileArg instanceof plugins.vinyl) {
|
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];
|
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);
|
|
|
|
}
|
|
|
|
;
|
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-07 21:38:24 +00:00
|
|
|
/**
|
2016-04-09 20:51:30 +00:00
|
|
|
* converts file to Object
|
2016-04-07 21:38:24 +00:00
|
|
|
* @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-07 21:38:24 +00:00
|
|
|
};
|
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 = '';
|
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) {
|
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;
|
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-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;
|
2016-04-04 13:44:00 +00:00
|
|
|
if (fileArg instanceof plugins.vinyl) {
|
|
|
|
fileString = exports.toStringSync(fileArg);
|
|
|
|
}
|
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
|
|
|
};
|
2016-10-27 16:55:18 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLm1lbW9yeS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0ZmlsZS5tZW1vcnkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLDBCQUF1QjtBQUV2QiwrQ0FBK0M7QUFDL0MsZ0VBQWdFO0FBRWhFLElBQUksUUFBUSxHQUFHLE9BQU8sQ0FBQyxRQUFRLENBQUMsQ0FBQyxRQUFRLENBQUE7QUFHekM7Ozs7OztHQU1HO0FBQ1EsUUFBQSxZQUFZLEdBQUcsVUFBUyxPQUFzRCxFQUFDLFVBQWtCLEdBQ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