Skip to content

Commit

Permalink
feat(node): add support for new TS solution setup in Node
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Dec 11, 2024
1 parent 0d868d2 commit de3a373
Show file tree
Hide file tree
Showing 41 changed files with 1,179 additions and 164 deletions.
9 changes: 6 additions & 3 deletions docs/generated/packages/express/generators/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["eslint"],
"default": "eslint"
"enum": ["eslint", "none"],
"default": "none",
"x-prompt": "Which linter would you like to use?",
"x-priority": "important"
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for unit tests.",
"default": "jest"
"default": "none",
"x-prompt": "Which unit test runner would you like to use?"
},
"tags": {
"type": "string",
Expand Down
7 changes: 5 additions & 2 deletions docs/generated/packages/nest/generators/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["eslint", "none"],
"default": "eslint"
"default": "none",
"x-prompt": "Which linter would you like to use?",
"x-priority": "important"
},
"unitTestRunner": {
"description": "Test runner to use for unit tests.",
"type": "string",
"enum": ["jest", "none"],
"default": "jest"
"default": "none",
"x-prompt": "Which unit test runner would you like to use?"
},
"e2eTestRunner": {
"type": "string",
Expand Down
8 changes: 6 additions & 2 deletions docs/generated/packages/nest/generators/library.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["eslint", "none"],
"default": "eslint"
"default": "none",
"x-prompt": "Which linter would you like to use?",
"x-priority": "important"
},
"unitTestRunner": {
"description": "Test runner to use for unit tests.",
"type": "string",
"enum": ["jest", "none"],
"default": "jest"
"default": "none",
"x-prompt": "Which unit test runner would you like to use?",
"x-priority": "important"
},
"tags": {
"description": "Add tags to the library (used for linting).",
Expand Down
24 changes: 15 additions & 9 deletions docs/generated/packages/node/generators/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,26 @@
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["eslint"],
"default": "eslint"
"enum": ["eslint", "none"],
"default": "none",
"x-prompt": "Which linter would you like to use?",
"x-priority": "important"
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for unit tests.",
"default": "jest"
"default": "none",
"x-priority": "important",
"x-prompt": "Which unit test runner would you like to use?"
},
"e2eTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for end-to-end tests",
"default": "none",
"x-priority": "important",
"x-prompt": "Which end-to-end test runner would you like to use?"
},
"tags": {
"type": "string",
Expand Down Expand Up @@ -109,12 +121,6 @@
"hidden": true,
"x-priority": "internal"
},
"e2eTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for end to end (e2e) tests",
"default": "jest"
},
"docker": {
"type": "boolean",
"description": "Add a docker build target"
Expand Down
12 changes: 8 additions & 4 deletions docs/generated/packages/node/generators/library.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["eslint"],
"default": "eslint"
"enum": ["eslint", "none"],
"default": "none",
"x-prompt": "Which linter would you like to use?",
"x-priority": "important"
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for unit tests.",
"default": "jest"
"default": "none",
"x-prompt": "Which unit test runner would you like to use?",
"x-priority": "important"
},
"tags": {
"type": "string",
Expand All @@ -69,7 +73,7 @@
},
"buildable": {
"type": "boolean",
"default": false,
"default": true,
"description": "Generate a buildable library.",
"x-priority": "important"
},
Expand Down
19 changes: 18 additions & 1 deletion packages/create-nx-workspace/bin/create-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,10 @@ async function determineNodeOptions(
let appName: string;
let framework: 'express' | 'fastify' | 'koa' | 'nest' | 'none';
let docker: boolean;
let linter: undefined | 'none' | 'eslint';
let formatter: undefined | 'none' | 'prettier';

const workspaces = parsedArgs.workspaces ?? false;

if (parsedArgs.preset) {
preset = parsedArgs.preset;
Expand All @@ -1010,7 +1014,9 @@ async function determineNodeOptions(
} else {
framework = await determineNodeFramework(parsedArgs);

const workspaceType = await determineStandaloneOrMonorepo();
const workspaceType = workspaces
? 'monorepo'
: await determineStandaloneOrMonorepo();
if (workspaceType === 'standalone') {
preset = Preset.NodeStandalone;
appName = parsedArgs.name;
Expand Down Expand Up @@ -1045,11 +1051,22 @@ async function determineNodeOptions(
docker = reply.docker === 'Yes';
}

if (workspaces) {
linter = await determineLinterOptions(parsedArgs);
formatter = await determineFormatterOptions(parsedArgs);
} else {
linter = 'eslint';
formatter = 'prettier';
}

return {
preset,
appName,
framework,
docker,
linter,
formatter,
workspaces,
};
}

Expand Down
1 change: 0 additions & 1 deletion packages/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
},
"dependencies": {
"@nx/devkit": "file:../devkit",
"@nx/js": "file:../js",
"@nx/node": "file:../node",
"tslib": "^2.3.0"
},
Expand Down
177 changes: 176 additions & 1 deletion packages/express/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readJson, Tree } from '@nx/devkit';
import { readJson, Tree, updateJson, writeJson } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { applicationGenerator } from './application';
import { Schema } from './schema';
Expand Down Expand Up @@ -134,4 +134,179 @@ describe('app', () => {
]);
});
});

