Skip to content

Commit ee243ac

Browse files
authored
refactor(nx-plugin): rename autorun to cli executor (#795)
1 parent 138e180 commit ee243ac

38 files changed

+385
-118
lines changed

e2e/nx-plugin-e2e/tests/__snapshots__/plugin-create-nodes.e2e.test.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`nx-plugin > should NOT add config targets dynamically if the project is
44
{
55
"code-pushup": {
66
"configurations": {},
7-
"executor": "@code-pushup/nx-plugin:autorun",
7+
"executor": "@code-pushup/nx-plugin:cli",
88
"options": {},
99
},
1010
}
@@ -26,7 +26,7 @@ exports[`nx-plugin > should add executor target dynamically if the project is co
2626
{
2727
"code-pushup": {
2828
"configurations": {},
29-
"executor": "@code-pushup/nx-plugin:autorun",
29+
"executor": "@code-pushup/nx-plugin:cli",
3030
"options": {},
3131
},
3232
}

e2e/nx-plugin-e2e/tests/executor-cli.e2e.test.ts

+85-12
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
materializeTree,
99
} from '@code-pushup/test-nx-utils';
1010
import { teardownTestFolder } from '@code-pushup/test-setup';
11-
import { removeColorCodes } from '@code-pushup/test-utils';
12-
import { executeProcess } from '@code-pushup/utils';
11+
import { osAgnosticPath, removeColorCodes } from '@code-pushup/test-utils';
12+
import { executeProcess, readJsonFile } from '@code-pushup/utils';
1313

1414
function relativePathToCwd(testDir: string): string {
1515
return relative(join(process.cwd(), testDir), process.cwd());
@@ -27,7 +27,7 @@ async function addTargetToWorkspace(
2727
targets: {
2828
...projectCfg.targets,
2929
['code-pushup']: {
30-
executor: '@code-pushup/nx-plugin:autorun',
30+
executor: '@code-pushup/nx-plugin:cli',
3131
},
3232
},
3333
});
@@ -37,19 +37,27 @@ async function addTargetToWorkspace(
3737
plugins: [
3838
{
3939
// @TODO replace with inline plugin
40-
fileImports: `import {customPlugin} from "${join(
41-
relativePathToCwd(cwd),
42-
pathRelativeToPackage,
43-
'dist/testing/test-utils',
40+
fileImports: `import {customPlugin} from "${osAgnosticPath(
41+
join(
42+
relativePathToCwd(cwd),
43+
pathRelativeToPackage,
44+
'dist/testing/test-utils',
45+
),
4446
)}";`,
4547
codeStrings: 'customPlugin()',
4648
},
4749
],
50+
upload: {
51+
server: 'https://dummy-server.dev',
52+
organization: 'dummy-organization',
53+
apiKey: 'dummy-api-key',
54+
project: 'dummy-project',
55+
},
4856
});
4957
await materializeTree(tree, cwd);
5058
}
5159

