From 8708bf86602043959558a78f47d1dfeec5674ff1 Mon Sep 17 00:00:00 2001 From: AgentEnder Date: Mon, 27 Feb 2023 16:58:13 -0500 Subject: [PATCH] feat(core): add deprecation wrapper for workspace-generator --- docs/generated/cli/workspace-generator.md | 40 ++++++++++- .../nx/documents/workspace-generator.md | 40 ++++++++++- e2e/nx-plugin/src/nx-plugin.test.ts | 25 ++++++- .../local-plugin-from-tools/schema.d.ts | 5 -- .../local-plugin-from-tools/schema.json | 24 ------- packages/nx/src/command-line/nx-commands.ts | 19 ++--- .../src/command-line/workspace-generators.ts | 41 +++++++---- .../workspace-generator.ts | 5 +- .../files/generators.json__tmpl__ | 16 ----- ...ve-workspace-generators-to-local-plugin.ts | 71 +++++++------------ scripts/depcheck/missing.ts | 2 - 11 files changed, 164 insertions(+), 124 deletions(-) delete mode 100644 packages/nx-plugin/src/generators/local-plugin-from-tools/schema.d.ts delete mode 100644 packages/nx-plugin/src/generators/local-plugin-from-tools/schema.json delete mode 100644 packages/workspace/src/migrations/update-16-0-0/files/generators.json__tmpl__ diff --git a/docs/generated/cli/workspace-generator.md b/docs/generated/cli/workspace-generator.md index d9b70527ec5814..b5332ac1316c49 100644 --- a/docs/generated/cli/workspace-generator.md +++ b/docs/generated/cli/workspace-generator.md @@ -1,11 +1,13 @@ --- title: 'workspace-generator - CLI command' -description: 'Use a local plugin instead. See: https://nx.dev/deprecated/workspace-generators' +description: 'Runs a workspace generator from the tools/generators directory' --- # workspace-generator -**Deprecated:** Use a local plugin instead. See: https://nx.dev/deprecated/workspace-generators + **Deprecated:** Use a local plugin instead. See: https://nx.dev/deprecated/workspace-generators + + Runs a workspace generator from the tools/generators directory ## Usage @@ -17,12 +19,46 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx` ## Options +### dryRun + +Type: `boolean` + +Default: `false` + +Preview the changes without updating files + +### generator + +Type: `string` + +Name of the generator (e.g., @nrwl/js:library, library) + ### help Type: `boolean` Show help +### interactive + +Type: `boolean` + +Default: `true` + +When false disables interactive input prompts for options + +### quiet + +Type: `boolean` + +Hides logs from tree operations (e.g. `CREATE package.json`) + +### verbose + +Type: `boolean` + +Prints additional information about the commands (e.g., stack traces) + ### version Type: `boolean` diff --git a/docs/generated/packages/nx/documents/workspace-generator.md b/docs/generated/packages/nx/documents/workspace-generator.md index d9b70527ec5814..b5332ac1316c49 100644 --- a/docs/generated/packages/nx/documents/workspace-generator.md +++ b/docs/generated/packages/nx/documents/workspace-generator.md @@ -1,11 +1,13 @@ --- title: 'workspace-generator - CLI command' -description: 'Use a local plugin instead. See: https://nx.dev/deprecated/workspace-generators' +description: 'Runs a workspace generator from the tools/generators directory' --- # workspace-generator -**Deprecated:** Use a local plugin instead. See: https://nx.dev/deprecated/workspace-generators + **Deprecated:** Use a local plugin instead. See: https://nx.dev/deprecated/workspace-generators + + Runs a workspace generator from the tools/generators directory ## Usage @@ -17,12 +19,46 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx` ## Options +### dryRun + +Type: `boolean` + +Default: `false` + +Preview the changes without updating files + +### generator + +Type: `string` + +Name of the generator (e.g., @nrwl/js:library, library) + ### help Type: `boolean` Show help +### interactive + +Type: `boolean` + +Default: `true` + +When false disables interactive input prompts for options + +### quiet + +Type: `boolean` + +Hides logs from tree operations (e.g. `CREATE package.json`) + +### verbose + +Type: `boolean` + +Prints additional information about the commands (e.g., stack traces) + ### version Type: `boolean` diff --git a/e2e/nx-plugin/src/nx-plugin.test.ts b/e2e/nx-plugin/src/nx-plugin.test.ts index 5c32f5392e9293..139b767ae3453c 100644 --- a/e2e/nx-plugin/src/nx-plugin.test.ts +++ b/e2e/nx-plugin/src/nx-plugin.test.ts @@ -259,9 +259,6 @@ describe('Nx Plugin', () => { expect(results).not.toContain(goodMigration); }); - /** - * @todo(@AgentEnder): reenable after figuring out @swc-node - */ describe('local plugins', () => { let plugin: string; beforeEach(() => { @@ -368,6 +365,28 @@ describe('Nx Plugin', () => { }); }); + describe('workspace-generator', () => { + let custom: string; + + it('should work with generate wrapper', () => { + custom = uniq('custom'); + const project = uniq('generated-project'); + runCLI(`g @nrwl/nx-plugin:plugin workspace-plugin --no-interactive`); + runCLI( + `g @nrwl/nx-plugin:generator ${custom} --project workspace-plugin --no-interactive` + ); + runCLI( + `workspace-generator ${custom} --name ${project} --no-interactive` + ); + expect(() => { + checkFilesExist( + `libs/${project}/src/index.ts`, + `libs/${project}/project.json` + ); + }); + }); + }); + describe('--directory', () => { it('should create a plugin in the specified directory', () => { const plugin = uniq('plugin'); diff --git a/packages/nx-plugin/src/generators/local-plugin-from-tools/schema.d.ts b/packages/nx-plugin/src/generators/local-plugin-from-tools/schema.d.ts deleted file mode 100644 index 8491134980d80d..00000000000000 --- a/packages/nx-plugin/src/generators/local-plugin-from-tools/schema.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -export interface LocalPluginFromToolsGeneratorSchema { - pluginName: string; - importPath: string; - toolsProjectRoot: string; -} diff --git a/packages/nx-plugin/src/generators/local-plugin-from-tools/schema.json b/packages/nx-plugin/src/generators/local-plugin-from-tools/schema.json deleted file mode 100644 index 6676f0902d4e0b..00000000000000 --- a/packages/nx-plugin/src/generators/local-plugin-from-tools/schema.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "$schema": "http://json-schema.org/schema", - "cli": "nx", - "$id": "LocalPluginFromTools", - "title": "", - "type": "object", - "description": "Creates a local plugin project from existing workspace generators.", - "properties": { - "pluginName": { - "type": "string", - "description": "The name of the plugin created to contain the workspace generators.", - "default": "nx-plugin" - }, - "importPath": { - "type": "string", - "description": "The library name used to import it, like @myorg/my-awesome-lib." - }, - "toolsProjectRoot": { - "type": "string", - "description": "Where the new file should be located. Defaults to tools/workspace-tools." - } - }, - "required": ["pluginName"] -} diff --git a/packages/nx/src/command-line/nx-commands.ts b/packages/nx/src/command-line/nx-commands.ts index 1cd935a4889785..7c9c86b9717368 100644 --- a/packages/nx/src/command-line/nx-commands.ts +++ b/packages/nx/src/command-line/nx-commands.ts @@ -268,15 +268,17 @@ export const commandsObject = yargs process.exit(0); }, }) - + /** + * @deprecated(v17): Remove `workspace-generator in v17. Use local plugins. + */ .command({ command: 'workspace-generator [name]', - describe: + describe: 'Runs a workspace generator from the tools/generators directory', + deprecated: 'Use a local plugin instead. See: https://nx.dev/deprecated/workspace-generators', - deprecated: true, aliases: ['workspace-schematic [name]'], builder: async (yargs) => - linkToNxDevAndExamples(yargs, 'workspace-generator'), + linkToNxDevAndExamples(withGenerateOptions(yargs), 'workspace-generator'), handler: workspaceGeneratorHandler, }) .command({ @@ -825,10 +827,11 @@ function withRunOneOptions(yargs: yargs.Argv) { } } -async function workspaceGeneratorHandler() { - await ( - await import('./workspace-generators') - ).workspaceGenerators(process.argv.slice(3)); +/** + * @deprecated(v17): Remove `workspace-generator in v17. Use local plugins. + */ +async function workspaceGeneratorHandler(args: yargs.Arguments) { + await (await import('./workspace-generators')).workspaceGenerators(args); process.exit(0); } diff --git a/packages/nx/src/command-line/workspace-generators.ts b/packages/nx/src/command-line/workspace-generators.ts index d0b57359b211db..df8981abff6287 100644 --- a/packages/nx/src/command-line/workspace-generators.ts +++ b/packages/nx/src/command-line/workspace-generators.ts @@ -1,18 +1,35 @@ +import yargs = require('yargs'); +import { readNxJson } from '../config/configuration'; +import { NxJsonConfiguration } from '../devkit-exports'; import { NX_PREFIX } from '../utils/logger'; import { output } from '../utils/output'; -export async function workspaceGenerators(args: string[]) { - const bodyLines: string[] = [ - 'Instead, Nx now supports executing generators or executors from ', - 'local plugins. To run a generator from a local plugin, ', - 'use `nx generate` like you would with any other generator.', - '', - 'For more information, see: https://nx.dev/deprecated/workspace-generators', - ]; - output.error({ +/** + * Wraps `workspace-generator` to invoke `generate`. + * + * @deprecated(v17): Remove `workspace-generator in v17. Use local plugins. + */ +export async function workspaceGenerators(args: yargs.Arguments) { + const generator = process.argv.slice(3); + + output.warn({ title: `${NX_PREFIX} Workspace Generators are no longer supported`, - bodyLines, + bodyLines: [ + 'Instead, Nx now supports executing generators or executors from ', + 'local plugins. To run a generator from a local plugin, ', + 'use `nx generate` like you would with any other generator.', + '', + 'For more information, see: https://nx.dev/deprecated/workspace-generators', + ], }); - // In case users have scripted around workspace-generator, this will ensure commands fail :) - process.exit(1); + + const nxJson: NxJsonConfiguration = readNxJson(); + const collection = nxJson.npmScope + ? `@${nxJson.npmScope}/workspace-plugin` + : 'workspace-plugin'; + + args._ = args._.slice(1); + args.generator = `${collection}:${generator}`; + + return (await import('./generate')).generate(process.cwd(), args); } diff --git a/packages/workspace/src/generators/workspace-generator/workspace-generator.ts b/packages/workspace/src/generators/workspace-generator/workspace-generator.ts index 0dbfc8fc22c87f..fe6e3a6e60228e 100644 --- a/packages/workspace/src/generators/workspace-generator/workspace-generator.ts +++ b/packages/workspace/src/generators/workspace-generator/workspace-generator.ts @@ -1,8 +1,5 @@ import { Schema } from './schema'; -import { - Tree, - stripIndents, -} from '@nx/devkit'; +import { Tree, stripIndents } from '@nx/devkit'; export default async function (host: Tree, schema: Schema) { const message = stripIndents`Workspace Generators are no longer supported. Instead, diff --git a/packages/workspace/src/migrations/update-16-0-0/files/generators.json__tmpl__ b/packages/workspace/src/migrations/update-16-0-0/files/generators.json__tmpl__ deleted file mode 100644 index 221a89c6354510..00000000000000 --- a/packages/workspace/src/migrations/update-16-0-0/files/generators.json__tmpl__ +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "<%= importPath %>", - "generators": { - <% generators.forEach((generator, idx) => { -%> - "<%= generator.name %>": { - "implementation": "<%= generator.implementation %>", - "schema": "<%= generator.schema %>", - <% if( generator.description ) { -%> - "description": "<%= generator.description %>" - <%_ } -%> - <% if( generator.cli ) { -%> - "cli": "<%= generator.cli %>" - <%_ } -%> - }<%= (idx < generators.length - 1) ? "," : "" %><% }) -%> - } -} diff --git a/packages/workspace/src/migrations/update-16-0-0/move-workspace-generators-to-local-plugin.ts b/packages/workspace/src/migrations/update-16-0-0/move-workspace-generators-to-local-plugin.ts index c19e9a97747329..bf213ebc6d9363 100644 --- a/packages/workspace/src/migrations/update-16-0-0/move-workspace-generators-to-local-plugin.ts +++ b/packages/workspace/src/migrations/update-16-0-0/move-workspace-generators-to-local-plugin.ts @@ -2,7 +2,6 @@ import { addDependenciesToPackageJson, ensurePackage, formatFiles, - generateFiles, getProjects, getWorkspaceLayout, joinPathFragments, @@ -10,6 +9,7 @@ import { readJson, readProjectConfiguration, Tree, + updateJson, writeJson, } from '@nrwl/devkit'; // nx-ignore-next-line @@ -21,40 +21,18 @@ import { import { moveGenerator } from '../../generators/move/move'; import { nxVersion } from '../../utils/versions'; import { PackageJson } from 'nx/src/utils/package-json'; -import { runTasksInSerial } from '../../utilities/run-tasks-in-serial'; const PROJECT_NAME = 'workspace-plugin'; const DESTINATION = `tools/${PROJECT_NAME}`; -function addFiles( - tree: Tree, - generators: (GeneratorsJsonEntry & { name: string })[], - destination: string, - importPath: string -) { - const templateOptions = { - generators, - tmpl: '', - importPath, - }; - generateFiles( - tree, - path.join(__dirname, 'files'), - destination, - templateOptions - ); -} - export default async function (tree: Tree) { const tasks = []; if (!tree.children('tools/generators').length) { return; } - const project = getProjects(tree).get(PROJECT_NAME); - if (project) { - await updateExistingPlugin(tree, project); - } else { + let project = getProjects(tree).get(PROJECT_NAME); + if (!project) { await createNewPlugin(tree); tasks.push( addDependenciesToPackageJson( @@ -62,23 +40,23 @@ export default async function (tree: Tree) { {}, { '@nrwl/nx-plugin': nxVersion, - '@nrwl/devkit': nxVersion, - // types/node is neccessary for pnpm since it's used in tsconfig and transitive - // dependencies are not resolved correctly - '@types/node': 'latest', } ) ); + project = readProjectConfiguration(tree, PROJECT_NAME); } + await updateExistingPlugin(tree, project); await formatFiles(tree); - return runTasksInSerial(...tasks); + return () => { + for (const task of tasks) { + task(); + } + }; } // Inspired by packages/nx/src/command-line/workspace-generators.ts function collectAndMoveGenerators(tree: Tree, destinationProjectRoot: string) { - const generators: ({ - name: string; - } & GeneratorsJsonEntry)[] = []; + const generators: Record = {}; const generatorsDir = 'tools/generators'; const destinationDir = joinPathFragments( destinationProjectRoot, @@ -90,12 +68,11 @@ function collectAndMoveGenerators(tree: Tree, destinationProjectRoot: string) { const schemaPath = joinPathFragments(childDir, 'schema.json'); if (tree.exists(schemaPath)) { const schema = readJson(tree, schemaPath); - generators.push({ - name: c, + generators[c] = { implementation: `./src/generators/${c}`, schema: `./src/generators/${joinPathFragments(c, 'schema.json')}`, description: schema.description ?? `Generator ${c}`, - }); + }; tree.rename(childDir, joinPathFragments(destinationDir, c)); } } @@ -107,6 +84,12 @@ async function createNewPlugin(tree: Tree) { const { pluginGenerator } = // nx-ignore-next-line require('@nrwl/nx-plugin/src/generators/plugin/plugin'); + const { createExecutorsJson } = + // nx-ignore-next-line + require('@nrwl/nx-plugin/src/generators/executor/executor'); + const { createGeneratorsJson } = + // nx-ignore-next-line + require('@nrwl/nx-plugin/src/generators/generator/generator'); // nx-ignore-next-line const { Linter } = ensurePackage('@nrwl/linter', nxVersion); @@ -126,13 +109,9 @@ async function createNewPlugin(tree: Tree) { unitTestRunner: 'jest', e2eTestRunner: 'none', }); + createExecutorsJson(tree, readProjectConfiguration(tree, PROJECT_NAME).root); + createGeneratorsJson(tree, readProjectConfiguration(tree, PROJECT_NAME).root); await moveGeneratedPlugin(tree, DESTINATION, importPath); - addFiles( - tree, - collectAndMoveGenerators(tree, DESTINATION), - DESTINATION, - importPath - ); } function moveGeneratedPlugin( @@ -173,9 +152,9 @@ function updateExistingPlugin(tree: Tree, project: ProjectConfiguration) { } const generatorsJson = readJson(tree, generatorsJsonPath); const generators = collectAndMoveGenerators(tree, project.root); - generatorsJson.generators ??= {}; - for (const { name, ...entry } of generators) { - generatorsJson.generators[name] = entry; - } + generatorsJson.generators = { + ...generators, + ...generatorsJson.generators, + }; writeJson(tree, generatorsJsonPath, generatorsJson); } diff --git a/scripts/depcheck/missing.ts b/scripts/depcheck/missing.ts index 5eb06a2719fbf9..ff5cc4a85fb45b 100644 --- a/scripts/depcheck/missing.ts +++ b/scripts/depcheck/missing.ts @@ -250,8 +250,6 @@ export default async function getMissingDependencies( package: { dependencies }, }); - console.log(missing['@nrwl/nx-plugin']); - const packagesMissing = Object.keys(missing).filter( (m) => !IGNORE_MATCHES_IN_PACKAGE['*'].includes(m) &&