This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
/
GitUtils.ts
143 lines (102 loc) · 3.53 KB
/
GitUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import {
resolve as pathResolve,
dirname as pathDirname
} from "path";
import { execa } from "execa";
import { SyncFileUtils } from "../core/SyncFileUtils";
import { LogService } from "../core/LogService";
import { isString } from "../core/types/String";
const LOG = LogService.createLogger('GitUtils');
export class GitUtils {
/**
* Returns the git directory path
*
* @param filePath
*/
static getGitDir (filePath: string): string | undefined {
let dirPath: string = filePath;
let newDirPath: string = dirPath;
do {
LOG.debug(`getGitDir: Searching git directory from `, dirPath);
dirPath = newDirPath;
if ( SyncFileUtils.fileExists(pathResolve(dirPath, '.git')) ) {
return dirPath;
}
newDirPath = pathDirname(dirPath);
} while ( newDirPath !== dirPath );
return undefined;
}
static async initGit () {
const currentGitDir = GitUtils.getGitDir(process.cwd());
if ( !currentGitDir ) {
LOG.debug(`Creating git directory`);
await GitUtils._git([ "init" ]);
} else {
LOG.warn(`Warning! Git directory already exists: `, currentGitDir);
}
}
static async addFiles (filePath: string | string[]) {
const files = isString(filePath) ? [ filePath ] : filePath;
LOG.debug(`addFiles: Adding files: `, filePath);
await GitUtils._git([ "add", ...files ]);
}
static async commit (message: string) {
LOG.debug(`commit with: `, message);
await GitUtils._git([ "commit", '-m', message ]);
}
/**
*
* git branch -M main
* @param newName
*/
static async renameMainBranch (newName: string) {
LOG.debug(`rename branch: `, newName);
await GitUtils._git([ "branch", '-M', newName ]);
}
static async addSubModule (
moduleUrl: string,
modulePath: string
) {
if ( !SyncFileUtils.fileExists(modulePath) ) {
await GitUtils._git([ "submodule", 'add', moduleUrl, modulePath ]);
} else {
LOG.warn(`Warning! Git sub module directory already exists: `, modulePath);
}
}
static async setSubModuleBranch (
modulePath: string,
moduleBranch: string
) {
await GitUtils._git(
[ "config", '-f', '.gitmodules', `submodule.${modulePath}.branch`, moduleBranch ]);
}
static async initSubModule (
moduleUrl: string,
modulePath: string,
moduleBranch: string
) {
const parentPath = pathDirname(modulePath);
// mkdir -p src/fi/hg
LOG.debug(`initSubModule: Creating: `, parentPath);
SyncFileUtils.mkdirp(parentPath);
// git submodule add git@github.com:sendanor/typescript.git src/fi/hg/ts
LOG.debug(`initSubModule: Adding submodule: `, moduleUrl, modulePath);
await GitUtils.addSubModule(moduleUrl, modulePath);
// git config -f .gitmodules submodule.src/fi/hg/ts.branch main
LOG.debug(
`initSubModule: Configuring branch for `, moduleUrl, modulePath, ': ', moduleBranch);
await GitUtils.setSubModuleBranch(modulePath, moduleBranch);
}
private static async _git (
args: string[]
): Promise<void> {
await execa(
'git',
args,
{
stdio: "inherit"
}
);
}
}