From 8dfed3658b9b5bf4657ac78c48b6fd98d2d55b88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=20Emir=20=C5=9Een?= Date: Tue, 7 May 2024 14:03:46 +0300 Subject: [PATCH] feat: check git status before initing git --- 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"],