Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nx-plugin): add support for the ts solution config setup to the @nx/plugin plugin #28724

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 22 additions & 0 deletions packages/jest/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
formatFiles,
GeneratorCallback,
logger,
output,
readJson,
readNxJson,
Expand All @@ -12,6 +13,7 @@ import {
getRootTsConfigFileName,
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 +76,7 @@ function normalizeOptions(
...schemaDefaults,
...options,
rootProject: project.root === '.' || project.root === '',
isTsSolutionSetup: isUsingTsSolutionSetup(tree),
};
}

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

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

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

if (!schema.skipFormat) {
await formatFiles(tree);
}
Expand Down Expand Up @@ -158,4 +166,18 @@ function getUnsupportedModuleResolutionWarningTask(
return () => {};
}

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
19 changes: 17 additions & 2 deletions packages/jest/src/generators/configuration/lib/create-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
readProjectConfiguration,
Tree,
} from '@nx/devkit';
import { isUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
import { join } from 'path';
import type { JestPresetExtension } from '../../../utils/config/config-file';
import { NormalizedJestProjectSchema } from '../schema';
Expand Down Expand Up @@ -33,6 +34,12 @@ export function createFiles(
transformerOptions = "{ tsconfig: '<rootDir>/tsconfig.spec.json' }";
}

const isTsSolutionSetup = isUsingTsSolutionSetup(tree);

const projectRoot = options.rootProject
? options.project
: projectConfig.root;
const rootOffset = offsetFromRoot(projectConfig.root);
generateFiles(tree, join(__dirname, filesFolder), projectConfig.root, {
tmpl: '',
...options,
Expand All @@ -45,9 +52,17 @@ export function createFiles(
transformerOptions,
js: !!options.js,
rootProject: options.rootProject,
projectRoot: options.rootProject ? options.project : projectConfig.root,
offsetFromRoot: offsetFromRoot(projectConfig.root),
projectRoot,
offsetFromRoot: rootOffset,
presetExt,
coverageDirectory: isTsSolutionSetup
? `test-output/jest/coverage`
: `${rootOffset}coverage/${projectRoot}`,
extendedConfig: isTsSolutionSetup
? `${rootOffset}tsconfig.base.json`
: './tsconfig.json',
outDir: isTsSolutionSetup ? `./out-tsc/jest` : `${rootOffset}dist/out-tsc`,
module: !isTsSolutionSetup ? 'commonjs' : undefined,
});

if (options.setupFile === 'none') {
Expand Down
Loading