Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
3dddb80b43 | |||
2ba06f27dc | |||
228f855a39 | |||
55ac57a122 | |||
7da57901dd | |||
12500a0900 | |||
36371d9469 | |||
4e9874a639 | |||
e55e30bdb3 | |||
44aed02dd3 | |||
8d8d7e7ff4 |
47
index.js
47
index.js
@ -1,10 +1,41 @@
|
|||||||
/// <reference path="typings/tsd.d.ts" />
|
/// <reference path="typings/tsd.d.ts" />
|
||||||
var through = require("through2");
|
var plugins = {
|
||||||
var path = require("path");
|
path: require("path"),
|
||||||
module.exports = function (jadeTemplate, mojo) {
|
fs: require("fs-extra"),
|
||||||
if (mojo === void 0) { mojo = undefined; }
|
yaml: require("js-yaml"),
|
||||||
return through.obj(function (file, enc, cb) {
|
beautylog: require("beautylog")("os")
|
||||||
//run callback function to signal end of plugin process.
|
|
||||||
return cb(null, file);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
var smartfile = {
|
||||||
|
//read File to string
|
||||||
|
readFileToString: function (filePath) {
|
||||||
|
var fileString;
|
||||||
|
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;
|
||||||
|
11
package.json
11
package.json
@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "smartfile",
|
"name": "smartfile",
|
||||||
"version": "0.0.2",
|
"version": "0.0.7",
|
||||||
"description": "offers smart ways to work with files in nodejs",
|
"description": "offers smart ways to work with files in nodejs",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(cd ts/compile && node compile.js)",
|
"test": "(cd ts/compile && node compile.js) && (node test.js)",
|
||||||
"reinstall": "(rm -r node_modules && npm install)",
|
"reinstall": "(rm -r node_modules && npm install)",
|
||||||
"release": "(git pull origin master && npm version patch && git push origin master && git checkout release && git merge master && git push origin release && git checkout master)",
|
"release": "(git pull origin master && npm version patch && git push origin master && git checkout release && git merge master && git push origin release && git checkout master)",
|
||||||
"startdev": "(git checkout master && git pull origin master)"
|
"startdev": "(git checkout master && git pull origin master)"
|
||||||
@ -24,10 +24,13 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/pushrocks/smartfile",
|
"homepage": "https://github.com/pushrocks/smartfile",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"pushrocks": "^1.0.18"
|
"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.22"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
7
test.js
Normal file
7
test.js
Normal file
@ -0,0 +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"));
|
||||||
|
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
test/mytest.txt
Normal file
1
test/mytest.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
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
|
@ -12,7 +12,16 @@ gulp.task('compileTS', function() {
|
|||||||
return stream;
|
return stream;
|
||||||
});
|
});
|
||||||
|
|
||||||
gulp.task('default',['compileTS'], function() {
|
gulp.task('compileTestTS', function() {
|
||||||
|
var stream = gulp.src('../test.ts')
|
||||||
|
.pipe(gulpTypescript({
|
||||||
|
out: "test.js"
|
||||||
|
}))
|
||||||
|
.pipe(gulp.dest("../../"));
|
||||||
|
return stream;
|
||||||
|
});
|
||||||
|
|
||||||
|
gulp.task('default',['compileTS','compileTestTS'], function() {
|
||||||
pr.beautylog.success('Typescript compiled');
|
pr.beautylog.success('Typescript compiled');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
50
ts/index.ts
50
ts/index.ts
@ -1,12 +1,42 @@
|
|||||||
/// <reference path="typings/tsd.d.ts" />
|
/// <reference path="typings/tsd.d.ts" />
|
||||||
var through = require("through2");
|
var plugins = {
|
||||||
var path = require("path");
|
path: require("path"),
|
||||||
|
fs: require("fs-extra"),
|
||||||
module.exports = (jadeTemplate,mojo = undefined) => {
|
yaml: require("js-yaml"),
|
||||||
|
beautylog: require("beautylog")("os")
|
||||||
return through.obj((file, enc, cb) => {
|
|
||||||
|
|
||||||
//run callback function to signal end of plugin process.
|
|
||||||
return cb(null, file);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
var smartfile:any = {
|
||||||
|
//read File to string
|
||||||
|
readFileToString: function(filePath) {
|
||||||
|
var fileString;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = smartfile;
|
||||||
|
7
ts/test.ts
Normal file
7
ts/test.ts
Normal file
@ -0,0 +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"));
|
||||||
|
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