[skip ci] plugin guard
This commit is contained in:
1 parent
c0dce836cf
commit
35b38c7cd8
17 files changed
+97
-15
No files matched your search
@@ -13,6 +13,7 @@ import {
|
||||
import path from "path";
|
||||
import { SGNode, SGScript } from "brighterscript/dist/parser/SGTypes";
|
||||
import { RawCodeStatement } from "./Classes/RawCodeStatement";
|
||||
import { exitOnCriticalErrors } from './critical-exit';
|
||||
|
||||
|
||||
const bindings_function_name = 'InitializeBindings';
|
||||
@@ -198,5 +199,5 @@ export class BindingsPlugin implements CompilerPlugin {
|
||||
}
|
||||
|
||||
export default () => {
|
||||
return new BindingsPlugin();
|
||||
return exitOnCriticalErrors(new BindingsPlugin());
|
||||
};
|
||||
@@ -13,6 +13,7 @@ import { SGChildren, SGComponent, SGInterface, SGScript } from 'brighterscript/d
|
||||
import { globSync } from 'glob';
|
||||
import fs from 'fs-extra'
|
||||
import path from 'path';
|
||||
import { exitOnCriticalErrors } from './critical-exit';
|
||||
|
||||
export class ComponentIncludesPlugin implements CompilerPlugin {
|
||||
public name = 'ComponentIncludesPlugin';
|
||||
@@ -160,5 +161,5 @@ export class ComponentIncludesPlugin implements CompilerPlugin {
|
||||
}
|
||||
|
||||
export default () => {
|
||||
return new ComponentIncludesPlugin();
|
||||
return exitOnCriticalErrors(new ComponentIncludesPlugin());
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
import { CompilerPlugin, ProgramBuilder } from 'brighterscript';
|
||||
|
||||
// A plugin that fails to load (e.g. a TypeScript error in its file) is only logged
|
||||
// by bsc and the build continues without it, exiting 0 (rokucommunity/brighterscript#1675).
|
||||
// Listed last in the plugins array, this guard fails the command when any configured
|
||||
// plugin is missing from the loaded set. It deliberately imports nothing from the
|
||||
// other plugin files, so it still loads when one of them is broken.
|
||||
const exitOnError = !!process.env.npm_lifecycle_event && !process.argv.includes('--lsp');
|
||||
|
||||
export class CriticalExitGuardPlugin implements CompilerPlugin {
|
||||
public name = 'CriticalExitGuardPlugin';
|
||||
|
||||
beforeProgramCreate(builder: ProgramBuilder) {
|
||||
if (!exitOnError) {
|
||||
return;
|
||||
}
|
||||
const configured = (builder as any)?.options?.plugins?.length ?? 0;
|
||||
const loaded = (builder as any)?.plugins?.plugins?.length ?? 0;
|
||||
if (loaded < configured) {
|
||||
console.error(`${configured - loaded} of ${configured} configured plugins failed to load (see "Error when loading plugin" above)`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default () => {
|
||||
return new CriticalExitGuardPlugin();
|
||||
};
|
||||
@@ -0,0 +1,37 @@
|
||||
import { Plugin } from 'brighterscript';
|
||||
|
||||
// bsc catches plugin exceptions and only logs them (rokucommunity/brighterscript#1675),
|
||||
// so a crashing plugin still yields a passing build/lint. When running under an npm
|
||||
// script (build, lint, test) an uncaught plugin error must fail the command, so every
|
||||
// hook is wrapped to exit non-zero. Long-lived hosts (LSP) keep log-and-continue.
|
||||
const exitOnError = !!process.env.npm_lifecycle_event && !process.argv.includes('--lsp');
|
||||
|
||||
export function exitOnCriticalErrors<T extends Plugin>(plugin: T): T {
|
||||
if (!exitOnError) {
|
||||
return plugin;
|
||||
}
|
||||
return new Proxy(plugin, {
|
||||
get(target, prop) {
|
||||
const value = (target as any)[prop];
|
||||
if (typeof value !== 'function') {
|
||||
return value;
|
||||
}
|
||||
return (...args: unknown[]) => {
|
||||
try {
|
||||
const result = value.apply(target, args);
|
||||
if (result instanceof Promise) {
|
||||
return result.catch((err: unknown) => exitCritically(target.name, prop, err));
|
||||
}
|
||||
return result;
|
||||
} catch (err) {
|
||||
exitCritically(target.name, prop, err);
|
||||
}
|
||||
};
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function exitCritically(pluginName: string, hook: PropertyKey, err: unknown): never {
|
||||
console.error(`Plugin ${pluginName} failed in ${String(hook)}:`, err instanceof Error ? (err.stack || err.message) : err);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
} from 'brighterscript';
|
||||
import { existsSync } from 'fs-extra';
|
||||
import path from 'path';
|
||||
import { exitOnCriticalErrors } from './critical-exit';
|
||||
|
||||
export class ImportValidationPlugin implements CompilerPlugin {
|
||||
public name = 'ImportValidationPlugin';
|
||||
@@ -45,5 +46,5 @@ export class ImportValidationPlugin implements CompilerPlugin {
|
||||
}
|
||||
|
||||
export default () => {
|
||||
return new ImportValidationPlugin();
|
||||
return exitOnCriticalErrors(new ImportValidationPlugin());
|
||||
};
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
DiagnosticSeverity,
|
||||
} from 'brighterscript';
|
||||
import path from 'path';
|
||||
import { exitOnCriticalErrors } from './critical-exit';
|
||||
|
||||
export class JobSystemPlugin implements CompilerPlugin {
|
||||
public name = 'JobSystemPlugin';
|
||||
@@ -115,5 +116,5 @@ export class JobSystemPlugin implements CompilerPlugin {
|
||||
}
|
||||
|
||||
export default () => {
|
||||
return new JobSystemPlugin();
|
||||
return exitOnCriticalErrors(new JobSystemPlugin());
|
||||
};
|
||||
@@ -7,6 +7,7 @@ import path from 'path';
|
||||
import fs from 'fs-extra'
|
||||
import json5 from 'json5';
|
||||
import YAML from "yaml";
|
||||
import { exitOnCriticalErrors } from './critical-exit';
|
||||
|
||||
const jsonExtensions = ['.json', '.jsonc', '.json5'];
|
||||
const yamlExtensions = ['.yaml', '.yml'];
|
||||
@@ -40,5 +41,5 @@ export class JsonYamlPlugin implements CompilerPlugin {
|
||||
}
|
||||
|
||||
export default () => {
|
||||
return new JsonYamlPlugin();
|
||||
return exitOnCriticalErrors(new JsonYamlPlugin());
|
||||
};
|
||||
@@ -27,6 +27,7 @@ import { globSync } from 'glob';
|
||||
import { SGNode } from "brighterscript/dist/parser/SGTypes";
|
||||
import json5 from 'json5';
|
||||
import YAML from "yaml";
|
||||
import { exitOnCriticalErrors } from './critical-exit';
|
||||
|
||||
const translatableXmlAttributes = ["text", "title", "short_title", "primaryTitle", "displayText", "description"];
|
||||
|
||||
@@ -417,5 +418,5 @@ export class LocaleValidationPlugin implements CompilerPlugin {
|
||||
}
|
||||
|
||||
export default () => {
|
||||
return new LocaleValidationPlugin();
|
||||
return exitOnCriticalErrors(new LocaleValidationPlugin());
|
||||
};
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
isBrsFile,
|
||||
} from 'brighterscript';
|
||||
import path from 'path';
|
||||
import { exitOnCriticalErrors } from './critical-exit';
|
||||
|
||||
const loggingFilePath = path.join('source', 'utils', 'Logging.bs');
|
||||
const logFunctions = {
|
||||
@@ -250,5 +251,5 @@ function ${newFunctionName}(${args.join(', ')}) as void
|
||||
}
|
||||
|
||||
export default () => {
|
||||
return new LoggerPlugin();
|
||||
return exitOnCriticalErrors(new LoggerPlugin());
|
||||
};
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
} from 'brighterscript';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { exitOnCriticalErrors } from './critical-exit';
|
||||
const ip = require('ip');
|
||||
|
||||
export class ManifestEditPlugin implements CompilerPlugin {
|
||||
@@ -76,5 +77,5 @@ export class ManifestEditPlugin implements CompilerPlugin {
|
||||
}
|
||||
|
||||
export default () => {
|
||||
return new ManifestEditPlugin();
|
||||
return exitOnCriticalErrors(new ManifestEditPlugin());
|
||||
};
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
} from 'brighterscript';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { exitOnCriticalErrors } from './critical-exit';
|
||||
|
||||
function copyDirSync(src: string, dest: string): void {
|
||||
fs.mkdirSync(dest, { recursive: true });
|
||||
@@ -82,5 +83,5 @@ export class OdcPlugin implements CompilerPlugin {
|
||||
}
|
||||
|
||||
export default () => {
|
||||
return new OdcPlugin();
|
||||
return exitOnCriticalErrors(new OdcPlugin());
|
||||
};
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
} from 'brighterscript';
|
||||
|
||||
import { RawCodeStatement } from './Classes/RawCodeStatement';
|
||||
import { exitOnCriticalErrors } from './critical-exit';
|
||||
|
||||
// The plugin processes the file that includes the Init function, not the file that includes the @oninit functions
|
||||
// This is because the modifications will be made to the Init function.
|
||||
@@ -209,5 +210,5 @@ export class OnInitPlugin implements CompilerPlugin {
|
||||
}
|
||||
|
||||
export default () => {
|
||||
return new OnInitPlugin();
|
||||
return exitOnCriticalErrors(new OnInitPlugin());
|
||||
};
|
||||
@@ -9,6 +9,7 @@ import { globSync } from 'glob';
|
||||
import path from 'path';
|
||||
import md5 from 'crypto-js/md5';
|
||||
import { Formatter, Runner } from 'brighterscript-formatter';
|
||||
import { exitOnCriticalErrors } from './critical-exit';
|
||||
const protobufSchema = require('protocol-buffers-schema')
|
||||
|
||||
export class ProtoGenPlugin implements CompilerPlugin {
|
||||
@@ -275,5 +276,5 @@ end function`;
|
||||
}
|
||||
|
||||
export default () => {
|
||||
return new ProtoGenPlugin();
|
||||
return exitOnCriticalErrors(new ProtoGenPlugin());
|
||||
};
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
Range,
|
||||
isBrsFile
|
||||
} from 'brighterscript';
|
||||
import { exitOnCriticalErrors } from './critical-exit';
|
||||
|
||||
type Validation = {
|
||||
code: number | string | undefined;
|
||||
@@ -76,5 +77,5 @@ export class ValidationPlugin implements CompilerPlugin {
|
||||
}
|
||||
|
||||
export default () => {
|
||||
return new ValidationPlugin();
|
||||
return exitOnCriticalErrors(new ValidationPlugin());
|
||||
};
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
isBrsFile
|
||||
} from "brighterscript";
|
||||
import { RawCodeStatement } from "./Classes/RawCodeStatement";
|
||||
import { exitOnCriticalErrors } from './critical-exit';
|
||||
|
||||
const annotationNames = ['all', 'get', 'post', 'put', 'delete', 'patch', 'head', 'options'];
|
||||
const httpRouterBaseClass = 'HttpRouter';
|
||||
@@ -155,5 +156,5 @@ export class WebServerPlugin implements CompilerPlugin {
|
||||
}
|
||||
|
||||
export default () => {
|
||||
return new WebServerPlugin();
|
||||
return exitOnCriticalErrors(new WebServerPlugin());
|
||||
};
|
||||
Reference in new issue
Block a user