restructure code and make remote and local file delivery conform

This commit is contained in:
2016-03-20 17:36:38 +01:00
parent 9afe7c8a9e
commit 9a05df4200
10 changed files with 88 additions and 65 deletions

View File

@ -3,6 +3,7 @@
import plugins = require("./smartfile.plugins");
import SmartfileChecks = require("./smartfile.checks");
import SmartfileFsaction = require("./smartfile.fsaction");
import SmartfileGet = require("./smartfile.get");
import SmartfileLocal = require("./smartfile.local");
import SmartfileRemote = require("./smartfile.remote");
@ -10,6 +11,7 @@ import SmartfileRemote = require("./smartfile.remote");
var smartfile:any = {
fsaction: SmartfileFsaction,
checks: SmartfileChecks,
get: SmartfileGet,
local: SmartfileLocal,
remote: SmartfileRemote,
requireReload: SmartfileLocal.requireReload

8
ts/smartfile.get.ts Normal file
View File

@ -0,0 +1,8 @@
/// <reference path="./typings/main.d.ts" />
import plugins = require("./smartfile.plugins");
export let filetype = function(pathArg:string):string {
let extName = plugins.path.extname(pathArg);
let fileType = extName.replace(/\.([a-z]*)/,"$1"); //remove . form fileType
return fileType;
};

View File

@ -0,0 +1,16 @@
/// <reference path="./typings/main.d.ts" />
import plugins = require("./smartfile.plugins");
export = function(fileStringArg:string, fileTypeArg){
switch (fileTypeArg) {
case "yml" :
case "yaml":
return plugins.yaml.safeLoad(fileStringArg);
case "json":
return JSON.parse(fileStringArg);
default:
plugins.beautylog.error("file type " + fileTypeArg.blue + " not supported");
break;
}
}

View File

@ -1,7 +1,8 @@
/// <reference path="./typings/main.d.ts" />
import plugins = require("./smartfile.plugins");
import SmartfileGet = require("./smartfile.get");
import SmartfileInterpreter = require("./smartfile.interpreter");
export let toFS = function(options:{from:string,toPath:string}, cb=undefined){
};
@ -22,33 +23,15 @@ export let toGulpDestSync = function(folderPathArg:string){
/**
*
* @param filePath
* @param filePathArg
* @param fileTypeArg
* @returns {any}
*/
export let toObjectSync = function(filePath,fileTypeArg = undefined) {
export let toObjectSync = function(filePathArg,fileTypeArg?) {
let fileString = plugins.fs.readFileSync(filePathArg, 'utf8');
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;
}
fileTypeArg ? fileType = fileTypeArg : fileType = SmartfileGet.filetype(filePathArg);
return SmartfileInterpreter(fileString,fileType);
};
/**

View File

@ -1,5 +1,7 @@
/// <reference path="./typings/main.d.ts" />
import plugins = require("./smartfile.plugins");
import SmartfileInterpreter = require("./smartfile.interpreter");
import SmartfileGet = require("./smartfile.get");
export let toFs = function(from:string,toPath:string) {
var done = plugins.q.defer();
@ -30,14 +32,14 @@ export let toGulpStreamSync = function(filePathArg:string,baseArg:string){
export let toObject = function(fromArg:string){
let done = plugins.q.defer();
plugins.request.get(fromArg, function (error, response, bodyString) {
let jsonObject;
let returnObject;
if (!error && response.statusCode == 200) {
jsonObject = JSON.parse(bodyString);
done.resolve(jsonObject);
returnObject = SmartfileInterpreter(bodyString,SmartfileGet.filetype(fromArg));
done.resolve(returnObject);
} else {
console.log('could not get remote file from ' + fromArg);
jsonObject = undefined;
done.reject(jsonObject);
returnObject = undefined;
done.reject(returnObject);
};
});
return done.promise;
@ -54,7 +56,7 @@ export let toString = (fromArg:string) => {
if (!error && response.statusCode == 200) {
done.resolve(bodyString);
} else {
plugins.beautylog.error('could not get get remote file from ' + fromArg);
plugins.beautylog.error('could not get remote file from ' + fromArg);
bodyString = undefined;
done.reject(bodyString);
};