smartfile/ts/smartfile.remote.ts

66 lines
1.8 KiB
TypeScript
Raw Normal View History

import "typings-global";
2016-03-18 09:19:46 +00:00
import plugins = require("./smartfile.plugins");
import SmartfileInterpreter = require("./smartfile.interpreter");
2016-03-18 09:19:46 +00:00
2016-03-19 21:07:59 +00:00
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);
2016-03-18 09:19:46 +00:00
});
return done.promise;
};
2016-03-18 09:19:46 +00:00
2016-03-19 21:07:59 +00:00
/**
*
* @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) {
2016-06-23 16:31:55 +00:00
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;
};
2016-03-18 09:19:46 +00:00
2016-03-19 21:07:59 +00:00
/**
*
* @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);
2016-03-19 21:07:59 +00:00
bodyString = undefined;
done.reject(bodyString);
};
});
return done.promise;
2016-03-19 21:07:59 +00:00
};