diff --git a/index.js b/index.js index 7189f56..d16795b 100644 --- a/index.js +++ b/index.js @@ -1,18 +1,32 @@ -/// -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 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") + }; + return plugins; + }; +})(SmartfilePlugins || (SmartfilePlugins = {})); +/// +var SmartfileSimple; +(function (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; - }, - readFileToObject: function (filePath, fileTypeArg) { + }; + var readFileToObject = function (filePath, fileTypeArg) { if (fileTypeArg === void 0) { fileTypeArg = "undefined"; } var fileType; if (fileTypeArg == "undefined") { @@ -36,6 +50,29 @@ var smartfile = { return plugins.fs.readJsonSync(filePath, {}); break; } - } -}; + }; + SmartfileSimple.init = function (objectArg) { + objectArg.readFileToString = readFileToString; + objectArg.readFileToObject = readFileToObject; + }; +})(SmartfileSimple || (SmartfileSimple = {})); +/// +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 = {})); +/// +/// +/// +/// +var plugins = SmartfilePlugins.init(); +var smartfile = {}; +SmartfileSimple.init(smartfile); +SmartfileVinyl.init(smartfile); module.exports = smartfile; diff --git a/package.json b/package.json index 706429b..103a6a9 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "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,13 +25,16 @@ }, "homepage": "https://github.com/pushrocks/smartfile", "dependencies": { - "beautylog": "1.0.4", + "beautylog": "1.0.6", "fs-extra": "0.26.2", - "js-yaml": "3.4.5" + "install": "^0.4.0", + "js-yaml": "3.4.6", + "vinyl": "^1.1.0", + "vinyl-file": "^1.3.0" }, "devDependencies": { "gulp": "3.9.0", - "gulp-typescript": "2.9.2", - "pushrocks": "1.0.22" + "gulp-typescript": "2.10.0", + "pushrocks": "1.0.23" } } diff --git a/test.js b/test.js index 4ba47b4..32e21c7 100644 --- a/test.js +++ b/test.js @@ -4,4 +4,5 @@ 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"); diff --git a/ts/index.ts b/ts/index.ts index e6f2d8f..ed3d54c 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1,42 +1,15 @@ -/// -var plugins = { - path: require("path"), - fs: require("fs-extra"), - yaml: require("js-yaml"), - beautylog: require("beautylog")("os") -}; +/// +/// +/// +/// +var plugins = SmartfilePlugins.init(); + + +var smartfile:any = {}; +SmartfileSimple.init(smartfile); +SmartfileVinyl.init(smartfile); -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; diff --git a/ts/smartfile.plugins.ts b/ts/smartfile.plugins.ts new file mode 100644 index 0000000..165e3d7 --- /dev/null +++ b/ts/smartfile.plugins.ts @@ -0,0 +1,14 @@ +/// +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; + } +} \ No newline at end of file diff --git a/ts/smartfile.simple.ts b/ts/smartfile.simple.ts new file mode 100644 index 0000000..c597110 --- /dev/null +++ b/ts/smartfile.simple.ts @@ -0,0 +1,39 @@ +/// +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; + }; +} \ No newline at end of file diff --git a/ts/smartfile.vinyl.ts b/ts/smartfile.vinyl.ts new file mode 100644 index 0000000..d1e8161 --- /dev/null +++ b/ts/smartfile.vinyl.ts @@ -0,0 +1,9 @@ +/// +module SmartfileVinyl { + var readFileToVinyl = function(filePathArg,options = {}) { + return plugins.vinylFile.readSync(filePathArg,options); + }; + export var init = function(objectArg) { + objectArg.readFileToVinyl = readFileToVinyl; + }; +} \ No newline at end of file diff --git a/ts/test.ts b/ts/test.ts index 80393c6..dcfb8db 100644 --- a/ts/test.ts +++ b/ts/test.ts @@ -4,4 +4,5 @@ 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"); \ No newline at end of file