diff --git a/index.js b/index.js index 4d05dfc..898d169 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,41 @@ /// -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 = { //read File to string readFileToString: function (filePath) { var fileString; - fileString = fs.readFileSync(filePath, "utf8"); + fileString = plugins.fs.readFileSync(filePath, "utf8"); return fileString; + }, + readFileToObject: function (filePath, fileTypeArg) { + if (fileTypeArg === void 0) { 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; + } } }; module.exports = smartfile; diff --git a/package.json b/package.json index 8229c34..91158c2 100644 --- a/package.json +++ b/package.json @@ -24,11 +24,13 @@ }, "homepage": "https://github.com/pushrocks/smartfile", "dependencies": { - "beautylog": "^1.0.2" + "beautylog": "1.0.4", + "fs-extra": "0.26.2", + "js-yaml": "3.4.5" }, "devDependencies": { "gulp": "3.9.0", "gulp-typescript": "2.9.2", - "pushrocks": "^1.0.19" + "pushrocks": "1.0.22" } } diff --git a/test.js b/test.js index 32fb94e..4ba47b4 100644 --- a/test.js +++ b/test.js @@ -2,3 +2,6 @@ var smartfile = require("./index.js"); 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")); +//var thisIsAnError = smartfile.readFileToObject("./test/mytestDoesNotExist.json"); diff --git a/test/mytest.json b/test/mytest.json new file mode 100644 index 0000000..3439934 --- /dev/null +++ b/test/mytest.json @@ -0,0 +1,8 @@ +{ + "key1": "this works", + "key2": "this works too", + "key3": { + "nestedkey1": "hello" + } +} + diff --git a/test/mytest.yaml b/test/mytest.yaml new file mode 100644 index 0000000..8ace546 --- /dev/null +++ b/test/mytest.yaml @@ -0,0 +1,4 @@ +key1: this works +key2: this works too +key3: + nestedkey1: hello \ No newline at end of file diff --git a/ts/index.ts b/ts/index.ts index ff715ac..7825afd 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1,13 +1,40 @@ /// -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; + } } }; diff --git a/ts/test.ts b/ts/test.ts index 8c3794b..80393c6 100644 --- a/ts/test.ts +++ b/ts/test.ts @@ -1,4 +1,7 @@ /// var smartfile = require("./index.js"); var beautylog = require("beautylog")("os"); -beautylog.info(smartfile.readFileToString("./test/mytest.txt")); \ No newline at end of file +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"); \ No newline at end of file