added capability to read a file to a vinyl file object

This commit is contained in:
Philipp Kunz
2015-12-03 20:48:32 +01:00
parent 3a76e9b572
commit 0c0449527d
8 changed files with 134 additions and 56 deletions

View File

@ -1,42 +1,15 @@
/// <reference path="typings/tsd.d.ts" />
var plugins = {
path: require("path"),
fs: require("fs-extra"),
yaml: require("js-yaml"),
beautylog: require("beautylog")("os")
};
/// <reference path="./typings/tsd.d.ts" />
/// <reference path="./smartfile.plugins.ts" />
/// <reference path="./smartfile.simple.ts" />
/// <reference path="./smartfile.vinyl.ts" />
var plugins = SmartfilePlugins.init();
var smartfile:any = {};
SmartfileSimple.init(smartfile);
SmartfileVinyl.init(smartfile);
var smartfile:any = {
//read File to string
readFileToString: function(filePath) {
var fileString;
fileString = plugins.fs.readFileSync(filePath, "utf8");
return fileString;
},
readFileToObject: function(filePath,fileTypeArg = "undefined") {
var fileType;
if (fileTypeArg == "undefined") {
fileType = plugins.path.extname(filePath);
} else {
fileType = fileTypeArg;
}
fileType = fileType.replace(/\.([a-z]*)/,"$1"); //remove . form fileType
switch (fileType) {
case "yml" :
case "yaml":
try {
return plugins.yaml.safeLoad(plugins.fs.readFileSync(filePath, 'utf8'));
} catch (e){
plugins.beautylog.error("check that " + filePath.blue + " points to a valid file");
}
break;
case "json":
return plugins.fs.readJsonSync(filePath,{});
break;
}
}
};
module.exports = smartfile;

14
ts/smartfile.plugins.ts Normal file
View File

@ -0,0 +1,14 @@
/// <reference path="./index.ts" />
module SmartfilePlugins {
export var init = function() {
var plugins = {
beautylog: require("beautylog")("os"),
fs: require("fs-extra"),
path: require("path"),
vinyl: require("vinyl"),
vinylFile: require("vinyl-file"),
yaml: require("js-yaml")
};
return plugins;
}
}

39
ts/smartfile.simple.ts Normal file
View File

@ -0,0 +1,39 @@
/// <reference path="./index.ts" />
module SmartfileSimple {
/**
* reads a file content to a String
* @param filePath
* @returns {string|Buffer|any}
*/
var readFileToString = function(filePath) {
var fileString;
fileString = plugins.fs.readFileSync(filePath, "utf8");
return fileString;
};
var readFileToObject = function(filePath,fileTypeArg = "undefined") {
var fileType;
if (fileTypeArg == "undefined") {
fileType = plugins.path.extname(filePath);
} else {
fileType = fileTypeArg;
}
fileType = fileType.replace(/\.([a-z]*)/,"$1"); //remove . form fileType
switch (fileType) {
case "yml" :
case "yaml":
try {
return plugins.yaml.safeLoad(plugins.fs.readFileSync(filePath, 'utf8'));
} catch (e){
plugins.beautylog.error("check that " + filePath.blue + " points to a valid file");
}
break;
case "json":
return plugins.fs.readJsonSync(filePath,{});
break;
}
};
export var init = function(objectArg) {
objectArg.readFileToString = readFileToString;
objectArg.readFileToObject = readFileToObject;
};
}

9
ts/smartfile.vinyl.ts Normal file
View File

@ -0,0 +1,9 @@
/// <reference path="./index.ts" />
module SmartfileVinyl {
var readFileToVinyl = function(filePathArg,options = {}) {
return plugins.vinylFile.readSync(filePathArg,options);
};
export var init = function(objectArg) {
objectArg.readFileToVinyl = readFileToVinyl;
};
}

View File

@ -4,4 +4,5 @@ var beautylog = require("beautylog")("os");
beautylog.info(smartfile.readFileToString("./test/mytest.txt"));
console.log(smartfile.readFileToObject("./test/mytest.yaml"));
console.log(smartfile.readFileToObject("./test/mytest.json"));
console.log(smartfile.readFileToVinyl("./test/mytest.json"));
//var thisIsAnError = smartfile.readFileToObject("./test/mytestDoesNotExist.json");