Skip to content

Commit 6bcee00

Browse files
jaysooclaude
andcommitted
fix(core): generate nxCloudId for template flow
Previously, the template flow was not calling connectToNxCloud generator, which meant no Cloud workspace was created and nxCloudId remained undefined. This caused readNxCloudToken to return undefined. Now the template flow calls connectToNxCloud after installing dependencies, which creates the Cloud workspace and writes nxCloudId to nx.json. The preset flow is unchanged and continues to call connectToNxCloud through the workspace generator as before. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 0cd5398 commit 6bcee00

File tree

5 files changed

+69
-8
lines changed

5 files changed

+69
-8
lines changed

packages/create-nx-workspace/.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"error",
3434
{
3535
"buildTargets": ["build-base"],
36-
"ignoredDependencies": []
36+
"ignoredDependencies": ["nx"]
3737
}
3838
]
3939
}

packages/create-nx-workspace/bin/create-nx-workspace.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ async function normalizeArgsMiddleware(
336336
: messages.metaCodeOfSelectedPromptMessage('setupNxCloudV2');
337337
Object.assign(argv, {
338338
nxCloud,
339+
useGitHub: nxCloud !== 'skip',
339340
nxCloudPromptCode,
340341
packageManager,
341342
defaultBase,

packages/create-nx-workspace/src/create-workspace.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { join } from 'path';
12
import { createEmptyWorkspace } from './create-empty-workspace';
23
import { createPreset } from './create-preset';
34
import { createSandbox } from './create-sandbox';
@@ -10,6 +11,7 @@ import {
1011
VcsPushStatus,
1112
} from './utils/git/git';
1213
import {
14+
connectToNxCloudForTemplate,
1315
createNxCloudOnboardingUrl,
1416
getNxCloudInfo,
1517
readNxCloudToken,
@@ -49,15 +51,29 @@ export async function createWorkspace<T extends CreateWorkspaceOptions>(
4951
let directory: string;
5052

5153
if (options.template) {
54+
if (!options.template.startsWith('nrwl/'))
55+
throw new Error(
56+
`Invalid template. Only templates from the 'nrwl' GitHub org are supported.`
57+
);
5258
const templateUrl = `https://github.com/${options.template}`;
53-
directory = name;
59+
const workingDir = process.cwd().replace(/\\/g, '/');
60+
directory = join(workingDir, name);
5461

55-
await cloneTemplate(templateUrl, directory);
62+
await cloneTemplate(templateUrl, name);
5663
await cleanupLockfiles(directory, packageManager);
5764

5865
// Install dependencies
5966
const pmc = getPackageManagerCommand(packageManager);
6067
await execAndWait(pmc.install, directory);
68+
69+
// Connect to Nx Cloud for template flow
70+
if (nxCloud !== 'skip') {
71+
await connectToNxCloudForTemplate(
72+
directory,
73+
'create-nx-workspace',
74+
useGitHub
75+
);
76+
}
6177
} else {
6278
// Preset flow - existing behavior
6379
const tmpDir = await createSandbox(packageManager);
@@ -112,8 +128,14 @@ export async function createWorkspace<T extends CreateWorkspaceOptions>(
112128
try {
113129
await initializeGitRepo(directory, { defaultBase, commit, connectUrl });
114130

115-
// Push to GitHub if commit was made, GitHub push is not skipped, and CI provider is GitHub
116-
if (commit && !skipGitHubPush && nxCloud === 'github') {
131+
// Push to GitHub if commit was made, GitHub push is not skipped, and:
132+
// - CI provider is GitHub (preset flow), OR
133+
// - Using template flow with Nx Cloud enabled (yes)
134+
if (
135+
commit &&
136+
!skipGitHubPush &&
137+
(nxCloud === 'github' || (isTemplate && nxCloud === 'yes'))
138+
) {
117139
pushedToVcs = await pushToGitHub(directory, {
118140
skipGitHubPush,
119141
name,

packages/create-nx-workspace/src/internal-utils/prompts.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ export async function determineNxCloud(
3333

3434
export async function determineNxCloudV2(
3535
parsedArgs: yargs.Arguments<{ nxCloud?: string; interactive?: boolean }>
36-
): Promise<'yes' | 'skip'> {
36+
): Promise<'github' | 'skip'> {
3737
// Provided via flag
3838
if (parsedArgs.nxCloud) {
39-
return parsedArgs.nxCloud === 'skip' ? 'skip' : 'yes';
39+
return parsedArgs.nxCloud === 'skip' ? 'skip' : 'github';
4040
}
4141

4242
// Non-interactive mode
@@ -62,7 +62,7 @@ export async function determineNxCloudV2(
6262
promptConfig.hint = () => hint;
6363
}
6464

65-
const result = await enquirer.prompt<{ nxCloud: 'yes' | 'skip' }>([
65+
const result = await enquirer.prompt<{ nxCloud: 'github' | 'skip' }>([
6666
promptConfig,
6767
]);
6868
return result.nxCloud;
@@ -115,10 +115,12 @@ async function nxCloudPrompt(key: MessageKey): Promise<NxCloud> {
115115

116116
export async function determineTemplate(
117117
parsedArgs: yargs.Arguments<{
118+
template?: string;
118119
preset?: string;
119120
interactive?: boolean;
120121
}>
121122
): Promise<string | 'skip'> {
123+
if (parsedArgs.template) return parsedArgs.template;
122124
if (parsedArgs.preset) return 'skip';
123125
if (!parsedArgs.interactive || isCI()) return 'skip';
124126
const { template } = await enquirer.prompt<{ template: string }>([

packages/create-nx-workspace/src/utils/nx/nx-cloud.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,42 @@ function getCloudMessageSource(
2626
: 'create-nx-workspace-success-ci-setup';
2727
}
2828

29+
export async function connectToNxCloudForTemplate(
30+
directory: string,
31+
installationSource: string,
32+
useGitHub?: boolean
33+
): Promise<string | null> {
34+
// nx-ignore-next-line
35+
const { connectToNxCloud } = require(require.resolve(
36+
'nx/src/nx-cloud/generators/connect-to-nx-cloud/connect-to-nx-cloud',
37+
{
38+
paths: [directory],
39+
}
40+
// nx-ignore-next-line
41+
)) as typeof import('nx/src/nx-cloud/generators/connect-to-nx-cloud/connect-to-nx-cloud');
42+
43+
// nx-ignore-next-line
44+
const { FsTree, flushChanges } = require(require.resolve(
45+
'nx/src/generators/tree',
46+
{
47+
paths: [directory],
48+
// nx-ignore-next-line
49+
}
50+
)) as typeof import('nx/src/generators/tree');
51+
52+
const tree = new FsTree(directory, false);
53+
const result = await connectToNxCloud(tree, {
54+
installationSource,
55+
directory: '',
56+
github: useGitHub,
57+
});
58+
59+
// Flush the tree changes to disk
60+
flushChanges(directory, tree.listChanges());
61+
62+
return result;
63+
}
64+
2965
export function readNxCloudToken(directory: string) {
3066
const nxCloudSpinner = ora(`Checking Nx Cloud setup`).start();
3167
// nx-ignore-next-line

0 commit comments

Comments
 (0)