Skip to content

Commit

Permalink
Migrate plugins and their tests
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Sep 26, 2023
1 parent 9508c73 commit 8e09fcd
Show file tree
Hide file tree
Showing 60 changed files with 374 additions and 132 deletions.
14 changes: 13 additions & 1 deletion src/plugins/ava/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ or `devDependencies`:
```json
{
"ava": {
"config": ["ava.config.{js,cjs,mjs}", "package.json"]
"config": ["ava.config.{js,cjs,mjs}", "package.json"],
"entry": [
"test.{js,cjs,mjs}",
"{src,source}/test.{js,cjs,mjs}",
"**/__tests__/**/*.{js,cjs,mjs}",
"**/*.spec.{js,cjs,mjs}",
"**/*.test.{js,cjs,mjs}",
"**/test-*.{js,cjs,mjs}",
"**/test/**/*.{js,cjs,mjs}",
"**/tests/**/*.{js,cjs,mjs}",
"!**/__tests__/**/__{helper,fixture}?(s)__/**/*",
"!**/test?(s)/**/{helper,fixture}?(s)/**/*"
]
}
}
```
Expand Down
32 changes: 25 additions & 7 deletions src/plugins/ava/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { _getDependenciesFromScripts } from '../../binaries/index.js';
import { timerify } from '../../util/Performance.js';
import { hasDependency, load } from '../../util/plugin.js';
import { toEntryPattern } from '../../util/protocols.js';
import type { PluginConfig } from './types.js';
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';

Expand All @@ -15,22 +16,39 @@ export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDepen

export const CONFIG_FILE_PATTERNS = ['ava.config.{js,cjs,mjs}', 'package.json'];

// `TEST_FILE_PATTERNS` in src/constants.ts are already included by default
export const ENTRY_FILE_PATTERNS = [];

