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

feat(nx-cloud): update gh onboarding url #26587

Closed
wants to merge 1 commit into from
Closed
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
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
36 changes: 21 additions & 15 deletions packages/create-nx-workspace/src/create-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createSandbox } from './create-sandbox';
import { createEmptyWorkspace } from './create-empty-workspace';
import { createPreset } from './create-preset';
import { setupCI } from './utils/ci/setup-ci';
import { initializeGitRepo } from './utils/git/git';
import { commitChanges, initializeGitRepo } from './utils/git/git';
import { getPackageNameFromThirdPartyPreset } from './utils/preset/get-third-party-preset';
import { mapErrorToBodyLines } from './utils/error-utils';

Expand Down Expand Up @@ -51,6 +51,23 @@ export async function createWorkspace<T extends CreateWorkspaceOptions>(
);
}

let gitSuccess = false;
if (!skipGit && commit) {
try {
await initializeGitRepo(directory, { defaultBase, commit });
gitSuccess = true;
} catch (e) {
if (e instanceof Error) {
output.error({
title: 'Could not initialize git repository',
bodyLines: mapErrorToBodyLines(e),
});
} else {
console.error(e);
}
}
}

let nxCloudInstallRes;
if (nxCloud !== 'skip') {
nxCloudInstallRes = await setupNxCloud(
Expand All @@ -61,25 +78,14 @@ export async function createWorkspace<T extends CreateWorkspaceOptions>(
);

if (nxCloud !== 'yes') {
await setupCI(
const nxCIsetupRes = await setupCI(
directory,
nxCloud,
packageManager,
nxCloudInstallRes?.code === 0
);
}
}
if (!skipGit && commit) {
try {
await initializeGitRepo(directory, { defaultBase, commit });
} catch (e) {
if (e instanceof Error) {
output.error({
title: 'Could not initialize git repository',
bodyLines: mapErrorToBodyLines(e),
});
} else {
console.error(e);
if (nxCIsetupRes?.code === 0) {
commitChanges(directory, `feat(nx): Generated CI workflow`);
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions packages/create-nx-workspace/src/utils/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,19 @@ export async function initializeGitRepo(
await execute(['commit', `-m "${message}"`]);
}
}

export function commitChanges(directory: string, message: string) {
try {
execSync('git add -A', { encoding: 'utf8', stdio: 'pipe', cwd: directory });
execSync('git commit --no-verify -F -', {
encoding: 'utf8',
stdio: 'pipe',
input: message,
cwd: directory,
});
} catch (e) {
console.error(`There was an error committing your Nx Cloud token.\n
Please commit the changes manually and push to your new repository.\n
\n${e}`);
}
}
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
10 changes: 7 additions & 3 deletions packages/nx/src/command-line/connect/connect-to-nx-cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export async function connectToNxCloudIfExplicitlyAsked(
}
}

export async function connectToNxCloudCommand(): Promise<boolean> {
export async function connectToNxCloudCommand(
command?: string
): Promise<boolean> {
const nxJson = readNxJson();

if (isNxCloudUsed(nxJson)) {
Expand Down Expand Up @@ -85,7 +87,9 @@ export async function connectToNxCloudCommand(): Promise<boolean> {
}

const tree = new FsTree(workspaceRoot, false, 'connect-to-nx-cloud');
const callback = await connectToNxCloud(tree, {});
const callback = await connectToNxCloud(tree, {
installationSource: command ?? 'nx-connect',
});
tree.lock();
flushChanges(workspaceRoot, tree.listChanges());
await callback();
Expand All @@ -96,7 +100,7 @@ export async function connectToNxCloudCommand(): Promise<boolean> {
export async function connectToNxCloudWithPrompt(command: string) {
const setNxCloud = await nxCloudPrompt('setupNxCloud');
const useCloud =
setNxCloud === 'yes' ? await connectToNxCloudCommand() : false;
setNxCloud === 'yes' ? await connectToNxCloudCommand(command) : false;
await recordStat({
command,
nxVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { readJson } from '../../../generators/utils/json';
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 { shortenedCloudUrl } from '../../utilities/url-shorten';
import { repoUsesGithub, shortenedCloudUrl } from '../../utilities/url-shorten';
import { commitChanges } from '../../../utils/git-utils';
import * as ora from 'ora';
import * as open from 'open';

function printCloudConnectionDisabledMessage() {
output.error({
Expand Down Expand Up @@ -75,9 +78,10 @@ async function createNxCloudWorkspace(

async function printSuccessMessage(
url: string,
token: string,
token: string | undefined,
installationSource: string,
github: boolean
usesGithub?: boolean,
directory?: string
) {
if (process.env.NX_NEW_CLOUD_ONBOARDING !== 'true') {
let origin = 'https://nx.app';
Expand All @@ -97,20 +101,63 @@ async function printSuccessMessage(
const connectCloudUrl = await shortenedCloudUrl(
installationSource,
token,
github
usesGithub
);

output.note({
title: `Your Nx Cloud workspace is ready.`,
bodyLines: [
`To claim it, connect it to your Nx Cloud account:`,
`- Commit and push your changes.`,
`- Create a pull request for the changes.`,
`- Go to the following URL to connect your workspace to Nx Cloud:

${connectCloudUrl}`,
],
});
if (installationSource === 'nx-connect' && usesGithub) {
try {
const cloudConnectSpinner = ora(
`Opening Nx Cloud ${connectCloudUrl} in your browser to connect your workspace.`
).start();
await sleep(2000);
open(connectCloudUrl);
cloudConnectSpinner.succeed();
} catch (e) {
output.note({
title: `Your Nx Cloud workspace is ready.`,
bodyLines: [
`To claim it, connect it to your Nx Cloud account:`,
`- Go to the following URL to connect your workspace to Nx Cloud:`,
'',
`${connectCloudUrl}`,
],
});
}
} else {
if (installationSource === 'create-nx-workspace') {
output.note({
title: `Your Nx Cloud workspace is ready.`,
bodyLines: [
`To claim it, connect it to your Nx Cloud account:`,
`- Push your repository to your git hosting provider.`,
`- Go to the following URL to connect your workspace to Nx Cloud:`,
'',
`${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.`,
bodyLines: [
`To claim it, connect it to your Nx Cloud account:`,
`- Commit and push your changes.`,
`- Create a pull request for the changes.`,
`- Go to the following URL to connect your workspace to Nx Cloud:`,
'',
`${connectCloudUrl}`,
],
});
}
}
}
}

Expand All @@ -119,6 +166,7 @@ interface ConnectToNxCloudOptions {
installationSource?: string;
hideFormatLogs?: boolean;
github?: boolean;
directory?: string;
}

function addNxCloudOptionsToNxJson(
Expand Down Expand Up @@ -152,27 +200,70 @@ export async function connectToNxCloud(
printCloudConnectionDisabledMessage();
};
} else {
// TODO: Change to using loading light client when that is enabled by default
const r = await createNxCloudWorkspace(
getRootPackageName(tree),
schema.installationSource,
getNxInitDate()
);
if (process.env.NX_NEW_CLOUD_ONBOARDING !== 'true') {
// TODO: Change to using loading light client when that is enabled by default
const r = await createNxCloudWorkspace(
getRootPackageName(tree),
schema.installationSource,
getNxInitDate()
);

addNxCloudOptionsToNxJson(tree, nxJson, r.token);
addNxCloudOptionsToNxJson(tree, nxJson, r.token);

await formatChangedFilesWithPrettierIfAvailable(tree, {
silent: schema.hideFormatLogs,
});
await formatChangedFilesWithPrettierIfAvailable(tree, {
silent: schema.hideFormatLogs,
});

return async () =>
await printSuccessMessage(
r.url,
r.token,
schema.installationSource,
schema.github
return async () =>
await printSuccessMessage(r.url, r.token, schema.installationSource);
} else {
const usesGithub = await repoUsesGithub(schema.github);

let responseFromCreateNxCloudWorkspace:
| {
token: string;
url: string;
}
| undefined;

// do NOT create Nx Cloud token (createNxCloudWorkspace)
// if user is using github and is running nx-connect
if (!(usesGithub && schema.installationSource === 'nx-connect')) {
responseFromCreateNxCloudWorkspace = await createNxCloudWorkspace(
getRootPackageName(tree),
schema.installationSource,
getNxInitDate()
);

addNxCloudOptionsToNxJson(
tree,
nxJson,
responseFromCreateNxCloudWorkspace?.token
);

await formatChangedFilesWithPrettierIfAvailable(tree, {
silent: schema.hideFormatLogs,
});
}
const apiUrl = removeTrailingSlash(
process.env.NX_CLOUD_API ||
process.env.NRWL_API ||
`https://cloud.nx.app`
);
return async () =>
await printSuccessMessage(
responseFromCreateNxCloudWorkspace?.url ?? apiUrl,
responseFromCreateNxCloudWorkspace?.token,
schema.installationSource,
usesGithub,
schema.directory
);
}
}
}

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

export default connectToNxCloud;
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
Loading
Loading