Skip to content

Commit

Permalink
feat(nx-plugin): add as provided prompt for executor generator (#19602)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Oct 13, 2023
1 parent c83102a commit 8249ace
Show file tree
Hide file tree
Showing 16 changed files with 253 additions and 96 deletions.
20 changes: 14 additions & 6 deletions docs/generated/packages/plugin/generators/executor.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "executor",
"factory": "./src/generators/executor/executor",
"factory": "./src/generators/executor/executor#executorGeneratorInternal",
"schema": {
"$schema": "http://json-schema.org/schema",
"cli": "nx",
Expand All @@ -20,9 +20,7 @@
"type": "string",
"description": "The name of the project.",
"alias": "p",
"$default": { "$source": "projectName" },
"x-prompt": "What is the name of the project for the executor?",
"x-priority": "important"
"$default": { "$source": "projectName" }
},
"name": {
"type": "string",
Expand All @@ -31,6 +29,11 @@
"x-prompt": "What name would you like to use for the executor?",
"x-priority": "important"
},
"directory": {
"type": "string",
"description": "The directory at which to create the executor file. When `--nameAndDirectoryFormat=as-provided`, it will be relative to the current working directory. Otherwise, it will be relative to the workspace root.",
"aliases": ["dir"]
},
"description": {
"type": "string",
"description": "Executor description."
Expand All @@ -51,19 +54,24 @@
"default": false,
"description": "Do not add an eslint configuration for plugin json files."
},
"nameAndDirectoryFormat": {
"description": "Whether to generate the component in the directory as provided, relative to the current working directory and ignoring the project (`as-provided`) or generate it using the project and directory relative to the workspace root (`derived`).",
"type": "string",
"enum": ["as-provided", "derived"]
},
"skipFormat": {
"type": "boolean",
"description": "Skip formatting files.",
"default": false,
"x-priority": "internal"
}
},
"required": ["project", "name"],
"required": ["name"],
"additionalProperties": false,
"presets": []
},
"description": "Create an executor for an Nx Plugin.",
"implementation": "/packages/plugin/src/generators/executor/executor.ts",
"implementation": "/packages/plugin/src/generators/executor/executor#executorGeneratorInternal.ts",
"aliases": [],
"hidden": false,
"path": "/packages/plugin/src/generators/executor/schema.json",
Expand Down
1 change: 1 addition & 0 deletions packages/devkit/internal-testing-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from 'nx/src/internal-testing-utils/assert-valid-migrations';
export * from 'nx/src/internal-testing-utils/run-migration-against-this-workspace';
export * from 'nx/src/internal-testing-utils/with-environment';
export * from 'nx/src/internal-testing-utils/temp-fs';
export { setCwd } from './src/generators/artifact-name-and-directory-utils';
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import * as enquirer from 'enquirer';
import { addProjectConfiguration } from 'nx/src/devkit-exports';
import { createTreeWithEmptyWorkspace } from 'nx/src/generators/testing-utils/create-tree-with-empty-workspace';
import type { Tree } from 'nx/src/generators/tree';
import { workspaceRoot } from 'nx/src/utils/workspace-root';
import { join } from 'path';
import { determineArtifactNameAndDirectoryOptions } from './artifact-name-and-directory-utils';
import {
determineArtifactNameAndDirectoryOptions,
setCwd,
} from './artifact-name-and-directory-utils';

