|
| 1 | +/* |
| 2 | + * GNU AGPL-3.0 License |
| 3 | + * |
| 4 | + * Copyright (c) 2021 - present core.ai . All rights reserved. |
| 5 | + * Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved. |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify it |
| 8 | + * under the terms of the GNU Affero General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License |
| 15 | + * for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see https://opensource.org/licenses/AGPL-3.0. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +/*globals path, logger*/ |
| 23 | + |
| 24 | +define(function (require, exports, module) { |
| 25 | + const ProjectManager = brackets.getModule("project/ProjectManager"), |
| 26 | + Commands = brackets.getModule("command/Commands"), |
| 27 | + CommandManager = brackets.getModule("command/CommandManager"), |
| 28 | + Strings = brackets.getModule("strings"), |
| 29 | + StringUtils = brackets.getModule("utils/StringUtils"), |
| 30 | + DocumentManager = brackets.getModule("document/DocumentManager"), |
| 31 | + DefaultDialogs = brackets.getModule("widgets/DefaultDialogs"), |
| 32 | + Dialogs = brackets.getModule("widgets/Dialogs"), |
| 33 | + UrlParams = brackets.getModule("utils/UrlParams").UrlParams, |
| 34 | + FileSystem = brackets.getModule("filesystem/FileSystem"); |
| 35 | + |
| 36 | + function _showError(message, title = Strings.ERROR_LOADING_EXTENSION) { |
| 37 | + Dialogs.showModalDialog( |
| 38 | + DefaultDialogs.DIALOG_ID_ERROR, |
| 39 | + title, message |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + function _validatePackageJson(docText) { |
| 44 | + try { |
| 45 | + let packageJson = JSON.parse(docText); |
| 46 | + let requiredFields = ["name", "title", "description", "homepage", "version", "author", "license", |
| 47 | + "engines"]; |
| 48 | + let missingFields = []; |
| 49 | + for(let requiredField of requiredFields){ |
| 50 | + if(!packageJson[requiredField]){ |
| 51 | + missingFields.push(requiredField); |
| 52 | + } |
| 53 | + } |
| 54 | + if(missingFields.length){ |
| 55 | + _showError(StringUtils.format(Strings.ERROR_INVALID_EXTENSION_PACKAGE_FIELDS, missingFields)); |
| 56 | + return false; |
| 57 | + } |
| 58 | + return true; |
| 59 | + } catch (e) { |
| 60 | + console.log("Cannot load extension", Strings.ERROR_INVALID_EXTENSION_PACKAGE); |
| 61 | + _showError(Strings.ERROR_INVALID_EXTENSION_PACKAGE); |
| 62 | + return false; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + function loadCurrentExtension() { |
| 67 | + const projectRoot = ProjectManager.getProjectRoot().fullPath; |
| 68 | + const file = FileSystem.getFileForPath(projectRoot + "package.json"); |
| 69 | + DocumentManager.getDocumentText(file).done(function (docText) { |
| 70 | + console.log(docText); |
| 71 | + if(!_validatePackageJson(docText)){ |
| 72 | + return; |
| 73 | + } |
| 74 | + CommandManager.execute(Commands.APP_RELOAD, false, projectRoot); |
| 75 | + }).fail((err)=>{ |
| 76 | + console.log("No extension package.json in ", file.fullPath, err); |
| 77 | + Dialogs.showModalDialog( |
| 78 | + DefaultDialogs.DIALOG_ID_ERROR, |
| 79 | + Strings.ERROR_LOADING_EXTENSION, |
| 80 | + Strings.ERROR_NO_EXTENSION_PACKAGE |
| 81 | + ); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + function unloadCurrentExtension() { |
| 86 | + CommandManager.execute(Commands.APP_RELOAD, false, []); |
| 87 | + } |
| 88 | + |
| 89 | + function isProjectLoadedAsExtension() { |
| 90 | + const params = new UrlParams(); |
| 91 | + |
| 92 | + // Make sure the Reload Without User Extensions parameter is removed |
| 93 | + params.parse(); |
| 94 | + return !!params.get("loadDevExtensionPath"); |
| 95 | + } |
| 96 | + |
| 97 | + exports.loadCurrentExtension = loadCurrentExtension; |
| 98 | + exports.unloadCurrentExtension = unloadCurrentExtension; |
| 99 | + exports.isProjectLoadedAsExtension = isProjectLoadedAsExtension; |
| 100 | +}); |
0 commit comments