Skip to content

Commit

Permalink
refactor: 优化 git init 的判断逻辑: (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
uaenaTzx committed Mar 16, 2024
1 parent 9d31105 commit e347b55
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
11 changes: 0 additions & 11 deletions packages/core/src/utils/checkGitInstallation.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/core/src/utils/createApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ora from "ora";
import { removeDirectory, getNpmPackage } from "./fileController";
import { ProjectTypes, PackageManagers } from "./questions";
import { projectLink } from "./constants";
import isGitInstalled from "./checkGitInstallation";
import isGitInstalled from "./gitCheck";
import createSuccessInfo from "./createSuccessInfo";
import createCommitlint from "./createCommitlint";
import { createPackageJson, createTemplateFile } from "./createFile";
Expand Down
9 changes: 4 additions & 5 deletions packages/core/src/utils/createAppTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import chalk from "chalk";

// import { removeDirectory } from "./fileController";
import { projectSelect } from "./select";
import isGitInstalled from "./checkGitInstallation";
// import { createPackageJson } from "./createFile";
import gitCheck from "./gitCheck";
import PackageAPI from "./packageAPI";
import { createFiles } from "./createFiles";
import { type Preset, getFilesForProject, getNpmForPackage } from "./preset";
Expand Down Expand Up @@ -39,7 +38,7 @@ export default async function createAppTest(projectName: string, options) {
// 删除已存在文件并创建新文件
console.log(shouldContinue);

execSync(`mkdir ${rootDirectory}`);
await execSync(`mkdir ${rootDirectory}`);
}

// 获取用户选择预设
Expand Down Expand Up @@ -69,12 +68,12 @@ export default async function createAppTest(projectName: string, options) {
// 拉取模板
// todo: 新模板未开发,先模拟过程
console.log("Creating a project...");
await execSync(`mkdir ${rootDirectory}/src`);

// 初始化 Git 仓库
if (isGitInstalled()) exec("git init", { cwd: rootDirectory });
if (gitCheck(rootDirectory)) exec("git init", { cwd: rootDirectory });

// todo: 插件未开发,先模拟过程

// 安装插件至 package.json
Object.keys(packageContent.devDependencies).forEach(async (dep) => {
console.log(dep, "installed");
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/utils/fileController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import { join } from "node:path";

import { packageVersion } from "./constants";

/**
* @param directoryPath 删除文件的路径,默认 node_modules
* @param verbose 如果为true,则显示删除信息
*/
/**
* @author moment
* @param directoryPath 删除文件的路径,默认 node_modules
Expand Down
26 changes: 26 additions & 0 deletions packages/core/src/utils/gitCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { execSync } from "child_process";

// 检查全局是否安装了 Git
const hasGit = (): boolean => {
try {
execSync("git --version");
return true; // Git已安装
} catch (error) {
return false; // Git未安装
}
};

// 检查项目是否已经有 Git 环境
const hasProjectGit = (cwd: string): boolean => {
try {
execSync("git status", { stdio: "ignore", cwd });
return true;
} catch (error) {
return false;
}
};

export default function gitCheck(rootDirectory: string): boolean {
if (!hasGit()) return false;
return !hasProjectGit(rootDirectory);
}

0 comments on commit e347b55

Please sign in to comment.