Skip to content

Commit

Permalink
feat(nx-plugin): slim down default generated nx-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Apr 18, 2023
1 parent 2dd59c3 commit 041a50d
Show file tree
Hide file tree
Showing 29 changed files with 181 additions and 218 deletions.
2 changes: 1 addition & 1 deletion docs/generated/manifests/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1835,7 +1835,7 @@
"/packages/nx-plugin/generators/e2e-project": {
"description": "Create a E2E application for a Nx Plugin.",
"file": "generated/packages/nx-plugin/generators/e2e-project.json",
"hidden": true,
"hidden": false,
"name": "e2e-project",
"originalFilePath": "/packages/nx-plugin/src/generators/e2e-project/schema.json",
"path": "/packages/nx-plugin/generators/e2e-project",
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/packages-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -1813,7 +1813,7 @@
{
"description": "Create a E2E application for a Nx Plugin.",
"file": "generated/packages/nx-plugin/generators/e2e-project.json",
"hidden": true,
"hidden": false,
"name": "e2e-project",
"originalFilePath": "/packages/nx-plugin/src/generators/e2e-project/schema.json",
"path": "nx-plugin/generators/e2e-project",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
"presets": []
},
"description": "Create a E2E application for a Nx Plugin.",
"hidden": true,
"implementation": "/packages/nx-plugin/src/generators/e2e-project/e2e.ts",
"aliases": [],
"hidden": false,
"path": "/packages/nx-plugin/src/generators/e2e-project/schema.json",
"type": "generator"
}
5 changes: 5 additions & 0 deletions docs/generated/packages/nx-plugin/generators/executor.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
"type": "boolean",
"default": false,
"description": "Should the boilerplate for a custom hasher be generated?"
},
"skipLintChecks": {
"type": "boolean",
"default": false,
"description": "Do not eslint configuration for plugin json files."
}
},
"required": ["project", "name"],
Expand Down
5 changes: 5 additions & 0 deletions docs/generated/packages/nx-plugin/generators/generator.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"enum": ["jest", "none"],
"description": "Test runner to use for unit tests.",
"default": "jest"
},
"skipLintChecks": {
"type": "boolean",
"default": false,
"description": "Do not eslint configuration for plugin json files."
}
},
"required": ["project", "name"],
Expand Down
5 changes: 5 additions & 0 deletions docs/generated/packages/nx-plugin/generators/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
"description": "Whether or not to include `package.json` updates.",
"alias": "p",
"default": false
},
"skipLintChecks": {
"type": "boolean",
"default": false,
"description": "Do not eslint configuration for plugin json files."
}
},
"required": ["project", "packageVersion"],
Expand Down
8 changes: 1 addition & 7 deletions docs/generated/packages/nx-plugin/generators/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for end to end (E2E) tests.",
"default": "jest"
"default": "none"
},
"standaloneConfig": {
"description": "Split the project configuration into `<projectRoot>/project.json` rather than including it inside `workspace.json`.",
Expand All @@ -87,15 +87,9 @@
"enum": ["tsc", "swc"],
"default": "tsc",
"description": "The compiler used by the build and test targets."
},
"minimal": {
"type": "boolean",
"description": "Generate the plugin with a minimal setup. This would involve not generating a default executor and generator.",
"default": false
}
},
"required": ["name"],
"additionalProperties": false,
"presets": []
},
"description": "Create a Nx Plugin.",
Expand Down
51 changes: 23 additions & 28 deletions e2e/nx-plugin/src/nx-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ProjectConfiguration } from '@nx/devkit';
import {
checkFilesExist,
expectTestsPass,
isNotWindows,
killPorts,
newProject,
readJson,
Expand All @@ -12,8 +11,6 @@ import {
uniq,
updateFile,
createFile,
readFile,
removeFile,
cleanupProject,
runCommand,
getPackageManagerCommand,
Expand All @@ -39,33 +36,16 @@ describe('Nx Plugin', () => {
const lintResults = runCLI(`lint ${plugin}`);
expect(lintResults).toContain('All files pass linting.');

expectTestsPass(await runCLIAsync(`test ${plugin}`));

const buildResults = runCLI(`build ${plugin}`);
expect(buildResults).toContain('Done compiling TypeScript files');
checkFilesExist(
`dist/libs/${plugin}/package.json`,
`dist/libs/${plugin}/generators.json`,
`dist/libs/${plugin}/executors.json`,
`dist/libs/${plugin}/src/index.js`,
`dist/libs/${plugin}/src/generators/${plugin}/schema.json`,
`dist/libs/${plugin}/src/generators/${plugin}/schema.d.ts`,
`dist/libs/${plugin}/src/generators/${plugin}/generator.js`,
`dist/libs/${plugin}/src/generators/${plugin}/files/src/index.ts__template__`,
`dist/libs/${plugin}/src/executors/build/executor.js`,
`dist/libs/${plugin}/src/executors/build/schema.d.ts`,
`dist/libs/${plugin}/src/executors/build/schema.json`
`dist/libs/${plugin}/src/index.js`
);
const project = readJson(`libs/${plugin}/project.json`);
expect(project).toMatchObject({
tags: [],
});
const e2eProject = readJson(`apps/${plugin}-e2e/project.json`);

expect(e2eProject).toMatchObject({
tags: [],
implicitDependencies: [`${plugin}`],
});
}, 90000);

// the test invoke ensureNxProject, which points to @nrwl/workspace collection
Expand All @@ -75,7 +55,9 @@ describe('Nx Plugin', () => {
// TODO: Re-enable this to work with pnpm
it(`should run the plugin's e2e tests`, async () => {
const plugin = uniq('plugin-name');
runCLI(`generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint`);
runCLI(
`generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint --e2eTestRunner jest`
);
const e2eResults = runCLI(`e2e ${plugin}-e2e`);
expect(e2eResults).toContain('Successfully ran target e2e');
expect(await killPorts()).toBeTruthy();
Expand Down Expand Up @@ -195,7 +177,9 @@ describe('Nx Plugin', () => {
const plugin = uniq('plugin');
const goodGenerator = uniq('good-generator');
const goodExecutor = uniq('good-executor');
const badExecutorBadImplPath = uniq('bad-executor');
const goodMigration = uniq('good-migration');
const badFactoryPath = uniq('bad-generator');
const badMigrationVersion = uniq('bad-version');
const missingMigrationVersion = uniq('missing-version');

Expand All @@ -207,10 +191,18 @@ describe('Nx Plugin', () => {
`generate @nrwl/nx-plugin:generator ${goodGenerator} --project=${plugin}`
);

runCLI(
`generate @nrwl/nx-plugin:generator ${badFactoryPath} --project=${plugin}`
);

runCLI(
`generate @nrwl/nx-plugin:executor ${goodExecutor} --project=${plugin}`
);

runCLI(
`generate @nrwl/nx-plugin:executor ${badExecutorBadImplPath} --project=${plugin}`
);

runCLI(
`generate @nrwl/nx-plugin:migration ${badMigrationVersion} --project=${plugin} --packageVersion="invalid"`
);
Expand All @@ -226,16 +218,19 @@ describe('Nx Plugin', () => {
updateFile(`libs/${plugin}/generators.json`, (f) => {
const json = JSON.parse(f);
// @proj/plugin:plugin has an invalid implementation path
json.generators[plugin].factory = `./generators/${plugin}/bad-path`;
json.generators[
badFactoryPath
].factory = `./generators/${plugin}/bad-path`;
// @proj/plugin:non-existant has a missing implementation path amd schema
json.generators['non-existant-generator'] = {};
return JSON.stringify(json);
});

updateFile(`libs/${plugin}/executors.json`, (f) => {
const json = JSON.parse(f);
// @proj/plugin:build has an invalid implementation path
json.executors['build'].implementation = './executors/build/bad-path';
// @proj/plugin:badExecutorBadImplPath has an invalid implementation path
json.executors[badExecutorBadImplPath].implementation =
'./executors/bad-path';
// @proj/plugin:non-existant has a missing implementation path amd schema
json.executors['non-existant-executor'] = {};
return JSON.stringify(json);
Expand All @@ -249,7 +244,7 @@ describe('Nx Plugin', () => {

const results = runCLI(`lint ${plugin}`, { silenceError: true });
expect(results).toContain(
`${plugin}: Implementation path should point to a valid file`
`${badFactoryPath}: Implementation path should point to a valid file`
);
expect(results).toContain(
`non-existant-generator: Missing required property - \`schema\``
Expand All @@ -260,7 +255,7 @@ describe('Nx Plugin', () => {
expect(results).not.toContain(goodGenerator);

expect(results).toContain(
`build: Implementation path should point to a valid file`
`${badExecutorBadImplPath}: Implementation path should point to a valid file`
);
expect(results).toContain(
`non-existant-executor: Missing required property - \`schema\``
Expand Down Expand Up @@ -392,7 +387,7 @@ describe('Nx Plugin', () => {
it('should create a plugin in the specified directory', () => {
const plugin = uniq('plugin');
runCLI(
`generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint --directory subdir `
`generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint --directory subdir --e2eTestRunner=jest`
);
checkFilesExist(`libs/subdir/${plugin}/package.json`);
const pluginProject = readProjectConfig(`subdir-${plugin}`);
Expand Down
6 changes: 2 additions & 4 deletions packages/nx-plugin/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"e2e-project": {
"factory": "./src/generators/e2e-project/e2e",
"schema": "./src/generators/e2e-project/schema.json",
"description": "Create a E2E application for a Nx Plugin.",
"hidden": true
"description": "Create a E2E application for a Nx Plugin."
},
"migration": {
"factory": "./src/generators/migration/migration",
Expand Down Expand Up @@ -51,8 +50,7 @@
"e2e-project": {
"factory": "./src/generators/e2e-project/e2e#e2eProjectSchematic",
"schema": "./src/generators/e2e-project/schema.json",
"description": "Create a E2E application for a Nx Plugin.",
"hidden": true
"description": "Create a E2E application for a Nx Plugin."
},
"migration": {
"factory": "./src/generators/migration/migration#migrationSchematic",
Expand Down
18 changes: 0 additions & 18 deletions packages/nx-plugin/src/generators/e2e-project/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,6 @@ describe('NxPlugin e2e-project Generator', () => {
expect(tree.exists('apps/my-plugin-e2e/jest.config.ts')).toBeTruthy();
});

it('should not generate tests when minimal flag is passed', async () => {
// ARRANGE & ACT
await e2eProjectGenerator(tree, {
pluginName: 'my-plugin',
pluginOutputPath: `dist/libs/my-plugin`,
npmPackageName: '@proj/my-plugin',
minimal: true,
});

const { root } = readProjectConfiguration(tree, 'my-plugin-e2e');

// ASSERT

expect(
tree.read(joinPathFragments(root, 'tests/my-plugin.spec.ts'), 'utf-8')
).not.toContain("it('should create ");
});

it('should setup the eslint builder', async () => {
await e2eProjectGenerator(tree, {
pluginName: 'my-plugin',
Expand Down
1 change: 0 additions & 1 deletion packages/nx-plugin/src/generators/e2e-project/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {

return {
...options,
minimal: options.minimal ?? false,
projectName,
linter: options.linter ?? Linter.EsLint,
pluginPropertyName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ensureNxProject,
readJson,
runNxCommandAsync,
runNxCommand,
uniq,
} from '@nx/nx-plugin/testing';

Expand All @@ -23,39 +24,14 @@ describe('<%= pluginName %> e2e', () => {
runNxCommandAsync('reset');
});

<% if(!minimal) { %>

it('should create <%= pluginName %>', async () => {
const project = uniq('<%= pluginName %>');
await runNxCommandAsync(
`generate <%=npmPackageName%>:<%= pluginName %> ${project}`
);
const result = await runNxCommandAsync(`build ${project}`);
expect(result.stdout).toContain('Executor ran');
}, 120000);

describe('--directory', () => {
it('should create src in the specified directory', async () => {
const project = uniq('<%= pluginName %>');
await runNxCommandAsync(
`generate <%=npmPackageName%>:<%= pluginName %> ${project} --directory subdir`
);
expect(() =>
checkFilesExist(`libs/subdir/${project}/src/index.ts`)
).not.toThrow();
}, 120000);
});

describe('--tags', () => {
it('should add tags to the project', async () => {
const projectName = uniq('<%= pluginName %>');
ensureNxProject('<%= npmPackageName %>', '<%= pluginOutputPath %>');
await runNxCommandAsync(
`generate <%=npmPackageName%>:<%= pluginName %> ${projectName} --tags e2etag,e2ePackage`
);
const project = readJson(`libs/${projectName}/project.json`);
expect(project.tags).toEqual(['e2etag', 'e2ePackage']);
}, 120000);
});
<% } %>
// Add some tests here to check that your plugin functionality works as expected.
// A sample test is included below to give you some ideas.
//
// it('should be able to build generated projects', async () => {
// const name = uniq('proj')
// await runNxCommandAsync(`generate <%= npmPackageName %>:{my-generator} --name ${name}`);
// expect(() => runNxCommand('build ${proj}')).not.toThrow();
// expect(() => checkFilesExist([`dist/${name}/index.js`])).not.toThrow();
// });
});
1 change: 0 additions & 1 deletion packages/nx-plugin/src/generators/e2e-project/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export interface Schema {
projectDirectory?: string;
pluginOutputPath?: string;
jestConfig?: string;
minimal?: boolean;
linter?: Linter;
skipFormat?: boolean;
rootProject?: boolean;
Expand Down
Loading

0 comments on commit 041a50d

Please sign in to comment.