<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> The Nx Cloud token is generated after the workspace and preset have been generated. This makes it hard to use the Nx Cloud token for generating the workspace and preset. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> The Nx Cloud token is generated in the workspace creation. This makes it easier to use the Nx Cloud token during the workspace creation process. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
99 lines
2.7 KiB
TypeScript
99 lines
2.7 KiB
TypeScript
import { CreateWorkspaceOptions } from './create-workspace-options';
|
|
import { output } from './utils/output';
|
|
import { getOnboardingInfo, readNxCloudToken } from './utils/nx/nx-cloud';
|
|
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 { getPackageNameFromThirdPartyPreset } from './utils/preset/get-third-party-preset';
|
|
import { mapErrorToBodyLines } from './utils/error-utils';
|
|
|
|
export async function createWorkspace<T extends CreateWorkspaceOptions>(
|
|
preset: string,
|
|
options: T
|
|
) {
|
|
const {
|
|
packageManager,
|
|
name,
|
|
nxCloud,
|
|
skipGit = false,
|
|
defaultBase = 'main',
|
|
commit,
|
|
cliName,
|
|
useGitHub,
|
|
} = options;
|
|
|
|
if (cliName) {
|
|
output.setCliName(cliName ?? 'NX');
|
|
}
|
|
|
|
const tmpDir = await createSandbox(packageManager);
|
|
|
|
// nx new requires a preset currently. We should probably make it optional.
|
|
const directory = await createEmptyWorkspace<T>(
|
|
tmpDir,
|
|
name,
|
|
packageManager,
|
|
{ ...options, preset }
|
|
);
|
|
|
|
// If the preset is a third-party preset, we need to call createPreset to install it
|
|
// For first-party presets, it will be created by createEmptyWorkspace instead.
|
|
// In createEmptyWorkspace, it will call `nx new` -> `@nx/workspace newGenerator` -> `@nx/workspace generatePreset`.
|
|
const thirdPartyPackageName = getPackageNameFromThirdPartyPreset(preset);
|
|
if (thirdPartyPackageName) {
|
|
await createPreset(
|
|
thirdPartyPackageName,
|
|
options,
|
|
packageManager,
|
|
directory
|
|
);
|
|
}
|
|
|
|
let connectUrl: string | undefined;
|
|
let nxCloudInfo: string | undefined;
|
|
if (nxCloud !== 'skip') {
|
|
const token = readNxCloudToken(directory) as string;
|
|
|
|
if (nxCloud !== 'yes') {
|
|
await setupCI(directory, nxCloud, packageManager);
|
|
}
|
|
|
|
const { connectCloudUrl, output } = await getOnboardingInfo(
|
|
nxCloud,
|
|
token,
|
|
directory,
|
|
useGitHub
|
|
);
|
|
connectUrl = connectCloudUrl;
|
|
nxCloudInfo = output;
|
|
}
|
|
|
|
if (!skipGit) {
|
|
try {
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
nxCloudInfo,
|
|
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;
|
|
}
|