(plugin, { filePath = EXTERNAL_PLUGINS_FILE } = {})
| 102 | } |
| 103 | |
| 104 | export function upsertExternalPlugin(plugin, { filePath = EXTERNAL_PLUGINS_FILE } = {}) { |
| 105 | const { plugins, errors } = readExternalPlugins({ |
| 106 | filePath, |
| 107 | localPluginNames: readLocalPluginNames(), |
| 108 | policy: "marketplace", |
| 109 | }); |
| 110 | |
| 111 | if (errors.length > 0) { |
| 112 | throw new Error(errors.join("\n")); |
| 113 | } |
| 114 | |
| 115 | const updatedPlugins = [...plugins]; |
| 116 | const existingIndex = updatedPlugins.findIndex((existingPlugin) => pluginsMatch(existingPlugin, plugin)); |
| 117 | const action = existingIndex === -1 ? "inserted" : "updated"; |
| 118 | |
| 119 | if (existingIndex === -1) { |
| 120 | updatedPlugins.push(plugin); |
| 121 | } else { |
| 122 | updatedPlugins[existingIndex] = plugin; |
| 123 | } |
| 124 | |
| 125 | updatedPlugins.sort((left, right) => left.name.localeCompare(right.name, undefined, { sensitivity: "base" })); |
| 126 | |
| 127 | const { errors: validationErrors } = validateExternalPlugins(updatedPlugins, { |
| 128 | localPluginNames: readLocalPluginNames(), |
| 129 | policy: "marketplace", |
| 130 | }); |
| 131 | |
| 132 | if (validationErrors.length > 0) { |
| 133 | throw new Error(validationErrors.join("\n")); |
| 134 | } |
| 135 | |
| 136 | const changed = JSON.stringify(updatedPlugins) !== JSON.stringify(plugins); |
| 137 | if (changed) { |
| 138 | fs.writeFileSync(filePath, `${JSON.stringify(updatedPlugins, null, 2)}\n`); |
| 139 | } |
| 140 | |
| 141 | return { |
| 142 | action, |
| 143 | changed, |
| 144 | plugin, |
| 145 | }; |
| 146 | } |
| 147 | |
| 148 | function readCliArgs(argv) { |
| 149 | const args = {}; |
no test coverage detected