Repository navigation
Expand file tree
/
Copy pathPackage.js
More file actions
1 lines (1 loc) · 6.22 KB
/
Copy pathPackage.js
File metadata and controls
1 lines (1 loc) · 6.22 KB
1
define(function(require,exports,module){var AppInit=require("utils/AppInit"),FileSystem=require("filesystem/FileSystem"),FileUtils=require("file/FileUtils"),StringUtils=require("utils/StringUtils"),Strings=require("strings"),ExtensionLoader=require("utils/ExtensionLoader"),PreferencesManager=require("preferences/PreferencesManager"),PathUtils=require("thirdparty/path-utils/path-utils"),ExtensionDownloader=require("extensibility/ExtensionDownloader");PreferencesManager.definePreference("proxy","string",void 0,{description:Strings.DESCRIPTION_PROXY});const DISABLED_EXTENSIONS_KEY="extensions.disabled";var Errors={ERROR_LOADING:"ERROR_LOADING",MALFORMED_URL:"MALFORMED_URL",UNSUPPORTED_PROTOCOL:"UNSUPPORTED_PROTOCOL"},InstallationStatuses={FAILED:"FAILED",INSTALLED:"INSTALLED",ALREADY_INSTALLED:"ALREADY_INSTALLED",SAME_VERSION:"SAME_VERSION",OLDER_VERSION:"OLDER_VERSION",NEEDS_UPDATE:"NEEDS_UPDATE",DISABLED:"DISABLED"},_uniqueId=0;function install(path,nameHint,_doUpdate){const d=new $.Deferred,destinationDirectory=ExtensionLoader.getUserExtensionPath(),disabledDirectory=destinationDirectory.replace(/\/user$/,"/disabled"),systemDirectory=FileUtils.getNativeBracketsDirectoryPath()+"/extensions/default/",operation=_doUpdate?"update":"install";return ExtensionDownloader[operation](path,destinationDirectory,{disabledDirectory:disabledDirectory,systemExtensionDirectory:systemDirectory,apiVersion:brackets.metadata.apiVersion,nameHint:nameHint,proxy:PreferencesManager.get("proxy")}).done(function(result){result.installationStatus!==InstallationStatuses.INSTALLED||_doUpdate?d.resolve(result):ExtensionLoader.loadExtension(result.name,{baseUrl:window.Phoenix.VFS.getVirtualServingURLForPath(result.installedTo),nativeDir:result.installedTo},"main").then(function(){d.resolve(result)},function(){d.reject(Errors.ERROR_LOADING)})}).fail(function(error){d.reject(error)}),d.promise()}function githubURLFilter(urlInfo){if("github.com"===urlInfo.parsed.hostname||"www.github.com"===urlInfo.parsed.hostname){var match=/^\/[^\/?]+\/([^\/?]+)(\/?)$/.exec(urlInfo.parsed.pathname);match?(match[2]||(urlInfo.url+="/"),urlInfo.url+="archive/master.zip",urlInfo.filenameHint=match[1]+".zip"):(match=/^\/[^\/?]+\/([^\/?]+)\/archive\/master.zip$/.exec(urlInfo.parsed.pathname))&&(urlInfo.filenameHint=match[1]+".zip")}}function _download(url,downloadId,destinationDirectory){const d=new $.Deferred,parsed=PathUtils.parseUrl(url);if(!parsed.hostname)return d.reject(Errors.MALFORMED_URL),d.promise();if("http:"!==parsed.protocol&&"https:"!==parsed.protocol&&"phtauri:"!==parsed.protocol&&"asset:"!==parsed.protocol)return d.reject(Errors.UNSUPPORTED_PROTOCOL),d.promise();parsed.filename=FileUtils.convertWindowsPathToUnixPath(parsed.filename);const urlInfo={url:url,parsed:parsed,filenameHint:parsed.filename,destinationDirectory:destinationDirectory};githubURLFilter(urlInfo);let filename=urlInfo.filenameHint;(filename=filename.replace(/[^a-zA-Z0-9_\- \(\)\.]/g,"_"))||(filename="extension.zip");const r=ExtensionDownloader.downloadFile(downloadId,urlInfo,PreferencesManager.get("proxy"));return r.done(function(result){d.resolve({localPath:FileUtils.convertWindowsPathToUnixPath(result),filenameHint:urlInfo.filenameHint})}).fail(function(err){d.reject(err)}),d.promise()}function cancelDownload(downloadId){ExtensionDownloader.abortDownload(downloadId)}function installFromPath(path,filenameHint){const d=new $.Deferred;return install(path,filenameHint).done(function(result){let installationStatus=result.installationStatus;installationStatus===InstallationStatuses.ALREADY_INSTALLED||installationStatus===InstallationStatuses.NEEDS_UPDATE||installationStatus===InstallationStatuses.SAME_VERSION||installationStatus===InstallationStatuses.OLDER_VERSION?d.resolve(result):result.errors&&result.errors.length>0?d.reject(result.errors[0]):result.disabledReason?d.reject(result.disabledReason):d.resolve(result)}).fail(function(err){d.reject(err)}),d.promise()}function installFromURL(url,destinationDirectory){const STATE_DOWNLOADING=1,STATE_INSTALLING=2,STATE_SUCCEEDED=3,STATE_FAILED=4,d=new $.Deferred;let state=1;var downloadId=_uniqueId++;return _download(url,downloadId,destinationDirectory).done(function(downloadResult){state=2,installFromPath(downloadResult.localPath,downloadResult.filenameHint).done(function(result){state=3,result.localPath=downloadResult.localPath,d.resolve(result)}).fail(function(err){state=4,FileSystem.getFileForPath(downloadResult.localPath).unlink(),url&&!url.startsWith(brackets.config.extension_url)||logger.reportError(err,"Failed to install "+url),d.reject(err)})}).fail(function(err){state=4,d.reject(err)}),{promise:d.promise(),cancel:function(){1===state&&cancelDownload(downloadId)}}}function formatError(error){function localize(key){return Strings[key]?Strings[key]:(console.log("Unknown installation error",key),Strings.UNKNOWN_ERROR)}return Array.isArray(error)?(error[0]=localize(error[0]),StringUtils.format.apply(window,error)):localize(error)}function remove(path){return ExtensionDownloader.remove(path)}function _toggleDisabledExtension(path,enabled){let arr=JSON.parse(PhStore.getItem(DISABLED_EXTENSIONS_KEY)||"[]");const io=arr.indexOf(path);!0===enabled&&-1!==io?arr.splice(io,1):!1===enabled&&-1===io&&arr.push(path),PhStore.setItem(DISABLED_EXTENSIONS_KEY,JSON.stringify(arr))}function disable(path){const result=new $.Deferred;return _toggleDisabledExtension(path,!1),result.resolve(),result.promise()}function enable(path){const result=new $.Deferred;return _toggleDisabledExtension(path,!0),ExtensionLoader.loadExtension(FileUtils.getBaseName(path),{baseUrl:path,nativeDir:path},"main").done(result.resolve).fail(result.reject),result.promise()}function installUpdate(path,nameHint){const d=new $.Deferred;return install(path,nameHint,!0).done(function(result){result.installationStatus!==InstallationStatuses.INSTALLED?d.reject(result.errors):d.resolve(result)}).fail(function(error){d.reject(error)}),d.promise()}AppInit.appReady(function(){}),exports.installFromURL=installFromURL,exports.installFromPath=installFromPath,exports.install=install,exports.remove=remove,exports.disable=disable,exports.enable=enable,exports.installUpdate=installUpdate,exports.formatError=formatError,exports.InstallationStatuses=InstallationStatuses,exports.DEFAULT_DISABLED_EXTENSIONS_KEY=DISABLED_EXTENSIONS_KEY});