@catladder/cli 2.4.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -53,7 +53,7 @@
53
53
  }
54
54
  ],
55
55
  "license": "MIT",
56
- "version": "2.4.0",
56
+ "version": "2.6.0",
57
57
  "scripts": {
58
58
  "lint": "eslint \"src/**/*.ts\"",
59
59
  "lint:fix": "eslint \"src/**/*.ts\" --fix",
@@ -0,0 +1,32 @@
1
+ import { exec } from "child-process-promise";
2
+
3
+ export type GitLocationScope = "global" | "local" | "system" | "worktree";
4
+ export type GitConfigOptions = {
5
+ location?: GitLocationScope;
6
+ comment?: string;
7
+ };
8
+
9
+ const argsJoin = (args: (string | undefined)[]): string =>
10
+ args.filter(Boolean).join(" ");
11
+
12
+ export async function gitConfigGet(
13
+ key: string,
14
+ { location }: Pick<GitConfigOptions, "location"> = {},
15
+ ) {
16
+ const { stdout } = await exec(
17
+ `git config get ${argsJoin([location ? `--${location}` : undefined])} ${key}`,
18
+ );
19
+ return stdout.trim();
20
+ }
21
+
22
+ export const gitConfigSet = (
23
+ key: string,
24
+ value: string,
25
+ { location, comment }: GitConfigOptions = {},
26
+ ) =>
27
+ exec(
28
+ `git config set ${argsJoin([
29
+ location ? `--${location}` : undefined,
30
+ comment ? `--comment=${comment}` : undefined,
31
+ ])} ${key} ${value}`,
32
+ );
@@ -0,0 +1,40 @@
1
+ import { gitConfigGet } from "./gitConfig";
2
+ import { exec } from "child-process-promise";
3
+
4
+ /**
5
+ * Find the absolute path of the current git project root.
6
+ */
7
+ export const getProjectRootPath = async (): Promise<string> => {
8
+ const { stdout } = await exec(`git rev-parse --show-toplevel`);
9
+ return stdout.trim();
10
+ };
11
+
12
+ /**
13
+ * Get the git remote hostname and project path from the git config.
14
+ */
15
+ export const getGitRemoteHostAndPath = async (): Promise<{
16
+ gitRemoteHost: string;
17
+ gitRemotePath: string;
18
+ }> => {
19
+ const remoteUrl = await gitConfigGet("remote.origin.url");
20
+ const remoteReg = /(https:\/\/|git@)([^:/]+)[:/]([^.]*)(\.git)?/;
21
+ const match = remoteUrl.match(remoteReg) ?? [];
22
+ const [, , gitRemoteHost, gitRemotePath] = match;
23
+ if (!gitRemoteHost?.length || !gitRemotePath?.length) {
24
+ throw new Error(
25
+ `Failed to parse git remote hostname and path from git configs remote.origin.url! ${remoteUrl}`,
26
+ );
27
+ }
28
+ return { gitRemoteHost, gitRemotePath };
29
+ };
30
+
31
+ export const gitProjectInformation = async () => {
32
+ const [{ gitRemoteHost, gitRemotePath }, projectRootPath] = await Promise.all(
33
+ [getGitRemoteHostAndPath(), getProjectRootPath()],
34
+ );
35
+ return {
36
+ gitRemoteHost,
37
+ gitRemotePath,
38
+ projectRootPath,
39
+ };
40
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./gitConfig";
2
+ export * from "./gitProjectInformation";
@@ -1,11 +1,11 @@
1
1
  import { getSecretVarName } from "@catladder/pipeline";
2
- import { exec } from "child-process-promise";
3
2
  import { has, isObject } from "lodash";
4
3
  import memoizee from "memoizee";
5
4
  import fetch from "node-fetch";
6
5
  import open from "open";
7
6
  import type { CommandInstance } from "vorpal";
8
7
  import { getPreference, hasPreference, setPreference } from "./preferences";
8
+ import { getGitRemoteHostAndPath } from "../git/gitProjectInformation";
9
9
 
10
10
  const TOKEN_KEY = "gitlab-personal-access-token";
11
11
 
@@ -17,14 +17,17 @@ export const setupGitlabToken = async (vorpal: CommandInstance) => {
17
17
  vorpal.log("");
18
18
  vorpal.log("☝ we open up the settings page for you!");
19
19
  vorpal.log("");
20
- const { shouldContinue } = await vorpal.prompt({
21
- default: true,
22
- message: "Ok",
23
- name: "shouldContinue",
24
- type: "prompt",
25
- });
26
-
27
- open("https://git.panter.ch/-/profile/personal_access_tokens");
20
+ const [{ shouldContinue }, { gitRemoteHost }] = await Promise.all([
21
+ vorpal.prompt({
22
+ default: true,
23
+ message: "Ok",
24
+ name: "shouldContinue",
25
+ type: "prompt",
26
+ }),
27
+ getGitRemoteHostAndPath(),
28
+ ]);
29
+
30
+ open(`https://${gitRemoteHost}/-/profile/personal_access_tokens`);
28
31
 
29
32
  vorpal.log("Please type in gitlab's personal access token");
30
33
 
@@ -58,11 +61,14 @@ export const doGitlabRequest = async <T = any>(
58
61
  data: any = undefined,
59
62
  method: Method = "GET",
60
63
  ): Promise<T> => {
61
- const rootToken = await getGitlabToken(vorpal);
64
+ const [rootToken, { gitRemoteHost }] = await Promise.all([
65
+ getGitlabToken(vorpal),
66
+ getGitRemoteHostAndPath(),
67
+ ]);
62
68
 
63
69
  //const method = data ? (update ? "PUT" : "POST") : "GET";
64
70
 
65
- const result = await fetch(`https://git.panter.ch/api/v4/${path}`, {
71
+ const result = await fetch(`https://${gitRemoteHost}/api/v4/${path}`, {
66
72
  method,
67
73
  headers: {
68
74
  "Content-Type": "application/json",
@@ -91,16 +97,10 @@ export const doGitlabRequest = async <T = any>(
91
97
  export const getProjectInfo = async (
92
98
  vorpal: CommandInstance | null,
93
99
  ): Promise<{ id: string; web_url: string }> => {
94
- const gitRemoteOriginUrl = (
95
- await exec("git config --get remote.origin.url")
96
- ).stdout.trim();
97
- const projectPath =
98
- /(https:\/\/|git@)git\.panter\.ch[:/]([^.]*)(\.git)?/g.exec(
99
- gitRemoteOriginUrl,
100
- );
100
+ const { gitRemotePath } = await getGitRemoteHostAndPath();
101
101
  const project = await doGitlabRequest(
102
102
  vorpal,
103
- `projects/${encodeURIComponent(projectPath[2])}`,
103
+ `projects/${encodeURIComponent(gitRemotePath)}`,
104
104
  );
105
105
  return project;
106
106
  };