now can read yaml
This commit is contained in:
35
ts/index.ts
35
ts/index.ts
@ -1,13 +1,40 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
var plugins = {
|
||||
path: require("path"),
|
||||
fs: require("fs-extra"),
|
||||
yaml: require("js-yaml"),
|
||||
beautylog: require("beautylog")("os")
|
||||
};
|
||||
|
||||
|
||||
var smartfile:any = {
|
||||
//read File to string
|
||||
readFileToString: (filePath) => {
|
||||
readFileToString: function(filePath) {
|
||||
var fileString;
|
||||
fileString = fs.readFileSync(filePath, "utf8");
|
||||
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 require(filePath);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,4 +1,7 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
var smartfile = require("./index.js");
|
||||
var beautylog = require("beautylog")("os");
|
||||
beautylog.info(smartfile.readFileToString("./test/mytest.txt"));
|
||||
beautylog.info(smartfile.readFileToString("./test/mytest.txt"));
|
||||
console.log(smartfile.readFileToObject("./test/mytest.yaml"));
|
||||
console.log(smartfile.readFileToObject("./test/mytest.json"));
|
||||
//var thisIsAnError = smartfile.readFileToObject("./test/mytestDoesNotExist.json");
|
Reference in New Issue
Block a user