smartfile/dist/smartfile.memory.js

93 lines
6.5 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-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
};
2017-04-29 14:50:06 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLm1lbW9yeS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0ZmlsZS5tZW1vcnkudHMiXSwibmFtZ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