| import { readFileSync } from 'fs' |
|
|
| import { normalize } from 'path' |
|
|
| import Extension from './extension' |
| import { |
| getAllExtensions, |
| removeExtension, |
| persistExtensions, |
| installExtensions, |
| getExtension, |
| getActiveExtensions, |
| addExtension, |
| } from './store' |
| import { ExtensionManager } from './manager' |
|
|
| export function init(options: any) { |
| |
| registerExtensionProtocol() |
|
|
| |
| if (options.extensionsPath) { |
| return useExtensions(options.extensionsPath) |
| } |
|
|
| return {} |
| } |
|
|
| |
| |
| |
| |
| |
| async function registerExtensionProtocol() { |
| let electron: any = undefined |
|
|
| try { |
| const moduleName = 'electron' |
| electron = await import(moduleName) |
| } catch (err) { |
| console.error('Electron is not available') |
| } |
| const extensionPath = ExtensionManager.instance.getExtensionsPath() |
| if (electron && electron.protocol) { |
| return electron.protocol?.registerFileProtocol('extension', (request: any, callback: any) => { |
| const entry = request.url.substr('extension://'.length - 1) |
|
|
| const url = normalize(extensionPath + entry) |
| callback({ path: url }) |
| }) |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function useExtensions(extensionsPath: string) { |
| if (!extensionsPath) throw Error('A path to the extensions folder is required to use extensions') |
| |
| ExtensionManager.instance.setExtensionsPath(extensionsPath) |
|
|
| |
| for (const extension of getAllExtensions()) { |
| if (extension.name) removeExtension(extension.name, false) |
| } |
|
|
| |
| const extensions = JSON.parse( |
| readFileSync(ExtensionManager.instance.getExtensionsFile(), 'utf-8') |
| ) |
| try { |
| |
| for (const p in extensions) { |
| loadExtension(extensions[p]) |
| } |
| persistExtensions() |
| } catch (error) { |
| |
| throw new Error( |
| 'Could not successfully rebuild list of installed extensions.\n' + |
| error + |
| '\nPlease check the extensions.json file in the extensions folder.' |
| ) |
| } |
|
|
| |
| return getStore() |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function loadExtension(ext: any) { |
| |
| const extension = new Extension() |
|
|
| for (const key in ext) { |
| if (Object.prototype.hasOwnProperty.call(ext, key)) { |
| |
| Object.defineProperty(extension, key, { |
| value: ext[key], |
| writable: true, |
| enumerable: true, |
| configurable: true, |
| }) |
| } |
| } |
| addExtension(extension, false) |
| extension.subscribe('pe-persist', persistExtensions) |
| } |
|
|
| |
| |
| |
| |
| export function getStore() { |
| if (!ExtensionManager.instance.getExtensionsFile()) { |
| throw new Error( |
| 'The extension path has not yet been set up. Please run useExtensions before accessing the store' |
| ) |
| } |
|
|
| return { |
| installExtensions, |
| getExtension, |
| getAllExtensions, |
| getActiveExtensions, |
| removeExtension, |
| } |
| } |
|
|