Skip to content

Commit

Permalink
feat(nx-plugin): add generator for plugin-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Apr 12, 2022
1 parent 3d44680 commit 0ad1d23
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 4 deletions.
9 changes: 8 additions & 1 deletion packages/nx-plugin/generators.json
@@ -1,7 +1,9 @@
{
"name": "nx/plugin",
"version": "0.1",
"extends": ["@nrwl/workspace"],
"extends": [
"@nrwl/workspace"
],
"generators": {
"plugin": {
"factory": "./src/generators/plugin/plugin",
Expand All @@ -28,6 +30,11 @@
"factory": "./src/generators/executor/executor",
"schema": "./src/generators/executor/schema.json",
"description": "Create a executor for an Nx Plugin."
},
"plugin-lint": {
"factory": "./src/generators/plugin-lint/generator",
"schema": "./src/generators/plugin-lint/schema.json",
"description": "plugin-lint generator"
}
},
"schematics": {
Expand Down
4 changes: 2 additions & 2 deletions packages/nx-plugin/src/executors/plugin-lint/schema.d.ts
@@ -1,5 +1,5 @@
export interface NxPluginLintExecutorOptions {
validateSchemas: string;
validateImplementationLocations: string;
validateSchemas: boolean;
validateImplementationLocations: boolean;
packageJson: string;
}
2 changes: 1 addition & 1 deletion packages/nx-plugin/src/executors/plugin-lint/schema.json
Expand Up @@ -17,7 +17,7 @@
"packageJson": {
"type": "string",
"description": "The path to the package.json file for the plugin",
"default": "generators.json"
"default": "package.json"
}
},
"additionalProperties": false,
Expand Down
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`plugin-lint generator should add plugin-lint target by default 1`] = `
Object {
"executor": "@nrwl/nx-plugin:plugin-lint",
"options": Object {},
}
`;
40 changes: 40 additions & 0 deletions packages/nx-plugin/src/generators/plugin-lint/generator.spec.ts
@@ -0,0 +1,40 @@
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { Tree, readProjectConfiguration } from '@nrwl/devkit';

import generator from './generator';
import pluginGenerator from '../plugin/plugin';
import { Linter } from '@nrwl/linter';

describe('plugin-lint generator', () => {
let appTree: Tree;
let libName: string;

beforeEach(() => {
appTree = createTreeWithEmptyWorkspace(2);
pluginGenerator(appTree, {
name: 'plugin',
importPath: '@acme/plugin',
compiler: 'tsc',
linter: Linter.EsLint,
skipFormat: false,
skipTsConfig: false,
unitTestRunner: 'jest',
});
});

it('should add plugin-lint target by default', async () => {
await generator(appTree, { projectName: 'plugin' });
const config = readProjectConfiguration(appTree, 'plugin').targets?.[
'plugin-lint'
];
expect(config).toMatchSnapshot();
});

it('should not overwrite existing target', async () => {
await generator(appTree, { projectName: 'plugin', targetName: 'build' });
const config = readProjectConfiguration(appTree, 'plugin').targets?.[
'build'
];
expect(config.executor).not.toEqual('@nrwl/nx-plugin:plugin-lint');
})
});
40 changes: 40 additions & 0 deletions packages/nx-plugin/src/generators/plugin-lint/generator.ts
@@ -0,0 +1,40 @@
import {
formatFiles,
logger,
readProjectConfiguration,
Tree,
updateProjectConfiguration,
} from '@nrwl/devkit';
import { PluginLintGeneratorSchema } from './schema';

function normalizeOptions(
options: PluginLintGeneratorSchema
): Required<PluginLintGeneratorSchema> {
return {
...options,
targetName: options.targetName ?? 'plugin-lint',
};
}

export default async function pluiginLintGenerator(
tree: Tree,
options: PluginLintGeneratorSchema
) {
const normalizedOptions = normalizeOptions(options);

const project = readProjectConfiguration(tree, normalizedOptions.projectName);
project.targets ??= {};
const existing = project.targets[normalizedOptions.targetName];
if (existing) {
logger.error(
`${normalizedOptions.projectName} > ${normalizedOptions.targetName} already exists.`
);
} else {
project.targets[normalizedOptions.targetName] = {
executor: '@nrwl/nx-plugin:plugin-lint',
options: {},
};
updateProjectConfiguration(tree, normalizedOptions.projectName, project);
}
await formatFiles(tree);
}
4 changes: 4 additions & 0 deletions packages/nx-plugin/src/generators/plugin-lint/schema.d.ts
@@ -0,0 +1,4 @@
export interface PluginLintGeneratorSchema {
projectName: string;
targetName?: string;
}
19 changes: 19 additions & 0 deletions packages/nx-plugin/src/generators/plugin-lint/schema.json
@@ -0,0 +1,19 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "PluginLint",
"title": "",
"type": "object",
"properties": {
"projectName": {
"type": "string",
"description": "Which project should be the target be added to?"
},
"targetName": {
"type": "string",
"description": "What should the target be called?",
"default": "plugin-lint"
}
},
"required": ["projectName", "targetName"]
}

0 comments on commit 0ad1d23

Please sign in to comment.