nx 19.2.0-rc.0 → 19.2.0-rc.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,12 +10,14 @@ function getNativeFileCacheLocation() {
10
10
  return process.env.NX_NATIVE_FILE_CACHE_DIRECTORY;
11
11
  }
12
12
  else {
13
- const shortHash = (0, crypto_1.createHash)('sha256')
14
- .update((0, os_1.userInfo)().username)
15
- .update(workspace_root_1.workspaceRoot)
16
- .digest('hex')
17
- .substring(0, 7);
18
- return (0, path_1.join)((0, os_1.tmpdir)(), `nx-native-file-cache-${shortHash}`);
13
+ const hash = (0, crypto_1.createHash)('sha256').update(workspace_root_1.workspaceRoot);
14
+ try {
15
+ hash.update((0, os_1.userInfo)().username);
16
+ }
17
+ catch (e) {
18
+ // if there's no user, we only use the workspace root for the hash and move on
19
+ }
20
+ return (0, path_1.join)((0, os_1.tmpdir)(), `nx-native-file-cache-${hash.digest('hex').substring(0, 7)}`);
19
21
  }
20
22
  }
21
23
  exports.getNativeFileCacheLocation = getNativeFileCacheLocation;
@@ -3,6 +3,7 @@ interface ConnectToNxCloudOptions {
3
3
  analytics?: boolean;
4
4
  installationSource?: string;
5
5
  hideFormatLogs?: boolean;
6
+ github?: boolean;
6
7
  }
7
8
  export declare function connectToNxCloud(tree: Tree, schema: ConnectToNxCloudOptions): Promise<() => void>;
8
9
  export default connectToNxCloud;
@@ -7,6 +7,7 @@ const output_1 = require("../../../utils/output");
7
7
  const json_1 = require("../../../generators/utils/json");
8
8
  const nx_json_1 = require("../../../generators/utils/nx-json");
9
9
  const format_changed_files_with_prettier_if_available_1 = require("../../../generators/internal-utils/format-changed-files-with-prettier-if-available");
