Skip to content

Commit

Permalink
feat(misc): add --integrated option to nx init command (#16148)
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez authored Apr 12, 2023
1 parent 63d0230 commit 4b32716
Show file tree
Hide file tree
Showing 11 changed files with 384 additions and 243 deletions.
14 changes: 14 additions & 0 deletions docs/generated/cli/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ nx init
```

Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`, `yarn nx`, or `pnpm nx`.

## Options

### help

Type: `boolean`

Show help

### version

Type: `boolean`

Show version number
14 changes: 14 additions & 0 deletions docs/generated/packages/nx/documents/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ nx init
```

Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`, `yarn nx`, or `pnpm nx`.

## Options

### help

Type: `boolean`

Show help

### version

Type: `boolean`

Show version number
36 changes: 35 additions & 1 deletion e2e/nx-init/src/nx-init-angular.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('nx init (Angular CLI)', () => {
cleanupProject();
});

it('should successfully convert an Angular CLI workspace to an Nx workspace', () => {
it('should successfully convert an Angular CLI workspace to an Nx standalone workspace', () => {
const output = runCommand(
`${pmc.runUninstalledPackage} nx@${getPublishedVersion()} init -y`
);
Expand Down Expand Up @@ -59,4 +59,38 @@ describe('nx init (Angular CLI)', () => {
`Successfully ran target build for project ${project}`
);
});

it('should successfully convert an Angular CLI workspace to an Nx integrated workspace', () => {
const output = runCommand(
`${
pmc.runUninstalledPackage
} nx@${getPublishedVersion()} init -y --integrated`
);

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', `apps/${project}/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/apps/${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}`
);
});
});
10 changes: 7 additions & 3 deletions packages/nx/src/command-line/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { runNxSync } from '../utils/child-process';
import { directoryExists, readJsonFile } from '../utils/fileutils';
import { PackageJson } from '../utils/package-json';

export async function initHandler() {
export interface InitArgs {
integrated: boolean;
}

export async function initHandler(options: InitArgs) {
const args = process.argv.slice(2).join(' ');
const flags = parser(args, {
boolean: ['useDotNxInstallation'],
Expand All @@ -36,9 +40,9 @@ export async function initHandler() {
} else if (existsSync('package.json')) {
const packageJson: PackageJson = readJsonFile('package.json');
if (existsSync('angular.json')) {
await addNxToAngularCliRepo();
await addNxToAngularCliRepo(options.integrated);
} else if (isCRA(packageJson)) {
await addNxToCraRepo();
await addNxToCraRepo(options.integrated);
} else if (isNestCLI(packageJson)) {
await addNxToNest(packageJson);
} else if (isMonorepo(packageJson)) {
Expand Down
15 changes: 13 additions & 2 deletions packages/nx/src/command-line/nx-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,9 @@ export const commandsObject = yargs
.command({
command: 'init',
describe: 'Adds nx.json file and installs nx if not installed already',
handler: async () => {
await (await import('./init')).initHandler();
builder: (yargs) => withIntegratedOption(yargs),
handler: async (args: any) => {
await (await import('./init')).initHandler(args);
process.exit(0);
},
})
Expand Down Expand Up @@ -1117,6 +1118,16 @@ function withListOptions(yargs) {
});
}

function withIntegratedOption(yargs) {
return yargs.option('integrated', {
type: 'boolean',
description:
'Migrate to an Nx integrated layout workspace. Only for CRA and Angular projects.',
// TODO(leo): keep it hidden until feature is released
hidden: true,
});
}

function runMigration() {
const runLocalMigrate = () => {
runNxSync(`_migrate ${process.argv.slice(3).join(' ')}`, {
Expand Down
Loading

0 comments on commit 4b32716

Please sign in to comment.