Skip to content

Commit

Permalink
feat(nx-cloud): include URL for connect in cnw commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Jun 21, 2024
1 parent 50445e4 commit be89b03
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
"type": "boolean",
"description": "If the user will be using GitHub as their git hosting provider",
"default": false
},
"directory": {
"type": "string",
"description": "The directory where the workspace is located",
"x-priority": "internal"
}
},
"additionalProperties": false,
Expand Down
7 changes: 1 addition & 6 deletions packages/create-nx-workspace/src/create-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,9 @@ export async function createWorkspace<T extends CreateWorkspaceOptions>(
packageManager,
nxCloudInstallRes?.code === 0
);
}

if (gitSuccess && nxCloudInstallRes?.code === 0) {
let message = `feat(nx): Added Nx Cloud token to your nx.json`;
if (nxCIsetupRes?.code === 0) {
message = `${message} and generated CI workflow`;
commitChanges(directory, `feat(nx): Generated CI workflow`);
}
commitChanges(directory, message);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/create-nx-workspace/src/utils/nx/nx-cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function setupNxCloud(
process.env.NX_NEW_CLOUD_ONBOARDING === 'true'
? `${
pmc.exec
} nx g nx:connect-to-nx-cloud --installationSource=create-nx-workspace ${
} nx g nx:connect-to-nx-cloud --installationSource=create-nx-workspace --directory=${directory} ${
useGitHub ? '--github' : ''
} --no-interactive`
: `${pmc.exec} nx g nx:connect-to-nx-cloud --no-interactive --quiet`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { NxJsonConfiguration } from '../../../config/nx-json';
import { readNxJson, updateNxJson } from '../../../generators/utils/nx-json';
import { formatChangedFilesWithPrettierIfAvailable } from '../../../generators/internal-utils/format-changed-files-with-prettier-if-available';
import { repoUsesGithub, shortenedCloudUrl } from '../../utilities/url-shorten';
import { commitChanges } from '../../../utils/git-utils';
import * as ora from 'ora';
import * as open from 'open';

Expand Down Expand Up @@ -79,7 +80,8 @@ async function printSuccessMessage(
url: string,
token: string | undefined,
installationSource: string,
usesGithub?: boolean
usesGithub?: boolean,
directory?: string
) {
if (process.env.NX_NEW_CLOUD_ONBOARDING !== 'true') {
let origin = 'https://nx.app';
Expand All @@ -106,6 +108,7 @@ async function printSuccessMessage(
try {
const cloudConnectSpinner = ora(
`Opening Nx Cloud in your browser to connect your workspace.
If that does not work, you can manually visit the following URL:
${connectCloudUrl}
`
Expand Down Expand Up @@ -136,6 +139,15 @@ ${connectCloudUrl}
`${connectCloudUrl}`,
],
});
commitChanges(
`feat(nx): Added Nx Cloud token to your nx.json
To connect your workspace to Nx Cloud, push your repository
to your git hosting provider and go to the following URL:
${connectCloudUrl}`,
directory
);
} else {
output.note({
title: `Your Nx Cloud workspace is ready.`,
Expand All @@ -158,6 +170,7 @@ interface ConnectToNxCloudOptions {
installationSource?: string;
hideFormatLogs?: boolean;
github?: boolean;
directory?: string;
}

function addNxCloudOptionsToNxJson(
Expand Down Expand Up @@ -246,7 +259,8 @@ export async function connectToNxCloud(
responseFromCreateNxCloudWorkspace?.url ?? apiUrl,
responseFromCreateNxCloudWorkspace?.token,
schema.installationSource,
usesGithub
usesGithub,
schema.directory
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"type": "boolean",
"description": "If the user will be using GitHub as their git hosting provider",
"default": false
},
"directory": {
"type": "string",
"description": "The directory where the workspace is located",
"x-priority": "internal"
}
},
"additionalProperties": false,
Expand Down
17 changes: 15 additions & 2 deletions packages/nx/src/utils/git-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execSync } from 'child_process';
import { logger } from '../devkit-exports';

export function getGithubSlugOrNull(): string | null {
try {
Expand Down Expand Up @@ -46,16 +47,28 @@ function parseGitHubUrl(url: string): string | null {
return null;
}

export function commitChanges(commitMessage: string): string | null {
export function commitChanges(
commitMessage: string,
directory?: string
): string | null {
try {
execSync('git add -A', { encoding: 'utf8', stdio: 'pipe' });
execSync('git commit --no-verify -F -', {
encoding: 'utf8',
stdio: 'pipe',
input: commitMessage,
cwd: directory,
});
} catch (err) {
throw new Error(`Error committing changes:\n${err.stderr}`);
if (directory) {
// We don't want to throw during create-nx-workspace
// because maybe there was an error when setting up git
// initially.
logger.verbose(`Git may not be set up correctly for this new workspace.
${err}`);
} else {
throw new Error(`Error committing changes:\n${err.stderr}`);
}
}

return getLatestCommitSha();
Expand Down
34 changes: 24 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit be89b03

Please sign in to comment.