describe('determineArtifactNameAndDirectoryOptions', () => {
let tree: Tree;
Expand All @@ -25,10 +26,6 @@ describe('determineArtifactNameAndDirectoryOptions', () => {
process.stdout.isTTY = originalIsTTYValue;
}

function setCwd(path: string) {
process.env.INIT_CWD = join(workspaceRoot, path);
}

function restoreCwd() {
if (originalInitCwd === undefined) {
delete process.env.INIT_CWD;
Expand All @@ -39,6 +36,7 @@ describe('determineArtifactNameAndDirectoryOptions', () => {

beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
setCwd('');
jest.clearAllMocks();

originalInteractiveValue = process.env.NX_INTERACTIVE;
Expand All @@ -47,6 +45,49 @@ describe('determineArtifactNameAndDirectoryOptions', () => {
originalInitCwd = process.env.INIT_CWD;
});

it('should accept a derivedDirectory which is relative to the project source root', async () => {
addProjectConfiguration(tree, 'app1', {
root: 'apps/app1',
sourceRoot: 'apps/app1/src',
projectType: 'application',
});

const res = await determineArtifactNameAndDirectoryOptions(tree, {
name: 'myComponent',
artifactType: 'component',
callingGenerator: '@my-org/my-plugin:component',
directory: 'components',
derivedDirectory: 'components',
project: 'app1',
nameAndDirectoryFormat: 'derived',
});

expect(res.filePath).toEqual(
'apps/app1/src/components/my-component/my-component.ts'
);
});

it('should accept a derivedDirectory which is relative to the project root', async () => {
addProjectConfiguration(tree, 'app1', {
root: 'apps/app1',
projectType: 'application',
});

const res = await determineArtifactNameAndDirectoryOptions(tree, {
name: 'myComponent',
artifactType: 'component',
callingGenerator: '@my-org/my-plugin:component',
directory: 'components',
derivedDirectory: 'components',
project: 'app1',
nameAndDirectoryFormat: 'derived',
});

expect(res.filePath).toEqual(
'apps/app1/components/my-component/my-component.ts'
);
});

it('should throw an error when the resolver directory is not under any project root', async () => {
addProjectConfiguration(tree, 'app1', {
root: 'apps/app1',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { prompt } from 'enquirer';
import type { ProjectConfiguration } from 'nx/src/config/workspace-json-project-json';
import type { Tree } from 'nx/src/generators/tree';
import { relative } from 'path';
import { join, relative } from 'path';
import { requireNx } from '../../nx';
import { names } from '../utils/names';

Expand Down Expand Up @@ -31,6 +31,7 @@ export type ArtifactGenerationOptions = {
pascalCaseFile?: boolean;
project?: string;
suffix?: string;
derivedDirectory?: string;
};

export type NameAndDirectoryOptions = {
Expand Down Expand Up @@ -74,6 +75,7 @@ export async function determineArtifactNameAndDirectoryOptions(
options.nameAndDirectoryFormat ?? (await determineFormat(formats, options));

validateResolvedProject(
tree,
formats[format]?.project,
options,
formats[format]?.directory
Expand Down Expand Up @@ -173,6 +175,7 @@ function getNameAndDirectoryOptionFormats(

if (!options.project) {
validateResolvedProject(
tree,
asProvidedOptions.project,
options,
asProvidedOptions.directory
Expand Down Expand Up @@ -278,14 +281,25 @@ function getDerivedOptions(
project.projectType === 'application' ? 'app' : 'lib',
extractedDirectory ?? ''
);
const derivedDirectory = options.flat
? normalizePath(baseDirectory)
: joinPathFragments(
baseDirectory,
options.pascalCaseDirectory
? names(derivedName).className
: names(derivedName).fileName
);
const derivedDirectory =
typeof options.derivedDirectory === 'string'
? joinPathFragments(
project.sourceRoot ?? project.root,
options.derivedDirectory,
options.flat
? ''
: options.pascalCaseDirectory
? names(derivedName).className
: names(derivedName).fileName
)
: options.flat
? normalizePath(baseDirectory)
: joinPathFragments(
baseDirectory,
options.pascalCaseDirectory
? names(derivedName).className
: names(derivedName).fileName
);

if (
options.directory &&
Expand Down Expand Up @@ -333,6 +347,7 @@ function getDerivedOptions(
}

function validateResolvedProject(
tree: Tree,
project: string | undefined,
options: ArtifactGenerationOptions,
normalizedDirectory: string
Expand Down Expand Up @@ -392,6 +407,13 @@ function getRelativeCwd(): string {
return normalizePath(relative(workspaceRoot, getCwd()));
}

/**
* Function for setting cwd during testing
*/
export function setCwd(path: string): void {
process.env.INIT_CWD = join(workspaceRoot, path);
}

function getCwd(): string {
return process.env.INIT_CWD?.startsWith(workspaceRoot)
? process.env.INIT_CWD
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"description": "Create a generator for an Nx Plugin."
},
"executor": {
"factory": "./src/generators/executor/executor",
"factory": "./src/generators/executor/executor#executorGeneratorInternal",
"schema": "./src/generators/executor/schema.json",
"description": "Create an executor for an Nx Plugin."
},
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/project.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "plugin",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/plugin",
"sourceRoot": "packages/plugin/src",
"projectType": "library",
"targets": {
"test": {},
Expand Down
Loading

1 comment on commit 8249ace

@vercel
Copy link

@vercel vercel bot commented on 8249ace Oct 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-five.vercel.app

Please sign in to comment.