Skip to content

Commit

Permalink
feat(nx-plugin): add configuration generator (#294)
Browse files Browse the repository at this point in the history
Close #61
  • Loading branch information
IKatsuba authored Nov 23, 2023
1 parent e8effbf commit ee21143
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/nx-plugin/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"factory": "./src/generators/init/generator",
"schema": "./src/generators/init/schema.json",
"description": "init generator"
},
"configuration": {
"factory": "./src/generators/configuration/generator",
"schema": "./src/generators/configuration/schema.json",
"description": "configuration generator"
}
}
}
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 packages/nx-plugin/src/generators/configuration/generator.ts
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;
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',
)}`,
},
});
});
});
3 changes: 3 additions & 0 deletions packages/nx-plugin/src/generators/configuration/schema.d.ts
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 packages/nx-plugin/src/generators/configuration/schema.json
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"]
}

0 comments on commit ee21143

Please sign in to comment.