Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(nx-plugin): explicit import extensions [DO NOT MERGE] #972

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
26 changes: 26 additions & 0 deletions e2e/nx-plugin-e2e/tests/executor-cli.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,32 @@ describe('executor command', () => {
).rejects.toThrow('');
});

it('should execute print-config executor with api key', async () => {
const cwd = path.join(testFileDir, 'execute-print-config-command');
await addTargetToWorkspace(tree, { cwd, project });

const { stdout, code } = await executeProcess({
command: 'npx',
args: [
'nx',
'run',
`${project}:code-pushup`,
'print-config',
'--upload.apiKey=a123a',
],
cwd,
});

expect(code).toBe(0);
const cleanStdout = removeColorCodes(stdout);
expect(cleanStdout).toContain('nx run my-lib:code-pushup print-config');
expect(cleanStdout).toContain('a123a');

await expect(() =>
readJsonFile(path.join(cwd, '.code-pushup', project, 'report.json')),
).rejects.toThrow('');
});

it('should execute collect executor and merge target and command-line options', async () => {
const cwd = path.join(testFileDir, 'execute-collect-with-merged-options');
await addTargetToWorkspace(
Expand Down
10 changes: 10 additions & 0 deletions packages/models/code-pushup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { eslintCoreConfigNx } from '../../code-pushup.preset.js';

Check failure on line 1 in packages/models/code-pushup.config.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
import { mergeConfigs } from '../../dist/packages/utils/src/index.js';

// see: https://github.com/code-pushup/cli/blob/main/packages/models/docs/models-reference.md#coreconfig
export default mergeConfigs(
{
plugins: [],
},
await eslintCoreConfigNx(),
);

Check warning on line 10 in packages/models/code-pushup.config.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 1-10 are not covered in any test case.
1 change: 1 addition & 0 deletions packages/models/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"exclude": [
"vite.config.unit.ts",
"vite.config.integration.ts",
"code-pushup.config.ts",
"zod2md.config.ts",
"src/**/*.test.ts",
"src/**/*.mock.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/models/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"outDir": "../../dist/out-tsc",
"types": ["vitest/globals", "vitest/importMeta", "vite/client", "node"]
},
"exclude": ["**/code-pushup.config.ts"],
"include": [
"vite.config.unit.ts",
"vite.config.integration.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = tseslint.config(
rules: {
// Nx plugins don't yet support ESM: https://github.com/nrwl/nx/issues/15682
'unicorn/prefer-module': 'off',
'n/file-extension-in-import': 'off',
// used instead of verbatimModuleSyntax tsconfig flag (requires ESM)
'@typescript-eslint/consistent-type-imports': [
'warn',
Expand Down
109 changes: 0 additions & 109 deletions packages/nx-plugin/src/executors/cli/executor.unit.test.ts

This file was deleted.

9 changes: 6 additions & 3 deletions packages/nx-plugin/src/executors/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@
const { projectPrefix, persist, upload, command } = options;
const needsUploadParams =
command === 'upload' || command === 'autorun' || command === undefined;
const uploadCfg = uploadConfig(
{ projectPrefix, ...upload },
normalizedContext,
);
const hasApiToken = uploadCfg?.apiKey != null;
return {
...parseAutorunExecutorOnlyOptions(options),
...globalConfig(options, normalizedContext),
persist: persistConfig({ projectPrefix, ...persist }, normalizedContext),
// @TODO This is a hack to avoid validation errors of upload config for commands that dont need it.
// Fix: use utils and execute the core logic directly
// Blocked by Nx plugins can't compile to es6
upload: needsUploadParams
? uploadConfig({ projectPrefix, ...upload }, normalizedContext)
: undefined,
...(needsUploadParams && hasApiToken ? { upload: uploadCfg } : {}),

Check failure on line 42 in packages/nx-plugin/src/executors/cli/utils.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
};
}

Expand Down Expand Up @@ -65,7 +68,7 @@
...targetOptions?.persist,
...cliOptions?.persist,
},
}

Check failure on line 71 in packages/nx-plugin/src/executors/cli/utils.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
: {}),
...(targetOptions?.upload || cliOptions?.upload
? {
Expand Down
9 changes: 4 additions & 5 deletions packages/nx-plugin/src/executors/cli/utils.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ describe('parseAutorunExecutorOptions', () => {
},
},
);
expect(osAgnosticPath(executorOptions.config)).toBe(
expect(osAgnosticPath(executorOptions.config ?? '')).toBe(
osAgnosticPath('root/code-pushup.config.ts'),
);
expect(executorOptions).toEqual(
expect.objectContaining({
progress: false,
verbose: false,
upload: { project: projectName },
}),
);

Expand All @@ -92,20 +91,20 @@ describe('parseAutorunExecutorOptions', () => {
}),
);

expect(osAgnosticPath(executorOptions.persist?.outputDir)).toBe(
expect(osAgnosticPath(executorOptions.persist?.outputDir ?? '')).toBe(
osAgnosticPath('workspaceRoot/.code-pushup/my-app'),
);
});

it.each<Command | undefined>(['upload', 'autorun', undefined])(
'should include upload config for command %s',
'should include upload config for command %s if API key is provided',
command => {
const projectName = 'my-app';
const executorOptions = parseAutorunExecutorOptions(
{
command,
upload: {
organization: 'code-pushup',
apiKey: '123456789',
},
},
{
Expand Down
16 changes: 8 additions & 8 deletions packages/nx-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { createNodes } from './plugin/index.js';
// default export for nx.json#plugins
export default createNodes;

export * from './internal/versions.js';
export { type InitGeneratorSchema } from './generators/init/schema.js';
export { initGenerator, initSchematic } from './generators/init/generator.js';
export type { ConfigurationGeneratorOptions } from './generators/configuration/schema.js';
export { configurationGenerator } from './generators/configuration/generator.js';
export type { AutorunCommandExecutorOptions } from './executors/cli/schema.js';
export { objectToCliArgs } from './executors/internal/cli.js';
export { generateCodePushupConfig } from './generators/configuration/code-pushup-config.js';
export { createNodes } from './plugin/index.js';
export { configurationGenerator } from './generators/configuration/generator.js';
export type { ConfigurationGeneratorOptions } from './generators/configuration/schema.js';
export { initGenerator, initSchematic } from './generators/init/generator.js';
export { type InitGeneratorSchema } from './generators/init/schema.js';
export {
executeProcess,
type ProcessConfig,
} from './internal/execute-process.js';
export { objectToCliArgs } from './executors/internal/cli.js';
export type { AutorunCommandExecutorOptions } from './executors/cli/schema.js';
export * from './internal/versions.js';
export { createNodes } from './plugin/index.js';
5 changes: 2 additions & 3 deletions packages/nx-plugin/src/internal/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { name } from '../../package.json';

export const PROJECT_JSON_FILE_NAME = 'project.json';
export const PACKAGE_NAME = name;
export const CODE_PUSHUP_CONFIG_REGEX = /^code-pushup(?:\.[\w-]+)?\.ts$/;
export const PACKAGE_NAME = '@code-pushup/nx-plugin';
export const DEFAULT_TARGET_NAME = 'code-pushup';
2 changes: 1 addition & 1 deletion packages/nx-plugin/src/internal/execute-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export type ProcessObserver = {
* // async process execution
* const result = await executeProcess({
* command: 'node',
* args: ['download-data.js'],
* args: ['download-data'],
* observer: {
* onStdout: updateProgress,
* error: handleError,
Expand Down
4 changes: 4 additions & 0 deletions packages/nx-plugin/src/internal/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export const cpCliVersion = loadPackageJson(
path.join(projectsFolder, 'models'),
).version;

/**
* Load the package.json file from the given folder path.
* @param folderPath
*/
function loadPackageJson(folderPath: string): PackageJson {
return readJsonFile<PackageJson>(path.join(folderPath, 'package.json'));
}
2 changes: 1 addition & 1 deletion packages/nx-plugin/src/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createTargets } from './target/targets.js';
import type { CreateNodesOptions } from './types.js';
import { normalizedCreateNodesContext } from './utils.js';

// name has to be "createNodes" to get picked up by Nx
// name has to be "createNodes" to get picked up by Nx <v20
export const createNodes: CreateNodes = [
`**/${PROJECT_JSON_FILE_NAME}`,
async (
Expand Down
1 change: 1 addition & 0 deletions packages/nx-plugin/src/plugin/plugin.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('@code-pushup/nx-plugin/plugin', () => {
context = {
nxJsonConfiguration: {},
workspaceRoot: '',
configFiles: [],
};
});

Expand Down
14 changes: 10 additions & 4 deletions packages/nx-plugin/src/plugin/target/targets.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { readdir } from 'node:fs/promises';
import { CP_TARGET_NAME } from '../constants.js';
import type { NormalizedCreateNodesContext } from '../types.js';
import type {
CreateNodesOptions,
ProjectConfigurationWithName,
} from '../types.js';
import { createConfigurationTarget } from './configuration-target.js';
import { CODE_PUSHUP_CONFIG_REGEX } from './constants.js';
import { createExecutorTarget } from './executor-target.js';

export async function createTargets(
normalizedContext: NormalizedCreateNodesContext,
) {
export type CreateTargetsOptions = {

Check warning on line 11 in packages/nx-plugin/src/plugin/target/targets.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> JSDoc coverage | Types coverage

Missing types documentation for CreateTargetsOptions
projectJson: ProjectConfigurationWithName;
projectRoot: string;
createOptions: CreateNodesOptions;
};
export async function createTargets(normalizedContext: CreateTargetsOptions) {

Check warning on line 16 in packages/nx-plugin/src/plugin/target/targets.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> JSDoc coverage | Functions coverage

Missing functions documentation for createTargets
const {
targetName = CP_TARGET_NAME,
bin,
Expand Down
17 changes: 11 additions & 6 deletions packages/nx-plugin/src/plugin/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type { CreateNodesContext, ProjectConfiguration } from '@nx/devkit';
import type {
CreateNodesContext,
CreateNodesContextV2,
ProjectConfiguration,
} from '@nx/devkit';
import type { WithRequired } from '@code-pushup/utils';
import type { DynamicTargetOptions } from '../internal/types.js';
import type { CreateTargetsOptions } from './target/targets.js';

export type ProjectPrefixOptions = {
projectPrefix?: string;
Expand All @@ -13,8 +18,8 @@
'name'
>;

export type NormalizedCreateNodesContext = CreateNodesContext & {
projectJson: ProjectConfigurationWithName;
projectRoot: string;
createOptions: CreateNodesOptions;
};
export type NormalizedCreateNodesContext = CreateNodesContext &
CreateTargetsOptions;

export type NormalizedCreateNodesV2Context = CreateNodesContextV2 &

Check warning on line 24 in packages/nx-plugin/src/plugin/types.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> JSDoc coverage | Types coverage

Missing types documentation for NormalizedCreateNodesV2Context
CreateTargetsOptions;
Loading
Loading