Skip to content

Commit

Permalink
feat(nx-plugin): slim down default generated nx-plugin (nrwl#16168)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored and peppoasap committed Apr 18, 2023
1 parent aba3386 commit 4cac0fd
Show file tree
Hide file tree
Showing 40 changed files with 316 additions and 260 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 add an eslint configuration for plugin json files."
}
},
"required": ["project", "name"],
Expand Down
11 changes: 11 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,17 @@
"enum": ["jest", "none"],
"description": "Test runner to use for unit tests.",
"default": "jest"
},
"skipLintChecks": {
"type": "boolean",
"default": false,
"description": "Do not add an eslint configuration for plugin json files."
},
"skipFormat": {
"type": "boolean",
"default": false,
"description": "Do not format files with prettier.",
"x-priority": "internal"
}
},
"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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"description": "Which project should be the configuration be added to?",
"$default": { "$source": "projectName" },
"x-priority": "important"
},
"skipFormat": {
"type": "boolean",
"x-priority": "internal",
"description": "Skip formatting files with prettier.",
"default": false
}
},
"required": ["projectName"],
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
57 changes: 20 additions & 37 deletions e2e/nx-plugin/src/nx-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,48 +36,18 @@ 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
// which walks up the directory to find it in the next repo itself, so it
// doesn't use the collection we are building
// we should change it to point to the right collection using relative path
// 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`);
const e2eResults = runCLI(`e2e ${plugin}-e2e`);
expect(e2eResults).toContain('Successfully ran target e2e');
expect(await killPorts()).toBeTruthy();
}, 250000);

it('should be able to generate a migration', async () => {
const plugin = uniq('plugin');
const version = '1.0.0';
Expand Down Expand Up @@ -192,7 +162,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 @@ -204,10 +176,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 @@ -223,16 +203,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 @@ -246,7 +229,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 @@ -257,7 +240,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 @@ -389,7 +372,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
8 changes: 1 addition & 7 deletions e2e/utils/create-project-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,10 @@ export function runCreateWorkspace(
export function runCreatePlugin(
name: string,
{
pluginName,
packageManager,
extraArgs,
useDetectedPm = false,
}: {
pluginName?: string;
packageManager?: 'npm' | 'yarn' | 'pnpm';
extraArgs?: string;
useDetectedPm?: boolean;
Expand All @@ -232,11 +230,7 @@ export function runCreatePlugin(

let command = `${
pm.runUninstalledPackage
} create-nx-plugin@${getPublishedVersion()} ${name}`;

if (pluginName) {
command += ` --pluginName=${pluginName} --no-nxCloud`;
}
} create-nx-plugin@${getPublishedVersion()} ${name} --no-nxCloud`;

if (packageManager && !useDetectedPm) {
command += ` --package-manager=${packageManager}`;
Expand Down
14 changes: 9 additions & 5 deletions e2e/workspace-create/src/create-nx-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ describe('create-nx-plugin', () => {

afterEach(() => cleanupProject());

it('should be able to create a plugin repo and run plugin e2e', () => {
const wsName = uniq('ws-plugin');
it('should be able to create a plugin repo build a plugin', () => {
const pluginName = uniq('plugin');

runCreatePlugin(wsName, {
runCreatePlugin(pluginName, {
packageManager,
pluginName,
});

checkFilesExist(
Expand All @@ -31,6 +29,12 @@ describe('create-nx-plugin', () => {
`executors.json`
);

expect(() => runCLI(`e2e e2e`)).not.toThrow();
runCLI(`build ${pluginName}`);

checkFilesExist(
`dist/package.json`,
`dist/generators.json`,
`dist/executors.json`
);
});
});
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
Loading

0 comments on commit 4cac0fd

Please sign in to comment.