improve memory.toFs(Sync)

This commit is contained in:
2016-06-24 03:36:51 +02:00
parent 4522907af3
commit fea9675b2a
8 changed files with 149 additions and 154 deletions

View File

@ -3,6 +3,54 @@ import "typings-global";
import plugins = require("./smartfile.plugins");
import SmartfileInterpreter = require("./smartfile.interpreter");
/*===============================================================
============================ Checks =============================
===============================================================*/
/**
*
* @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;
};
/**
* Checks if given path points to an existing directory
*/
export let isDirectory = function(pathArg):boolean{
return plugins.fs.statSync(pathArg).isDirectory();
};
/**
* Checks if a given path points to an existing file
*/
export let isFile = function(pathArg):boolean{
return plugins.fs.statSync(pathArg).isFile();
};
/*===============================================================
============================ FS ACTIONS =========================
===============================================================*/
@ -50,11 +98,6 @@ export let removeSync = function(pathArg:string):boolean{
============================ Write/Read =========================
===============================================================*/
export let toFS = function(options:{from:string,toPath:string}, cb=undefined){
};
/**
*
* @param filePathArg
@ -132,41 +175,3 @@ export let listFoldersSync = function(pathArg){
return plugins.fs.statSync(plugins.path.join(pathArg, file)).isDirectory();
});
};
/**
*
* @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;
};
export let isDirectory = function(pathArg):boolean{
return plugins.fs.statSync(pathArg).isDirectory();
};
export let isFile = function(pathArg):boolean{
return plugins.fs.statSync(pathArg).isFile();
};

View File

@ -2,6 +2,7 @@ import "typings-global";
import plugins = require("./smartfile.plugins");
import SmartfileInterpreter = require("./smartfile.interpreter");
import vinyl = require("vinyl");
let Readable = require("stream").Readable;
/**
* allows you to create a gulp stream
@ -96,33 +97,31 @@ export let toStringSync = function(fileArg:plugins.vinyl){
* @param fileNameArg
* @param fileBaseArg
*/
export let toFs = function(fileArg,optionsArg:{fileName:string,filePath:string}){
export let toFs = function(fileContentArg:string|vinyl,filePathArg){
let done = plugins.q.defer();
//function checks to abort if needed
if (!fileArg || !optionsArg || !(typeof optionsArg.fileName === "string"))
throw new Error("expected a valid arguments");
if (!(typeof optionsArg.filePath === "string")) optionsArg.filePath = "/";
if (!fileContentArg || !filePathArg) throw new Error("expected valid arguments");
let filePath:string = plugins.path.join(optionsArg.filePath,optionsArg.fileName);
// prepare actual write action
let fileString:string;
if (fileArg instanceof plugins.vinyl){
fileString = toStringSync(fileArg);
} else if (typeof fileArg === "string") {
fileString = fileArg;
let filePath:string = filePathArg;
if (fileContentArg instanceof plugins.vinyl){
fileString = toStringSync(fileContentArg);
} else if (typeof fileContentArg === "string") {
fileString = fileContentArg;
}
plugins.fs.writeFile(filePath,fileString,"utf8",done.resolve);
return done.promise;
};
export let toFsSync = function(fileArg,optionsArg:{fileName:string,filePath:string}){
export let toFsSync = function(fileArg,filePathArg:string){
//function checks to abort if needed
if (!fileArg || !optionsArg || !(typeof optionsArg.fileName === "string"))
throw new Error("expected a valid arguments");
if (!(typeof optionsArg.filePath === "string")) optionsArg.filePath = "/";
if (!fileArg || !filePathArg) throw new Error("expected a valid arguments");
let filePath = plugins.path.join(optionsArg.filePath,optionsArg.fileName);
// prepare actual write action
let fileString:string;
let filePath:string = filePathArg;
if (fileArg instanceof plugins.vinyl){
fileString = toStringSync(fileArg);