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(nest): nx init nest #14254

Merged
merged 1 commit into from
Jan 13, 2023
Merged
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
65 changes: 65 additions & 0 deletions e2e/nx-init/src/nx-init-nest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
e2eCwd,
exists,
getPackageManagerCommand,
getPublishedVersion,
runCLI,
} from '@nrwl/e2e/utils';
import { execSync } from 'child_process';
import { removeSync } from 'fs-extra';

describe('nx init (for NestCLI)', () => {
const pmc = getPackageManagerCommand({
packageManager: 'npm',
});
const projectName = 'nest-app';
const projectRoot = `${e2eCwd}/${projectName}`;
const cliOptions = { cwd: projectRoot };

afterEach(() => {
removeSync(projectRoot);
});

it('should convert NestCLI application to Nx standalone', () => {
execSync(
`${pmc.runUninstalledPackage} @nestjs/cli new ${projectName} --package-manager=npm`,
{
cwd: e2eCwd,
encoding: 'utf-8',
env: process.env,
stdio: ['pipe', 'pipe', 'pipe'],
}
);

const output = execSync(
`${
pmc.runUninstalledPackage
} nx@${getPublishedVersion()} init -y --cacheable=format`,
{
cwd: projectRoot,
encoding: 'utf-8',
env: process.env,
stdio: ['pipe', 'pipe', 'pipe'],
}
);

expect(output).toContain('Enabled computation caching');

// nest-cli.json is removed
expect(exists(`${projectRoot}/nest-cli.json`)).toBeFalsy();

// root nx.json exists
expect(exists(`${projectRoot}/nx.json`)).toBeTruthy();
// root project.json exists
expect(exists(`${projectRoot}/project.json`)).toBeTruthy();

runCLI('build', cliOptions);
expect(
exists(`${projectRoot}/dist/${projectName}/src/main.js`)
).toBeTruthy();

// run build again for cache
const buildOutput = runCLI('build', cliOptions);
expect(buildOutput).toContain('Nx read the output from the cache');
}, 10000);
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@floating-ui/react-dom": "^1.0.1",
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/cli": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/schematics": "^9.0.0",
"@nestjs/swagger": "^6.0.0",
Expand Down
9 changes: 7 additions & 2 deletions packages/js/src/utils/inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ function buildInlineGraphExternals(
}

function movePackage(from: string, to: string) {
if (from === to) return;
copySync(from, to, { overwrite: true });
removeSync(from);
}
Expand All @@ -269,7 +270,8 @@ function updateImports(
function recursiveUpdateImport(
dirPath: string,
importRegex: RegExp,
inlinedDepsDestOutputRecord: Record<string, string>
inlinedDepsDestOutputRecord: Record<string, string>,
rootParentDir?: string
) {
const files = readdirSync(dirPath, { withFileTypes: true });
for (const file of files) {
Expand All @@ -282,6 +284,8 @@ function recursiveUpdateImport(
const fileContent = readFileSync(filePath, 'utf-8');
const updatedContent = fileContent.replace(importRegex, (matched) => {
const result = matched.replace(/['"]/g, '');
// If a match is the same as the rootParentDir, we're checking its own files so we return the matched as in no changes.
if (result === rootParentDir) return matched;
const importPath = `"${relative(
dirPath,
inlinedDepsDestOutputRecord[result]
Expand All @@ -293,7 +297,8 @@ function recursiveUpdateImport(
recursiveUpdateImport(
join(dirPath, file.name),
importRegex,
inlinedDepsDestOutputRecord
inlinedDepsDestOutputRecord,
rootParentDir || file.name
);
}
}
Expand Down
7 changes: 4 additions & 3 deletions packages/nest/src/generators/library/lib/add-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ export function addProject(tree: Tree, options: NormalizedOptions): void {
executor: '@nrwl/js:tsc',
outputs: ['{options.outputPath}'],
options: {
outputPath: options.libsDir
? `dist/${options.libsDir}/${options.projectDirectory}`
: `dist/${options.projectDirectory}`,
outputPath:
options.libsDir && options.libsDir !== '.'
? `dist/${options.libsDir}/${options.projectDirectory}`
: `dist/${options.projectDirectory}`,
tsConfig: `${options.projectRoot}/tsconfig.lib.json`,
packageJson: `${options.projectRoot}/package.json`,
main: `${options.projectRoot}/src/index.ts`,
Expand Down
29 changes: 22 additions & 7 deletions packages/nx/src/command-line/init.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { execSync } from 'child_process';
import { existsSync } from 'fs';
import { readJsonFile, directoryExists } from '../utils/fileutils';
import { addNxToNest } from '../nx-init/add-nx-to-nest';
import { addNxToNpmRepo } from '../nx-init/add-nx-to-npm-repo';
import { directoryExists, readJsonFile } from '../utils/fileutils';
import { PackageJson } from '../utils/package-json';

export async function initHandler() {
const args = process.argv.slice(2).join(' ');
Expand All @@ -10,17 +12,20 @@ export async function initHandler() {
console.log(`Using version ${process.env.NX_VERSION}`);
}
if (existsSync('package.json')) {
const packageJson: PackageJson = readJsonFile('package.json');
if (existsSync('angular.json')) {
// TODO(leo): remove make-angular-cli-faster
execSync(`npx --yes make-angular-cli-faster@${version} ${args}`, {
stdio: [0, 1, 2],
});
} else if (isCRA()) {
} else if (isCRA(packageJson)) {
// TODO(jack): remove cra-to-nx
execSync(`npx --yes cra-to-nx@${version} ${args}`, {
stdio: [0, 1, 2],
});
} else if (isMonorepo()) {
} else if (isNestCLI(packageJson)) {
await addNxToNest(packageJson);
} else if (isMonorepo(packageJson)) {
// TODO: vsavkin remove add-nx-to-monorepo
execSync(`npx --yes add-nx-to-monorepo@${version} ${args}`, {
stdio: [0, 1, 2],
Expand All @@ -35,8 +40,7 @@ export async function initHandler() {
}
}

function isCRA() {
const packageJson = readJsonFile('package.json');
function isCRA(packageJson: PackageJson) {
const combinedDependencies = {
...packageJson.dependencies,
...packageJson.devDependencies,
Expand All @@ -54,8 +58,19 @@ function isCRA() {
);
}

function isMonorepo() {
const packageJson = readJsonFile('package.json');
function isNestCLI(packageJson: PackageJson) {
const combinedDependencies = {
...packageJson.dependencies,
...packageJson.devDependencies,
};
return (
existsSync('nest-cli.json') &&
combinedDependencies['@nestjs/core'] &&
combinedDependencies['@nestjs/cli']
);
}

function isMonorepo(packageJson: PackageJson) {
if (!!packageJson.workspaces) return true;

if (existsSync('pnpm-workspace.yaml') || existsSync('pnpm-workspace.yml'))
Expand Down
5 changes: 5 additions & 0 deletions packages/nx/src/config/nx-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export type TargetDependencies = Record<
(TargetDependencyConfig | string)[]
>;

export interface NrwlJsPluginConfig {
analyzeSourceFiles?: boolean;
analyzePackageJson?: boolean;
}

/**
* Nx.json configuration
*
Expand Down
Loading