From b969be0315f09bfed7824e5fd8f2527e27a40f90 Mon Sep 17 00:00:00 2001 From: Steven Prybylynskyi Date: Fri, 29 Dec 2023 00:10:38 +0100 Subject: [PATCH] style: fix lint issues --- package.json | 5 ++-- src/constants.ts | 2 +- src/helpers/compileTypes.ts | 11 ++++----- src/make-federated-types.ts | 22 ++++++++++------- src/plugin.ts | 48 +++++++++++++++++++++++-------------- src/types.ts | 6 ++--- 6 files changed, 56 insertions(+), 38 deletions(-) diff --git a/package.json b/package.json index f75547e..ec176bd 100644 --- a/package.json +++ b/package.json @@ -13,11 +13,12 @@ "dist" ], "bin": { - "make-federated-types": "dist/make-federated-types.js" + "make-federated-types": "dist/make-federated-types.js", + "download-federated-types": "dist/download-federated-types.js" }, "scripts": { "build": "tsc", - "lint": "eslint src/**/*.ts", + "lint": "eslint 'src/**/*.ts'", "test": "jest" }, "dependencies": { diff --git a/src/constants.ts b/src/constants.ts index 64e2e3b..89b3c7a 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -5,7 +5,7 @@ export const DEFAULT_DIR_DIST = 'dist'; export const DEFAULT_DIR_EMITTED_TYPES = '@types'; export const DEFAULT_DIR_GLOBAL_TYPES = 'src/@types'; -export const DEFAULT_DIR_DOWNLOADED_TYPES = `src/@types/remotes`; +export const DEFAULT_DIR_DOWNLOADED_TYPES = 'src/@types/remotes'; export const DEFAULT_DOWNLOAD_TYPES_INTERVAL_IN_SECONDS = 60; diff --git a/src/helpers/compileTypes.ts b/src/helpers/compileTypes.ts index 1b68da7..51f4b90 100644 --- a/src/helpers/compileTypes.ts +++ b/src/helpers/compileTypes.ts @@ -127,12 +127,11 @@ export function rewritePathsWithExposedFederatedModules( const declaredModulePaths: string[] = []; // Collect all instances of `declare module "..."` - let execResults: null | string[] = []; - while (true) { - execResults = regexDeclareModule.exec(typings); - if (execResults === null) { - break; - } + for ( + let execResults: null | string[] = regexDeclareModule.exec(typings); + execResults !== null; + execResults = regexDeclareModule.exec(typings) + ) { declaredModulePaths.push(execResults[1]); } diff --git a/src/make-federated-types.ts b/src/make-federated-types.ts index 50757e2..1bb99f7 100644 --- a/src/make-federated-types.ts +++ b/src/make-federated-types.ts @@ -1,11 +1,18 @@ #!/usr/bin/env node -import parseArgs from 'minimist'; import path from 'path'; -import { DEFAULT_DIR_DIST, DEFAULT_DIR_EMITTED_TYPES, DEFAULT_DIR_GLOBAL_TYPES, TS_CONFIG_FILE } from './constants'; -import { compileTypes, rewritePathsWithExposedFederatedModules } from './helpers/compileTypes'; -import { assertRunningFromRoot, getFederationConfig } from './helpers/cli'; +import parseArgs from 'minimist'; + +import { + DEFAULT_DIR_DIST, DEFAULT_DIR_EMITTED_TYPES, DEFAULT_DIR_GLOBAL_TYPES, TS_CONFIG_FILE, +} from './constants'; +import { + compileTypes, rewritePathsWithExposedFederatedModules, +} from './helpers/compileTypes'; +import { + assertRunningFromRoot, getFederationConfig, +} from './helpers/cli'; assertRunningFromRoot(); @@ -14,7 +21,6 @@ type Argv = { 'federation-config': string, 'output-types-folder': string, 'tsconfig': string, - } const argv = parseArgs(process.argv.slice(2), { @@ -22,7 +28,7 @@ const argv = parseArgs(process.argv.slice(2), { 'global-types': 'g', 'output-types-folder': 'o', 'federation-config': 'c', - 'tsconfig': 't', + tsconfig: 't', } as Partial, }); @@ -32,13 +38,13 @@ const compileFiles = Object.values(federationConfig.exposes); const outDir = argv['output-types-folder'] || path.join(DEFAULT_DIR_DIST, DEFAULT_DIR_EMITTED_TYPES); const outFile = path.join(outDir, 'index.d.ts'); const dirGlobalTypes = argv['global-types'] || DEFAULT_DIR_GLOBAL_TYPES; -const tsconfigPath = argv['tsconfig'] || TS_CONFIG_FILE; +const tsconfigPath = argv.tsconfig || TS_CONFIG_FILE; console.log(`Emitting types for ${compileFiles.length} exposed module(s)`); const { isSuccess, typeDefinitions } = compileTypes( tsconfigPath, - compileFiles as string[], + compileFiles, outFile, dirGlobalTypes, ); diff --git a/src/plugin.ts b/src/plugin.ts index c7c8fdf..ca4e206 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,5 +1,8 @@ import path from 'path'; -import { Compiler, WebpackPluginInstance } from 'webpack'; + +import { + Compiler, WebpackPluginInstance, +} from 'webpack'; import { CLOUDBEDS_DEPLOYMENT_ENV_WITH_DISABLED_REMOTE_TYPES_DOWNLOAD, @@ -11,9 +14,13 @@ import { TS_CONFIG_FILE, } from './constants'; import { getRemoteManifestUrls } from './helpers/cloudbedsRemoteManifests'; -import { compileTypes, rewritePathsWithExposedFederatedModules } from './helpers/compileTypes'; +import { + compileTypes, rewritePathsWithExposedFederatedModules, +} from './helpers/compileTypes'; import { downloadTypes } from './helpers/downloadTypes'; -import { getLoggerHint, setLogger } from './helpers/logger'; +import { + getLoggerHint, setLogger, +} from './helpers/logger'; import { isEveryUrlValid } from './helpers/validation'; import { FederationConfig, @@ -22,16 +29,16 @@ import { } from './types'; let isCompiledOnce = false; -let isDownloadedOnce = false; +const isDownloadedOnce = false; const DEFAULT_MODULE_FEDERATION_PLUGIN_NAME = 'ModuleFederationPlugin'; export class ModuleFederationTypesPlugin implements WebpackPluginInstance { constructor(public options?: ModuleFederationTypesPluginOptions) {} - async apply(compiler: Compiler): Promise { + apply(compiler: Compiler): void { const PLUGIN_NAME = this.constructor.name; - let logger = setLogger(compiler.getInfrastructureLogger(PLUGIN_NAME)); + const logger = setLogger(compiler.getInfrastructureLogger(PLUGIN_NAME)); const remoteEntryUrls = this.options?.remoteEntryUrls; const remoteManifestUrls = getRemoteManifestUrls(this.options); @@ -58,13 +65,17 @@ export class ModuleFederationTypesPlugin implements WebpackPluginInstance { } // Allow for other module federation plugins such as this "NextFederationPlugin" - const moduleFederationPluginName = this.options?.moduleFederationPluginName ?? DEFAULT_MODULE_FEDERATION_PLUGIN_NAME; + const moduleFederationPluginName = this.options?.moduleFederationPluginName + ?? DEFAULT_MODULE_FEDERATION_PLUGIN_NAME; // Get ModuleFederationPlugin config - const federationOptions = compiler.options.plugins.find((plugin) => { - return plugin!.constructor.name === moduleFederationPluginName; - }); + const federationOptions = compiler.options.plugins.find( + plugin => plugin!.constructor.name === moduleFederationPluginName, + ); + + // eslint-disable-next-line no-underscore-dangle const federationPluginOptions: ModuleFederationPluginOptions = (federationOptions as any)?._options; + if (!federationPluginOptions?.name) { logger.warn( `Plugin disabled as ${moduleFederationPluginName} is not configured properly. The 'name' option is missing.`, @@ -101,21 +112,19 @@ export class ModuleFederationTypesPlugin implements WebpackPluginInstance { }; // Import types from remote modules - const downloadRemoteTypes = async () => { - return downloadTypes( - dirEmittedTypes, - dirDownloadedTypes, + const downloadRemoteTypes = async () => downloadTypes( + dirEmittedTypes, + dirDownloadedTypes, remotes as Dict, remoteEntryUrls, remoteManifestUrls, - ); - }; + ); // Determine whether compilation of types should be performed continuously // followed by downloading of types when idle for a certain period of time let recompileIntervalId: ReturnType; const shouldSyncContinuously = (compiler.options.mode === 'development') - && (this.options?.downloadTypesWhenIdleIntervalInSeconds !== -1) + && (this.options?.downloadTypesWhenIdleIntervalInSeconds !== -1); const downloadTypesWhenIdleIntervalInSeconds = this.options?.downloadTypesWhenIdleIntervalInSeconds || DEFAULT_DOWNLOAD_TYPES_INTERVAL_IN_SECONDS; @@ -127,7 +136,9 @@ export class ModuleFederationTypesPlugin implements WebpackPluginInstance { () => { logger.log( new Date().toLocaleString(), - 'Downloading types every', downloadTypesWhenIdleIntervalInSeconds, 'seconds', + 'Downloading types every', + downloadTypesWhenIdleIntervalInSeconds, + 'seconds', ); downloadRemoteTypes(); }, @@ -143,6 +154,7 @@ export class ModuleFederationTypesPlugin implements WebpackPluginInstance { logger.log('Downloading types on startup'); return downloadRemoteTypes(); }); + // eslint-disable-next-line @typescript-eslint/no-misused-promises compiler.hooks.watchRun.tap(PLUGIN_NAME, () => { if (!isDownloadedOnce) { logger.log('Downloading types on startup'); diff --git a/src/types.ts b/src/types.ts index b696828..61293c4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -28,13 +28,13 @@ export type ModuleFederationPluginOptions = ConstructorParameters