Skip to content

Commit

Permalink
cleanup(core): inline angular cli nested project migration logic into…
Browse files Browse the repository at this point in the history
… nx init (#15396)
  • Loading branch information
leosvelperez committed Mar 7, 2023
1 parent fca1f97 commit e1a2c98
Show file tree
Hide file tree
Showing 10 changed files with 644 additions and 27 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
/packages/nx/src/adapter @AgentEnder @leosvelperez
/packages/nx/src/native @vsavkin @FrozenPandaz @Cammisuli
/packages/nx/src/lock-file @meeroslav @FrozenPandaz
/packages/nx/src/nx-init/angular/** @Coly010 @leosvelperez @FrozenPandaz
/e2e/nx*/** @FrozenPandaz @AgentEnder @vsavkin
/packages/workspace/** @FrozenPandaz @AgentEnder @vsavkin
/e2e/workspace-create/** @FrozenPandaz @AgentEnder @vsavkin
Expand Down
65 changes: 65 additions & 0 deletions e2e/nx-init/src/nx-init-angular.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { PackageManager } from 'nx/src/utils/package-manager';
import {
checkFilesDoNotExist,
checkFilesExist,
cleanupProject,
getPackageManagerCommand,
getPublishedVersion,
getSelectedPackageManager,
runCLI,
runCommand,
runNgNew,
uniq,
} from '../../utils';

describe('nx init (Angular CLI)', () => {
let project: string;
let packageManager: PackageManager;
let pmc: ReturnType<typeof getPackageManagerCommand>;

beforeEach(() => {
project = uniq('proj');
packageManager = getSelectedPackageManager();
// TODO: solve issues with pnpm and remove this fallback
packageManager = packageManager === 'pnpm' ? 'yarn' : packageManager;
pmc = getPackageManagerCommand({ packageManager });
});

afterEach(() => {
cleanupProject();
});

it('should successfully convert an Angular CLI workspace to an Nx workspace', () => {
runNgNew(project, packageManager);

const output = runCommand(
`${pmc.runUninstalledPackage} nx@${getPublishedVersion()} init -y`
);

expect(output).toContain('Nx is now enabled in your workspace!');
// angular.json should have been deleted
checkFilesDoNotExist('angular.json');
// check nx config files exist
checkFilesExist('nx.json', 'project.json');

// check build
const coldBuildOutput = runCLI(`build ${project} --outputHashing none`);
expect(coldBuildOutput).toContain(
`> nx run ${project}:build:production --outputHashing none`
);
expect(coldBuildOutput).toContain(
`Successfully ran target build for project ${project}`
);
checkFilesExist(`dist/${project}/main.js`);

// run build again to check is coming from cache
const cachedBuildOutput = runCLI(`build ${project} --outputHashing none`);
expect(cachedBuildOutput).toContain(
`> nx run ${project}:build:production --outputHashing none [local cache]`
);
expect(cachedBuildOutput).toContain('Nx read the output from the cache');
expect(cachedBuildOutput).toContain(
`Successfully ran target build for project ${project}`
);
});
});
5 changes: 3 additions & 2 deletions e2e/utils/create-project-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from './get-env-info';
import * as isCI from 'is-ci';

import { angularCliVersion } from '@nrwl/workspace/src/utils/versions';
import { angularCliVersion as defaultAngularCliVersion } from '@nrwl/workspace/src/utils/versions';
import { dump } from '@zkochan/js-yaml';
import { execSync, ExecSyncOptions } from 'child_process';

Expand Down Expand Up @@ -247,7 +247,8 @@ export function packageInstall(

export function runNgNew(
projectName: string,
packageManager = getSelectedPackageManager()
packageManager = getSelectedPackageManager(),
angularCliVersion = defaultAngularCliVersion
): string {
projName = projectName;

Expand Down
5 changes: 2 additions & 3 deletions packages/add-nx-to-monorepo/src/add-nx-to-monorepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ async function addNxToMonorepo() {
repoRoot,
targetDefaults,
cacheableOperations,
scriptOutputs,
undefined
scriptOutputs
);

addDepsToPackageJson(repoRoot, useCloud);
Expand All @@ -111,7 +110,7 @@ async function addNxToMonorepo() {
runInstall(repoRoot);

if (useCloud) {
initCloud(repoRoot);
initCloud(repoRoot, 'nx-init-monorepo');
}

printFinalMessage();
Expand Down
12 changes: 5 additions & 7 deletions packages/nx/src/command-line/init.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { execSync } from 'child_process';
import { existsSync } from 'fs';
import { prerelease } from 'semver';
import * as parser from 'yargs-parser';
import { addNxToNest } from '../nx-init/add-nx-to-nest';
import { addNxToNpmRepo } from '../nx-init/add-nx-to-npm-repo';
import { addNxToAngularCliRepo } from '../nx-init/angular';
import { generateEncapsulatedNxSetup } from '../nx-init/encapsulated/add-nx-scripts';
import { directoryExists, readJsonFile } from '../utils/fileutils';
import { PackageJson } from '../utils/package-json';
import * as parser from 'yargs-parser';
import { generateEncapsulatedNxSetup } from '../nx-init/encapsulated/add-nx-scripts';
import { prerelease } from 'semver';

export async function initHandler() {
const args = process.argv.slice(2).join(' ');
Expand Down Expand Up @@ -43,10 +44,7 @@ export async function initHandler() {
} else 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],
});
await addNxToAngularCliRepo();
} else if (isCRA(packageJson)) {
// TODO(jack): remove cra-to-nx
execSync(`npx --yes cra-to-nx@${version} ${args}`, {
Expand Down
5 changes: 2 additions & 3 deletions packages/nx/src/nx-init/add-nx-to-nest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ export async function addNxToNest(packageJson: PackageJson) {
repoRoot,
[],
[...cacheableOperations, ...nestCacheableScripts],
{},
packageJson.name
{}
);

const pmc = getPackageManagerCommand();
Expand All @@ -136,7 +135,7 @@ export async function addNxToNest(packageJson: PackageJson) {
runInstall(repoRoot);

if (useCloud) {
initCloud(repoRoot);
initCloud(repoRoot, 'nx-init-nest');
}

printFinalMessage();
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/nx-init/add-nx-to-npm-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export async function addNxToNpmRepo() {
useCloud = false;
}

createNxJsonFile(repoRoot, [], cacheableOperations, {}, packageJson.name);
createNxJsonFile(repoRoot, [], cacheableOperations, {});

const pmc = getPackageManagerCommand();

Expand All @@ -93,7 +93,7 @@ export async function addNxToNpmRepo() {
runInstall(repoRoot, pmc);

if (useCloud) {
initCloud(repoRoot);
initCloud(repoRoot, 'nx-init-npm-repo');
}

printFinalMessage();
Expand Down
Loading

1 comment on commit e1a2c98

@vercel
Copy link

@vercel vercel bot commented on e1a2c98 Mar 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.