Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nx-plugin): remove cli property from generators and executors schema.json file #16259

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions e2e/nx-misc/src/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ describe('migrate', () => {
description: '1.1.0',
factory: './run11',
},
},
generators: {
run20: {
version: '2.0.0',
description: '2.0.0',
Expand All @@ -309,6 +311,7 @@ describe('migrate', () => {
updateFile(
`./node_modules/migrate-parent-package/run11.js`,
`
var angular_devkit_core1 = require("@angular-devkit/core");
exports.default = function default_1() {
return function(host) {
host.create('file-11', 'content11')
Expand Down
3 changes: 3 additions & 0 deletions e2e/nx-misc/src/nxw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ describe('nx wrapper / .nx installation', () => {
description: '1.1.0',
factory: './run11',
},
},
generators: {
run20: {
version: '2.0.0',
description: '2.0.0',
Expand All @@ -136,6 +138,7 @@ describe('nx wrapper / .nx installation', () => {
updateFile(
`.nx/installation/node_modules/migrate-parent-package/run11.js`,
`
var angular_devkit_core1 = require("@angular-devkit/core");
exports.default = function default_1() {
return function(host) {
host.create('file-11', 'content11')
Expand Down
6 changes: 6 additions & 0 deletions packages/nx-plugin/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
"cli": "nx",
"description": "Update nx plugin jest test files to support jest 29 changes (https://jestjs.io/docs/upgrading-to-jest29)",
"factory": "./src/migrations/update-15-9-0/jest-29-tests"
},
"update-remove-cli-prop": {
"version": "16.0.0-beta.1",
"cli": "nx",
"description": "Removes CLI property within schema.json files and moves generators and schematics to the proper root node in migrations.json",
"factory": "./src/migrations/update-16-0-0/cli-in-schema-json"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"$schema": "http://json-schema.org/schema",
"version": 2,
"cli": "nx",
"title": "<%= className %> executor",
"description": "",
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "<%= className %>",
"title": "",
"type": "object",
Expand Down
13 changes: 1 addition & 12 deletions packages/nx-plugin/src/generators/migration/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ import type { Tree } from '@nrwl/devkit';
import type { Schema } from './schema';
import * as path from 'path';
import { addMigrationJsonChecks } from '../lint-checks/generator';
import type { Linter as EsLint } from 'eslint';
import {
NxMigrationsConfiguration,
PackageJson,
PackageJsonTargetConfiguration,
readNxMigrateConfig,
} from 'nx/src/utils/package-json';
import { PackageJson, readNxMigrateConfig } from 'nx/src/utils/package-json';
interface NormalizedSchema extends Schema {
projectRoot: string;
projectSourceRoot: string;
Expand Down Expand Up @@ -74,11 +68,6 @@ function updateMigrationsJson(host: Tree, options: NormalizedSchema) {
? readJson(host, migrationsPath)
: {};

if (migrations.schematics) {
migrations.generators = migrations.schematics;
delete migrations.schematics;
}

const generators = migrations.generators ?? {};
generators[options.name] = {
version: options.packageVersion,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
import {
ExecutorsJson,
GeneratorsJson,
joinPathFragments,
MigrationsJson,
readJson,
readProjectConfiguration,
Tree,
updateJson,
writeJson,
} from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { Linter } from '@nrwl/linter';
import { PackageJson } from 'nx/src/utils/package-json';
import executorGenerator from '../../generators/executor/executor';
import generatorGenerator from '../../generators/generator/generator';
import pluginGenerator from '../../generators/plugin/plugin';
import { updateCliPropsForPlugins } from './cli-in-schema-json';

describe('updateCliPropsForPlugins', () => {
it('should move non-nx generators to schematics for migrations.json', async () => {
const tree = createTreeWithEmptyWorkspace();
const { root } = await createPlugin(tree);
updatePluginPackageJson(tree, {
'nx-migrations': 'migrations.json',
});
writeJson<MigrationsJson>(
tree,
joinPathFragments(root, 'migrations.json'),
{
version: '1.0.0',
generators: {
'migration-1': {
version: '1.0.0',
description: 'My Plugin 1',
factory: './migrations/my-plugin-1',
},
},
}
);
await updateCliPropsForPlugins(tree);
const updated = readJson<MigrationsJson>(
tree,
joinPathFragments(root, 'migrations.json')
);
expect(updated.generators).not.toHaveProperty('migration-1');
expect(updated.schematics).toHaveProperty('migration-1');
});

it('should move nx generators to generators for migrations.json', async () => {
const tree = createTreeWithEmptyWorkspace();
const { root } = await createPlugin(tree);
updatePluginPackageJson(tree, {
'nx-migrations': 'migrations.json',
});
writeJson<MigrationsJson>(
tree,
joinPathFragments(root, 'migrations.json'),
{
version: '1.0.0',
schematics: {
'migration-1': {
version: '1.0.0',
description: 'My Plugin 1',
factory: './migrations/my-plugin-1',
cli: 'nx',
},
},
}
);
await updateCliPropsForPlugins(tree);
const updated = readJson<MigrationsJson>(
tree,
joinPathFragments(root, 'migrations.json')
);
expect(updated.schematics).not.toHaveProperty('migration-1');
expect(updated.generators).toHaveProperty('migration-1');
});

it('should move both nx generators to generators and non-nx schematics to schematics for migrations.json', async () => {
const tree = createTreeWithEmptyWorkspace();
const { root } = await createPlugin(tree);
updatePluginPackageJson(tree, {
'nx-migrations': 'migrations.json',
});
writeJson<MigrationsJson>(
tree,
joinPathFragments(root, 'migrations.json'),
{
version: '1.0.0',
schematics: {
'migration-1': {
version: '1.0.0',
description: 'My Plugin 1',
factory: './migrations/my-plugin-1',
cli: 'nx',
},
'migration-2': {
version: '1.0.0',
description: 'My Plugin 2',
factory: './migrations/my-plugin-2',
},
},
generators: {
'migration-3': {
version: '1.0.0',
description: 'My Plugin 3',
factory: './migrations/my-plugin-3',
cli: 'nx',
},
'migration-4': {
version: '1.0.0',
description: 'My Plugin 4',
factory: './migrations/my-plugin-4',
},
},
}
);
await updateCliPropsForPlugins(tree);
const updated = readJson<MigrationsJson>(
tree,
joinPathFragments(root, 'migrations.json')
);
expect(updated.schematics).not.toHaveProperty('migration-1');
expect(updated.generators).toHaveProperty('migration-1');
expect(updated.schematics).toHaveProperty('migration-2');
expect(updated.generators).not.toHaveProperty('migration-2');
expect(updated.schematics).not.toHaveProperty('migration-3');
expect(updated.generators).toHaveProperty('migration-3');
expect(updated.schematics).toHaveProperty('migration-4');
expect(updated.generators).not.toHaveProperty('migration-4');
});

it('should remove cli property from executors', async () => {
const tree = createTreeWithEmptyWorkspace();
const { root, name } = await createPlugin(tree);
executorGenerator(tree, {
name: 'my-executor',
project: name,
unitTestRunner: 'jest',
includeHasher: false,
});
const schemaPath = joinPathFragments(
root,
'src/executors/my-executor/schema.json'
);
updateJson(tree, schemaPath, (schema) => {
schema.cli = 'nx';
return schema;
});
await updateCliPropsForPlugins(tree);
const updated = readJson(tree, schemaPath);
expect(updated).not.toHaveProperty('cli');
});

it('should remove cli property from builders', async () => {
const tree = createTreeWithEmptyWorkspace();
const { root, name } = await createPlugin(tree);
executorGenerator(tree, {
name: 'my-executor',
project: name,
unitTestRunner: 'jest',
includeHasher: false,
});
updateJson<ExecutorsJson>(
tree,
joinPathFragments(root, 'executors.json'),
(json) => {
json.builders = json.executors;
delete json.builders;
return json;
}
);
const schemaPath = joinPathFragments(
root,
'src/executors/my-executor/schema.json'
);
updateJson(tree, schemaPath, (schema) => {
schema.cli = 'nx';
return schema;
});
await updateCliPropsForPlugins(tree);
const updated = readJson(tree, schemaPath);
expect(updated).not.toHaveProperty('cli');
});

it('should remove cli property from generators', async () => {
const tree = createTreeWithEmptyWorkspace();
const { root, name } = await createPlugin(tree);
generatorGenerator(tree, {
name: 'my-generator',
project: name,
unitTestRunner: 'jest',
});
const schemaPath = joinPathFragments(
root,
'src/generators/my-generator/schema.json'
);
updateJson(tree, schemaPath, (schema) => {
schema.cli = 'nx';
return schema;
});
await updateCliPropsForPlugins(tree);
const updated = readJson(tree, schemaPath);
expect(updated).not.toHaveProperty('cli');
});

it('should remove cli property from schematics', async () => {
const tree = createTreeWithEmptyWorkspace();
const { root, name } = await createPlugin(tree);
generatorGenerator(tree, {
name: 'my-schematic',
project: name,
unitTestRunner: 'jest',
});
updateJson<GeneratorsJson>(
tree,
joinPathFragments(root, 'generators.json'),
(json) => {
json.schematics = json.generators;
delete json.generators;
return json;
}
);
const schemaPath = joinPathFragments(
root,
'src/generators/my-schematic/schema.json'
);
updateJson(tree, schemaPath, (schema) => {
schema.cli = 'nx';
return schema;
});
await updateCliPropsForPlugins(tree);
const updated = readJson(tree, schemaPath);
expect(updated).not.toHaveProperty('cli');
});
});

async function createPlugin(tree: Tree) {
await pluginGenerator(tree, {
name: 'my-plugin',
compiler: 'tsc',
linter: Linter.EsLint,
unitTestRunner: 'jest',
skipFormat: true,
skipLintChecks: false,
skipTsConfig: false,
});
return readProjectConfiguration(tree, 'my-plugin');
}

function updatePluginPackageJson(
tree: Tree,
packageJsonProps: Partial<PackageJson>
) {
const { root } = readProjectConfiguration(tree, 'my-plugin');
updateJson(tree, root + '/package.json', (json) => {
const base = { json, ...packageJsonProps };
for (const prop in base) {
if (base[prop] === null || base[prop] === undefined) {
delete json[prop];
}
}
return base;
});
}
Loading