Skip to content

Commit

Permalink
feat(nx-plugin): migrate plugins to remove cli prop where feasible an…
Browse files Browse the repository at this point in the history
…d update migrations.json
  • Loading branch information
AgentEnder committed Apr 12, 2023
1 parent 45dc757 commit 4a07183
Show file tree
Hide file tree
Showing 5 changed files with 533 additions and 43 deletions.
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"
}
}
}
5 changes: 0 additions & 5 deletions packages/nx-plugin/src/generators/migration/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,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

0 comments on commit 4a07183

Please sign in to comment.