Skip to content

Commit c01be59

Browse files
authored
feat: check git status before initing git (#389)
1 parent dfc9556 commit c01be59

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "superplate-cli",
3-
"version": "1.19.0",
3+
"version": "1.20.0",
44
"description": "The frontend boilerplate with superpowers",
55
"license": "MIT",
66
"repository": {

src/Helper/git/index.ts

+32
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,36 @@ export const GitHelper = {
3232
throw new Error(e instanceof Error ? e.message : (e as string));
3333
}
3434
},
35+
CanGitInit: async (root: string): Promise<boolean> => {
36+
const [isGit, isHg] = await Promise.all([
37+
isInGitRepository(root),
38+
isInMercurialRepository(root),
39+
]);
40+
41+
return !isGit && !isHg;
42+
},
3543
};
44+
45+
async function isInGitRepository(root: string): Promise<boolean> {
46+
try {
47+
await promisify(exec)("git rev-parse --is-inside-work-tree", {
48+
cwd: root,
49+
});
50+
return true;
51+
} catch (_) {
52+
//
53+
}
54+
return false;
55+
}
56+
57+
async function isInMercurialRepository(root: string): Promise<boolean> {
58+
try {
59+
await promisify(exec)("hg --cwd . root", {
60+
cwd: root,
61+
});
62+
return true;
63+
} catch (_) {
64+
//
65+
}
66+
return false;
67+
}

src/saofile.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
extendBase,
44
getPluginsArray,
55
get_potential_package_managers,
6+
GitHelper,
67
handleIgnore,
78
mergeBabel,
89
mergeJSONFiles,
@@ -404,7 +405,10 @@ const saoConfig: GeneratorConfig = {
404405
* Git init and install packages
405406
*/
406407
if (!debug) {
407-
saoInstance.gitInit();
408+
const canGitInit = await GitHelper.CanGitInit(saoInstance.outDir);
409+
if (canGitInit) {
410+
saoInstance.gitInit();
411+
}
408412
await saoInstance.npmInstall({
409413
npmClient: npmClient,
410414
installArgs: ["--silent"],

0 commit comments

Comments
 (0)