Skip to content

Commit

Permalink
feat(nx-plugin): add support for the ts solution config setup to the …
Browse files Browse the repository at this point in the history
…`@nx/plugin` plugin (#28724)

<!-- 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 Nov 12, 2024
1 parent 2d77495 commit f7f26d8
Show file tree
Hide file tree
Showing 60 changed files with 1,005 additions and 447 deletions.
11 changes: 8 additions & 3 deletions docs/generated/packages/js/executors/tsc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
"generateExportsField": {
"type": "boolean",
"alias": "exports",
"description": "Update the output package.json file's 'exports' field. This field is used by Node and bundles.",
"description": "Update the output package.json file's 'exports' field. This field is used by Node and bundlers. Ignored when `generatePackageJson` is set to `false`.",
"default": false,
"x-priority": "important"
},
"additionalEntryPoints": {
"type": "array",
"description": "Additional entry-points to add to exports field in the package.json file.",
"description": "Additional entry-points to add to exports field in the package.json file. Ignored when `generatePackageJson` is set to `false`.",
"items": { "type": "string" },
"x-priority": "important"
},
Expand Down Expand Up @@ -132,9 +132,14 @@
},
"generateLockfile": {
"type": "boolean",
"description": "Generate a lockfile (e.g. package-lock.json) that matches the workspace lockfile to ensure package versions match.",
"description": "Generate a lockfile (e.g. package-lock.json) that matches the workspace lockfile to ensure package versions match. Ignored when `generatePackageJson` is set to `false`.",
"default": false,
"x-priority": "internal"
},
"generatePackageJson": {
"type": "boolean",
"description": "Generate package.json file in the output folder.",
"default": true
}
},
"required": ["main", "outputPath", "tsConfig"],
Expand Down
16 changes: 9 additions & 7 deletions docs/generated/packages/plugin/generators/create-package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-package",
"factory": "./src/generators/create-package/create-package",
"factory": "./src/generators/create-package/create-package#createPackageGeneratorInternal",
"schema": {
"$schema": "https://json-schema.org/schema",
"cli": "nx",
Expand Down Expand Up @@ -31,15 +31,13 @@
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for unit tests.",
"default": "jest"
"enum": ["none", "jest"],
"description": "Test runner to use for unit tests."
},
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["eslint"],
"default": "eslint"
"enum": ["none", "eslint"]
},
"tags": {
"type": "string",
Expand All @@ -62,13 +60,17 @@
"type": "string",
"description": "The name of the e2e project.",
"x-prompt": "What is the name of the e2e project? Leave blank to skip e2e tests"
},
"useProjectJson": {
"type": "boolean",
"description": "Use a `project.json` configuration file instead of inlining the Nx configuration in the `package.json` file."
}
},
"required": ["directory", "name", "project"],
"presets": []
},
"description": "Create a package which can be used by npx to create a new workspace",
"implementation": "/packages/plugin/src/generators/create-package/create-package.ts",
"implementation": "/packages/plugin/src/generators/create-package/create-package#createPackageGeneratorInternal.ts",
"aliases": [],
"hidden": false,
"path": "/packages/plugin/src/generators/create-package/schema.json",
Expand Down
7 changes: 5 additions & 2 deletions docs/generated/packages/plugin/generators/e2e-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["eslint", "none"],
"default": "eslint"
"enum": ["none", "eslint"]
},
"minimal": {
"type": "boolean",
Expand All @@ -46,6 +45,10 @@
"type": "boolean",
"default": false,
"x-priority": "internal"
},
"useProjectJson": {
"type": "boolean",
"description": "Use a `project.json` configuration file instead of inlining the Nx configuration in the `package.json` file."
}
},
"required": ["pluginName", "npmPackageName"],
Expand Down
18 changes: 11 additions & 7 deletions docs/generated/packages/plugin/generators/plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plugin",
"factory": "./src/generators/plugin/plugin",
"factory": "./src/generators/plugin/plugin#pluginGeneratorInternal",
"schema": {
"$schema": "https://json-schema.org/schema",
"cli": "nx",
Expand Down Expand Up @@ -34,14 +34,14 @@
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["eslint"],
"default": "eslint"
"enum": ["none", "eslint"],
"x-priority": "important"
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for unit tests.",
"default": "jest"
"type": "string",
"enum": ["none", "jest"],
"x-priority": "important"
},
"tags": {
"type": "string",
Expand Down Expand Up @@ -92,13 +92,17 @@
"type": "boolean",
"description": "Generates a boilerplate for publishing the plugin to npm.",
"default": false
},
"useProjectJson": {
"type": "boolean",
"description": "Use a `project.json` configuration file instead of inlining the Nx configuration in the `package.json` file."
}
},
"required": ["directory"],
"presets": []
},
"description": "Create a Nx Plugin.",
"implementation": "/packages/plugin/src/generators/plugin/plugin.ts",
"implementation": "/packages/plugin/src/generators/plugin/plugin#pluginGeneratorInternal.ts",
"aliases": [],
"hidden": false,
"path": "/packages/plugin/src/generators/plugin/schema.json",
Expand Down
8 changes: 6 additions & 2 deletions docs/generated/packages/plugin/generators/preset.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "preset",
"factory": "./src/generators/preset/generator",
"factory": "./src/generators/preset/generator#presetGeneratorInternal",
"schema": {
"$schema": "https://json-schema.org/schema",
"cli": "nx",
Expand All @@ -27,6 +27,10 @@
"createPackageName": {
"type": "string",
"description": "Name of package which creates a workspace"
},
"useProjectJson": {
"type": "boolean",
"description": "Use a `project.json` configuration file instead of inlining the Nx configuration in the `package.json` file."
}
},
"required": ["pluginName"],
Expand All @@ -35,7 +39,7 @@
"description": "Initializes a workspace with an nx-plugin inside of it. Use as: `create-nx-workspace --preset @nx/plugin`.",
"hidden": true,
"x-use-standalone-layout": true,
"implementation": "/packages/plugin/src/generators/preset/generator.ts",
"implementation": "/packages/plugin/src/generators/preset/generator#presetGeneratorInternal.ts",
"aliases": [],
"path": "/packages/plugin/src/generators/preset/schema.json",
"type": "generator"
Expand Down
20 changes: 10 additions & 10 deletions e2e/plugin/src/nx-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('Nx Plugin', () => {

runCLI(`generate @nx/plugin:plugin ${plugin} --linter=eslint`);
runCLI(
`generate @nx/plugin:migration --path=${plugin}/src/migrations/update-${version} --packageVersion=${version} --packageJsonUpdates=false`
`generate @nx/plugin:migration --path=${plugin}/src/migrations/update-${version}/update-${version} --packageVersion=${version} --packageJsonUpdates=false`
);

const lintResults = runCLI(`lint ${plugin}`);
Expand Down Expand Up @@ -92,7 +92,7 @@ describe('Nx Plugin', () => {

runCLI(`generate @nx/plugin:plugin ${plugin} --linter=eslint`);
runCLI(
`generate @nx/plugin:generator ${plugin}/src/generators/${generator} --name ${generator}`
`generate @nx/plugin:generator ${plugin}/src/generators/${generator}/generator --name ${generator}`
);

const lintResults = runCLI(`lint ${plugin}`);
Expand Down Expand Up @@ -129,7 +129,7 @@ describe('Nx Plugin', () => {

runCLI(`generate @nx/plugin:plugin ${plugin} --linter=eslint`);
runCLI(
`generate @nx/plugin:executor --name ${executor} --path=${plugin}/src/executors/${executor} --includeHasher`
`generate @nx/plugin:executor --name ${executor} --path=${plugin}/src/executors/${executor}/executor --includeHasher`
);

const lintResults = runCLI(`lint ${plugin}`);
Expand Down Expand Up @@ -178,19 +178,19 @@ describe('Nx Plugin', () => {
runCLI(`generate @nx/plugin:plugin ${plugin} --linter=eslint`);

runCLI(
`generate @nx/plugin:generator --name=${goodGenerator} --path=${plugin}/src/generators/${goodGenerator}`
`generate @nx/plugin:generator --name=${goodGenerator} --path=${plugin}/src/generators/${goodGenerator}/generator`
);

runCLI(
`generate @nx/plugin:generator --name=${badFactoryPath} --path=${plugin}/src/generators/${badFactoryPath}`
`generate @nx/plugin:generator --name=${badFactoryPath} --path=${plugin}/src/generators/${badFactoryPath}/generator`
);

runCLI(
`generate @nx/plugin:executor --name=${goodExecutor} --path=${plugin}/src/executors/${goodExecutor}`
`generate @nx/plugin:executor --name=${goodExecutor} --path=${plugin}/src/executors/${goodExecutor}/executor`
);

runCLI(
`generate @nx/plugin:executor --name=${badExecutorBadImplPath} --path=${plugin}/src/executors/${badExecutorBadImplPath}`
`generate @nx/plugin:executor --name=${badExecutorBadImplPath} --path=${plugin}/src/executors/${badExecutorBadImplPath}/executor`
);

runCLI(
Expand Down Expand Up @@ -308,11 +308,11 @@ describe('Nx Plugin', () => {
const generatedProject = uniq('project');

runCLI(
`generate @nx/plugin:generator --name ${generator} --path ${plugin}/src/generators/${generator}`
`generate @nx/plugin:generator --name ${generator} --path ${plugin}/src/generators/${generator}/generator`
);

runCLI(
`generate @nx/plugin:executor --name ${executor} --path ${plugin}/src/executors/${executor}`
`generate @nx/plugin:executor --name ${executor} --path ${plugin}/src/executors/${executor}/executor`
);

updateFile(
Expand Down Expand Up @@ -349,7 +349,7 @@ describe('Nx Plugin', () => {

expect(() => {
runCLI(
`generate @nx/plugin:generator ${plugin}/src/generators/${generator} --name ${generator}`
`generate @nx/plugin:generator ${plugin}/src/generators/${generator}/generator --name ${generator}`
);

runCLI(
Expand Down
25 changes: 2 additions & 23 deletions packages/cypress/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import {
writeJson,
} from '@nx/devkit';
import { resolveImportPath } from '@nx/devkit/src/generators/project-name-and-root-utils';
import { promptWhenInteractive } from '@nx/devkit/src/generators/prompt';
import { Linter, LinterType } from '@nx/eslint';
import {
getRelativePathToRootTsConfig,
initGenerator as jsInitGenerator,
} from '@nx/js';
import { normalizeLinterOption } from '@nx/js/src/utils/generator-prompts';
import {
getProjectPackageManagerWorkspaceState,
getProjectPackageManagerWorkspaceStateWarningTask,
Expand Down Expand Up @@ -165,28 +165,7 @@ function ensureDependencies(tree: Tree, options: NormalizedSchema) {
}

async function normalizeOptions(tree: Tree, options: CypressE2EConfigSchema) {
const isTsSolutionSetup = isUsingTsSolutionSetup(tree);

let linter = options.linter;
if (!linter) {
const choices = isTsSolutionSetup
? [{ name: 'none' }, { name: 'eslint' }]
: [{ name: 'eslint' }, { name: 'none' }];
const defaultValue = isTsSolutionSetup ? 'none' : 'eslint';

linter = await promptWhenInteractive<{
linter: 'none' | 'eslint';
}>(
{
type: 'select',
name: 'linter',
message: `Which linter would you like to use?`,
choices,
initial: 0,
},
{ linter: defaultValue }
).then(({ linter }) => linter);
}
const linter = await normalizeLinterOption(tree, options.linter);

const projectConfig: ProjectConfiguration | undefined =
readProjectConfiguration(tree, options.project);
Expand Down
29 changes: 23 additions & 6 deletions packages/jest/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import {
formatFiles,
GeneratorCallback,
output,
readJson,
logger,
readNxJson,
readProjectConfiguration,
runTasksInSerial,
Tree,
} from '@nx/devkit';
import {
getRootTsConfigFileName,
initGenerator as jsInitGenerator,
} from '@nx/js';
import { initGenerator as jsInitGenerator } from '@nx/js';
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
import { JestPluginOptions } from '../../plugins/plugin';
import { getPresetExt } from '../../utils/config/config-file';
import { jestInitGenerator } from '../init/init';
Expand Down Expand Up @@ -74,6 +71,7 @@ function normalizeOptions(
...schemaDefaults,
...options,
rootProject: project.root === '.' || project.root === '',
isTsSolutionSetup: isUsingTsSolutionSetup(tree),
};
}

Expand Down Expand Up @@ -115,15 +113,34 @@ export async function configurationGeneratorInternal(
);
}
});

if (!hasPlugin || options.addExplicitTargets) {
updateWorkspace(tree, options);
}

if (options.isTsSolutionSetup) {
ignoreTestOutput(tree);
}

if (!schema.skipFormat) {
await formatFiles(tree);
}

return runTasksInSerial(...tasks);
}

function ignoreTestOutput(tree: Tree): void {
if (!tree.exists('.gitignore')) {
logger.warn(`Couldn't find a root .gitignore file to update.`);
}

let content = tree.read('.gitignore', 'utf-8');
if (/^test-output$/gm.test(content)) {
return;
}

content = `${content}\ntest-output\n`;
tree.write('.gitignore', content);
}

export default configurationGenerator;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
preset: '<%= offsetFromRoot %>jest.preset.<%= presetExt %>',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],<% if(testEnvironment) { %>
testEnvironment: '<%= testEnvironment %>',<% } %>
coverageDirectory: '<%= offsetFromRoot %>coverage/<%= projectRoot %>',
coverageDirectory: '<%= coverageDirectory %>',
transform: {
'^.+\\.(ts|mjs|js|html)$': [
'jest-preset-angular',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"extends": "./tsconfig.json",
"extends": "<%= extendedConfig %>",
"compilerOptions": {
"outDir": "<%= offsetFromRoot %>dist/out-tsc",
"module": "commonjs",
"outDir": "<%= outDir %>",<% if (module) { %>
"module": "<%= module %>",<% } %>
"target": "es2016",
"types": ["jest", "node"]
},<% if(setupFile !== 'none') { %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<% if (supportTsx){ %>'^.+\\.[tj]sx?$'<% } else { %>'^.+\\.[tj]s$'<% } %>: <% if (transformerOptions) { %>['<%= transformer %>', <%- transformerOptions %>]<% } else { %>'<%= transformer %>'<% } %>
},
<% if (supportTsx) { %>moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],<% } else { %>moduleFileExtensions: ['ts', 'js', 'html'],<% } %><% } %>
coverageDirectory: '<%= offsetFromRoot %>coverage/<%= projectRoot %>'<% if(rootProject){ %>,
coverageDirectory: '<%= coverageDirectory %>'<% if(rootProject){ %>,
testMatch: [
'<rootDir>/src/**/__tests__/**/*.[jt]s?(x)',
'<rootDir>/src/**/*(*.)@(spec|test).[jt]s?(x)',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"extends": "./tsconfig.json",
"extends": "<%= extendedConfig %>",
"compilerOptions": {
"outDir": "<%= offsetFromRoot %>dist/out-tsc",
"module": "commonjs",
"outDir": "<%= outDir %>",<% if (module) { %>
"module": "<%= module %>",<% } %>
"types": ["jest", "node"]
},<% if(setupFile !== 'none') { %>
"files": ["src/test-setup.ts"],<% } %>
Expand Down
Loading

0 comments on commit f7f26d8

Please sign in to comment.