Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check git status before git init #389

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading