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 11, 2023
1 parent e81bdd4 commit 069ace9
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 202 deletions.
2 changes: 1 addition & 1 deletion docs/generated/manifests/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1923,7 +1923,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 @@ -1898,7 +1898,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"
}
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
28 changes: 5 additions & 23 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 '@nrwl/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 @@ -392,7 +374,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 '@nrwl/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
8 changes: 4 additions & 4 deletions packages/nx-plugin/src/generators/executor/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ function addHasherFiles(host: Tree, options: NormalizedSchema) {
}
}

function createExecutorsJson(host: Tree, options: NormalizedSchema) {
export function createExecutorsJson(host: Tree, projectRoot: string) {
updateJson<PackageJson>(
host,
joinPathFragments(options.projectRoot, 'package.json'),
joinPathFragments(projectRoot, 'package.json'),
(json) => {
json.executors ??= './executors.json';
return json;
}
);
writeJson<ExecutorsJson>(
host,
joinPathFragments(options.projectRoot, 'executors.json'),
joinPathFragments(projectRoot, 'executors.json'),
{
executors: {},
}
Expand All @@ -102,7 +102,7 @@ function updateExecutorJson(host: Tree, options: NormalizedSchema) {
executorsPath = joinPathFragments(options.projectRoot, 'executors.json');
}
if (!host.exists(executorsPath)) {
createExecutorsJson(host, options);
createExecutorsJson(host, options.projectRoot);
}

return updateJson(host, executorsPath, (json) => {
Expand Down
2 changes: 0 additions & 2 deletions packages/nx-plugin/src/generators/generator/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ describe('NxPlugin Generator Generator', () => {
'libs/my-plugin/generators.json'
);

console.log(generatorJson.generators['preset']);

expect(
generatorJson.generators['preset']['x-use-standalone-layout']
).toEqual(true);
Expand Down
8 changes: 4 additions & 4 deletions packages/nx-plugin/src/generators/generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ function addFiles(host: Tree, options: NormalizedSchema) {
}
}

function createGeneratorsJson(host: Tree, options: NormalizedSchema) {
export function createGeneratorsJson(host: Tree, projectRoot: string) {
updateJson<PackageJson>(
host,
joinPathFragments(options.projectRoot, 'package.json'),
joinPathFragments(projectRoot, 'package.json'),
(json) => {
json.generators ??= './generators.json';
return json;
}
);
writeJson<GeneratorsJson>(
host,
joinPathFragments(options.projectRoot, 'generators.json'),
joinPathFragments(projectRoot, 'generators.json'),
{
generators: {},
}
Expand All @@ -119,7 +119,7 @@ function updateGeneratorJson(host: Tree, options: NormalizedSchema) {
generatorsPath = joinPathFragments(options.projectRoot, 'generators.json');
}
if (!host.exists(generatorsPath)) {
createGeneratorsJson(host, options);
createGeneratorsJson(host, options.projectRoot);
}

return updateJson<GeneratorsJson>(host, generatorsPath, (json) => {
Expand Down
Loading

0 comments on commit 069ace9

Please sign in to comment.