Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
3dddb80b43 | |||
2ba06f27dc | |||
228f855a39 | |||
55ac57a122 | |||
7da57901dd | |||
12500a0900 | |||
36371d9469 | |||
4e9874a639 | |||
e55e30bdb3 |
36
index.js
36
index.js
@ -1,13 +1,41 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
var path = require("path");
|
||||
var pr = require("pushrocks");
|
||||
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 plugins.fs.readJsonSync(filePath, {});
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
module.exports = smartfile;
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "smartfile",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.7",
|
||||
"description": "offers smart ways to work with files in nodejs",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
@ -24,10 +24,13 @@
|
||||
},
|
||||
"homepage": "https://github.com/pushrocks/smartfile",
|
||||
"dependencies": {
|
||||
"pushrocks": "^1.0.18"
|
||||
"beautylog": "1.0.4",
|
||||
"fs-extra": "0.26.2",
|
||||
"js-yaml": "3.4.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"gulp": "3.9.0",
|
||||
"gulp-typescript": "2.9.2"
|
||||
"gulp-typescript": "2.9.2",
|
||||
"pushrocks": "1.0.22"
|
||||
}
|
||||
}
|
||||
|
7
test.js
7
test.js
@ -1,2 +1,7 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
var smartfile = require("./index.js");
|
||||
console.log(smartfile.readFileToString("./test/mytest.txt"));
|
||||
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");
|
||||
|
8
test/mytest.json
Normal file
8
test/mytest.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"key1": "this works",
|
||||
"key2": "this works too",
|
||||
"key3": {
|
||||
"nestedkey1": "hello"
|
||||
}
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
This is some test text. If this is displayed when running "npm test", then the test has succeeded.
|
||||
This is some test text stored in ./test/mytest.txt -> If this is displayed when running "npm test", then the test has succeeded.
|
4
test/mytest.yaml
Normal file
4
test/mytest.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
key1: this works
|
||||
key2: this works too
|
||||
key3:
|
||||
nestedkey1: hello
|
36
ts/index.ts
36
ts/index.ts
@ -1,14 +1,40 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
var path = require("path");
|
||||
var pr = require("pushrocks");
|
||||
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 plugins.fs.readJsonSync(filePath,{});
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,2 +1,7 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
var smartfile = require("./index.js");
|
||||
console.log(smartfile.readFileToString("./test/mytest.txt"));
|
||||
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");
|
Reference in New Issue
Block a user