contensis-cli 1.0.0-beta.83 → 1.0.0-beta.85

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.
@@ -0,0 +1,118 @@
1
+ import giturl from 'giturl';
2
+ import hostedGitInfo from 'hosted-git-info';
3
+ import parseGitConfig from 'parse-git-config';
4
+ import path from 'path';
5
+
6
+ import { linuxSlash } from './os';
7
+ import { readFile, readFiles } from '~/providers/file-provider';
8
+
9
+ const GITLAB_CI_FILENAME = '.gitlab-ci.yml';
10
+
11
+ type GitConfig = parseGitConfig.Config;
12
+
13
+ export type GitTypes = hostedGitInfo.Hosts;
14
+
15
+ export class GitHelper {
16
+ private gitRepoPath: string;
17
+ config = {} as GitConfig;
18
+ info: hostedGitInfo | undefined;
19
+ home: string | undefined;
20
+
21
+ get ciFileName() {
22
+ return this.workflows
23
+ ? this.type === 'github'
24
+ ? this.workflows.length > 1
25
+ ? '[multiple workflows]'
26
+ : this.workflows?.[0]
27
+ : GITLAB_CI_FILENAME
28
+ : '[unknown]';
29
+ }
30
+ get name() {
31
+ return (
32
+ this.info?.project || this.home?.split('/').pop() || '[set arg --name]'
33
+ );
34
+ }
35
+ get originUrl() {
36
+ return this.config.remote.origin.url;
37
+ }
38
+ get secretsUri() {
39
+ return `${
40
+ this.type === 'github'
41
+ ? `${this.home}/settings/secrets/actions`
42
+ : `${this.home}/-/settings/ci_cd`
43
+ }`;
44
+ }
45
+ get type() {
46
+ return this.info?.type || this.hostType();
47
+ }
48
+ get workflows() {
49
+ return this.type === 'github'
50
+ ? this.githubWorkflows()
51
+ : this.gitlabWorkflow();
52
+ }
53
+ constructor(gitRepoPath: string = process.cwd()) {
54
+ this.gitRepoPath = gitRepoPath;
55
+ this.config = this.gitConfig();
56
+ this.home = giturl.parse(this.originUrl);
57
+ this.info = this.gitInfo();
58
+ // console.log(this.config);
59
+ // console.log(this.home);
60
+ // console.log(this.info);
61
+ }
62
+ gitcwd = () => path.join(this.gitRepoPath);
63
+ gitInfo = (url: string = this.originUrl) => hostedGitInfo.fromUrl(url);
64
+ hostType = (url: string = this.originUrl): GitTypes => {
65
+ if (url.includes('github.com')) return 'github';
66
+ return 'gitlab';
67
+ // if (url.includes('gitlab.com')) return 'gl';
68
+ // if (url.includes('gitlab.zengenti.com')) return 'gl';
69
+ };
70
+ gitConfig = (cwd = this.gitRepoPath) => {
71
+ // Find .git/config in project cwd
72
+ const config = parseGitConfig.sync({
73
+ path: '.git/config',
74
+ expandKeys: true,
75
+ });
76
+ // console.log(cwd, config);
77
+ if (Object.keys(config || {}).length) return config;
78
+
79
+ // Recursively check the directory heirarchy for existance of a .git/config
80
+ const pathParts = linuxSlash(cwd).split('/');
81
+ for (let i = 1; i <= pathParts.length; i++) {
82
+ const relPath = `${Array(i).fill('..').join('/')}/.git/config`;
83
+ // Does not appear to work when using a shortened cwd, using relative path instead
84
+ const config = parseGitConfig.sync({
85
+ path: relPath,
86
+ expandKeys: true,
87
+ });
88
+ // console.log(relPath, config);
89
+ if (Object.keys(config || {}).length) {
90
+ this.gitRepoPath = path.join(
91
+ this.gitRepoPath,
92
+ Array(i).fill('..').join('/')
93
+ );
94
+ return config;
95
+ }
96
+ }
97
+ return config;
98
+ };
99
+ githubWorkflows = () => {
100
+ const workflowPath = path.join(this.gitcwd(), '.github/workflows');
101
+ const workflowFiles = readFiles(workflowPath, false);
102
+ // console.log('gh workflows: ', workflowFiles);
103
+ const addFolderSuffix = (files: string[]) =>
104
+ files.map(f => `.github/workflows/${f}`);
105
+
106
+ if (workflowFiles.some(f => f.includes('build')))
107
+ return addFolderSuffix(workflowFiles.filter(f => f.includes('build')));
108
+ return addFolderSuffix(workflowFiles);
109
+ };
110
+ gitlabWorkflow = (ciFileName = GITLAB_CI_FILENAME) => {
111
+ const workflowPath = this.gitcwd();
112
+ const workflowFilePath = path.join(workflowPath, ciFileName);
113
+ const workflowFile = readFile(workflowFilePath);
114
+ // console.log(ciFileName, workflowFile);
115
+
116
+ return workflowFile;
117
+ };
118
+ }
package/src/util/os.ts ADDED
@@ -0,0 +1,7 @@
1
+ import os from 'os';
2
+
3
+ export const winSlash = (str: string) =>
4
+ os.platform() === 'win32' ? str.replaceAll('/', '\\') : str;
5
+
6
+ export const linuxSlash = (str: string) =>
7
+ os.platform() === 'win32' ? str.replaceAll('\\', '/') : str;
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const LIB_VERSION = "1.0.0-beta.83";
1
+ export const LIB_VERSION = "1.0.0-beta.85";