From c01be5929a1eb24f5dc505e764e093df8da887bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=20Emir=20=C5=9Een?= Date: Wed, 8 May 2024 14:30:12 +0300 Subject: [PATCH] feat: check git status before initing git (#389) --- package.json | 2 +- src/Helper/git/index.ts | 32 ++++++++++++++++++++++++++++++++ src/saofile.ts | 6 +++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index b6045e9d..bf2c4138 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "superplate-cli", - "version": "1.19.0", + "version": "1.20.0", "description": "The frontend boilerplate with superpowers", "license": "MIT", "repository": { diff --git a/src/Helper/git/index.ts b/src/Helper/git/index.ts index 44fd39f8..d9f3b190 100644 --- a/src/Helper/git/index.ts +++ b/src/Helper/git/index.ts @@ -32,4 +32,36 @@ export const GitHelper = { throw new Error(e instanceof Error ? e.message : (e as string)); } }, + CanGitInit: async (root: string): Promise => { + const [isGit, isHg] = await Promise.all([ + isInGitRepository(root), + isInMercurialRepository(root), + ]); + + return !isGit && !isHg; + }, }; + +async function isInGitRepository(root: string): Promise { + try { + await promisify(exec)("git rev-parse --is-inside-work-tree", { + cwd: root, + }); + return true; + } catch (_) { + // + } + return false; +} + +async function isInMercurialRepository(root: string): Promise { + try { + await promisify(exec)("hg --cwd . root", { + cwd: root, + }); + return true; + } catch (_) { + // + } + return false; +} diff --git a/src/saofile.ts b/src/saofile.ts index cdd7a492..e25c0122 100644 --- a/src/saofile.ts +++ b/src/saofile.ts @@ -3,6 +3,7 @@ import { extendBase, getPluginsArray, get_potential_package_managers, + GitHelper, handleIgnore, mergeBabel, mergeJSONFiles, @@ -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"],