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

fix(misc): handle workspaces if no plugin selected in nx init and only generate files after prompts #21606

Merged
merged 1 commit into from
Feb 5, 2024
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
13 changes: 12 additions & 1 deletion packages/nx/src/command-line/init/implementation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '../../../utils/package-manager';
import { joinPathFragments } from '../../../utils/path';
import { nxVersion } from '../../../utils/versions';
import { readFileSync, writeFileSync } from 'fs';
import { existsSync, readFileSync, writeFileSync } from 'fs';

export function createNxJsonFile(
repoRoot: string,
Expand Down Expand Up @@ -223,3 +223,14 @@ export function printFinalMessage({
].filter(Boolean),
});
}

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

if (existsSync('pnpm-workspace.yaml') || existsSync('pnpm-workspace.yml'))
return true;

if (existsSync('lerna.json')) return true;

return false;
}
12 changes: 1 addition & 11 deletions packages/nx/src/command-line/init/init-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { runNxSync } from '../../utils/child-process';
import { directoryExists, readJsonFile } from '../../utils/fileutils';
import { PackageJson } from '../../utils/package-json';
import { nxVersion } from '../../utils/versions';
import { isMonorepo } from './implementation/utils';

export interface InitArgs {
addE2e: boolean;
Expand Down Expand Up @@ -106,17 +107,6 @@ function isNestCLI(packageJson: PackageJson) {
);
}

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

if (existsSync('pnpm-workspace.yaml') || existsSync('pnpm-workspace.yml'))
return true;

if (existsSync('lerna.json')) return true;

return false;
}

function setupDotNxInstallation(version: string) {
if (process.platform !== 'win32') {
console.log(
Expand Down
25 changes: 14 additions & 11 deletions packages/nx/src/command-line/init/init-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { nxVersion } from '../../utils/versions';
import {
addDepsToPackageJson,
createNxJsonFile,
isMonorepo,
runInstall,
updateGitIgnore,
} from './implementation/utils';
Expand All @@ -19,6 +20,7 @@ import { addNxToAngularCliRepo } from './implementation/angular';
import { globWithWorkspaceContext } from '../../utils/workspace-context';
import { connectExistingRepoToNxCloudPrompt } from '../connect/connect-to-nx-cloud';
import { addNxToNpmRepo } from './implementation/add-nx-to-npm-repo';
import { addNxToMonorepo } from './implementation/add-nx-to-monorepo';

export interface InitArgs {
interactive: boolean;
Expand Down Expand Up @@ -59,29 +61,30 @@ export async function initHandler(options: InitArgs): Promise<void> {
return;
}

const repoRoot = process.cwd();
const cacheableOperations: string[] = [];
createNxJsonFile(repoRoot, [], cacheableOperations, {});

const pmc = getPackageManagerCommand();

updateGitIgnore(repoRoot);

const detectPluginsResponse = await detectPlugins();

if (!detectPluginsResponse?.plugins.length) {
// If no plugins are detected/chosen, guide users to setup
// their targetDefaults correctly so their package scripts will work.
await addNxToNpmRepo({
interactive: options.interactive,
});
const packageJson: PackageJson = readJsonFile('package.json');
if (isMonorepo(packageJson)) {
await addNxToMonorepo({ interactive: options.interactive });
} else {
await addNxToNpmRepo({ interactive: options.interactive });
}
} else {
const useNxCloud =
options.nxCloud ??
(options.interactive
? await connectExistingRepoToNxCloudPrompt()
: false);

const repoRoot = process.cwd();
const pmc = getPackageManagerCommand();

createNxJsonFile(repoRoot, [], [], {});
updateGitIgnore(repoRoot);

addDepsToPackageJson(repoRoot, detectPluginsResponse?.plugins ?? []);

output.log({ title: '📦 Installing Nx' });
Expand Down