smartfile/ts/index.ts

43 lines
1.2 KiB
TypeScript
Raw Normal View History

2015-11-03 18:57:29 +00:00
/// <reference path="typings/tsd.d.ts" />
2015-11-24 17:52:24 +00:00
var plugins = {
path: require("path"),
fs: require("fs-extra"),
yaml: require("js-yaml"),
beautylog: require("beautylog")("os")
};
2015-11-03 18:57:29 +00:00
2015-11-03 19:14:41 +00:00
var smartfile:any = {
//read File to string
2015-11-24 17:52:24 +00:00
readFileToString: function(filePath) {
2015-11-03 19:14:41 +00:00
var fileString;
2015-11-24 17:52:24 +00:00
fileString = plugins.fs.readFileSync(filePath, "utf8");
2015-11-03 19:14:41 +00:00
return fileString;
2015-11-24 17:52:24 +00:00
},
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":
2015-11-24 17:56:53 +00:00
return plugins.fs.readJsonSync(filePath,{});
2015-11-24 17:52:24 +00:00
break;
}
2015-11-03 19:14:41 +00:00
}
2015-11-03 18:57:29 +00:00
};
2015-11-03 19:14:41 +00:00
module.exports = smartfile;