Skip to content

Commit

Permalink
Prevent unnessary invocations of plugin dependency finder
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Sep 27, 2023
1 parent a8dea3f commit 977e756
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 10 deletions.
16 changes: 12 additions & 4 deletions src/WorkspaceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as npm from './manifest/index.js';
import * as plugins from './plugins/index.js';
import { debugLogArray, debugLogObject } from './util/debug.js';
import { _pureGlob, negate, hasProductionSuffix, hasNoProductionSuffix, prependDirToPattern } from './util/glob.js';
import { getKeysByValue } from './util/object.js';
import { get, getKeysByValue } from './util/object.js';
import { join, toPosix } from './util/path.js';
import {
fromEntryPattern,
Expand Down Expand Up @@ -240,13 +240,21 @@ export class WorkspaceWorker {
if (!pluginConfig) continue;

const patterns = this.getConfigurationFilePatterns(pluginName);
const configFilePaths = await _pureGlob({ patterns, cwd, ignore, gitignore: false });
const allConfigFilePaths = await _pureGlob({ patterns, cwd, ignore, gitignore: false });

const configFilePaths = allConfigFilePaths.filter(
filePath =>
!filePath.endsWith('package.json') ||
get(this.manifest, 'PACKAGE_JSON_PATH' in plugin ? plugin.PACKAGE_JSON_PATH : pluginName)
);

debugLogArray(`Found ${plugin.NAME} config file paths`, configFilePaths);

// TODO Fix up
// Bail out, no config files found for this plugin
if (patterns.length > 0 && configFilePaths.length === 0) continue;
if (patterns.length === 0 && configFilePaths.length === 0) configFilePaths.push('fake');

// Plugin has no config files configured, call it once to still get the entry:/production: patterns
if (patterns.length === 0) configFilePaths.push('__FAKE__');

const pluginDependencies: Set<string> = new Set();

Expand Down
2 changes: 2 additions & 0 deletions src/plugins/commitizen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const NAME = 'Commitizen';
/** @public */
export const ENABLERS = ['commitizen'];

export const PACKAGE_JSON_PATH = 'config.commitizen';

export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(dependencies, ENABLERS);

export const CONFIG_FILE_PATTERNS = ['.czrc', '.cz.json', 'package.json'];
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/eslint/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { isInternal, dirname, toAbsolute } from '../../util/path.js';
import { load } from '../../util/plugin.js';
import { _resolve } from '../../util/require.js';
import { fallback } from './fallback.js';
import { PACKAGE_JSON_PATH } from './index.js';
import type { ESLintConfig, OverrideConfig } from './types.js';
import type { PackageJson } from '@npmcli/package-json';

Expand Down Expand Up @@ -42,7 +43,7 @@ export const getDependenciesDeep: GetDependenciesDeep = async (configFilePath, d
const addAll = (deps: string[] | Set<string>) => deps.forEach(dependency => dependencies.add(dependency));

const config = configFilePath.endsWith('package.json')
? options.manifest.eslintConfig
? options.manifest[PACKAGE_JSON_PATH]
: /(\.(jsonc?|ya?ml)|rc)$/.test(configFilePath)
? await load(configFilePath)
: await fallback(configFilePath);
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/eslint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export const NAME = 'ESLint';
/** @public */
export const ENABLERS = ['eslint'];

export const PACKAGE_JSON_PATH = 'eslintConfig';

export const isEnabled: IsPluginEnabledCallback = ({ dependencies, manifest, config }) =>
hasDependency(dependencies, ENABLERS) ||
'eslint' in config ||
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/npm-package-json-lint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const NAME = 'npm-package-json-lint';
/** @public */
export const ENABLERS = ['npm-package-json-lint'];

export const PACKAGE_JSON_PATH = 'npmpackagejsonlint';

export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(dependencies, ENABLERS);

export const CONFIG_FILE_PATTERNS = ['.npmpackagejsonlintrc.json', 'npmpackagejsonlint.config.js', 'package.json'];
Expand All @@ -20,7 +22,7 @@ const findNpmPkgJsonLintConfigDependencies: GenericPluginCallback = async (
) => {
if (isProduction) return [];
const config: NpmPkgJsonLintConfig = configFilePath.endsWith('package.json')
? manifest.npmpackagejsonlint
? manifest[PACKAGE_JSON_PATH]
: await load(configFilePath);
return config?.extends ? [config.extends] : [];
};
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/release-it/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const NAME = 'Release It';
/** @public */
export const ENABLERS = ['release-it'];

export const PACKAGE_JSON_PATH = 'release-it';

export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(dependencies, ENABLERS);

export const CONFIG_FILE_PATTERNS = [
Expand All @@ -24,7 +26,7 @@ const findReleaseItDependencies: GenericPluginCallback = async (configFilePath,
if (isProduction) return [];

const config: ReleaseItConfig = configFilePath.endsWith('package.json')
? manifest['release-it']
? manifest[PACKAGE_JSON_PATH]
: await load(configFilePath);

if (!config) return [];
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/remark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const NAME = 'Remark';
/** @public */
export const ENABLERS = ['remark-cli'];

export const PACKAGE_JSON_PATH = 'remarkConfig';

export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(dependencies, ENABLERS);

export const CONFIG_FILE_PATTERNS = [
Expand All @@ -27,7 +29,7 @@ const findRemarkDependencies: GenericPluginCallback = async (configFilePath, { m
if (isProduction) return [];

const config: RemarkConfig = configFilePath.endsWith('package.json')
? manifest.remarkConfig
? manifest[PACKAGE_JSON_PATH]
: await load(configFilePath);

if (!config) return [];
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/semantic-release/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const NAME = 'Semantic Release';
/** @public */
export const ENABLERS = ['semantic-release'];

export const PACKAGE_JSON_PATH = 'release';

export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(dependencies, ENABLERS);

export const CONFIG_FILE_PATTERNS = [
Expand All @@ -22,7 +24,9 @@ export const CONFIG_FILE_PATTERNS = [
const findSemanticReleaseDependencies: GenericPluginCallback = async (configFilePath, { manifest, isProduction }) => {
if (isProduction) return [];

const config: PluginConfig = configFilePath.endsWith('package.json') ? manifest.release : await load(configFilePath);
const config: PluginConfig = configFilePath.endsWith('package.json')
? manifest[PACKAGE_JSON_PATH]
: await load(configFilePath);
const plugins = config?.plugins ?? [];
return plugins.map(plugin => (Array.isArray(plugin) ? plugin[0] : plugin));
};
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/typedoc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const NAME = 'TypeDoc';
/** @public */
export const ENABLERS = ['typedoc'];

export const PACKAGE_JSON_PATH = 'typedocOptions';

export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(dependencies, ENABLERS);

export const CONFIG_FILE_PATTERNS = [
Expand All @@ -25,7 +27,7 @@ const findTypeDocDependencies: GenericPluginCallback = async (configFilePath, {
if (isProduction) return [];

const config: PluginConfig = configFilePath.endsWith('package.json')
? manifest.typedocOptions
? manifest[PACKAGE_JSON_PATH]
: configFilePath.endsWith('tsconfig.json')
? (await load(configFilePath)).typedocOptions
: await load(configFilePath);
Expand Down
3 changes: 3 additions & 0 deletions src/util/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ export const getKeysByValue = <T>(obj: T, value: unknown): (keyof T)[] => {
}
return keys;
};

// @ts-expect-error just pass good objects alright
export const get = (obj: unknown, path: string) => path.split('.').reduce((o, p) => o && o[p], obj);

0 comments on commit 977e756

Please sign in to comment.