describe('TS solution setup', () => {
beforeEach(() => {
appTree = createTreeWithEmptyWorkspace();
updateJson(appTree, 'package.json', (json) => {
json.workspaces = ['packages/*', 'apps/*'];
return json;
});
writeJson(appTree, 'tsconfig.base.json', {
compilerOptions: {
composite: true,
declaration: true,
},
});
writeJson(appTree, 'tsconfig.json', {
extends: './tsconfig.base.json',
files: [],
references: [],
});
});

it('should add project references when using TS solution', async () => {
await applicationGenerator(appTree, {
directory: 'myapp',
} as Schema);

expect(readJson(appTree, 'tsconfig.json').references)
.toMatchInlineSnapshot(`
[
{
"path": "./myapp-e2e",
},
{
"path": "./myapp",
},
]
`);
expect(readJson(appTree, 'myapp/package.json')).toMatchInlineSnapshot(`
{
"name": "@proj/myapp",
"nx": {
"name": "myapp",
"projectType": "application",
"sourceRoot": "myapp/src",
"tags": [],
"targets": {
"build": {
"configurations": {
"development": {},
"production": {},
},
"defaultConfiguration": "production",
"executor": "@nx/webpack:webpack",
"options": {
"assets": [
"myapp/src/assets",
],
"compiler": "tsc",
"main": "myapp/src/main.ts",
"outputPath": "dist/myapp",
"target": "node",
"tsConfig": "myapp/tsconfig.app.json",
"webpackConfig": "myapp/webpack.config.js",
},
"outputs": [
"{options.outputPath}",
],
},
"lint": {
"executor": "@nx/eslint:lint",
},
"serve": {
"configurations": {
"development": {
"buildTarget": "myapp:build:development",
},
"production": {
"buildTarget": "myapp:build:production",
},
},
"defaultConfiguration": "development",
"dependsOn": [
"build",
],
"executor": "@nx/js:node",
"options": {
"buildTarget": "myapp:build",
"runBuildTargetDependencies": false,
},
},
"test": {
"options": {
"passWithNoTests": true,
},
},
},
},
"private": true,
"version": "0.0.1",
}
`);
expect(readJson(appTree, 'myapp/tsconfig.json')).toMatchInlineSnapshot(`
{
"compilerOptions": {
"esModuleInterop": true,
},
"extends": "../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.app.json",
},
{
"path": "./tsconfig.spec.json",
},
],
}
`);
expect(readJson(appTree, 'myapp/tsconfig.app.json'))
.toMatchInlineSnapshot(`
{
"compilerOptions": {
"module": "nodenext",
"moduleResolution": "nodenext",
"outDir": "out-tsc/myapp",
"rootDir": "src",
"types": [
"node",
"express",
],
},
"exclude": [
"dist",
"jest.config.ts",
"src/**/*.spec.ts",
"src/**/*.test.ts",
"eslint.config.js",
"eslint.config.cjs",
"eslint.config.mjs",
],
"extends": "../tsconfig.base.json",
"include": [
"src/**/*.ts",
],
}
`);
expect(readJson(appTree, 'myapp/tsconfig.spec.json'))
.toMatchInlineSnapshot(`
{
"compilerOptions": {
"module": "nodenext",
"moduleResolution": "nodenext",
"outDir": "./out-tsc/jest",
"types": [
"jest",
"node",
],
},
"extends": "../tsconfig.base.json",
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts",
],
"references": [
{
"path": "./tsconfig.app.json",
},
],
}
`);
});
});
});
3 changes: 0 additions & 3 deletions packages/express/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
determineProjectNameAndRootOptions,
ensureProjectName,
} from '@nx/devkit/src/generators/project-name-and-root-utils';
import { assertNotUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
import { applicationGenerator as nodeApplicationGenerator } from '@nx/node';
import { tslibVersion } from '@nx/node/src/utils/versions';
import { join } from 'path';
Expand Down Expand Up @@ -75,8 +74,6 @@ export async function applicationGenerator(tree: Tree, schema: Schema) {
}

export async function applicationGeneratorInternal(tree: Tree, schema: Schema) {
assertNotUsingTsSolutionSetup(tree, 'express', 'application');

const options = await normalizeOptions(tree, schema);

const tasks: GeneratorCallback[] = [];
Expand Down
Loading

0 comments on commit de3a373

Please sign in to comment.