Skip to content

Commit

Permalink
feat(core): add deprecation wrapper for workspace-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Apr 5, 2023
1 parent 7b35f66 commit 09af47c
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 81 deletions.
40 changes: 38 additions & 2 deletions docs/generated/cli/workspace-generator.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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`
Expand Down
40 changes: 38 additions & 2 deletions docs/generated/packages/nx/documents/workspace-generator.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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`
Expand Down
25 changes: 22 additions & 3 deletions e2e/nx-plugin/src/nx-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,6 @@ describe('Nx Plugin', () => {
expect(results).not.toContain(goodMigration);
});

/**
* @todo(@AgentEnder): reenable after figuring out @swc-node
*/
describe('local plugins', () => {
let plugin: string;
beforeEach(() => {
Expand Down Expand Up @@ -388,6 +385,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');
Expand Down

This file was deleted.

This file was deleted.

19 changes: 11 additions & 8 deletions packages/nx/src/command-line/nx-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -824,10 +826,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);
}

Expand Down
41 changes: 29 additions & 12 deletions packages/nx/src/command-line/workspace-generators.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
readJson,
readProjectConfiguration,
Tree,
updateJson,
writeJson,
} from '@nrwl/devkit';
// nx-ignore-next-line
Expand Down Expand Up @@ -51,34 +52,29 @@ export default async function (tree: Tree) {
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(
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);
}

// Inspired by packages/nx/src/command-line/workspace-generators.ts
function collectAndMoveGenerators(tree: Tree, destinationProjectRoot: string) {
const generators: ({
name: string;
} & GeneratorsJsonEntry)[] = [];
const generators: Record<string, GeneratorsJsonEntry> = {};
const generatorsDir = 'tools/generators';
const destinationDir = joinPathFragments(
destinationProjectRoot,
Expand All @@ -90,12 +86,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));
}
}
Expand Down Expand Up @@ -127,12 +122,6 @@ async function createNewPlugin(tree: Tree) {
e2eTestRunner: 'none',
});
await moveGeneratedPlugin(tree, DESTINATION, importPath);
addFiles(
tree,
collectAndMoveGenerators(tree, DESTINATION),
DESTINATION,
importPath
);
}

function moveGeneratedPlugin(
Expand Down Expand Up @@ -173,9 +162,9 @@ function updateExistingPlugin(tree: Tree, project: ProjectConfiguration) {
}
const generatorsJson = readJson<GeneratorsJson>(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);
}
2 changes: 0 additions & 2 deletions scripts/depcheck/missing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,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) &&
Expand Down

0 comments on commit 09af47c

Please sign in to comment.