Skip to content

Commit

Permalink
feat: check git status before initing git (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
aliemir authored May 8, 2024
1 parent dfc9556 commit c01be59
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "superplate-cli",
"version": "1.19.0",
"version": "1.20.0",
"description": "The frontend boilerplate with superpowers",
"license": "MIT",
"repository": {
Expand Down
32 changes: 32 additions & 0 deletions src/Helper/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,36 @@ export const GitHelper = {
throw new Error(e instanceof Error ? e.message : (e as string));
}
},
CanGitInit: async (root: string): Promise<boolean> => {
const [isGit, isHg] = await Promise.all([
isInGitRepository(root),
isInMercurialRepository(root),
]);

return !isGit && !isHg;
},
};

async function isInGitRepository(root: string): Promise<boolean> {
try {
await promisify(exec)("git rev-parse --is-inside-work-tree", {
cwd: root,
});
return true;
} catch (_) {
//
}
return false;
}

async function isInMercurialRepository(root: string): Promise<boolean> {
try {
await promisify(exec)("hg --cwd . root", {
cwd: root,
});
return true;
} catch (_) {
//
}
return false;
}
6 changes: 5 additions & 1 deletion src/saofile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
extendBase,
getPluginsArray,
get_potential_package_managers,
GitHelper,
handleIgnore,
mergeBabel,
mergeJSONFiles,
Expand Down Expand Up @@ -404,7 +405,10 @@ const saoConfig: GeneratorConfig = {
* Git init and install packages
*/
if (!debug) {
saoInstance.gitInit();
const canGitInit = await GitHelper.CanGitInit(saoInstance.outDir);
if (canGitInit) {
saoInstance.gitInit();
}
await saoInstance.npmInstall({
npmClient: npmClient,
installArgs: ["--silent"],
Expand Down

0 comments on commit c01be59

Please sign in to comment.