-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nx-plugin): add project prefix to plugin (#792)
- Loading branch information
Showing
17 changed files
with
269 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
import { mkdir, writeFile } from "node:fs/promises"; | ||
import { join } from "node:path"; | ||
import { mkdir, writeFile } from 'node:fs/promises'; | ||
import { join } from 'node:path'; | ||
|
||
export async function createNpmWorkspace(cwd: string) { | ||
await mkdir(cwd, { recursive: true }); | ||
await writeFile(join(cwd, 'package.json'), JSON.stringify({ | ||
name: 'create-npm-workspace', | ||
version: '0.0.1', | ||
scripts: { | ||
test: 'echo "Error: no test specified" && exit 1', | ||
}, | ||
keywords: [], | ||
}, null, 2)); | ||
await writeFile( | ||
join(cwd, 'package.json'), | ||
JSON.stringify( | ||
{ | ||
name: 'create-npm-workspace', | ||
version: '0.0.1', | ||
scripts: { | ||
test: 'echo "Error: no test specified" && exit 1', | ||
}, | ||
keywords: [], | ||
}, | ||
null, | ||
2, | ||
), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { Tree, updateProjectConfiguration } from '@nx/devkit'; | ||
import { rm } from 'node:fs/promises'; | ||
import { join, relative } from 'node:path'; | ||
import { readProjectConfiguration } from 'nx/src/generators/utils/project-configuration'; | ||
import { afterEach, expect } from 'vitest'; | ||
import { generateCodePushupConfig } from '@code-pushup/nx-plugin'; | ||
import { | ||
generateWorkspaceAndProject, | ||
materializeTree, | ||
} from '@code-pushup/test-nx-utils'; | ||
import { removeColorCodes } from '@code-pushup/test-utils'; | ||
import { executeProcess } from '@code-pushup/utils'; | ||
|
||
function relativePathToCwd(testDir: string): string { | ||
return relative(join(process.cwd(), testDir), process.cwd()); | ||
} | ||
|
||
async function addTargetToWorkspace( | ||
tree: Tree, | ||
options: { cwd: string; project: string }, | ||
) { | ||
const { cwd, project } = options; | ||
const pathRelativeToPackage = relative(join(cwd, 'libs', project), cwd); | ||
const projectCfg = readProjectConfiguration(tree, project); | ||
updateProjectConfiguration(tree, project, { | ||
...projectCfg, | ||
targets: { | ||
...projectCfg.targets, | ||
['code-pushup']: { | ||
executor: `${join( | ||
relativePathToCwd(cwd), | ||
'dist/packages/nx-plugin', | ||
)}:autorun`, | ||
}, | ||
}, | ||
}); | ||
const { root } = projectCfg; | ||
generateCodePushupConfig(tree, root, { | ||
fileImports: `import type {CoreConfig} from "${join( | ||
relativePathToCwd(cwd), | ||
pathRelativeToPackage, | ||
'dist/packages/models', | ||
)}";`, | ||
plugins: [ | ||
{ | ||
fileImports: `import {customPlugin} from "${join( | ||
relativePathToCwd(cwd), | ||
pathRelativeToPackage, | ||
'dist/testing/test-utils', | ||
)}";`, | ||
codeStrings: 'customPlugin()', | ||
}, | ||
], | ||
}); | ||
await materializeTree(tree, cwd); | ||
} | ||
|
||
describe('executor autorun', () => { | ||
let tree: Tree; | ||
const project = 'my-lib'; | ||
const baseDir = 'tmp/nx-plugin-e2e/executor'; | ||
|
||
beforeEach(async () => { | ||
tree = await generateWorkspaceAndProject(project); | ||
}); | ||
|
||
afterEach(async () => { | ||
await rm(baseDir, { recursive: true, force: true }); | ||
}); | ||
|
||
it('should execute autorun executor', async () => { | ||
const cwd = join(baseDir, 'execute-dynamic-executor'); | ||
await addTargetToWorkspace(tree, { cwd, project }); | ||
|
||
const { stdout, code } = await executeProcess({ | ||
command: 'npx', | ||
args: ['nx', 'run', `${project}:code-pushup`, '--dryRun'], | ||
cwd, | ||
}); | ||
|
||
expect(code).toBe(0); | ||
const cleanStdout = removeColorCodes(stdout); | ||
expect(cleanStdout).toContain('nx run my-lib:code-pushup --dryRun'); | ||
}); | ||
}); |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
export type DynamicTargetOptions = { | ||
// @TODO add prefix https://github.com/code-pushup/cli/issues/619 | ||
targetName?: string; | ||
bin?: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const CODE_PUSHUP_CONFIG_REGEX = | ||
/^code-pushup\.config\.(\w*\.)*(ts|js|mjs)$/; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
import { TargetConfiguration } from '@nx/devkit'; | ||
import { RunCommandsOptions } from 'nx/src/executors/run-commands/run-commands.impl'; | ||
import { PACKAGE_NAME } from '../../internal/constants'; | ||
import { ProjectPrefixOptions } from '../types'; | ||
|
||
export function createExecutorTarget(options?: { | ||
bin?: string; | ||
}): TargetConfiguration<RunCommandsOptions> { | ||
const { bin = PACKAGE_NAME } = options ?? {}; | ||
projectPrefix?: string; | ||
}): TargetConfiguration<ProjectPrefixOptions> { | ||
const { bin = PACKAGE_NAME, projectPrefix } = options ?? {}; | ||
return { | ||
executor: `${bin}:autorun`, | ||
...(projectPrefix | ||
? { | ||
options: { | ||
projectPrefix, | ||
}, | ||
} | ||
: {}), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.