UNPKG

create-nx-workspace

Version:

Smart Repos · Fast Builds

99 lines (98 loc) 4.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createWorkspace = createWorkspace; exports.extractConnectUrl = extractConnectUrl; const output_1 = require("./utils/output"); const nx_cloud_1 = require("./utils/nx/nx-cloud"); const create_sandbox_1 = require("./create-sandbox"); const create_empty_workspace_1 = require("./create-empty-workspace"); const create_preset_1 = require("./create-preset"); const setup_ci_1 = require("./utils/ci/setup-ci"); const git_1 = require("./utils/git/git"); const get_third_party_preset_1 = require("./utils/preset/get-third-party-preset"); const error_utils_1 = require("./utils/error-utils"); const preset_1 = require("./utils/preset/preset"); async function createWorkspace(preset, options, rawArgs) { const { packageManager, name, nxCloud, skipGit = false, defaultBase = 'main', commit, cliName, useGitHub, skipGitHubPush = false, verbose = false, } = options; if (cliName) { output_1.output.setCliName(cliName ?? 'NX'); } const tmpDir = await (0, create_sandbox_1.createSandbox)(packageManager); const workspaceGlobs = getWorkspaceGlobsFromPreset(preset); // nx new requires a preset currently. We should probably make it optional. const directory = await (0, create_empty_workspace_1.createEmptyWorkspace)(tmpDir, name, packageManager, { ...options, preset, workspaceGlobs }); // 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 = (0, get_third_party_preset_1.getPackageNameFromThirdPartyPreset)(preset); if (thirdPartyPackageName) { await (0, create_preset_1.createPreset)(thirdPartyPackageName, options, packageManager, directory); } let connectUrl; let nxCloudInfo; if (nxCloud !== 'skip') { const token = (0, nx_cloud_1.readNxCloudToken)(directory); if (nxCloud !== 'yes') { await (0, setup_ci_1.setupCI)(directory, nxCloud, packageManager); } connectUrl = await (0, nx_cloud_1.createNxCloudOnboardingUrl)(nxCloud, token, directory, useGitHub); } let pushedToVcs = git_1.VcsPushStatus.SkippedGit; if (!skipGit) { try { await (0, git_1.initializeGitRepo)(directory, { defaultBase, commit, connectUrl }); // Push to GitHub if commit was made, GitHub push is not skipped, and CI provider is GitHub if (commit && !skipGitHubPush && nxCloud === 'github') { pushedToVcs = await (0, git_1.pushToGitHub)(directory, { skipGitHubPush, name, defaultBase, verbose, }); } } catch (e) { if (e instanceof Error) { output_1.output.error({ title: 'Could not initialize git repository', bodyLines: (0, error_utils_1.mapErrorToBodyLines)(e), }); } else { console.error(e); } } } if (connectUrl) { nxCloudInfo = await (0, nx_cloud_1.getNxCloudInfo)(nxCloud, connectUrl, pushedToVcs, rawArgs?.nxCloud); } return { nxCloudInfo, directory, pushedToVcs, }; } function extractConnectUrl(text) { const urlPattern = /(https:\/\/[^\s]+\/connect\/[^\s]+)/g; const match = text.match(urlPattern); return match ? match[0] : null; } function getWorkspaceGlobsFromPreset(preset) { // Should match how apps are created in `packages/workspace/src/generators/preset/preset.ts`. switch (preset) { case preset_1.Preset.AngularMonorepo: case preset_1.Preset.Expo: case preset_1.Preset.Express: case preset_1.Preset.Nest: case preset_1.Preset.NextJs: case preset_1.Preset.NodeMonorepo: case preset_1.Preset.Nuxt: case preset_1.Preset.ReactNative: case preset_1.Preset.ReactMonorepo: case preset_1.Preset.VueMonorepo: case preset_1.Preset.WebComponents: return ['apps/*']; default: return ['packages/*']; } }