10
+ const url_shorten_1 = require("../../utilities/url-shorten");
10
11
  function printCloudConnectionDisabledMessage() {
11
12
  output_1.output.error({
12
13
  title: `Connections to Nx Cloud are disabled for this workspace`,
@@ -56,20 +57,36 @@ async function createNxCloudWorkspace(workspaceName, installationSource, nxInitD
56
57
  }
57
58
  return response.data;
58
59
  }
59
- function printSuccessMessage(url) {
60
- let origin = 'https://nx.app';
61
- try {
62
- origin = new node_url_1.URL(url).origin;
60
+ async function printSuccessMessage(url, token, installationSource, github) {
61
+ if (process.env.NX_NEW_CLOUD_ONBOARDING !== 'true') {
62
+ let origin = 'https://nx.app';
63
+ try {
64
+ origin = new node_url_1.URL(url).origin;
65
+ }
66
+ catch (e) { }
67
+ output_1.output.note({
68
+ title: `Your Nx Cloud workspace is public`,
69
+ bodyLines: [
70
+ `To restrict access, connect it to your Nx Cloud account:`,
71
+ `- Push your changes`,
72
+ `- Login at ${origin} to connect your repository`,
73
+ ],
74
+ });
75
+ }
76
+ else {
77
+ const connectCloudUrl = await (0, url_shorten_1.shortenedCloudUrl)(installationSource, token, github);
78
+ output_1.output.note({
79
+ title: `Your Nx Cloud workspace is ready.`,
80
+ bodyLines: [
81
+ `To claim it, connect it to your Nx Cloud account:`,
82
+ `- Commit and push your changes.`,
83
+ `- Create a pull request for the changes.`,
84
+ `- Go to the following URL to connect your workspace to Nx Cloud:
85
+
86
+ ${connectCloudUrl}`,
87
+ ],
88
+ });
63
89
  }
64
- catch (e) { }
65
- output_1.output.note({
66
- title: `Your Nx Cloud workspace is public`,
67
- bodyLines: [
68
- `To restrict access, connect it to your Nx Cloud account:`,
69
- `- Push your changes`,
70
- `- Login at ${origin} to connect your repository`,
71
- ],
72
- });
73
90
  }
74
91
  function addNxCloudOptionsToNxJson(tree, nxJson, token) {
75
92
  nxJson ??= {
@@ -97,7 +114,7 @@ async function connectToNxCloud(tree, schema) {
97
114
  await (0, format_changed_files_with_prettier_if_available_1.formatChangedFilesWithPrettierIfAvailable)(tree, {
98
115
  silent: schema.hideFormatLogs,
99
116
  });
100
- return () => printSuccessMessage(r.url);
117
+ return async () => await printSuccessMessage(r.url, r.token, schema.installationSource, schema.github);
101
118
  }
102
119
  }
103
120
  exports.connectToNxCloud = connectToNxCloud;
@@ -20,6 +20,11 @@
20
20
  "type": "boolean",
21
21
  "description": "Hide formatting logs",
22
22
  "x-priority": "internal"
23
+ },
24
+ "github": {
25
+ "type": "boolean",
26
+ "description": "If the user will be using GitHub as their git hosting provider",
27
+ "default": false
23
28
  }
24
29
  },
25
30
  "additionalProperties": false,
@@ -0,0 +1 @@
1
+ export declare function shortenedCloudUrl(installationSource: string, accessToken: string, github?: boolean): Promise<string>;
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shortenedCloudUrl = void 0;
4
+ const devkit_exports_1 = require("../../devkit-exports");
5
+ const git_utils_1 = require("../../utils/git-utils");
6
+ async function shortenedCloudUrl(installationSource, accessToken, github) {
7
+ const githubSlug = (0, git_utils_1.getGithubSlugOrNull)();
8
+ const apiUrl = removeTrailingSlash(process.env.NX_CLOUD_API || process.env.NRWL_API || `https://cloud.nx.app`);
9
+ const installationSupportsGitHub = await getInstallationSupportsGitHub(apiUrl);
10
+ const usesGithub = (githubSlug || github) &&
11
+ (apiUrl.includes('cloud.nx.app') ||
12
+ apiUrl.includes('eu.nx.app') ||
13
+ installationSupportsGitHub);
14
+ const source = getSource(installationSource);
15
+ try {
16
+ const response = await require('axios').post(`${apiUrl}/nx-cloud/onboarding`, {
17
+ type: usesGithub ? 'GITHUB' : 'MANUAL',
18
+ source,
19
+ accessToken: usesGithub ? null : accessToken,
20
+ selectedRepositoryName: githubSlug,
21
+ });
22
+ if (!response?.data || response.data.message) {
23
+ throw new Error(response?.data?.message ?? 'Failed to shorten Nx Cloud URL');
24
+ }
25
+ return `${apiUrl}/connect/${response.data}`;
26
+ }
27
+ catch (e) {
28
+ devkit_exports_1.logger.verbose(`Failed to shorten Nx Cloud URL.
29
+ ${e}`);
30
+ return getURLifShortenFailed(usesGithub, githubSlug, apiUrl, accessToken, source);
31
+ }
32
+ }
33
+ exports.shortenedCloudUrl = shortenedCloudUrl;
34
+ function removeTrailingSlash(apiUrl) {
35
+ return apiUrl[apiUrl.length - 1] === '/' ? apiUrl.slice(0, -1) : apiUrl;
36
+ }
37
+ function getSource(installationSource) {
38
+ if (installationSource.includes('nx-init')) {
39
+ return 'nx-init';
40
+ }
41
+ else if (installationSource.includes('nx-connect')) {
42
+ return 'nx-connect';
43
+ }
44
+ else if (installationSource.includes('create-nx-workspace')) {
45
+ return 'create-nx-workspace';
46
+ }
47
+ else {
48
+ return 'other';
49
+ }
50
+ }
51
+ function getURLifShortenFailed(usesGithub, githubSlug, apiUrl, accessToken, source) {
52
+ if (usesGithub) {
53
+ if (githubSlug) {
54
+ return `${apiUrl}/setup/connect-workspace/vcs?provider=GITHUB&selectedRepositoryName=${encodeURIComponent(githubSlug)}&source=${source}`;
55
+ }
56
+ else {
57
+ return `${apiUrl}/setup/connect-workspace/vcs?provider=GITHUB&source=${source}`;
58
+ }
59
+ }
60
+ return `${apiUrl}/setup/connect-workspace/manual?accessToken=${accessToken}&source=${source}`;
61
+ }
62
+ async function getInstallationSupportsGitHub(apiUrl) {
63
+ try {
64
+ const response = await require('axios').get(`${apiUrl}/vcs-integrations`);
65
+ if (!response?.data || response.data.message) {
66
+ throw new Error(response?.data?.message ?? 'Failed to shorten Nx Cloud URL');
67
+ }
68
+ return !!response.data.github;
69
+ }
70
+ catch (e) {
71
+ if (process.env.NX_VERBOSE_LOGGING) {
72
+ devkit_exports_1.logger.warn(`Failed to access vcs-integrations endpoint.
73
+ ${e}`);
74
+ }
75
+ return false;
76
+ }
77
+ }
@@ -0,0 +1,4 @@
1
+ export declare function getGithubSlugOrNull(): string | null;
2
+ export declare function extractUserAndRepoFromGitHubUrl(gitRemotes: string): string | null;
3
+ export declare function commitChanges(commitMessage: string): string | null;
4
+ export declare function getLatestCommitSha(): string | null;
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getLatestCommitSha = exports.commitChanges = exports.extractUserAndRepoFromGitHubUrl = exports.getGithubSlugOrNull = void 0;
4
+ const child_process_1 = require("child_process");
5
+ function getGithubSlugOrNull() {
6
+ try {
7
+ const gitRemote = (0, child_process_1.execSync)('git remote -v').toString();
8
+ return extractUserAndRepoFromGitHubUrl(gitRemote);
9
+ }
10
+ catch (e) {
11
+ return null;
12
+ }
13
+ }
14
+ exports.getGithubSlugOrNull = getGithubSlugOrNull;
15
+ function extractUserAndRepoFromGitHubUrl(gitRemotes) {
16
+ const regex = /^\s*(\w+)\s+(git@github\.com:|https:\/\/github\.com\/)([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)\.git/gm;
17
+ let firstGitHubUrl = null;
18
+ let match;
19
+ while ((match = regex.exec(gitRemotes)) !== null) {
20
+ const remoteName = match[1];
21
+ const url = match[2] + match[3] + '/' + match[4] + '.git';
22
+ if (remoteName === 'origin') {
23
+ return parseGitHubUrl(url);
24
+ }
25
+ if (!firstGitHubUrl) {
26
+ firstGitHubUrl = url;
27
+ }
28
+ }
29
+ return firstGitHubUrl ? parseGitHubUrl(firstGitHubUrl) : null;
30
+ }
31
+ exports.extractUserAndRepoFromGitHubUrl = extractUserAndRepoFromGitHubUrl;
32
+ function parseGitHubUrl(url) {
33
+ const sshPattern = /git@github\.com:([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)\.git/;
34
+ const httpsPattern = /https:\/\/github\.com\/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)\.git/;
35
+ let match = url.match(sshPattern) || url.match(httpsPattern);
36
+ if (match) {
37
+ return `${match[1]}/${match[2]}`;
38
+ }
39
+ return null;
40
+ }
41
+ function commitChanges(commitMessage) {
42
+ try {
43
+ (0, child_process_1.execSync)('git add -A', { encoding: 'utf8', stdio: 'pipe' });
44
+ (0, child_process_1.execSync)('git commit --no-verify -F -', {
45
+ encoding: 'utf8',
46
+ stdio: 'pipe',
47
+ input: commitMessage,
48
+ });
49
+ }
50
+ catch (err) {
51
+ throw new Error(`Error committing changes:\n${err.stderr}`);
52
+ }
53
+ return getLatestCommitSha();
54
+ }
55
+ exports.commitChanges = commitChanges;
56
+ function getLatestCommitSha() {
57
+ try {
58
+ return (0, child_process_1.execSync)('git rev-parse HEAD', {
59
+ encoding: 'utf8',
60
+ stdio: 'pipe',
61
+ }).trim();
62
+ }
63
+ catch {
64
+ return null;
65
+ }
66
+ }
67
+ exports.getLatestCommitSha = getLatestCommitSha;