/*

 Copyright (c) 2012 IBM Corporation and others.
 All rights reserved. This program and the accompanying materials are made 
 available under the terms of the Eclipse Public License v1.0 
 (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 

 Contributors: IBM Corporation - initial API and implementation

 Copyright (c) 2011, 2014 IBM Corporation and others.
 All rights reserved. This program and the accompanying materials are made 
 available under the terms of the Eclipse Public License v1.0 
 (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 

 Contributors:
     IBM Corporation - initial API and implementation

 Copyright (c) 2013 IBM Corporation and others.
 All rights reserved. This program and the accompanying materials are made 
 available under the terms of the Eclipse Public License v1.0 
 (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 

 Contributors: IBM Corporation - initial API and implementation

 Copyright (c) 2012 IBM Corporation and others.
 All rights reserved. This program and the accompanying materials are made 
 available under the terms of the Eclipse Public License v1.0 
 (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution 
 License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). 

 Contributors:
     IBM Corporation - initial API and implementation
*/
(function(e,d){"function"===typeof define&&define.amd?define("orion/Deferred",d):"object"===typeof exports?module.exports=d():(e.orion=e.orion||{},e.orion.Deferred=d())})(this,function(){function e(){for(var p;p=c.shift();)p();a=!1}function d(p){c.push(p);a||(a=!0,g())}function l(a){return function(c){a(c)}}function f(){function a(){for(var h;h=m.shift();){var b=h.deferred,c="fulfilled"===k?"resolve":"reject";h=h[c];if("function"===typeof h)try{var f=h(q),p=f&&("object"===typeof f||"function"===typeof f)&&
f.then;if("function"===typeof p)if(f===b.promise)b.reject(new TypeError);else{var d=f.cancel;"function"===typeof d?b._parentCancel=d.bind(f):delete b._parentCancel;p.call(f,l(b.resolve),l(b.reject),l(b.progress))}else b.resolve(f)}catch(e){b.reject(e)}else b[c](q)}}function c(h){delete b._parentCancel;k="rejected";q=h;m.length&&d(a)}function e(h){function g(n){return function(b){(!k||"assumed"===k)&&n(b)}}delete b._parentCancel;try{var l=h&&("object"===typeof h||"function"===typeof h)&&h.then;if("function"===
typeof l)if(h===b)c(new TypeError);else{k="assumed";var u=h&&h.cancel;if("function"!==typeof u){var r=new f;h=r.promise;try{l(r.resolve,r.reject,r.progress)}catch(v){r.reject(v)}u=h.cancel;l=h.then}q=h;l.call(h,g(e),g(c));b._parentCancel=u.bind(h)}else k="fulfilled",q=h,m.length&&d(a)}catch(s){g(c)(s)}}function g(){var h=b._parentCancel;h?(delete b._parentCancel,h()):k||(h=Error("Cancel"),h.name="Cancel",c(h))}var q,k,m=[],b=this;this.resolve=function(h){k||e(h);return b};this.reject=function(h){k||
c(h);return b};this.progress=function(h){k||m.forEach(function(b){if(b.progress)try{b.progress(h)}catch(a){}});return b.promise};this.cancel=function(){b._parentCancel?setTimeout(g,0):g();return b};this.then=function(h,c,g){var e=new f;e._parentCancel=b.promise.cancel;m.push({resolve:h,reject:c,progress:g,deferred:e});("fulfilled"===k||"rejected"===k)&&d(a);return e.promise};this.promise={then:b.then,cancel:b.cancel}}var c=[],a=!1,g=function(){if("undefined"!==typeof process&&"function"===typeof process.nextTick){var a=
process.nextTick;return function(){a(e)}}if("function"===typeof MutationObserver){var c=document.createElement("div");(new MutationObserver(e)).observe(c,{attributes:!0});return function(){c.setAttribute("class","_tick")}}return function(){setTimeout(e,0)}}();f.all=function(a,c){function e(a,c){l||(k[a]=c,0===--g&&b.resolve(k))}function d(a,f){if(!l){if(c)try{e(a,c(f));return}catch(g){f=g}b.reject(f)}}var g=a.length,k=[],l=!1,b=new f;b.then(void 0,function(){l=!0;a.forEach(function(a){a.cancel&&a.cancel()})});
0===g?b.resolve(k):a.forEach(function(a,b){a.then(e.bind(void 0,b),d.bind(void 0,b))});return b.promise};f.when=function(a,c,g,e){var d;a&&"function"===typeof a.then||(d=new f,d.resolve(a),a=d.promise);return a.then(c,g,e)};return f});
define("orion/xhr",["orion/Deferred"],function(e){function d(d,f,c,a){var g="undefined"!==typeof c.response?c.response:c.responseText,e;try{e=c.status}catch(t){e=0}d={args:f,response:g,responseText:"string"===typeof g?g:null,status:e,url:d,xhr:c};"undefined"!==typeof a&&(d.error=a);return d}return function(l,f,c){c=c||{};var a=3<arguments.length&&arguments[3]?arguments[3]:new XMLHttpRequest,g=new e,p=c.headers||{},t=c.log||!1,x;"undefined"===typeof p["X-Requested-With"]&&(p["X-Requested-With"]="XMLHttpRequest");
if("undefined"!==typeof c.data&&("POST"===l||"PUT"===l))x=c.data;var r=!1,q=!1;g.promise.then(void 0,function(b){r=!0;!q&&(b instanceof Error&&"Cancel"===b.name)&&a.abort()});a.onabort=function(){q=!0;if(!r){var a=Error("Cancel");a.name="Cancel";g.reject(a)}};a.onload=function(){var b=d(f,c,a);200<=a.status&&400>a.status?g.resolve(b):(g.reject(b),t&&"undefined"!==typeof console&&console.log(Error(a.statusText)))};a.onerror=function(){var b=d(f,c,a);g.reject(b);t&&"undefined"!==typeof console&&console.log(Error(a.statusText))};
a.onprogress=function(b){b.xhr=a;g.progress(b)};try{a.open(l,f,!0);"string"===typeof c.responseType&&(a.responseType=c.responseType);if("number"===typeof c.timeout)if("number"===typeof a.timeout)a.timeout=c.timeout,a.addEventListener("timeout",function(b){g.reject(d(f,c,a,"Timeout exceeded"))});else{var k=setTimeout(function(){g.reject(d(f,c,a,"Timeout exceeded"))},c.timeout);g.promise.then(clearTimeout.bind(null,k),clearTimeout.bind(null,k))}Object.keys(p).forEach(function(b){a.setRequestHeader(b,
p[b])});a.send(x||null)}catch(m){g.reject(d(f,c,a,m))}return g.promise}});
(function(e,d){"function"===typeof define&&define.amd?define("orion/plugin",["orion/Deferred"],d):"object"===typeof exports?module.exports=d(require("orion/Deferred")):(e.orion=e.orion||{},e.orion.PluginProvider=d(e.orion.Deferred))})(this,function(e){function d(d,f){this.__objectId=d;this.__methods=f}return function(l){function f(n){m&&("undefined"===typeof ArrayBuffer&&(n=JSON.stringify(n)),m===self?m.postMessage(n):m.postMessage(n,"*"))}function c(){var n=[];Object.keys(v).forEach(function(a){var b=
v[a];n.push({serviceId:a,names:b.names,methods:b.methods,properties:b.properties})});return{headers:q||{},services:n}}function a(n,a){if(a&&a instanceof XMLHttpRequest){var b,c;try{b=a.status,c=a.statusText}catch(f){b=0,c=""}return{status:b||0,statusText:c}}return a}function g(n){var b=n?JSON.parse(JSON.stringify(n,a)):n;n instanceof Error&&(b.__isError=!0,b.message=b.message||n.message,b.name=b.name||n.name);return b}function p(a){if(!m)return(new e).reject(Error("plugin not connected"));a.id=String(b++);
var c=new e;u[a.id]=c;c.then(null,function(b){k&&(b instanceof Error&&"Cancel"===b.name)&&s({requestId:a.id,method:"cancel",params:b.message?[b.message]:[]})});var f=Object.prototype.toString;a.params.forEach(function(b,e){if("[object Object]"===f.call(b)&&!(b instanceof d)){var g,k;for(g in b)"[object Function]"===f.call(b[g])&&(k=k||[],k.push(g));if(k){var l=h++;y[l]=b;g=function(){delete y[l]};c.then(g,g);a.params[e]=new d(l,k)}}});s(a);return c.promise}function t(a,b){a||0===a?s({id:a,result:null,
error:b}):console.log(b)}function x(a,b,c,f){f.forEach(function(a,b){if(a&&"undefined"!==typeof a.__objectId){var c={};a.__methods.forEach(function(b){c[b]=function(){return p({objectId:a.__objectId,method:b,params:Array.prototype.slice.call(arguments)})}});f[b]=c}});var d=void 0===typeof a?null:{id:a,result:null,error:null};try{var e=c.apply(b,f);d&&(e&&"function"===typeof e.then?(w[a]=e,e.then(function(b){delete w[a];d.result=b;s(d)},function(b){w[a]&&(delete w[a],d.error=g(b),s(d))},function(){s({responseId:a,
method:"progress",params:Array.prototype.slice.call(arguments)})})):(d.result=e,s(d)))}catch(h){d&&(d.error=g(h),s(d))}}function r(a){if(!(a.source!==m&&"undefined"!==typeof window)){a="string"!==typeof a.data?a.data:JSON.parse(a.data);try{if(a.method){var b=a.method,c=a.params||[];if("serviceId"in a){var d=v[a.serviceId];d||t(a.id,"service not found");d=d.implementation;b in d?x(a.id,d,d[b],c):t(a.id,"method not found")}else if("objectId"in a){var f=y[a.objectId];f||t(a.id,"object not found");!b in
f?x(a.id,f,f[b],c):t(a.id,"method not found")}else if("requestId"in a){var e=w[a.requestId];e&&("cancel"===b&&e.cancel)&&e.cancel.apply(e,c)}else if("responseId"in a){var g=u[a.responseId];g&&("progress"===b&&g.progress)&&g.progress.apply(g,c)}else throw Error("Bad method: "+a.method);}else{var h=u[String(a.id)];delete u[String(a.id)];a.error?h.reject(a.error):h.resolve(a.result)}}catch(k){console.log("Plugin._messageHandler "+k)}}}var q=l,k=!1,m=null,b=0,h=0,z=0,w={},u={},y={},v={},s=f;this.updateHeaders=
function(a){if(k)throw Error("Cannot update headers. Plugin Provider is connected");q=a};this.registerServiceProvider=this.registerService=function(a,b,c){if(k)throw Error("Cannot register service. Plugin Provider is connected");"string"===typeof a?a=[a]:Array.isArray(a)||(a=[]);var d=null,f=[];for(d in b)"function"===typeof b[d]&&f.push(d);v[z++]={names:a,methods:f,implementation:b,properties:c||{},listeners:{}}};this.connect=function(a,b){if(!k){if("undefined"===typeof window)m=self;else if(window!==
window.parent)m=window.parent;else if(null!==window.opener)m=window.opener;else{b&&b("No valid plugin target");return}addEventListener("message",r,!1);var d={method:"plugin",params:[c()]};f(d);k=!0}a&&a()};this.disconnect=function(){k&&(removeEventListener("message",r),m=null,k=!1)}}});
define("plugins/npmPlugin",["orion/xhr","orion/Deferred","orion/plugin"],function(e,d,l){d=new l({name:"Orion NPM",version:"1.0",description:"Orion NPM"});d.registerServiceProvider("orion.shell.command",null,{name:"npm",description:"Commands for interacting with node npm"});d.registerServiceProvider("orion.shell.command",{callback:function(d,c){d.cwd=c.cwd;d.type="shrinkwrap";return e("POST","/npm",{headers:{"Orion-Version":"1"},data:JSON.stringify(d),timeout:3E5}).then(function(a){return a.response?
JSON.parse(a.response).cmdOutput:""},function(a){return a.error})}},{name:"npm shrinkwrap",description:"npm shrinkwrap",parameters:null});d.registerServiceProvider("orion.shell.command",{callback:function(d,c){d.cwd=c.cwd;d.type="install";return e("POST","/npm",{headers:{"Orion-Version":"1"},data:JSON.stringify(d),timeout:3E5}).then(function(a){return a.response?JSON.parse(a.response).cmdOutput:""},function(a){return a.error})}},{name:"npm install",description:"npm install",parameters:[{name:"package",
type:"string",description:"package to install",defaultValue:null}]});d.connect()});
//@ sourceMappingURL=npmPlugin.js.map