Skip to content

fix(nx-plugin): adjust upload config handling #937

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

Merged
merged 33 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f653376
fix(nx-plugin): adjust upload config handling
BioPhoton Feb 14, 2025
ccce7b1
fix(nx-plugin): remove file extensions in imports
BioPhoton Feb 22, 2025
d10f1be
fix(nx-plugin): add createNodesV2
BioPhoton Feb 22, 2025
ff94e39
fix(nx-plugin): add createNodesV2
BioPhoton Feb 22, 2025
45c8667
ci: setup plugin
BioPhoton Feb 22, 2025
a3316bc
ci: setup cp for models
BioPhoton Feb 22, 2025
48143dc
wip
BioPhoton Feb 22, 2025
42f0bd4
wip
BioPhoton Mar 1, 2025
233cfd7
wip 1
BioPhoton Mar 3, 2025
c2ec88c
fix(nx-plugin): fix lint
BioPhoton Mar 5, 2025
15983d8
fix(nx-plugin): fix lint
BioPhoton Mar 5, 2025
3596149
fix: wip
BioPhoton Mar 5, 2025
d375686
docs: add js docs
BioPhoton Mar 5, 2025
bf87631
Merge branch 'main' into nx-plugin/fix-upload-config
BioPhoton Mar 5, 2025
a754393
refactor(nx-plugin): explicit import extensions
matejchalk Mar 10, 2025
2bcfcc7
Update packages/nx-plugin/src/plugin/target/targets.ts
BioPhoton Mar 10, 2025
385d77c
fix: update nx-verdaccio pkg (#954)
BioPhoton Mar 5, 2025
fa5635e
fix(utils): ignore non-json lines in fromJsonLines utility
matejchalk Mar 5, 2025
9e68eb1
fix(plugin-js-packages): ignore non-empty stderr
matejchalk Mar 5, 2025
88fd037
release: 0.64.2 [skip ci]
matejchalk Mar 5, 2025
00aae50
feat(utils): add score filter to md report generation (#956)
BioPhoton Mar 5, 2025
76c2b47
release: 0.65.0 [skip ci]
matejchalk Mar 5, 2025
2437497
fix: adjust logic
BioPhoton Mar 10, 2025
8de3562
Update packages/nx-plugin/eslint.config.js
BioPhoton Mar 10, 2025
f5cf751
fix: wip
BioPhoton Mar 10, 2025
fb846ff
fix: wip
BioPhoton Mar 10, 2025
59145b7
Merge branch 'main' into nx-plugin/fix-upload-config
BioPhoton Mar 10, 2025
90daca5
fix: e2e tests
BioPhoton Mar 10, 2025
acbd3ab
fix: e2e tests
BioPhoton Mar 10, 2025
3e32e37
fix: wip
BioPhoton Mar 10, 2025
7cf9479
fix: wip
BioPhoton Mar 10, 2025
74c39cb
fix: wip e2e
BioPhoton Mar 10, 2025
f17ce38
fix: wip e2e 2
BioPhoton Mar 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
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(),
);
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 @@ export function parseAutorunExecutorOptions(
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 } : {}),
};
}

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 = {
projectJson: ProjectConfigurationWithName;
projectRoot: string;
createOptions: CreateNodesOptions;
};
export async function createTargets(normalizedContext: CreateTargetsOptions) {
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 @@ export type ProjectConfigurationWithName = WithRequired<
'name'
>;

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

export type NormalizedCreateNodesV2Context = CreateNodesContextV2 &
CreateTargetsOptions;
Loading
Loading