Skip to content

Commit

Permalink
feat(misc): only create one commit with cloud onboard URL on cnw
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Jul 24, 2024
1 parent 7a642ee commit afd8b41
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 52 deletions.
59 changes: 59 additions & 0 deletions packages/create-nx-workspace/src/create-workspace.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { extractConnectUrl } from './create-workspace';

describe('extractConnectUrl', () => {
test('should extract the correct URL from the given string', () => {
const inputString = `
NX Your Nx Cloud workspace is ready.
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:
https://staging.nx.app/connect/O8dfB0jYgvd
`;
const expectedUrl = 'https://staging.nx.app/connect/O8dfB0jYgvd';
expect(extractConnectUrl(inputString)).toBe(expectedUrl);
});

test('should return null if no URL is present', () => {
const inputString = `
NX Your Nx Cloud workspace is ready.
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:
No URL here.
`;
expect(extractConnectUrl(inputString)).toBeNull();
});

test('should handle URLs with different domains and paths', () => {
const inputString = `
NX Your Nx Cloud workspace is ready.
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:
https://example.com/connect/abcd1234
`;
const expectedUrl = 'https://example.com/connect/abcd1234';
expect(extractConnectUrl(inputString)).toBe(expectedUrl);
});

test('should handle URLs with query parameters and fragments', () => {
const inputString = `
NX Your Nx Cloud workspace is ready.
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:
https://example.com/connect/abcd1234?query=param#fragment
`;
const expectedUrl =
'https://example.com/connect/abcd1234?query=param#fragment';
expect(extractConnectUrl(inputString)).toBe(expectedUrl);
});
});
47 changes: 26 additions & 21 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 { commitChanges, initializeGitRepo } from './utils/git/git';
import { 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,23 +51,6 @@ 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 @@ -78,14 +61,30 @@ export async function createWorkspace<T extends CreateWorkspaceOptions>(
);

if (nxCloud !== 'yes') {
const nxCIsetupRes = await setupCI(
await setupCI(
directory,
nxCloud,
packageManager,
nxCloudInstallRes?.code === 0
);
if (nxCIsetupRes?.code === 0) {
commitChanges(directory, `feat(nx): Generated CI workflow`);
}
}

if (!skipGit) {
try {
let connectUrl;
if (nxCloudInstallRes?.code === 0) {
connectUrl = extractConnectUrl(nxCloudInstallRes?.stdout);
}
await initializeGitRepo(directory, { defaultBase, commit, connectUrl });
} catch (e) {
if (e instanceof Error) {
output.error({
title: 'Could not initialize git repository',
bodyLines: mapErrorToBodyLines(e),
});
} else {
console.error(e);
}
}
}
Expand All @@ -95,3 +94,9 @@ export async function createWorkspace<T extends CreateWorkspaceOptions>(
directory,
};
}

export function extractConnectUrl(text: string): string | null {
const urlPattern = /(https:\/\/[^\s]+\/connect\/[^\s]+)/g;
const match = text.match(urlPattern);
return match ? match[0] : null;
}
28 changes: 11 additions & 17 deletions packages/create-nx-workspace/src/utils/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function initializeGitRepo(
options: {
defaultBase: string;
commit?: { message: string; name: string; email: string };
connectUrl?: string | null;
}
) {
const execute = (args: ReadonlyArray<string>, ignoreErrorStream = false) => {
Expand Down Expand Up @@ -80,23 +81,16 @@ export async function initializeGitRepo(
}
await execute(['add', '.']);
if (options.commit) {
const message = options.commit.message || 'initial commit';
await execute(['commit', `-m "${message}"`]);
}
}
let message = `${options.commit.message}` || 'initial commit';
if (options.connectUrl) {
message = `${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}`);
To connect your workspace to Nx Cloud, push your repository
to your git hosting provider and go to the following URL:
${options.connectUrl}
`;
}
await execute(['commit', `-m "${message}"`]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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 { getCloudUrl } from '../../utilities/get-cloud-options';
import { commitChanges } from '../../../utils/git-utils';
import * as ora from 'ora';
import * as open from 'open';

Expand Down Expand Up @@ -72,8 +71,7 @@ async function createNxCloudWorkspace(
async function printSuccessMessage(
token: string | undefined,
installationSource: string,
usesGithub: boolean,
directory?: string
usesGithub: boolean
) {
const connectCloudUrl = await shortenedCloudUrl(
installationSource,
Expand Down Expand Up @@ -112,15 +110,6 @@ async function printSuccessMessage(
`${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 Down Expand Up @@ -208,8 +197,7 @@ export async function connectToNxCloud(
await printSuccessMessage(
responseFromCreateNxCloudWorkspace?.token,
schema.installationSource,
usesGithub,
schema.directory
usesGithub
);
}
}
Expand Down

0 comments on commit afd8b41

Please sign in to comment.