Compare commits

...

12 Commits

Author SHA1 Message Date
9edab1b3ec 0.0.9 2015-12-08 20:33:32 +01:00
c0ee2996ce added requireReload function 2015-12-08 20:33:13 +01:00
f1d89beb06 0.0.8 2015-12-03 20:48:38 +01:00
0c0449527d added capability to read a file to a vinyl file object 2015-12-03 20:48:32 +01:00
3a76e9b572 updated readme 2015-11-28 15:47:30 +01:00
3dddb80b43 0.0.7 2015-11-24 18:57:00 +01:00
2ba06f27dc now using fs-extra for josn 2015-11-24 18:56:53 +01:00
228f855a39 now can read yaml 2015-11-24 18:52:24 +01:00
55ac57a122 0.0.6 2015-11-03 20:29:27 +01:00
7da57901dd fix dep 2015-11-03 20:29:19 +01:00
12500a0900 0.0.5 2015-11-03 20:22:21 +01:00
36371d9469 updated test 2015-11-03 20:22:16 +01:00
12 changed files with 218 additions and 68 deletions

View File

@ -1,39 +1,6 @@
# gulp-bootstrap # smartfile
works with gulp, but does nothing. Use it to bootstrap your own gulp plugin with TypeScript, Travis and npm. make files easily accessible for processing in javascript.
### Buildstatus/Dependencies
[![Build Status](https://travis-ci.org/pushrocks/gulp-bootstrap.svg?branch=master)](https://travis-ci.org/pushrocks/gulp-bootstrap)
[![devDependency Status](https://david-dm.org/pushrocks/gulp-bootstrap/dev-status.svg)](https://david-dm.org/pushrocks/gulp-bootstrap#info=devDependencies)
### Usage
This npm package comes with everything you need to start your own gulp plugin.
Features:
* TypeScript: Code your plugin in TypeScript
* Use gulp to compile TypeScript without the global gulp CLI Tool.
* Use travis to deploy to npm
* Have a master branch for the latest dev version
* Have a release branch for the latest npm version
This package **doesn't require global gulp** (just local -> simply do npm install`) to compile TypeScript.
* to compile TypeScript do `npm test` (You should chain your own tests to this command later on)
* to setup release do `npm run setup`
* to release a patch do `npm run release`
We recommend using travis for npm releasing and test integration.
#### The structure
```
gulp-bootstrap/
|
|- ts/
| |- compile/
| | |- compile.js **** contains gulp task`
| |- index.ts **** Your main TypeScript file.
|
|- index.js **** the compiled module
```
## Supported file types:
* .yml .yaml
* .json

View File

@ -1,13 +1,91 @@
/// <reference path="typings/tsd.d.ts" /> /// <reference path="./index.ts" />
var path = require("path"); var SmartfilePlugins;
var pr = require("pushrocks"); (function (SmartfilePlugins) {
var fs = require("fs"); SmartfilePlugins.init = function () {
var smartfile = { var plugins = {
//read File to string beautylog: require("beautylog")("os"),
readFileToString: function (filePath) { fs: require("fs-extra"),
path: require("path"),
vinyl: require("vinyl"),
vinylFile: require("vinyl-file"),
yaml: require("js-yaml"),
requireReload: require("require-reload")
};
return plugins;
};
})(SmartfilePlugins || (SmartfilePlugins = {}));
/// <reference path="./index.ts" />
var SmartfileSimple;
(function (SmartfileSimple) {
/**
* reads a file content to a String
* @param filePath
* @returns {string|Buffer|any}
*/
var readFileToString = function (filePath) {
var fileString; var fileString;
fileString = fs.readFileSync(filePath, "utf8"); fileString = plugins.fs.readFileSync(filePath, "utf8");
return fileString; return fileString;
} };
}; var 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;
}
};
SmartfileSimple.init = function (objectArg) {
objectArg.readFileToString = readFileToString;
objectArg.readFileToObject = readFileToObject;
};
})(SmartfileSimple || (SmartfileSimple = {}));
/// <reference path="./index.ts" />
var SmartfileVinyl;
(function (SmartfileVinyl) {
var readFileToVinyl = function (filePathArg, options) {
if (options === void 0) { options = {}; }
return plugins.vinylFile.readSync(filePathArg, options);
};
SmartfileVinyl.init = function (objectArg) {
objectArg.readFileToVinyl = readFileToVinyl;
};
})(SmartfileVinyl || (SmartfileVinyl = {}));
/// <reference path="./index.ts" />
var SmartfileRequire;
(function (SmartfileRequire) {
var requireReload = function (path) {
return plugins.requireReload(path);
};
SmartfileRequire.init = function (objectArg) {
objectArg.requireReload = requireReload;
};
})(SmartfileRequire || (SmartfileRequire = {}));
/// <reference path="./typings/tsd.d.ts" />
/// <reference path="./smartfile.plugins.ts" />
/// <reference path="./smartfile.simple.ts" />
/// <reference path="./smartfile.vinyl.ts" />
/// <reference path="./smartfile.require.ts" />
var plugins = SmartfilePlugins.init();
var smartfile = {};
SmartfileSimple.init(smartfile);
SmartfileVinyl.init(smartfile);
SmartfileRequire.init(smartfile);
module.exports = smartfile; module.exports = smartfile;

View File

@ -1,13 +1,14 @@
{ {
"name": "smartfile", "name": "smartfile",
"version": "0.0.4", "version": "0.0.9",
"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) && (node test.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)" "update": "(git checkout master && git pull origin master && npm install)",
"upgrade": "(npm run update) && (ncu upgradeAll && npm install)"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -24,10 +25,16 @@
}, },
"homepage": "https://github.com/pushrocks/smartfile", "homepage": "https://github.com/pushrocks/smartfile",
"dependencies": { "dependencies": {
"pushrocks": "^1.0.18" "beautylog": "1.0.6",
"fs-extra": "0.26.2",
"js-yaml": "3.4.6",
"require-reload": "0.2.2",
"vinyl": "^1.1.0",
"vinyl-file": "^1.3.0"
}, },
"devDependencies": { "devDependencies": {
"gulp": "3.9.0", "gulp": "3.9.0",
"gulp-typescript": "2.9.2" "gulp-typescript": "2.10.0",
"pushrocks": "1.0.23"
} }
} }

10
test.js
View File

@ -1,3 +1,9 @@
/// <reference path="typings/tsd.d.ts" />
var smartfile = require("./index.js"); var smartfile = require("./index.js");
var pr = require("pushrocks"); var beautylog = require("beautylog")("os");
pr.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"));
console.log(smartfile.readFileToVinyl("./test/mytest.json"));
//var thisIsAnError = smartfile.readFileToObject("./test/mytestDoesNotExist.json");
beautylog.success("Test passed!");

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,16 +1,17 @@
/// <reference path="typings/tsd.d.ts" /> /// <reference path="./typings/tsd.d.ts" />
var path = require("path"); /// <reference path="./smartfile.plugins.ts" />
var pr = require("pushrocks"); /// <reference path="./smartfile.simple.ts" />
var fs = require("fs"); /// <reference path="./smartfile.vinyl.ts" />
/// <reference path="./smartfile.require.ts" />
var plugins = SmartfilePlugins.init();
var smartfile:any = {};
SmartfileSimple.init(smartfile);
SmartfileVinyl.init(smartfile);
SmartfileRequire.init(smartfile);
var smartfile:any = {
//read File to string
readFileToString: (filePath) => {
var fileString;
fileString = fs.readFileSync(filePath, "utf8");
return fileString;
}
};
module.exports = smartfile; module.exports = smartfile;

15
ts/smartfile.plugins.ts Normal file
View File

@ -0,0 +1,15 @@
/// <reference path="./index.ts" />
module SmartfilePlugins {
export var init = function() {
var plugins = {
beautylog: require("beautylog")("os"),
fs: require("fs-extra"),
path: require("path"),
vinyl: require("vinyl"),
vinylFile: require("vinyl-file"),
yaml: require("js-yaml"),
requireReload: require("require-reload")
};
return plugins;
}
}

10
ts/smartfile.require.ts Normal file
View File

@ -0,0 +1,10 @@
/// <reference path="./index.ts" />
module SmartfileRequire {
var requireReload = function(path:string){
return plugins.requireReload(path);
};
export var init = function(objectArg){
objectArg.requireReload = requireReload;
}
}

39
ts/smartfile.simple.ts Normal file
View File

@ -0,0 +1,39 @@
/// <reference path="./index.ts" />
module SmartfileSimple {
/**
* reads a file content to a String
* @param filePath
* @returns {string|Buffer|any}
*/
var readFileToString = function(filePath) {
var fileString;
fileString = plugins.fs.readFileSync(filePath, "utf8");
return fileString;
};
var 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;
}
};
export var init = function(objectArg) {
objectArg.readFileToString = readFileToString;
objectArg.readFileToObject = readFileToObject;
};
}

9
ts/smartfile.vinyl.ts Normal file
View File

@ -0,0 +1,9 @@
/// <reference path="./index.ts" />
module SmartfileVinyl {
var readFileToVinyl = function(filePathArg,options = {}) {
return plugins.vinylFile.readSync(filePathArg,options);
};
export var init = function(objectArg) {
objectArg.readFileToVinyl = readFileToVinyl;
};
}

View File

@ -1,3 +1,9 @@
/// <reference path="typings/tsd.d.ts" />
var smartfile = require("./index.js"); var smartfile = require("./index.js");
var pr = require("pushrocks") var beautylog = require("beautylog")("os");
pr.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"));
console.log(smartfile.readFileToVinyl("./test/mytest.json"));
//var thisIsAnError = smartfile.readFileToObject("./test/mytestDoesNotExist.json");
beautylog.success("Test passed!");