@extjs/sencha-cmd-linux-32
Version:
Productivity and performance optimization tool for building applications with Sencha Ext JS and Sencha Touch.
236 lines (200 loc) • 6.31 kB
JavaScript
/*
* Copyright (c) 2012-2016. Sencha Inc.
*/
;
var Fashion = require('./export/Base.js');
var isNode = (function () {
try {
return Object.prototype.toString.call(global.process) === '[object process]';
} catch (e) {
return false;
}
})();
var isPhantom = typeof phantom !== 'undefined';
var isRhino = typeof importPackage !== 'undefined';
var isBrowser = !(isNode || isRhino || isPhantom);
var canSetPrototype = (function () {
var a = {x: 42},
b = {};
try {
b.__proto__ = a;
} catch (e) {
// oh well...
}
return b.x === 42;
})();
var fs;
if (isNode || isPhantom) {
fs = require('fs');
}
class Env {
constructor() {
Fashion.apply(this, {
isNode: isNode,
isPhantom: isPhantom,
isRhino: isRhino,
isBrowser: isBrowser,
canSetPrototype: canSetPrototype,
fs: fs
});
}
exists(path) {
var fs = this.fs;
try {
if (this.isRhino && !this.isPhantom) {
return new java.io.File(path).exists();
}
if (this.isPhantom) {
return fs.exists(path);
}
this.readFile(path);
return true;
} catch (e) {
return false;
}
}
join(dir, subpath) {
return dir + "/" + subpath;
}
readFileRhino(file) {
Fashion.raise("function 'Fashion.Env.readFileRhino' has no default implementation");
}
readFile(file) {
if (this.isRhino) {
return this.readFileRhino(file);
}
if (this.isNode) {
return this.fs.readFileSync(file);
}
return this.doRequest({
url: file,
async: false,
method: 'GET'
});
}
loadFileRhino(file, success, error) {
Fashion.raise("function 'Fashion.Env.readFileRhino' has no default implementation");
}
loadFile(file, success, error, options, retries) {
if (this.isBrowser) {
retries = retries || 0;
this.doRequest(Fashion.merge({
url: file,
async: true,
params: {
_dc: new Date().getTime()
},
onComplete: function (options, xhr) {
if (success) {
success(xhr.responseText, xhr);
}
},
onError: function () {
if (retries < 3) {
this.loadFile(file, success, error, options, retries + 1);
}
else {
error && error.apply(error, arguments);
}
}
}, options));
} else if (this.isNode) {
this.fs.readFile(file, 'utf-8', function (err, data) {
if (err) {
if (error) {
error(err);
}
} else {
if (data.charCodeAt(0) === 0xFEFF || data.charCodeAt(0) === 0xFFFE) {
data = data.substr(1);
}
success(data);
}
});
} else if (this.isRhino) {
this.loadFileRhino(file, success, error);
}
}
doRequest(options) {
var url = options.url,
method = options.method || 'GET',
data = options.data || null,
async = options.async !== false,
onComplete = options.onComplete,
onError = options.onError,
scope = options.scope || this,
params = options.params,
queryParams = [],
arrayBufferSupported = false,
queryParamStr, xhr, content, sep;
if (params) {
for (var name in params) {
queryParams.push(name + "=" + params[name]);
}
queryParamStr = queryParams.join('&');
if (queryParamStr !== '') {
sep = url.indexOf('?') > -1 ? '&' : '?';
url = url + sep + queryParamStr;
}
}
if (typeof XMLHttpRequest !== 'undefined') {
xhr = new XMLHttpRequest();
arrayBufferSupported = typeof xhr.responseType === 'string';
} else {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
//console.log("requesting url : " + url);
xhr.open(method, url, async);
if (async) {
xhr.timeout = 5 * 1000;
}
if (options.binary) {
if (arrayBufferSupported) {
xhr.responseType = 'arraybuffer';
xhr.getBinaryData = function () {
return new Uint8Array(this.response);
};
} else {
xhr.overrideMimeType("text/plain; charset=x-user-defined");
xhr.getBinaryData = function () {
return this.responseText;
};
}
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
try {
if (xhr.status === 200) {
if (onComplete) {
onComplete.call(scope, options, xhr);
}
} else {
if (onError) {
onError.call(scope, options, xhr);
}
}
} catch (err) {
Fashion.error((err.stack || err) + '');
if (onError) {
onError.call(scope, options, xhr, err);
}
} finally {
//advanceRequestQueue();
}
}
};
xhr.onerror = onError;
if (typeof data === "function") {
data = data();
}
if (typeof data !== 'string') {
data = JSON.stringify(data);
}
xhr.send(data);
if (!async) {
content = xhr.responseText;
return content;
}
}
};
module.exports = new Env();