smartfile/ts/smartfile.remote.ts
2016-07-01 06:07:58 +02:00

66 lines
1.8 KiB
TypeScript

import "typings-global";
import plugins = require("./smartfile.plugins");
import SmartfileInterpreter = require("./smartfile.interpreter");
export let toFs = function(from:string,toPath:string) {
var done = plugins.Q.defer();
var stream = plugins.request(from).pipe(plugins.fs.createWriteStream(toPath));
stream.on('finish',function(){
done.resolve(toPath);
});
return done.promise;
};
/**
*
* @param filePathArg
* @returns {*}
*/
export let toGulpStreamSync = function(filePathArg:string,baseArg:string){
let stream = plugins.g.remoteSrc(filePathArg, {
base: baseArg
});
return stream;
};
/**
*
* @param fromArg
* @returns {any}
*/
export let toObject = function(fromArg:string){
let done = plugins.Q.defer();
plugins.request.get(fromArg, function (error, response, bodyString) {
let returnObject;
if (!error && response.statusCode == 200) {
returnObject = SmartfileInterpreter.objectFile(bodyString,SmartfileInterpreter.filetype(fromArg));
done.resolve(returnObject);
} else {
console.log('could not get remote file from ' + fromArg);
returnObject = undefined;
done.reject(returnObject);
};
});
return done.promise;
};
/**
*
* @param fromArg
* @returns {any}
*/
export let toString = (fromArg:string) => {
let done = plugins.Q.defer();
plugins.request.get(fromArg, function (error, response, bodyString) {
if (!error && response.statusCode == 200) {
done.resolve(bodyString);
} else {
plugins.beautylog.error('could not get remote file from ' + fromArg);
bodyString = undefined;
done.reject(bodyString);
};
});
return done.promise;
};