-
Notifications
You must be signed in to change notification settings - Fork 15
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 configuration generator (#294)
Close #61
- Loading branch information
Showing
6 changed files
with
136 additions
and
0 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
16 changes: 16 additions & 0 deletions
16
packages/nx-plugin/src/generators/configuration/files/code-pushup.config.ts.template
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,16 @@ | ||
import type { CoreConfig } from '@code-pushup/models'; | ||
|
||
const config: CoreConfig = { | ||
persist: { | ||
outputDir: '.code-pushup', | ||
format: ['json', 'md', 'stdout'], | ||
}, | ||
plugins: [ | ||
// ... | ||
], | ||
categories: [ | ||
// ... | ||
], | ||
}; | ||
|
||
export default config; |
47 changes: 47 additions & 0 deletions
47
packages/nx-plugin/src/generators/configuration/generator.ts
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,47 @@ | ||
import { | ||
Tree, | ||
formatFiles, | ||
generateFiles, | ||
readProjectConfiguration, | ||
updateProjectConfiguration, | ||
} from '@nx/devkit'; | ||
import * as path from 'path'; | ||
import { join } from 'path'; | ||
import { AddToProjectGeneratorSchema } from './schema'; | ||
|
||
export async function addToProjectGenerator( | ||
tree: Tree, | ||
options: AddToProjectGeneratorSchema, | ||
) { | ||
const projectConfiguration = readProjectConfiguration(tree, options.project); | ||
|
||
const { root } = projectConfiguration; | ||
|
||
if (tree.exists(path.join(root, 'code-pushup.config.ts'))) { | ||
console.log('Code PushUp already configured for this project'); | ||
return; | ||
} | ||
|
||
generateFiles(tree, path.join(__dirname, 'files'), root, options); | ||
|
||
updateProjectConfiguration(tree, options.project, { | ||
...projectConfiguration, | ||
targets: { | ||
...projectConfiguration.targets, | ||
'code-pushup': { | ||
executor: 'nx:run-commands', | ||
options: { | ||
command: `code-pushup autorun --no-progress --config=${join( | ||
'./', | ||
root, | ||
'code-pushup.config.ts', | ||
)}`, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
await formatFiles(tree); | ||
} | ||
|
||
export default addToProjectGenerator; |
46 changes: 46 additions & 0 deletions
46
packages/nx-plugin/src/generators/configuration/generator.unit.test.ts
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,46 @@ | ||
import { | ||
Tree, | ||
addProjectConfiguration, | ||
readProjectConfiguration, | ||
} from '@nx/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; | ||
import { join } from 'path'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { addToProjectGenerator } from './generator'; | ||
|
||
describe('init generator', () => { | ||
let tree: Tree; | ||
const testProjectName = 'test-app'; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
|
||
addProjectConfiguration(tree, testProjectName, { | ||
root: testProjectName, | ||
projectType: 'library', | ||
sourceRoot: `${testProjectName}/src`, | ||
targets: {}, | ||
}); | ||
}); | ||
|
||
it('should add code-pushup.config.js to the project root', async () => { | ||
await addToProjectGenerator(tree, { project: testProjectName }); | ||
|
||
const projectConfiguration = readProjectConfiguration( | ||
tree, | ||
testProjectName, | ||
); | ||
|
||
expect(tree.exists('test-app/code-pushup.config.ts')).toBe(true); | ||
expect(projectConfiguration.targets?.['code-pushup']).toEqual({ | ||
executor: 'nx:run-commands', | ||
options: { | ||
command: `code-pushup autorun --no-progress --config=${join( | ||
'./', | ||
projectConfiguration.root, | ||
'code-pushup.config.ts', | ||
)}`, | ||
}, | ||
}); | ||
}); | ||
}); |
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,3 @@ | ||
export interface AddToProjectGeneratorSchema { | ||
project: string; | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/nx-plugin/src/generators/configuration/schema.json
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,19 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"$id": "AddToProject", | ||
"title": "", | ||
"type": "object", | ||
"properties": { | ||
"project": { | ||
"type": "string", | ||
"description": "The name of the project.", | ||
"x-prompt": "Which project should configure Code Pushup?", | ||
"x-dropdown": "projects", | ||
"$default": { | ||
"$source": "argv", | ||
"index": 0 | ||
} | ||
} | ||
}, | ||
"required": ["project"] | ||
} |