52-
describe('executor autorun', () => {
60+
describe('executor command', () => {
5361
let tree: Tree;
5462
const project = 'my-lib';
5563
const baseDir = 'tmp/e2e/nx-plugin-e2e/__test__/executor/cli';
@@ -62,10 +70,9 @@ describe('executor autorun', () => {
6270
await teardownTestFolder(baseDir);
6371
});
6472

65-
it('should execute autorun executor', async () => {
66-
const cwd = join(baseDir, 'execute-dynamic-executor');
73+
it('should execute no specific command by default', async () => {
74+
const cwd = join(baseDir, 'execute-default-command');
6775
await addTargetToWorkspace(tree, { cwd, project });
68-
6976
const { stdout, code } = await executeProcess({
7077
command: 'npx',
7178
args: ['nx', 'run', `${project}:code-pushup`, '--dryRun'],
@@ -74,6 +81,72 @@ describe('executor autorun', () => {
7481

7582
expect(code).toBe(0);
7683
const cleanStdout = removeColorCodes(stdout);
77-
expect(cleanStdout).toContain('nx run my-lib:code-pushup --dryRun');
84+
expect(cleanStdout).toContain('nx run my-lib:code-pushup');
85+
});
86+
87+
it('should execute print-config executor', async () => {
88+
const cwd = join(baseDir, 'execute-print-config-command');
89+
await addTargetToWorkspace(tree, { cwd, project });
90+
91+
const { stdout, code } = await executeProcess({
92+
command: 'npx',
93+
args: ['nx', 'run', `${project}:code-pushup`, 'print-config'],
94+
cwd,
95+
});
96+
97+
expect(code).toBe(0);
98+
const cleanStdout = removeColorCodes(stdout);
99+
expect(cleanStdout).toContain('nx run my-lib:code-pushup print-config');
100+
101+
await expect(() =>
102+
readJsonFile(join(cwd, '.code-pushup', project, 'report.json')),
103+
).rejects.toThrow('');
104+
});
105+
106+
it('should execute collect executor and add report to sub folder named by project', async () => {
107+
const cwd = join(baseDir, 'execute-collect-command');
108+
await addTargetToWorkspace(tree, { cwd, project });
109+
110+
const { stdout, code } = await executeProcess({
111+
command: 'nx',
112+
args: ['run', `${project}:code-pushup`, 'collect'],
113+
cwd,
114+
});
115+
116+
expect(code).toBe(0);
117+
const cleanStdout = removeColorCodes(stdout);
118+
expect(cleanStdout).toContain('nx run my-lib:code-pushup collect');
119+
120+
const report = await readJsonFile(
121+
join(cwd, '.code-pushup', project, 'report.json'),
122+
);
123+
expect(report).toStrictEqual(
124+
expect.objectContaining({
125+
plugins: [
126+
expect.objectContaining({
127+
slug: 'good-feels',
128+
audits: [
129+
expect.objectContaining({
130+
displayValue: '✅ Perfect! 👌',
131+
slug: 'always-perfect',
132+
}),
133+
],
134+
}),
135+
],
136+
}),
137+
);
138+
});
139+
140+
it('should execute upload executor to throw if no report is present', async () => {
141+
const cwd = join(baseDir, 'execute-upload-command');
142+
await addTargetToWorkspace(tree, { cwd, project });
143+
144+
await expect(
145+
executeProcess({
146+
command: 'npx',
147+
args: ['nx', 'run', `${project}:code-pushup`, 'upload'],
148+
cwd,
149+
}),
150+
).rejects.toThrow(/report.json/);
78151
});
79152
});

e2e/nx-plugin-e2e/tests/generator-configuration.e2e.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('nx-plugin g configuration', () => {
6565
expect.objectContaining({
6666
targets: expect.objectContaining({
6767
'code-pushup': {
68-
executor: '@code-pushup/nx-plugin:autorun',
68+
executor: '@code-pushup/nx-plugin:cli',
6969
},
7070
}),
7171
}),
@@ -108,7 +108,7 @@ describe('nx-plugin g configuration', () => {
108108
expect.objectContaining({
109109
targets: expect.objectContaining({
110110
'code-pushup': {
111-
executor: '@code-pushup/nx-plugin:autorun',
111+
executor: '@code-pushup/nx-plugin:cli',
112112
},
113113
}),
114114
}),
@@ -149,7 +149,7 @@ describe('nx-plugin g configuration', () => {
149149
expect.objectContaining({
150150
targets: expect.objectContaining({
151151
'code-pushup': {
152-
executor: '@code-pushup/nx-plugin:autorun',
152+
executor: '@code-pushup/nx-plugin:cli',
153153
},
154154
}),
155155
}),
@@ -193,7 +193,7 @@ describe('nx-plugin g configuration', () => {
193193
expect.objectContaining({
194194
targets: expect.not.objectContaining({
195195
'code-pushup': {
196-
executor: '@code-pushup/nx-plugin:autorun',
196+
executor: '@code-pushup/nx-plugin:cli',
197197
},
198198
}),
199199
}),

e2e/nx-plugin-e2e/tests/plugin-create-nodes.e2e.test.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ describe('nx-plugin', () => {
146146
expect(projectJson.targets).toStrictEqual({
147147
['code-pushup']: {
148148
configurations: {},
149-
executor: `@code-pushup/nx-plugin:autorun`,
149+
executor: `@code-pushup/nx-plugin:cli`,
150150
options: {},
151151
},
152152
});
@@ -174,16 +174,25 @@ describe('nx-plugin', () => {
174174
codeStrings: 'customPlugin()',
175175
},
176176
],
177+
upload: {
178+
server: 'http://staging.code-pushup.dev',
179+
organization: 'code-pushup',
180+
apiKey: '12345678',
181+
},
177182
});
178183

179184
await materializeTree(tree, cwd);
180185

181-
const { stdout } = await executeProcess({
186+
const { stdout, stderr } = await executeProcess({
182187
command: 'npx',
183188
args: ['nx', 'run', `${project}:code-pushup`, '--dryRun'],
184189
cwd,
185190
});
186191

192+
const cleanStderr = removeColorCodes(stderr);
193+
// @TODO create test environment for working plugin. This here misses package-lock.json to execute correctly
194+
expect(cleanStderr).toContain('DryRun execution of: npx @code-pushup/cli');
195+
187196
const cleanStdout = removeColorCodes(stdout);
188197
expect(cleanStdout).toContain(
189198
'NX Successfully ran target code-pushup for project my-lib',
@@ -208,7 +217,7 @@ describe('nx-plugin', () => {
208217

209218
expect(projectJson.targets).toStrictEqual({
210219
['code-pushup']: expect.objectContaining({
211-
executor: 'XYZ:autorun',
220+
executor: 'XYZ:cli',
212221
}),
213222
});
214223
});
@@ -231,7 +240,7 @@ describe('nx-plugin', () => {
231240

232241
expect(projectJson.targets).toStrictEqual({
233242
['code-pushup']: expect.objectContaining({
234-
executor: `@code-pushup/nx-plugin:autorun`,
243+
executor: `@code-pushup/nx-plugin:cli`,
235244
options: {
236245
projectPrefix: 'cli',
237246
},

e2e/nx-plugin-e2e/vite.config.e2e.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default defineConfig({
66
cacheDir: '../../node_modules/.vite/nx-plugin-e2e',
77
test: {
88
reporters: ['basic'],
9-
testTimeout: 60_000,
9+
testTimeout: 160_000,
1010
globals: true,
1111
alias: tsconfigPathAliases(),
1212
pool: 'threads',

global-setup.e2e.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { rm, writeFile } from 'node:fs/promises';
12
import { join } from 'node:path';
23
import { setup as globalSetup } from './global-setup';
34
import { setupTestFolder, teardownTestFolder } from './testing/test-setup/src';
@@ -35,6 +36,7 @@ export async function setup() {
3536

3637
// package publish
3738
const { registry } = activeRegistry.registryData;
39+
await writeFile('.npmrc', `@code-pushup:registry=${registry}`);
3840
try {
3941
console.info('Publish packages');
4042
nxRunManyPublish({
@@ -64,5 +66,6 @@ export async function teardown() {
6466
stopLocalRegistry(stop);
6567
nxRunManyNpmUninstall({ parallel: 1 });
6668
}
69+
await rm('.npmrc');
6770
await teardownTestFolder(e2eDir);
6871
}

global-setup.verdaccio.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ export async function setup() {
3838
export async function teardown() {
3939
// NOTICE - Time saving optimization
4040
// We skip uninstalling packages as the folder is deleted anyway
41-
// comment out to see the folder and web interface
42-
// await nxStopVerdaccioAndTeardownEnv(activeRegistry);
41+
42+
await nxStopVerdaccioAndTeardownEnv(activeRegistry);
4343
}

packages/nx-plugin/README.md

+47-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
# @code-pushup/nx-plugin
22

3+
### Plugin
4+
5+
Register this plugin in your `nx.json` to leverage a set of generators and executors to integrate Code PushUp into a Nx workspace.
6+
7+
#### Registration
8+
9+
```jsonc
10+
// nx.json
11+
{
12+
//...
13+
"plugins": ["@code-pushup/nx-plugin"]
14+
}
15+
```
16+
17+
Resulting targets:
18+
19+
- `nx run <project-name>:code-pushup--configuration` (no config file present)
20+
- `nx run <project-name>:code-pushup` (`code-pushup.config.{ts,mjs,js}` is present)
21+
322
### Generators
423

524
#### Init
625

726
Install JS packages and register plugin.
8-
See [init docs](./src/generators/init/README.md) for details
27+
See [init generator docs](./src/generators/init/README.md) for details
928

1029
Examples:
1130

@@ -15,9 +34,35 @@ Examples:
1534
#### Configuration
1635

1736
Adds a `code-pushup` target to your `project.json`.
18-
See [configuration docs](./src/generators/configuration/README.md) for details
37+
See [configuration generator docs](./src/generators/configuration/README.md) for details
1938

2039
Examples:
2140

2241
- `nx g @code-pushup/nx-plugin:configuration --project=<project-name>`
2342
- `nx g @code-pushup/nx-plugin:configuration --project=<project-name> --targetName=cp`
43+
44+
### Executor
45+
46+
#### CLI
47+
48+
Install JS packages configure a target in your project json.
49+
See [CLI executor docs](./src/executor/cli/README.md) for details
50+
51+
Examples:
52+
53+
```json
54+
{
55+
"name": "my-project",
56+
"targets": {
57+
"code-pushup": {
58+
"executor": "@code-pushup/nx-plugin:cli",
59+
"options": {
60+
"projectPrefix": "workspace-name"
61+
}
62+
}
63+
}
64+
}
65+
```
66+
67+
- `nx run <project-name>:code-pushup`
68+
- `nx run <project-name>:code-pushup print-config --persist.filename=custom-report`
Loading

packages/nx-plugin/executors.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"executors": {
3-
"autorun": {
4-
"implementation": "./src/executors/autorun/executor",
5-
"schema": "./src/executors/autorun/schema.json",
6-
"description": "CodePushup CLI autorun command executor. Executes the @code-pushup/cli autorun command See: https://github.com/code-pushup/cli/blob/main/packages/cli/README.md#autorun-command"
3+
"cli": {
4+
"implementation": "./src/executors/cli/executor",
5+
"schema": "./src/executors/cli/schema.json",
6+
"description": "CodePushup CLI executor. Executes the @code-pushup/cli command's See: https://github.com/code-pushup/cli/blob/main/packages/cli/README.md#commands"
77
}
88
}
99
}

packages/nx-plugin/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
"dependencies": {
1919
"@nx/devkit": "^17.1.3",
20-
"tslib": "^2.6.2",
20+
"tslib": "2.6.3",
2121
"nx": "^17.1.3",
2222
"@code-pushup/models": "0.51.0",
2323
"zod": "^3.22.4",

packages/nx-plugin/src/executors/autorun/constants.ts

-1
This file was deleted.

0 commit comments

Comments
 (0)