const findAvaDependencies: GenericPluginCallback = async (configFilePath, { cwd, manifest }) => {
export const ENTRY_FILE_PATTERNS = [
`test.{js,cjs,mjs}`,
`{src,source}/test.{js,cjs,mjs}`,
`**/__tests__/**/*.{js,cjs,mjs}`,
`**/*.spec.{js,cjs,mjs}`,
`**/*.test.{js,cjs,mjs}`,
`**/test-*.{js,cjs,mjs}`,
`**/test/**/*.{js,cjs,mjs}`,
`**/tests/**/*.{js,cjs,mjs}`,
'!**/__tests__/**/__{helper,fixture}?(s)__/**/*',
'!**/test?(s)/**/{helper,fixture}?(s)/**/*',
];

const findAvaDependencies: GenericPluginCallback = async (configFilePath, { cwd, manifest, isProduction }) => {
const config: PluginConfig = configFilePath.endsWith('package.json') ? manifest.ava : await load(configFilePath);

const requireArgs = (config?.require ?? []).map(require => `--require ${require}`);
const otherArgs = config?.nodeArguments ?? [];
const entryPatterns = (config?.files ?? ENTRY_FILE_PATTERNS).map(toEntryPattern);
if (isProduction) return entryPatterns;

if (!config) return [];

const requireArgs = (config.require ?? []).map(require => `--require ${require}`);
const otherArgs = config.nodeArguments ?? [];

const cmd = `node ${otherArgs.join(' ')} ${requireArgs.join(' ')}`;

return _getDependenciesFromScripts([cmd], {
const dependencies = _getDependenciesFromScripts([cmd], {
cwd,
manifest,
knownGlobalsOnly: true,
});

return [...entryPatterns, ...dependencies];
};

export const findDependencies = timerify(findAvaDependencies);
1 change: 1 addition & 0 deletions src/plugins/ava/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type PluginConfig = {
files?: string[];
require?: string[];
nodeArguments?: string[];
};
3 changes: 2 additions & 1 deletion src/plugins/babel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export const getDependenciesFromConfig = (config: BabelConfigObj): string[] => {
return compact([...presets, ...plugins, ...nested]);
};

const findBabelDependencies: GenericPluginCallback = async (configFilePath, { manifest }) => {
const findBabelDependencies: GenericPluginCallback = async (configFilePath, { manifest, isProduction }) => {
if (isProduction) return [];
let config: BabelConfig = configFilePath.endsWith('package.json') ? manifest.babel : await load(configFilePath);
if (typeof config === 'function') {
config = config(api);
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/capacitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDepen

export const CONFIG_FILE_PATTERNS = ['capacitor.config.ts'];

const findCapacitorDependencies: GenericPluginCallback = async configFilePath => {
const findCapacitorDependencies: GenericPluginCallback = async (configFilePath, { isProduction }) => {
if (isProduction) return [];
const config: CapacitorConfig = await load(configFilePath);
return config.includePlugins ?? [];
};
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/changesets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDepen

export const CONFIG_FILE_PATTERNS = ['.changeset/config.json'];

const findChangesetsDependencies: GenericPluginCallback = async configFilePath => {
const findChangesetsDependencies: GenericPluginCallback = async (configFilePath, { isProduction }) => {
if (isProduction) return [];
const config: ChangesetsConfig = await load(configFilePath);
return Array.isArray(config.changelog)
? [config.changelog[0]]
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/commitizen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDepen

export const CONFIG_FILE_PATTERNS = ['.czrc', '.cz.json', 'package.json'];

const findPluginDependencies: GenericPluginCallback = async (configFilePath, { manifest }) => {
const findPluginDependencies: GenericPluginCallback = async (configFilePath, { manifest, isProduction }) => {
if (isProduction) return [];
const config: PluginConfig = configFilePath.endsWith('package.json')
? manifest.config?.commitizen
: await load(configFilePath);
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/commitlint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const CONFIG_FILE_PATTERNS = [
'package.json',
];

const findCommitLintDependencies: GenericPluginCallback = async (configFilePath, { manifest }) => {
const findCommitLintDependencies: GenericPluginCallback = async (configFilePath, { manifest, isProduction }) => {
if (isProduction) return [];
const config: CommitLintConfig = configFilePath.endsWith('package.json')
? manifest.commitlint
: await load(configFilePath);
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/cspell/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const CONFIG_FILE_PATTERNS = [
'cSpell.json',
];

const findCspellDependencies: GenericPluginCallback = async configFilePath => {
const findCspellDependencies: GenericPluginCallback = async (configFilePath, { isProduction }) => {
if (isProduction) return [];
const config: PluginConfig = await load(configFilePath);
const imports = config?.import ?? [];
return imports;
Expand Down
8 changes: 7 additions & 1 deletion src/plugins/cypress/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { hasDependency } from '../../util/plugin.js';
import type { IsPluginEnabledCallback } from '../../types/plugins.js';
import { toEntryPattern } from '../../util/protocols.js';
import type { GenericPluginCallback, IsPluginEnabledCallback } from '../../types/plugins.js';

// https://docs.cypress.io/guides/references/configuration

Expand All @@ -16,3 +17,8 @@ export const ENTRY_FILE_PATTERNS = [
'cypress/e2e/**/*.cy.{js,jsx,ts,tsx}',
'cypress/plugins/index.js', // Deprecated since Cypress v10
];

export const findDependencies: GenericPluginCallback = async (configFilePath, { isProduction }) => {
if (isProduction) return [];
return ENTRY_FILE_PATTERNS.map(toEntryPattern);
};
3 changes: 2 additions & 1 deletion src/plugins/eslint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const ENTRY_FILE_PATTERNS = ['eslint.config.js'];
// Note: shareable configs should use `peerDependencies` for plugins
// https://eslint.org/docs/latest/developer-guide/shareable-configs#publishing-a-shareable-config

const findESLintDependencies: GenericPluginCallback = async (configFilePath, { cwd, manifest }) => {
const findESLintDependencies: GenericPluginCallback = async (configFilePath, { cwd, manifest, isProduction }) => {
if (isProduction) return [];
const dependencies = await getDependenciesDeep(configFilePath, new Set(), { cwd, manifest });
return Array.from(dependencies);
};
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/gatsby/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { timerify } from '../../util/Performance.js';
import { hasDependency, load } from '../../util/plugin.js';
import { toProductionEntryPattern } from '../../util/protocols.js';
import type { GatsbyActions, GatsbyConfig, GatsbyNode } from './types.js';
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';

Expand All @@ -23,9 +24,12 @@ export const PRODUCTION_ENTRY_FILE_PATTERNS = [
'src/html.{js,jsx,ts,tsx}',
];

const findGatsbyDependencies: GenericPluginCallback = async configFilePath => {
const findGatsbyDependencies: GenericPluginCallback = async (configFilePath, { isProduction }) => {
const config: GatsbyConfig | GatsbyNode = await load(configFilePath);

const entryPatterns = PRODUCTION_ENTRY_FILE_PATTERNS.map(toProductionEntryPattern);
if (isProduction) return entryPatterns;

if (/gatsby-config/.test(configFilePath)) {
return (config as GatsbyConfig).plugins.map(plugin => (typeof plugin === 'string' ? plugin : plugin.resolve));
}
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/github-actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export const isEnabled: IsPluginEnabledCallback = async ({ cwd }) =>

export const CONFIG_FILE_PATTERNS = ['.github/workflows/*.yml', '.github/**/action.{yml,yaml}'];

const findGithubActionsDependencies: GenericPluginCallback = async (configFilePath, { cwd, manifest }) => {
const findGithubActionsDependencies: GenericPluginCallback = async (
configFilePath,
{ cwd, manifest, isProduction }
) => {
if (isProduction) return [];

const config = await load(configFilePath);

if (!config) return [];
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/husky/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const gitHookPaths = getGitHookPaths('.husky');

export const CONFIG_FILE_PATTERNS = [...gitHookPaths];

const findHuskyDependencies: GenericPluginCallback = async (configFilePath, { cwd, manifest }) => {
const findHuskyDependencies: GenericPluginCallback = async (configFilePath, { cwd, manifest, isProduction }) => {
if (isProduction) return [];

const script = readFileSync(configFilePath);

return _getDependenciesFromScripts(String(script), {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/jest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ or `devDependencies`:
```json
{
"jest": {
"config": ["jest.config.{js,ts,mjs,cjs,json}", "package.json"]
"config": ["jest.config.{js,ts,mjs,cjs,json}", "package.json"],
"entry": ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"]
}
}
```
Expand Down
33 changes: 23 additions & 10 deletions src/plugins/jest/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { join, isInternal, toAbsolute, dirname } from '../../util/path.js';
import { timerify } from '../../util/Performance.js';
import { hasDependency, load } from '../../util/plugin.js';
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';
import { toEntryPattern } from '../../util/protocols.js';
import type {
IsPluginEnabledCallback,
GenericPluginCallback,
GenericPluginCallbackOptions,
} from '../../types/plugins.js';
import type { Config } from '@jest/types';

// https://jestjs.io/docs/configuration
Expand All @@ -16,8 +21,7 @@ export const isEnabled: IsPluginEnabledCallback = ({ dependencies, manifest }) =

export const CONFIG_FILE_PATTERNS = ['jest.config.{js,ts,mjs,cjs,json}', 'package.json'];

// `TEST_FILE_PATTERNS` in src/constants.ts are already included by default
export const ENTRY_FILE_PATTERNS = [];
export const ENTRY_FILE_PATTERNS = ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'];

const resolveExtensibleConfig = async (configFilePath: string) => {
const config = await load(configFilePath);
Expand All @@ -34,12 +38,16 @@ const resolveExtensibleConfig = async (configFilePath: string) => {

type JestOptions = Config.InitialOptions | (() => Config.InitialOptions) | (() => Promise<Config.InitialOptions>);

const resolveDependencies = (config: Config.InitialOptions): string[] => {
const resolveDependencies = (config: Config.InitialOptions, options: GenericPluginCallbackOptions): string[] => {
const { isProduction } = options;
const entryPatterns = (config.testMatch ?? ENTRY_FILE_PATTERNS).map(toEntryPattern);
if (isProduction) return entryPatterns;

const presets = (config.preset ? [config.preset] : []).map(preset =>
isInternal(preset) ? preset : join(preset, 'jest-preset')
);
const projects = Array.isArray(config.projects)
? config.projects.map(config => (typeof config === 'string' ? config : resolveDependencies(config))).flat()
? config.projects.map(config => (typeof config === 'string' ? config : resolveDependencies(config, options))).flat()
: [];
const runner = config.runner ? [config.runner] : [];
const environments = config.testEnvironment === 'jsdom' ? ['jest-environment-jsdom'] : [];
Expand All @@ -64,6 +72,7 @@ const resolveDependencies = (config: Config.InitialOptions): string[] => {
const testResultsProcessor = config.testResultsProcessor ? [config.testResultsProcessor] : [];

return [
...entryPatterns,
...presets,
...projects,
...runner,
Expand All @@ -79,7 +88,9 @@ const resolveDependencies = (config: Config.InitialOptions): string[] => {
];
};

const findJestDependencies: GenericPluginCallback = async (configFilePath, { cwd, manifest }) => {
const findJestDependencies: GenericPluginCallback = async (configFilePath, options) => {
const { manifest, cwd } = options;

let config: JestOptions = configFilePath.endsWith('package.json')
? manifest.jest
: await resolveExtensibleConfig(configFilePath);
Expand All @@ -88,12 +99,14 @@ const findJestDependencies: GenericPluginCallback = async (configFilePath, { cwd

if (!config) return [];

const rootDir = config.rootDir ?? '';
const rootDir = config.rootDir ? join(dirname(configFilePath), config.rootDir) : dirname(configFilePath);

const replaceRootDir = (name: string) => (name.includes('<rootDir>') ? name.replace(/<rootDir>/, rootDir) : name);

const replaceRootDir = (name: string) =>
name.includes('<rootDir>') ? join(cwd, name.replace(/^.*<rootDir>/, rootDir)) : name;
const dependencies = resolveDependencies(config, options);

return resolveDependencies(config).map(replaceRootDir);
const matchCwd = new RegExp('^' + toEntryPattern(cwd) + '/');
return dependencies.map(replaceRootDir).map(dependency => dependency.replace(matchCwd, toEntryPattern('')));
};

export const findDependencies = timerify(findJestDependencies);
6 changes: 4 additions & 2 deletions src/plugins/lefthook/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { readFileSync } from 'fs';
import { fromBinary } from '../../util/protocols.js';
import { _getDependenciesFromScripts } from '../../binaries/index.js';
import { getGitHookPaths } from '../../util/git.js';
import { getValuesByKeyDeep } from '../../util/object.js';
import { timerify } from '../../util/Performance.js';
import { hasDependency, load } from '../../util/plugin.js';
import { fromBinary } from '../../util/protocols.js';
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';

// https://github.com/evilmartians/lefthook
Expand All @@ -20,7 +20,9 @@ const gitHookPaths = getGitHookPaths();

export const CONFIG_FILE_PATTERNS = ['lefthook.yml', ...gitHookPaths];

const findLefthookDependencies: GenericPluginCallback = async (configFilePath, { cwd, manifest }) => {
const findLefthookDependencies: GenericPluginCallback = async (configFilePath, { cwd, manifest, isProduction }) => {
if (isProduction) return [];

const dependencies = manifest.devDependencies ? Object.keys(manifest.devDependencies) : [];

if (configFilePath.endsWith('.yml')) {
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/lint-staged/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export const CONFIG_FILE_PATTERNS = [
'package.json',
];

const findLintStagedDependencies: GenericPluginCallback = async (configFilePath, { cwd, manifest }) => {
const findLintStagedDependencies: GenericPluginCallback = async (configFilePath, { cwd, manifest, isProduction }) => {
if (isProduction) return [];

let config: LintStagedConfig = configFilePath.endsWith('package.json')
? manifest['lint-staged']
: await load(configFilePath);
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/markdownlint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDepen

export const CONFIG_FILE_PATTERNS = ['.markdownlint.{json,jsonc}', '.markdownlint.{yml,yaml}'];

const findMarkdownlintConfigDependencies: GenericPluginCallback = async (configFilePath, { manifest }) => {
const findMarkdownlintConfigDependencies: GenericPluginCallback = async (
configFilePath,
{ manifest, isProduction }
) => {
if (isProduction) return [];

const config: MarkdownlintConfig = await load(configFilePath);
const extend = config?.extends ? [config.extends] : [];
const scripts = manifest.scripts
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/mocha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ or `devDependencies`:
```json
{
"mocha": {
"config": [".mocharc.{js,cjs,json,jsonc,yml,yaml}", "package.json"]
"config": [".mocharc.{js,cjs,json,jsonc,yml,yaml}", "package.json"],
"entry": ["**/test/*.{js,cjs,mjs}"]
}
}
```
Expand Down
19 changes: 13 additions & 6 deletions src/plugins/mocha/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { timerify } from '../../util/Performance.js';
import { hasDependency, load } from '../../util/plugin.js';
import { toEntryPattern } from '../../util/protocols.js';
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';

// https://mochajs.org/#configuring-mocha-nodejs
Expand All @@ -13,13 +14,19 @@ export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDepen

export const CONFIG_FILE_PATTERNS = ['.mocharc.{js,cjs,json,jsonc,yml,yaml}', 'package.json'];

const findMochaDependencies: GenericPluginCallback = async (configFilePath, { manifest }) => {
export const ENTRY_FILE_PATTERNS = ['**/test/*.{js,cjs,mjs}'];

const findMochaDependencies: GenericPluginCallback = async (configFilePath, { manifest, isProduction }) => {
const entryPatterns = ENTRY_FILE_PATTERNS.map(toEntryPattern);
if (isProduction) return entryPatterns;

const config = configFilePath.endsWith('package.json') ? manifest.mocha : await load(configFilePath);
if (config) {
const require = config.require;
return require ? [require].flat() : [];
}
return [];

if (!config) return [];

const require = config.require ? [config.require].flat() : [];

return [...require, ...entryPatterns];
};

export const findDependencies = timerify(findMochaDependencies);

0 comments on commit 8e09fcd

Please sign in to comment.