Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
9edab1b3ec | |||
c0ee2996ce | |||
f1d89beb06 | |||
0c0449527d | |||
3a76e9b572 | |||
3dddb80b43 | |||
2ba06f27dc | |||
228f855a39 |
43
README.md
43
README.md
@ -1,39 +1,6 @@
|
||||
# gulp-bootstrap
|
||||
works with gulp, but does nothing. Use it to bootstrap your own gulp plugin with TypeScript, Travis and npm.
|
||||
|
||||
### Buildstatus/Dependencies
|
||||
[](https://travis-ci.org/pushrocks/gulp-bootstrap)
|
||||
[](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
|
||||
```
|
||||
# smartfile
|
||||
make files easily accessible for processing in javascript.
|
||||
|
||||
## Supported file types:
|
||||
* .yml .yaml
|
||||
* .json
|
||||
|
97
index.js
97
index.js
@ -1,12 +1,91 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
var smartfile = {
|
||||
//read File to string
|
||||
readFileToString: function (filePath) {
|
||||
/// <reference path="./index.ts" />
|
||||
var SmartfilePlugins;
|
||||
(function (SmartfilePlugins) {
|
||||
SmartfilePlugins.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;
|
||||
};
|
||||
})(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;
|
||||
fileString = fs.readFileSync(filePath, "utf8");
|
||||
fileString = plugins.fs.readFileSync(filePath, "utf8");
|
||||
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;
|
||||
|
16
package.json
16
package.json
@ -1,13 +1,14 @@
|
||||
{
|
||||
"name": "smartfile",
|
||||
"version": "0.0.6",
|
||||
"version": "0.0.9",
|
||||
"description": "offers smart ways to work with files in nodejs",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "(cd ts/compile && node compile.js) && (node test.js)",
|
||||
"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)",
|
||||
"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": {
|
||||
"type": "git",
|
||||
@ -24,11 +25,16 @@
|
||||
},
|
||||
"homepage": "https://github.com/pushrocks/smartfile",
|
||||
"dependencies": {
|
||||
"beautylog": "^1.0.2"
|
||||
"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": {
|
||||
"gulp": "3.9.0",
|
||||
"gulp-typescript": "2.9.2",
|
||||
"pushrocks": "^1.0.19"
|
||||
"gulp-typescript": "2.10.0",
|
||||
"pushrocks": "1.0.23"
|
||||
}
|
||||
}
|
||||
|
5
test.js
5
test.js
@ -2,3 +2,8 @@
|
||||
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"));
|
||||
console.log(smartfile.readFileToVinyl("./test/mytest.json"));
|
||||
//var thisIsAnError = smartfile.readFileToObject("./test/mytestDoesNotExist.json");
|
||||
beautylog.success("Test passed!");
|
||||
|
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"
|
||||
}
|
||||
}
|
||||
|
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
|
24
ts/index.ts
24
ts/index.ts
@ -1,15 +1,17 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
/// <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: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;
|
||||
|
15
ts/smartfile.plugins.ts
Normal file
15
ts/smartfile.plugins.ts
Normal 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
10
ts/smartfile.require.ts
Normal 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
39
ts/smartfile.simple.ts
Normal 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
9
ts/smartfile.vinyl.ts
Normal 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;
|
||||
};
|
||||
}
|
@ -1,4 +1,9 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
var smartfile = require("./index.js");
|
||||
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"));
|
||||
console.log(smartfile.readFileToVinyl("./test/mytest.json"));
|
||||
//var thisIsAnError = smartfile.readFileToObject("./test/mytestDoesNotExist.json");
|
||||
beautylog.success("Test passed!");
|
||||
|
Reference in New Issue
Block a user