Skip to content

Commit

Permalink
feat(core): add migration to update workspace generators to a local p…
Browse files Browse the repository at this point in the history
…lugin (#12700)
  • Loading branch information
AgentEnder authored Apr 19, 2023
1 parent f04f316 commit 1743ff1
Show file tree
Hide file tree
Showing 26 changed files with 538 additions and 665 deletions.
36 changes: 30 additions & 6 deletions docs/generated/cli/workspace-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ description: 'Runs a workspace generator from the tools/generators directory'

# workspace-generator

Runs a workspace generator from the tools/generators directory
**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,23 +19,45 @@ 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

### list-generators
### interactive

Type: `boolean`

List the available workspace-generators
Default: `true`

### name
When false disables interactive input prompts for options

Type: `string`
### quiet

Type: `boolean`

Hides logs from tree operations (e.g. `CREATE package.json`)

### verbose

Type: `boolean`

The name of your generator
Prints additional information about the commands (e.g., stack traces)

### version

Expand Down
36 changes: 30 additions & 6 deletions docs/generated/packages/nx/documents/workspace-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ description: 'Runs a workspace generator from the tools/generators directory'

# workspace-generator

Runs a workspace generator from the tools/generators directory
**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,23 +19,45 @@ 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

### list-generators
### interactive

Type: `boolean`

List the available workspace-generators
Default: `true`

### name
When false disables interactive input prompts for options

Type: `string`
### quiet

Type: `boolean`

Hides logs from tree operations (e.g. `CREATE package.json`)

### verbose

Type: `boolean`

The name of your generator
Prints additional information about the commands (e.g., stack traces)

### version

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
1 change: 0 additions & 1 deletion e2e/nx-misc/src/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ describe('Nx Commands', () => {
// check for schematics
expect(listOutput).toContain('workspace');
expect(listOutput).toContain('library');
expect(listOutput).toContain('workspace-generator');

// check for builders
expect(listOutput).toContain('run-commands');
Expand Down
177 changes: 0 additions & 177 deletions e2e/nx-misc/src/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,180 +756,3 @@ describe('Workspace Tests', () => {
});
});
});

describe('workspace-generator', () => {
const packageManager = getSelectedPackageManager() || 'pnpm';
const proj = uniq('workspace');

beforeAll(() => {
runCreateWorkspace(proj, {
preset: 'ts',
packageManager,
});
});

afterAll(() => cleanupProject());

let custom: string;
let failing: string;

beforeEach(() => {
custom = uniq('custom');
failing = uniq('custom-failing');
runCLI(`g @nrwl/workspace:workspace-generator ${custom} --no-interactive`);
runCLI(`g @nrwl/workspace:workspace-generator ${failing} --no-interactive`);

checkFilesExist(
`tools/generators/${custom}/index.ts`,
`tools/generators/${custom}/schema.json`
);
checkFilesExist(
`tools/generators/${failing}/index.ts`,
`tools/generators/${failing}/schema.json`
);
});

it('should compile only generator files with dependencies', () => {
const workspace = uniq('workspace');

updateFile(
'tools/utils/command-line-utils.ts',
`
export const noop = () => {}
`
);
updateFile(
'tools/utils/logger.ts',
`
export const log = (...args: any[]) => console.log(...args)
`
);
updateFile(
`tools/generators/utils.ts`,
`
export const noop = ()=>{}
`
);
updateFile(`tools/generators/${custom}/index.ts`, (content) => {
return `
import { log } from '../../utils/logger'; \n
${content}
`;
});

runCLI(`workspace-generator ${custom} ${workspace} --no-interactive -d`);

expect(() =>
checkFilesExist(
`dist/out-tsc/tools/generators/${custom}/index.js`,
`dist/out-tsc/tools/generators/utils.js`,
`dist/out-tsc/tools/utils/logger.js`
)
).not.toThrow();
expect(() =>
checkFilesExist(`dist/out-tsc/tools/utils/utils.js`)
).toThrow();
});

it('should support workspace-specific generators', async () => {
const json = readJson(`tools/generators/${custom}/schema.json`);
json.properties['directory'] = {
type: 'string',
description: 'lib directory',
};
json.properties['skipTsConfig'] = {
type: 'boolean',
description: 'skip changes to tsconfig',
};
json.properties['inlineprop'] = json.properties['name'];
json.required = ['inlineprop'];
delete json.properties['name'];

updateFile(`tools/generators/${custom}/schema.json`, JSON.stringify(json));

const indexFile = readFile(`tools/generators/${custom}/index.ts`);
updateFile(
`tools/generators/${custom}/index.ts`,
indexFile.replace(
'name: schema.name',
'name: schema.inlineprop, directory: schema.directory, skipTsConfig: schema.skipTsConfig'
)
);

const helpOutput = runCLI(`workspace-generator ${custom} --help`);
expect(helpOutput).toContain(
`workspace-generator ${custom} [inlineprop] (options)`
);
expect(helpOutput).toContain(`--directory`);
expect(helpOutput).toContain(`--skipTsConfig`);

const workspace = uniq('workspace');
const dryRunOutput = runCLI(
`workspace-generator ${custom} ${workspace} --no-interactive --directory=dir --skipTsConfig=true -d`
);
expect(exists(`packages/dir/${workspace}/src/index.ts`)).toEqual(false);
expect(dryRunOutput).toContain(
`CREATE packages/dir/${workspace}/src/index.ts`
);

runCLI(
`workspace-generator ${custom} ${workspace} --no-interactive --directory=dir`
);
checkFilesExist(`packages/dir/${workspace}/src/index.ts`);

const jsonFailing = readJson(`tools/generators/${failing}/schema.json`);
jsonFailing.properties = {};
jsonFailing.required = [];
updateFile(
`tools/generators/${failing}/schema.json`,
JSON.stringify(jsonFailing)
);

updateFile(
`tools/generators/${failing}/index.ts`,
`
export default function() {
throw new Error();
}
`
);

try {
await runCLI(`workspace-generator ${failing} --no-interactive`);
fail(`Should exit 1 for a workspace-generator that throws an error`);
} catch (e) {}

const listOutput = runCLI('workspace-generator --list-generators');
expect(listOutput).toContain(custom);
expect(listOutput).toContain(failing);
}, 1000000);

it('should support angular devkit schematics', () => {
const angularDevkitSchematic = uniq('angular-devkit-schematic');
runCLI(
`g @nrwl/workspace:workspace-generator ${angularDevkitSchematic} --no-interactive`
);

const json = readJson(
`tools/generators/${angularDevkitSchematic}/schema.json`
);
json.properties = {};
json.required = [];
delete json.cli;
updateFile(
`tools/generators/${angularDevkitSchematic}/schema.json`,
JSON.stringify(json)
);

updateFile(
`tools/generators/${angularDevkitSchematic}/index.ts`,
`
export default function() {
return (tree) => tree;
}
`
);

runCLI(`workspace-generator ${angularDevkitSchematic} --no-interactive`);
});
});
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 @@ -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(() => {
Expand Down Expand Up @@ -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');
Expand Down
3 changes: 1 addition & 2 deletions packages/nx-plugin/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"error",
"@angular-devkit/architect",
"@angular-devkit/core",
"@angular-devkit/schematics",
"@nx/workspace"
"@angular-devkit/schematics"
]
}
},
Expand Down
Loading

0 comments on commit 1743ff1

Please sign in to comment.