added capability to read a file to a vinyl file object

This commit is contained in:
Philipp Kunz 2015-12-03 20:48:32 +01:00
parent 3a76e9b572
commit 0c0449527d
8 changed files with 134 additions and 56 deletions

View File

@ -1,18 +1,32 @@
/// <reference path="typings/tsd.d.ts" />
var plugins = {
path: require("path"),
/// <reference path="./index.ts" />
var SmartfilePlugins;
(function (SmartfilePlugins) {
SmartfilePlugins.init = function () {
var plugins = {
beautylog: require("beautylog")("os"),
fs: require("fs-extra"),
yaml: require("js-yaml"),
beautylog: require("beautylog")("os")
};
var smartfile = {
//read File to string
readFileToString: function (filePath) {
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;
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 = {}));
/// <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;

View File

@ -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"
}
}

View File

@ -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");

View File

@ -1,42 +1,15 @@
/// <reference path="typings/tsd.d.ts" />
var plugins = {
path: require("path"),
fs: require("fs-extra"),
yaml: require("js-yaml"),
beautylog: require("beautylog")("os")
};
/// <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: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;

14
ts/smartfile.plugins.ts Normal file
View 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
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

@ -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");