Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
f1d89beb06 | |||
0c0449527d | |||
3a76e9b572 | |||
3dddb80b43 | |||
2ba06f27dc | |||
228f855a39 | |||
55ac57a122 | |||
7da57901dd | |||
12500a0900 | |||
36371d9469 | |||
4e9874a639 | |||
e55e30bdb3 |
43
README.md
43
README.md
@ -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
|
|
||||||
[](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
|
|
||||||
```
|
|
||||||
|
|
||||||
|
## Supported file types:
|
||||||
|
* .yml .yaml
|
||||||
|
* .json
|
||||||
|
85
index.js
85
index.js
@ -1,13 +1,78 @@
|
|||||||
/// <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")
|
||||||
|
};
|
||||||
|
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="./typings/tsd.d.ts" />
|
||||||
|
/// <reference path="./smartfile.plugins.ts" />
|
||||||
|
/// <reference path="./smartfile.simple.ts" />
|
||||||
|
/// <reference path="./smartfile.vinyl.ts" />
|
||||||
|
var plugins = SmartfilePlugins.init();
|
||||||
|
var smartfile = {};
|
||||||
|
SmartfileSimple.init(smartfile);
|
||||||
|
SmartfileVinyl.init(smartfile);
|
||||||
module.exports = smartfile;
|
module.exports = smartfile;
|
||||||
|
15
package.json
15
package.json
@ -1,13 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "smartfile",
|
"name": "smartfile",
|
||||||
"version": "0.0.3",
|
"version": "0.0.8",
|
||||||
"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",
|
||||||
|
"install": "^0.4.0",
|
||||||
|
"js-yaml": "3.4.6",
|
||||||
|
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
8
test.js
8
test.js
@ -1,2 +1,8 @@
|
|||||||
|
/// <reference path="typings/tsd.d.ts" />
|
||||||
var smartfile = require("./index.js");
|
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"));
|
||||||
|
console.log(smartfile.readFileToVinyl("./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
|
23
ts/index.ts
23
ts/index.ts
@ -1,16 +1,15 @@
|
|||||||
/// <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" />
|
||||||
|
var plugins = SmartfilePlugins.init();
|
||||||
|
|
||||||
|
|
||||||
|
var smartfile:any = {};
|
||||||
|
SmartfileSimple.init(smartfile);
|
||||||
|
SmartfileVinyl.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;
|
||||||
|
14
ts/smartfile.plugins.ts
Normal file
14
ts/smartfile.plugins.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
/// <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")
|
||||||
|
};
|
||||||
|
return plugins;
|
||||||
|
}
|
||||||
|
}
|
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,2 +1,8 @@
|
|||||||
|
/// <reference path="typings/tsd.d.ts" />
|
||||||
var smartfile = require("./index.js");
|
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"));
|
||||||
|
console.log(smartfile.readFileToVinyl("./test/mytest.json"));
|
||||||
|
//var thisIsAnError = smartfile.readFileToObject("./test/mytestDoesNotExist.json");
|
Reference in New Issue
Block a user