Skip to content

Commit 6a32696

Browse files
AgentEnderFrozenPandaz
authored andcommitted
feat(core): prompt for setup mode when running nx init in empty git directory (#35226)
## Current Behavior When running `nx init` in an empty git directory (no `package.json`), the V2 init handler silently defaults to the `.nx` installation method without prompting the user. This may not be suitable for users who intend to create a JavaScript/TypeScript project and would prefer a `package.json`-based setup. ## Expected Behavior When running `nx init` in an empty git directory, users are now prompted to choose between two setup methods: - **`.nx installation`** — recommended for non-JavaScript projects (Gradle, .NET, etc.) - **`package.json installation`** — recommended for JavaScript/TypeScript projects The prompt only appears when: - No `package.json` exists in the directory - The `--useDotNxInstallation` flag was not explicitly passed - Running in interactive mode (not AI agent mode) If the user chooses `package.json`, a minimal `package.json` is created and the existing npm-repo setup flow takes over. If they choose `.nx`, the existing dot-nx setup flow is used. In both cases, the workspace is created in the current directory (not a subfolder). ## Related Issue(s) Fixes NXC-3983 (cherry picked from commit ea644b3)
1 parent 13d69ef commit 6a32696

1 file changed

Lines changed: 41 additions & 1 deletion

File tree

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { existsSync } from 'fs';
2+
import { basename } from 'path';
23

34
import { prompt } from 'enquirer';
45
import { prerelease } from 'semver';
56
import { NxJsonConfiguration, readNxJson } from '../../config/nx-json';
6-
import { readJsonFile } from '../../utils/fileutils';
7+
import { readJsonFile, writeJsonFile } from '../../utils/fileutils';
78
import { getPackageNameFromImportPath } from '../../utils/get-package-name-from-import-path';
89
import { output } from '../../utils/output';
910
import { PackageJson } from '../../utils/package-json';
@@ -143,6 +144,45 @@ async function initHandlerImpl(options: InitArgs): Promise<void> {
143144
return;
144145
}
145146

147+
// When in an empty directory (no package.json) and the user hasn't explicitly
148+
// chosen a setup method, prompt them to pick between .nx and package.json setup.
149+
// Skip the prompt when stdin is not a TTY (e.g. CI, e2e tests) to avoid hangs.
150+
if (
151+
!existsSync('package.json') &&
152+
!options.useDotNxInstallation &&
153+
options.interactive &&
154+
!aiMode &&
155+
process.stdin.isTTY
156+
) {
157+
const setupMode = await prompt<{ setupMode: string }>([
158+
{
159+
type: 'select',
160+
name: 'setupMode',
161+
message: 'How would you like to set up Nx in this directory?',
162+
choices: [
163+
{
164+
name: '.nx installation (recommended for non-JavaScript projects)',
165+
},
166+
{
167+
name: 'package.json installation (recommended for JavaScript/TypeScript projects)',
168+
},
169+
],
170+
},
171+
]).then((r) => r.setupMode);
172+
173+
if (setupMode.startsWith('package.json')) {
174+
// Create a minimal package.json so the JS/TS workflow takes over
175+
const workspaceName = basename(process.cwd());
176+
writeJsonFile('package.json', {
177+
name: workspaceName,
178+
version: '0.0.0',
179+
private: true,
180+
});
181+
} else {
182+
options.useDotNxInstallation = true;
183+
}
184+
}
185+
146186
const _isNonJs = !existsSync('package.json') || options.useDotNxInstallation;
147187
const packageJson: PackageJson = _isNonJs
148188
? null

0 commit comments

Comments
 (0)