Skip to content

Commit

Permalink
feat(testing): add support for the ts solution config setup to the pl…
Browse files Browse the repository at this point in the history
…aywright plugin (#28636)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
leosvelperez authored Oct 28, 2024
1 parent f357b4e commit 4b70d1b
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 175 deletions.
12 changes: 6 additions & 6 deletions docs/generated/packages/playwright/generators/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
"x-priority": "important",
"default": "e2e"
},
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["none", "eslint"],
"x-priority": "important"
},
"js": {
"type": "boolean",
"description": "Generate JavaScript files rather than TypeScript files.",
Expand All @@ -34,12 +40,6 @@
"type": "string",
"description": "The address of the web server."
},
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["eslint", "none"],
"default": "eslint"
},
"setParserOptionsProject": {
"type": "boolean",
"description": "Whether or not to configure the ESLint `parserOptions.project` option. We do not do this by default for lint performance reasons.",
Expand Down
36 changes: 22 additions & 14 deletions packages/devkit/src/generators/project-name-and-root-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,8 @@ export async function determineProjectNameAndRootOptions(

let importPath: string | undefined = undefined;
if (options.projectType === 'library') {
importPath = options.importPath;

if (!importPath) {
if (name.startsWith('@')) {
importPath = name;
} else {
const npmScope = getNpmScope(tree);
importPath =
projectRoot === '.'
? readJson<{ name?: string }>(tree, 'package.json').name ??
getImportPath(npmScope, name)
: getImportPath(npmScope, name);
}
}
importPath =
options.importPath ?? resolveImportPath(tree, name, projectRoot);
}

return {
Expand All @@ -117,6 +105,26 @@ export async function determineProjectNameAndRootOptions(
};
}

export function resolveImportPath(
tree: Tree,
projectName: string,
projectRoot: string
): string {
let importPath: string;
if (projectName.startsWith('@')) {
importPath = projectName;
} else {
const npmScope = getNpmScope(tree);
importPath =
projectRoot === '.'
? readJson<{ name?: string }>(tree, 'package.json').name ??
getImportPath(npmScope, projectName)
: getImportPath(npmScope, projectName);
}

return importPath;
}

export async function ensureProjectName(
tree: Tree,
options: Omit<ProjectGenerationOptions, 'projectType'>,
Expand Down
19 changes: 19 additions & 0 deletions packages/devkit/src/generators/prompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { prompt } from 'enquirer';
import { isCI } from 'nx/src/devkit-internals';

export async function promptWhenInteractive<T>(
questions: Parameters<typeof prompt>[0],
defaultValue: T
): Promise<T> {
if (!isInteractive()) {
return defaultValue;
}

return await prompt(questions);
}

function isInteractive(): boolean {
return (
!isCI() && !!process.stdout.isTTY && process.env.NX_INTERACTIVE === 'true'
);
}
27 changes: 5 additions & 22 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ import {
determineProjectNameAndRootOptions,
ensureProjectName,
} from '@nx/devkit/src/generators/project-name-and-root-utils';

import { promptWhenInteractive } from '@nx/devkit/src/generators/prompt';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/target-defaults-utils';
import { logShowProjectCommand } from '@nx/devkit/src/utils/log-show-project-command';
import { prompt } from 'enquirer';
import { findMatchingProjects } from 'nx/src/utils/find-matching-projects';
import { isCI } from 'nx/src/utils/is-ci';
import { type PackageJson } from 'nx/src/utils/package-json';
import { join } from 'path';
import type { CompilerOptions } from 'typescript';
import { getProjectPackageManagerWorkspaceState } from '../../utils/package-manager-workspaces';
import {
getProjectPackageManagerWorkspaceState,
getProjectPackageManagerWorkspaceStateWarningTask,
} from '../../utils/package-manager-workspaces';
import { addSwcConfig } from '../../utils/swc/add-swc-config';
import { getSwcDependencies } from '../../utils/swc/add-swc-dependencies';
import { getNeededCompilerOptionOverrides } from '../../utils/typescript/configuration';
Expand Down Expand Up @@ -63,7 +64,6 @@ import type {
LibraryGeneratorSchema,
NormalizedLibraryGeneratorOptions,
} from './schema';
import { getProjectPackageManagerWorkspaceStateWarningTask } from './utils/package-manager-workspaces';
import {
ensureProjectIsExcludedFromPluginRegistrations,
ensureProjectIsIncludedInPluginRegistrations,
Expand Down Expand Up @@ -680,23 +680,6 @@ function replaceJestConfig(
});
}

function isNonInteractive(): boolean {
return (
isCI() || !process.stdout.isTTY || process.env.NX_INTERACTIVE !== 'true'
);
}

async function promptWhenInteractive<T>(
questions: Parameters<typeof prompt>[0],
defaultValue: T
): Promise<T> {
if (isNonInteractive()) {
return defaultValue;
}

return await prompt(questions);
}

async function normalizeOptions(
tree: Tree,
options: LibraryGeneratorSchema
Expand Down

This file was deleted.

52 changes: 52 additions & 0 deletions packages/js/src/utils/package-manager-workspaces.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import {
detectPackageManager,
getPackageManagerVersion,
isWorkspacesEnabled,
output,
readJson,
type GeneratorCallback,
type Tree,
} from '@nx/devkit';
import { minimatch } from 'minimatch';
import { join } from 'node:path/posix';
import { getGlobPatternsFromPackageManagerWorkspaces } from 'nx/src/plugins/package-json';
import { lt } from 'semver';

export type ProjectPackageManagerWorkspaceState =
| 'included'
Expand Down Expand Up @@ -35,3 +39,51 @@ export function getProjectPackageManagerWorkspaceState(
export function isUsingPackageManagerWorkspaces(tree: Tree): boolean {
return isWorkspacesEnabled(detectPackageManager(tree.root), tree.root);
}

export function getProjectPackageManagerWorkspaceStateWarningTask(
projectPackageManagerWorkspaceState: ProjectPackageManagerWorkspaceState,
workspaceRoot: string
): GeneratorCallback {
return (): void => {
if (projectPackageManagerWorkspaceState !== 'excluded') {
return;
}

const packageManager = detectPackageManager(workspaceRoot);
let adviseMessage =
'updating the "workspaces" option in the workspace root "package.json" file with the project root or pattern that includes it';
let packageManagerWorkspaceSetupDocs: string;
if (packageManager === 'pnpm') {
adviseMessage =
'updating the "pnpm-workspace.yaml" file with the project root or pattern that includes it';
packageManagerWorkspaceSetupDocs =
'https://pnpm.io/workspaces and https://pnpm.io/pnpm-workspace_yaml';
} else if (packageManager === 'yarn') {
const yarnVersion = getPackageManagerVersion(
packageManager,
workspaceRoot
);
if (lt(yarnVersion, '2.0.0')) {
packageManagerWorkspaceSetupDocs =
'https://classic.yarnpkg.com/lang/en/docs/workspaces/';
} else {
packageManagerWorkspaceSetupDocs =
'https://yarnpkg.com/features/workspaces';
}
} else if (packageManager === 'npm') {
packageManagerWorkspaceSetupDocs =
'https://docs.npmjs.com/cli/v10/using-npm/workspaces';
} else if (packageManager === 'bun') {
packageManagerWorkspaceSetupDocs =
'https://bun.sh/docs/install/workspaces';
}

output.warn({
title: `The project is not included in the package manager workspaces configuration`,
bodyLines: [
`Please add the project to the package manager workspaces configuration by ${adviseMessage}.`,
`Read more about the ${packageManager} workspaces feature and how to set it up at ${packageManagerWorkspaceSetupDocs}.`,
],
});
};
}
1 change: 1 addition & 0 deletions packages/nx/src/devkit-internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ export { LoadedNxPlugin } from './project-graph/plugins/internal-api';
export * from './project-graph/error-types';
export { registerTsProject } from './plugins/js/utils/register';
export { interpolate } from './tasks-runner/utils';
export { isCI } from './utils/is-ci';
Loading

0 comments on commit 4b70d1b

Please sign in to comment.