now can read yaml

This commit is contained in:
Philipp Kunz 2015-11-24 18:52:24 +01:00
parent 55ac57a122
commit 228f855a39
7 changed files with 86 additions and 10 deletions

View File

@ -1,12 +1,41 @@
/// <reference path="typings/tsd.d.ts" /> /// <reference path="typings/tsd.d.ts" />
var path = require("path"); var plugins = {
var fs = require("fs"); path: require("path"),
fs: require("fs-extra"),
yaml: require("js-yaml"),
beautylog: require("beautylog")("os")
};
var smartfile = { var smartfile = {
//read File to string //read File to string
readFileToString: function (filePath) { readFileToString: function (filePath) {
var fileString; var fileString;
fileString = fs.readFileSync(filePath, "utf8"); fileString = plugins.fs.readFileSync(filePath, "utf8");
return fileString; 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; module.exports = smartfile;

View File

@ -24,11 +24,13 @@
}, },
"homepage": "https://github.com/pushrocks/smartfile", "homepage": "https://github.com/pushrocks/smartfile",
"dependencies": { "dependencies": {
"beautylog": "^1.0.2" "beautylog": "1.0.4",
"fs-extra": "0.26.2",
"js-yaml": "3.4.5"
}, },
"devDependencies": { "devDependencies": {
"gulp": "3.9.0", "gulp": "3.9.0",
"gulp-typescript": "2.9.2", "gulp-typescript": "2.9.2",
"pushrocks": "^1.0.19" "pushrocks": "1.0.22"
} }
} }

View File

@ -2,3 +2,6 @@
var smartfile = require("./index.js"); var smartfile = require("./index.js");
var beautylog = require("beautylog")("os"); 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");

8
test/mytest.json Normal file
View File

@ -0,0 +1,8 @@
{
"key1": "this works",
"key2": "this works too",
"key3": {
"nestedkey1": "hello"
}
}

4
test/mytest.yaml Normal file
View File

@ -0,0 +1,4 @@
key1: this works
key2: this works too
key3:
nestedkey1: hello

View File

@ -1,13 +1,40 @@
/// <reference path="typings/tsd.d.ts" /> /// <reference path="typings/tsd.d.ts" />
var path = require("path"); var plugins = {
var fs = require("fs"); path: require("path"),
fs: require("fs-extra"),
yaml: require("js-yaml"),
beautylog: require("beautylog")("os")
};
var smartfile:any = { var smartfile:any = {
//read File to string //read File to string
readFileToString: (filePath) => { readFileToString: function(filePath) {
var fileString; var fileString;
fileString = fs.readFileSync(filePath, "utf8"); fileString = plugins.fs.readFileSync(filePath, "utf8");
return fileString; 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;
}
} }
}; };

View File

@ -2,3 +2,6 @@
var smartfile = require("./index.js"); var smartfile = require("./index.js");
var beautylog = require("beautylog")("os"); 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");