Skip to content

Commit

Permalink
chore(core): replace workspace-generator command with deprecation notice
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Feb 24, 2023
1 parent 0216467 commit cb4900a
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 495 deletions.
16 changes: 2 additions & 14 deletions docs/generated/cli/workspace-generator.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
title: 'workspace-generator - CLI command'
description: 'Runs a workspace generator from the tools/generators directory'
description: 'Use a local plugin instead. See: https://nx.dev/deprecated/workspace-generators'
---

# workspace-generator

Runs a workspace generator from the tools/generators directory
**Deprecated:** Use a local plugin instead. See: https://nx.dev/deprecated/workspace-generators

## Usage

Expand All @@ -23,18 +23,6 @@ Type: `boolean`

Show help

### list-generators

Type: `boolean`

List the available workspace-generators

### name

Type: `string`

The name of your generator

### version

Type: `boolean`
Expand Down
16 changes: 2 additions & 14 deletions docs/generated/packages/nx/documents/workspace-generator.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
title: 'workspace-generator - CLI command'
description: 'Runs a workspace generator from the tools/generators directory'
description: 'Use a local plugin instead. See: https://nx.dev/deprecated/workspace-generators'
---

# workspace-generator

Runs a workspace generator from the tools/generators directory
**Deprecated:** Use a local plugin instead. See: https://nx.dev/deprecated/workspace-generators

## Usage

Expand All @@ -23,18 +23,6 @@ Type: `boolean`

Show help

### list-generators

Type: `boolean`

List the available workspace-generators

### name

Type: `string`

The name of your generator

### version

Type: `boolean`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,7 @@
"title": "Create a custom generator",
"description": "Create a custom generator.",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Generator name.",
"$default": { "$source": "argv", "index": 0 },
"x-prompt": "What name would you like to use for the workspace generator?"
},
"skipFormat": {
"description": "Skip formatting files.",
"type": "boolean",
"default": false,
"x-priority": "internal"
}
},
"properties": {},
"required": ["name"],
"presets": []
},
Expand Down
4 changes: 4 additions & 0 deletions docs/shared/deprecated/workspace-generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Check the [nx-plugin guide](/packages/nx-plugin) for information on creating a n

## Converting workspace generators to local generators

{% callout type=\"info\" %}
When migrating to Nx 16, a new workspace plugin is automatically generated in the tools folder if you already have workspace-generators.
{% /callout %}

- If you don't already have a local plugin, use Nx to generate one:

```shell
Expand Down
141 changes: 4 additions & 137 deletions packages/nx/src/command-line/nx-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,12 @@ export const commandsObject = yargs

.command({
command: 'workspace-generator [name]',
describe: 'Runs a workspace generator from the tools/generators directory',
describe:
'Use a local plugin instead. See: https://nx.dev/deprecated/workspace-generators',
deprecated: true,
aliases: ['workspace-schematic [name]'],
builder: async (yargs) =>
linkToNxDevAndExamples(
await withWorkspaceGeneratorOptions(yargs, process.argv.slice(3)),
'workspace-generator'
),
linkToNxDevAndExamples(yargs, 'workspace-generator'),
handler: workspaceGeneratorHandler,
})
.command({
Expand Down Expand Up @@ -793,138 +792,6 @@ function withRunOneOptions(yargs: yargs.Argv) {
}
}

type OptionArgumentDefinition = {
type: yargs.Options['type'];
describe?: string;
default?: any;
choices?: yargs.Options['type'][];
demandOption?: boolean;
};

type WorkspaceGeneratorProperties = {
[name: string]:
| {
type: yargs.Options['type'];
description?: string;
default?: any;
enum?: yargs.Options['type'][];
demandOption?: boolean;
}
| {
type: yargs.PositionalOptionsType;
description?: string;
default?: any;
enum?: yargs.PositionalOptionsType[];
$default: {
$source: 'argv';
index: number;
};
};
};

function isPositionalProperty(
property: WorkspaceGeneratorProperties[keyof WorkspaceGeneratorProperties]
): property is { type: yargs.PositionalOptionsType } {
return property['$default']?.['$source'] === 'argv';
}

async function withWorkspaceGeneratorOptions(
yargs: yargs.Argv,
args: string[]
) {
// filter out only positional arguments
args = args.filter((a) => !a.startsWith('-'));
if (args.length) {
// this is an actual workspace generator
return withCustomGeneratorOptions(yargs, args[0]);
} else {
yargs
.option('list-generators', {
describe: 'List the available workspace-generators',
type: 'boolean',
})
.positional('name', {
type: 'string',
describe: 'The name of your generator',
});
/**
* Don't require `name` if only listing available
* schematics
*/
if ((await yargs.argv).listGenerators !== true) {
yargs.demandOption('name');
}
return yargs;
}
}

async function withCustomGeneratorOptions(
yargs: yargs.Argv,
generatorName: string
) {
const schema = (
await import('./workspace-generators')
).workspaceGeneratorSchema(generatorName);
const options = [];
const positionals = [];

Object.entries(
(schema.properties ?? {}) as WorkspaceGeneratorProperties
).forEach(([name, prop]) => {
const option: { name: string; definition: OptionArgumentDefinition } = {
name,
definition: {
describe: prop.description,
type: prop.type,
default: prop.default,
choices: prop.enum,
},
};
if (schema.required && schema.required.includes(name)) {
option.definition.demandOption = true;
}
options.push(option);
if (isPositionalProperty(prop)) {
positionals.push({
name,
definition: {
describe: prop.description,
type: prop.type,
choices: prop.enum,
},
});
}
});

let command = generatorName;
positionals.forEach(({ name }) => {
command += ` [${name}]`;
});
if (options.length) {
command += ' (options)';
}

yargs
.command({
// this is the default and only command
command,
describe: schema.description || '',
builder: (y) => {
options.forEach(({ name, definition }) => {
y.option(name, definition);
});
positionals.forEach(({ name, definition }) => {
y.positional(name, definition);
});
return linkToNxDevAndExamples(y, 'workspace-generator');
},
handler: workspaceGeneratorHandler,
})
.fail(() => void 0); // no action is needed on failure as Nx will handle it based on schema validation

return yargs;
}

async function workspaceGeneratorHandler() {
await (
await import('./workspace-generators')
Expand Down
Loading

0 comments on commit cb4900a

Please sign in to comment.