This commit is contained in:
2016-03-14 03:50:14 +01:00
parent 0ad455a483
commit 641218e841
24 changed files with 309 additions and 294 deletions

View File

@ -1,19 +1,17 @@
/// <reference path="./typings/main.d.ts" />
/// <reference path="./smartfile.plugins.ts" />
/// <reference path="./smartfile.check.ts" />
/// <reference path="./smartfile.simple.ts" />
/// <reference path="./smartfile.vinyl.ts" />
/// <reference path="./smartfile.require.ts" />
var plugins = SmartfilePlugins.init();
import plugins = require("./smartfile.plugins");
import SmartfileChecks = require("./smartfile.checks");
import SmartfileSimple = require("./smartfile.simple");
var smartfile:any = {};
SmartfileCheck.init(smartfile);
SmartfileSimple.init(smartfile);
SmartfileVinyl.init(smartfile);
SmartfileRequire.init(smartfile);
var smartfile:any = {
copy: SmartfileSimple.copy,
checks: SmartfileChecks,
readFileToString: SmartfileSimple.readFileToString,
readFileToObject: SmartfileSimple.readFileToObject,
readFileToVinyl: SmartfileSimple.readFileToVinyl,
requireReload: SmartfileSimple.requireReload
};
module.exports = smartfile;
export = smartfile;

View File

@ -1,26 +0,0 @@
/// <reference path="./index.ts" />
module SmartfileCheck {
var checks = {
fileExistsSync: function(filePath):boolean {
var fileExistsBool:boolean = false;
try {
plugins.fs.readFileSync(filePath)
fileExistsBool = true
}
catch(err){
fileExistsBool = false;
}
return fileExistsBool;
},
fileExists: function(filePath){
var done = plugins.q.defer();
plugins.fs.access(filePath, plugins.fs.R_OK, function (err) {
err ? done.reject() : done.resolve();
});
return done.promise;
}
};
export var init = function(objectArg){
objectArg.checks = checks;
}
}

32
ts/smartfile.checks.ts Normal file
View File

@ -0,0 +1,32 @@
/// <reference path="./typings/main.d.ts" />
import plugins = require("./smartfile.plugins");
/**
*
* @param filePath
* @returns {boolean}
*/
export let fileExistsSync = function(filePath):boolean {
let fileExistsBool:boolean = false;
try {
plugins.fs.readFileSync(filePath);
fileExistsBool = true
}
catch(err){
fileExistsBool = false;
}
return fileExistsBool;
};
/**
*
* @param filePath
* @returns {any}
*/
export let fileExists = function(filePath){
let done = plugins.q.defer();
plugins.fs.access(filePath, plugins.fs.R_OK, function (err) {
err ? done.reject() : done.resolve();
});
return done.promise;
};

View File

@ -1,16 +1,10 @@
/// <reference path="./index.ts" />
module SmartfilePlugins {
export var init = function() {
var plugins = {
beautylog: require("beautylog"),
fs: require("fs-extra"),
path: require("path"),
q: require("q"),
vinyl: require("vinyl"),
vinylFile: require("vinyl-file"),
yaml: require("js-yaml"),
requireReload: require("require-reload")
};
return plugins;
}
}
/// <reference path="./typings/main.d.ts" />
export let beautylog = require("beautylog");
export let fs = require("fs-extra");
export let path = require("path");
export let q = require("q");
export let vinyl = require("vinyl");
export let vinylFile = require("vinyl-file");
export let yaml = require("js-yaml");
export let requireReload = require("require-reload");
export let shelljs = require("shelljs");

View File

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

View File

@ -1,39 +1,68 @@
/// <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;
};
}
/// <reference path="./typings/main.d.ts" />
import plugins = require("./smartfile.plugins");
export let copy = function(fromArg:string,toArg:string){
plugins.shelljs.cp("-r",fromArg,toArg);
};
/**
* reads a file content to a String
* @param filePath
* @returns {string|Buffer|any}
*/
export let readFileToString = function(filePath) {
let fileString;
fileString = plugins.fs.readFileSync(filePath, "utf8");
return fileString;
};
/**
*
* @param filePath
* @param fileTypeArg
* @returns {any}
*/
export let readFileToObject = function(filePath,fileTypeArg = undefined) {
let fileType;
if (typeof 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,{});
default:
plugins.beautylog.error("file type " + fileType.blue + " not supported");
break;
}
};
/**
*
* @param filePathArg
* @param options
* @returns {number}
*/
export let readFileToVinyl = function(filePathArg,options = {}) {
return plugins.vinylFile.readSync(filePathArg,options);
};
/**
* lets you reload files hot.
* @param path
* @returns {any}
*/
export let requireReload = function(path:string){
return plugins.requireReload(path);
};

View File

@ -1,9 +0,0 @@
/// <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,51 +0,0 @@
/// <reference path="typings/main.d.ts" />
var smartfile = require("../index.js");
var beautylog = require("beautylog");
var should = require("should");
var vinyl = require("vinyl");
describe("smartfile",function(){
describe(".readFileToString".yellow,function(){
it("should read a file to a string",function(){
should.equal(
smartfile.readFileToString("./test/mytest.txt"),
"Some TestString &&%$"
);
});
});
describe(".readFileToObject".yellow,function(){
it("should read an " + ".yaml".blue + " file to an object",function(){
var testData = smartfile.readFileToObject("./test/mytest.yaml");
testData.should.have.property("key1","this works");
testData.should.have.property("key2","this works too");
});
it("should read an " + ".json".blue + " file to an object",function(){
var testData = smartfile.readFileToObject("./test/mytest.json");
testData.should.have.property("key1","this works");
testData.should.have.property("key2","this works too");
});
});
describe(".readFileToVinyl".yellow,function(){
it("should read an " + ".json OR .yaml".blue + " file to an " + "vinyl file object".cyan,function(){
var testData = smartfile.readFileToVinyl("./test/mytest.json");
(vinyl.isVinyl(testData)).should.be.true();
});
});
describe(".checks".yellow,function(){
describe(".fileExistsSync".yellow,function(){
it("should return an accurate boolean",function(){
(smartfile.checks.fileExistsSync("./test/mytest.json")).should.be.true();
(smartfile.checks.fileExistsSync("./test/notthere.json")).should.be.false();
});
});
describe(".fileExists".yellow,function(){
it("should return a working promise",function(){
(smartfile.checks.fileExists("./test/mytest.json")).should.be.Promise();
(smartfile.checks.fileExists("./test/mytest.json")).should.be.fulfilled();
(smartfile.checks.fileExists("./test/notthere.json")).should.not.be.fulfilled();
});
})
});
});

View File

@ -3,6 +3,6 @@
"browserify": "github:DefinitelyTyped/DefinitelyTyped/browserify/browserify.d.ts",
"colors": "github:DefinitelyTyped/DefinitelyTyped/colors/colors.d.ts",
"mocha": "github:Bartvds/tsd-deftools/typings/DefinitelyTyped/mocha/mocha.d.ts",
"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts"
"node": "registry:dt/node#4.0.0+20160311162451"
}
}