Skip to content

Commit 5faef5d

Browse files
authored
feat(nest): nx init nest (#14254)
1 parent e5edcb8 commit 5faef5d

File tree

11 files changed

+750
-39
lines changed

11 files changed

+750
-39
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import {
2+
e2eCwd,
3+
exists,
4+
getPackageManagerCommand,
5+
getPublishedVersion,
6+
runCLI,
7+
} from '@nrwl/e2e/utils';
8+
import { execSync } from 'child_process';
9+
import { removeSync } from 'fs-extra';
10+
11+
describe('nx init (for NestCLI)', () => {
12+
const pmc = getPackageManagerCommand({
13+
packageManager: 'npm',
14+
});
15+
const projectName = 'nest-app';
16+
const projectRoot = `${e2eCwd}/${projectName}`;
17+
const cliOptions = { cwd: projectRoot };
18+
19+
afterEach(() => {
20+
removeSync(projectRoot);
21+
});
22+
23+
it('should convert NestCLI application to Nx standalone', () => {
24+
execSync(
25+
`${pmc.runUninstalledPackage} @nestjs/cli new ${projectName} --package-manager=npm`,
26+
{
27+
cwd: e2eCwd,
28+
encoding: 'utf-8',
29+
env: process.env,
30+
stdio: ['pipe', 'pipe', 'pipe'],
31+
}
32+
);
33+
34+
const output = execSync(
35+
`${
36+
pmc.runUninstalledPackage
37+
} nx@${getPublishedVersion()} init -y --cacheable=format`,
38+
{
39+
cwd: projectRoot,
40+
encoding: 'utf-8',
41+
env: process.env,
42+
stdio: ['pipe', 'pipe', 'pipe'],
43+
}
44+
);
45+
46+
expect(output).toContain('Enabled computation caching');
47+
48+
// nest-cli.json is removed
49+
expect(exists(`${projectRoot}/nest-cli.json`)).toBeFalsy();
50+
51+
// root nx.json exists
52+
expect(exists(`${projectRoot}/nx.json`)).toBeTruthy();
53+
// root project.json exists
54+
expect(exists(`${projectRoot}/project.json`)).toBeTruthy();
55+
56+
runCLI('build', cliOptions);
57+
expect(
58+
exists(`${projectRoot}/dist/${projectName}/src/main.js`)
59+
).toBeTruthy();
60+
61+
// run build again for cache
62+
const buildOutput = runCLI('build', cliOptions);
63+
expect(buildOutput).toContain('Nx read the output from the cache');
64+
}, 10000);
65+
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@floating-ui/react-dom": "^1.0.1",
4747
"@nestjs/common": "^9.0.0",
4848
"@nestjs/core": "^9.0.0",
49+
"@nestjs/cli": "^9.0.0",
4950
"@nestjs/platform-express": "^9.0.0",
5051
"@nestjs/schematics": "^9.0.0",
5152
"@nestjs/swagger": "^6.0.0",

packages/js/src/utils/inline.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ function buildInlineGraphExternals(
245245
}
246246

247247
function movePackage(from: string, to: string) {
248+
if (from === to) return;
248249
copySync(from, to, { overwrite: true });
249250
removeSync(from);
250251
}
@@ -269,7 +270,8 @@ function updateImports(
269270
function recursiveUpdateImport(
270271
dirPath: string,
271272
importRegex: RegExp,
272-
inlinedDepsDestOutputRecord: Record<string, string>
273+
inlinedDepsDestOutputRecord: Record<string, string>,
274+
rootParentDir?: string
273275
) {
274276
const files = readdirSync(dirPath, { withFileTypes: true });
275277
for (const file of files) {
@@ -282,6 +284,8 @@ function recursiveUpdateImport(
282284
const fileContent = readFileSync(filePath, 'utf-8');
283285
const updatedContent = fileContent.replace(importRegex, (matched) => {
284286
const result = matched.replace(/['"]/g, '');
287+
// If a match is the same as the rootParentDir, we're checking its own files so we return the matched as in no changes.
288+
if (result === rootParentDir) return matched;
285289
const importPath = `"${relative(
286290
dirPath,
287291
inlinedDepsDestOutputRecord[result]
@@ -293,7 +297,8 @@ function recursiveUpdateImport(
293297
recursiveUpdateImport(
294298
join(dirPath, file.name),
295299
importRegex,
296-
inlinedDepsDestOutputRecord
300+
inlinedDepsDestOutputRecord,
301+
rootParentDir || file.name
297302
);
298303
}
299304
}

packages/nest/src/generators/library/lib/add-project.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ export function addProject(tree: Tree, options: NormalizedOptions): void {
1515
executor: '@nrwl/js:tsc',
1616
outputs: ['{options.outputPath}'],
1717
options: {
18-
outputPath: options.libsDir
19-
? `dist/${options.libsDir}/${options.projectDirectory}`
20-
: `dist/${options.projectDirectory}`,
18+
outputPath:
19+
options.libsDir && options.libsDir !== '.'
20+
? `dist/${options.libsDir}/${options.projectDirectory}`
21+
: `dist/${options.projectDirectory}`,
2122
tsConfig: `${options.projectRoot}/tsconfig.lib.json`,
2223
packageJson: `${options.projectRoot}/package.json`,
2324
main: `${options.projectRoot}/src/index.ts`,

packages/nx/src/command-line/init.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { execSync } from 'child_process';
22
import { existsSync } from 'fs';
3-
import { readJsonFile, directoryExists } from '../utils/fileutils';
3+
import { addNxToNest } from '../nx-init/add-nx-to-nest';
44
import { addNxToNpmRepo } from '../nx-init/add-nx-to-npm-repo';
5+
import { directoryExists, readJsonFile } from '../utils/fileutils';
6+
import { PackageJson } from '../utils/package-json';
57

68
export async function initHandler() {
79
const args = process.argv.slice(2).join(' ');
@@ -10,17 +12,20 @@ export async function initHandler() {
1012
console.log(`Using version ${process.env.NX_VERSION}`);
1113
}
1214
if (existsSync('package.json')) {
15+
const packageJson: PackageJson = readJsonFile('package.json');
1316
if (existsSync('angular.json')) {
1417
// TODO(leo): remove make-angular-cli-faster
1518
execSync(`npx --yes make-angular-cli-faster@${version} ${args}`, {
1619
stdio: [0, 1, 2],
1720
});
18-
} else if (isCRA()) {
21+
} else if (isCRA(packageJson)) {
1922
// TODO(jack): remove cra-to-nx
2023
execSync(`npx --yes cra-to-nx@${version} ${args}`, {
2124
stdio: [0, 1, 2],
2225
});
23-
} else if (isMonorepo()) {
26+
} else if (isNestCLI(packageJson)) {
27+
await addNxToNest(packageJson);
28+
} else if (isMonorepo(packageJson)) {
2429
// TODO: vsavkin remove add-nx-to-monorepo
2530
execSync(`npx --yes add-nx-to-monorepo@${version} ${args}`, {
2631
stdio: [0, 1, 2],
@@ -35,8 +40,7 @@ export async function initHandler() {
3540
}
3641
}
3742

38-
function isCRA() {
39-
const packageJson = readJsonFile('package.json');
43+
function isCRA(packageJson: PackageJson) {
4044
const combinedDependencies = {
4145
...packageJson.dependencies,
4246
...packageJson.devDependencies,
@@ -54,8 +58,19 @@ function isCRA() {
5458
);
5559
}
5660

57-
function isMonorepo() {
58-
const packageJson = readJsonFile('package.json');
61+
function isNestCLI(packageJson: PackageJson) {
62+
const combinedDependencies = {
63+
...packageJson.dependencies,
64+
...packageJson.devDependencies,
65+
};
66+
return (
67+
existsSync('nest-cli.json') &&
68+
combinedDependencies['@nestjs/core'] &&
69+
combinedDependencies['@nestjs/cli']
70+
);
71+
}
72+
73+
function isMonorepo(packageJson: PackageJson) {
5974
if (!!packageJson.workspaces) return true;
6075

6176
if (existsSync('pnpm-workspace.yaml') || existsSync('pnpm-workspace.yml'))

packages/nx/src/config/nx-json.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export type TargetDependencies = Record<
2727
(TargetDependencyConfig | string)[]
2828
>;
2929

30+
export interface NrwlJsPluginConfig {
31+
analyzeSourceFiles?: boolean;
32+
analyzePackageJson?: boolean;
33+
}
34+
3035
/**
3136
* Nx.json configuration
3237
*

0 commit comments

Comments
 (0)