mirror of
https://github.com/gristlabs/grist-core.git
synced 2024-10-27 20:44:07 +00:00
51ff72c15e
Summary: Building: - Builds no longer wait for tsc for either client, server, or test targets. All use esbuild which is very fast. - Build still runs tsc, but only to report errors. This may be turned off with `SKIP_TSC=1` env var. - Grist-core continues to build using tsc. - Esbuild requires ES6 module semantics. Typescript's esModuleInterop is turned on, so that tsc accepts and enforces correct usage. - Client-side code is watched and bundled by webpack as before (using esbuild-loader) Code changes: - Imports must now follow ES6 semantics: `import * as X from ...` produces a module object; to import functions or class instances, use `import X from ...`. - Everything is now built with isolatedModules flag. Some exports were updated for it. Packages: - Upgraded browserify dependency, and related packages (used for the distribution-building step). - Building the distribution now uses esbuild's minification. babel-minify is no longer used. Test Plan: Should have no behavior changes, existing tests should pass, and docker image should build too. Reviewers: georgegevoian Reviewed By: georgegevoian Subscribers: alexmojaki Differential Revision: https://phab.getgrist.com/D3506
166 lines
4.6 KiB
TypeScript
166 lines
4.6 KiB
TypeScript
import {DirectoryScanEntry, LocalPlugin} from 'app/common/plugin';
|
|
import log from 'app/server/lib/log';
|
|
import {readManifest} from 'app/server/lib/manifest';
|
|
import {getAppPathTo} from 'app/server/lib/places';
|
|
import * as fse from 'fs-extra';
|
|
import * as path from 'path';
|
|
|
|
/**
|
|
* Various plugins' related directories.
|
|
*/
|
|
export interface PluginDirectories {
|
|
/**
|
|
* Directory where built in plugins are located.
|
|
*/
|
|
readonly builtIn?: string;
|
|
/**
|
|
* Directory where user installed plugins are localted.
|
|
*/
|
|
readonly installed?: string;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* The plugin manager class is responsible for providing both built in and installed plugins and
|
|
* spawning server side plugins's.
|
|
*
|
|
* Usage:
|
|
*
|
|
* const pluginManager = new PluginManager(appRoot, userRoot);
|
|
* await pluginManager.initialize();
|
|
*
|
|
*/
|
|
export class PluginManager {
|
|
|
|
public pluginsLoaded: Promise<void>;
|
|
|
|
// ========== Instance members and methods ==========
|
|
private _dirs: PluginDirectories;
|
|
private _validPlugins: LocalPlugin[] = [];
|
|
private _entries: DirectoryScanEntry[] = [];
|
|
|
|
|
|
/**
|
|
* @param {string} userRoot: path to user's grist directory; `null` is allowed, to only uses built in plugins.
|
|
*
|
|
*/
|
|
public constructor(public appRoot?: string, userRoot?: string) {
|
|
this._dirs = {
|
|
installed: userRoot ? path.join(userRoot, 'plugins') : undefined,
|
|
builtIn: appRoot ? getAppPathTo(appRoot, 'plugins') : undefined
|
|
};
|
|
}
|
|
|
|
public dirs(): PluginDirectories { return this._dirs; }
|
|
|
|
/**
|
|
* Create tmp dir and load plugins.
|
|
*/
|
|
public async initialize(): Promise<void> {
|
|
try {
|
|
await (this.pluginsLoaded = this.loadPlugins());
|
|
} catch (err) {
|
|
log.error("PluginManager's initialization failed: ", err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Re-load plugins (literally re-run `loadPlugins`).
|
|
*/
|
|
// TODO: it's not clear right now what we do on reload. Do we deactivate plugins that were removed
|
|
// from the fs? Do we update plugins that have changed on the fs ?
|
|
public async reloadPlugins(): Promise<void> {
|
|
return await this.loadPlugins();
|
|
}
|
|
|
|
/**
|
|
* Discover both builtIn and user installed plugins. Logs any failures that happens when scanning
|
|
* a directory (ie: manifest missing or manifest validation errors etc...)
|
|
*/
|
|
public async loadPlugins(): Promise<void> {
|
|
this._entries = [];
|
|
|
|
// Load user installed plugins
|
|
if (this._dirs.installed) {
|
|
this._entries.push(...await scanDirectory(this._dirs.installed, "installed"));
|
|
}
|
|
|
|
// Load builtIn plugins
|
|
if (this._dirs.builtIn) {
|
|
this._entries.push(...await scanDirectory(this._dirs.builtIn, "builtIn"));
|
|
}
|
|
|
|
if (!process.env.GRIST_EXPERIMENTAL_PLUGINS ||
|
|
process.env.GRIST_EXPERIMENTAL_PLUGINS === '0') {
|
|
// Remove experimental plugins
|
|
this._entries = this._entries.filter(entry => {
|
|
if (entry.manifest && entry.manifest.experimental) {
|
|
log.warn("Ignoring experimental plugin %s", entry.id);
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
|
|
this._validPlugins = this._entries.filter(entry => !entry.errors).map(entry => entry as LocalPlugin);
|
|
|
|
this._logScanningReport();
|
|
}
|
|
|
|
public getPlugins(): LocalPlugin[] {
|
|
return this._validPlugins;
|
|
}
|
|
|
|
|
|
private _logScanningReport() {
|
|
const invalidPlugins = this._entries.filter( entry => entry.errors);
|
|
if (invalidPlugins.length) {
|
|
for (const plugin of invalidPlugins) {
|
|
log.warn(`Error loading plugins: Failed to load extension from ${plugin.path}\n` +
|
|
(plugin.errors!).map(m => " - " + m).join("\n ")
|
|
);
|
|
}
|
|
}
|
|
log.info(`Found ${this._validPlugins.length} valid plugins on the system`);
|
|
for (const p of this._validPlugins) {
|
|
log.debug("PLUGIN %s -- %s", p.id, p.path);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
async function scanDirectory(dir: string, kind: "installed"|"builtIn"): Promise<DirectoryScanEntry[]> {
|
|
const plugins: DirectoryScanEntry[] = [];
|
|
let listDir;
|
|
|
|
try {
|
|
listDir = await fse.readdir(dir);
|
|
} catch (e) {
|
|
// non existing dir is treated as an empty dir
|
|
log.info(`No plugins directory: ${e.message}`);
|
|
return [];
|
|
}
|
|
|
|
for (const id of listDir) {
|
|
const folderPath = path.join(dir, id),
|
|
plugin: DirectoryScanEntry = {
|
|
path: folderPath,
|
|
id: `${kind}/${id}`
|
|
};
|
|
try {
|
|
plugin.manifest = await readManifest(folderPath);
|
|
} catch (e) {
|
|
plugin.errors = [];
|
|
if (e.message) {
|
|
plugin.errors.push(e.message);
|
|
}
|
|
if (e.notices) {
|
|
plugin.errors.push(...e.notices);
|
|
}
|
|
}
|
|
plugins.push(plugin);
|
|
}
|
|
return plugins;
|
|
}
|