Skip to content

Commit

Permalink
feat: add list
Browse files Browse the repository at this point in the history
  • Loading branch information
xie392 committed Nov 24, 2024
1 parent 319fc6f commit 8b16dfe
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 27 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"dependencies": {
"chalk": "^5.3.0",
"commander": "^12.1.0",
"degit": "^2.8.4",
"inquirer": "^12.1.0",
"ora": "^8.1.1"
"ora": "^8.1.1",
"simple-git": "^3.27.0"
},
"repository": {
"type": "git",
Expand Down
38 changes: 28 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const APP_NAME = "create-xie392-template";
export const REPO_AddRESS = "github:xie392/create-xie392-template";
export const REPO_AddRESS = "https://gitee.com/xie392/project-template.git";
19 changes: 19 additions & 0 deletions src/helpers/branch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { REPO_AddRESS } from "~/constants.js";
import { simpleGit } from "simple-git";

export async function getBranchList() {
const git = simpleGit();
const remoteBranches = await git.listRemote(["--heads", REPO_AddRESS]);
const branches = remoteBranches
.split("\n")
.filter((line) => line.includes("refs/heads/"))
.map((line) => {
const branchName = line.replace(/.*refs\/heads\//, "").trim();
return {
name: branchName,
value: branchName,
};
});

return branches.filter((v) => v.name !== "master");
}
5 changes: 4 additions & 1 deletion src/helpers/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Branch } from "~/enum.js";
import Logger from "~/utils/logger.js";
import { validatedAppName } from "~/utils/validated.js";
import inquirer from "inquirer";
import { getBranchList } from "~/helpers/branch.js";

const defaultOptions = {
name: "new-project",
Expand All @@ -10,6 +11,8 @@ const defaultOptions = {

export async function runCli() {
try {
const branches = (await getBranchList()) ?? defaultOptions.packages;

const answers = await inquirer.prompt([
{
type: "input",
Expand All @@ -22,7 +25,7 @@ export async function runCli() {
type: "list",
name: "branch",
message: "What template do you want to use?",
choices: defaultOptions.packages,
choices: branches,
},
]);
return answers;
Expand Down
22 changes: 11 additions & 11 deletions src/helpers/create-project.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Branch } from "~/enum.js";
import Logger from "~/utils/logger.js";
import degit from "degit";
import { simpleGit } from "simple-git";
import path from "path";
import chalk from "chalk";
import ora from "ora";
Expand All @@ -19,23 +19,23 @@ export async function createProject(answers: ICreateProjectOptions) {
// 结构项目名和分支名
const { name, branch } = answers;

// 使用 degit 工具从指定的 Git 仓库拉取项目模板
const emitter = degit(`${REPO_AddRESS}#${branch}`, {
cache: false,
force: true,
});
const git = simpleGit();

// 解析目标目录的绝对路径
const targetDir = path.resolve(process.cwd(), name);

Logger.log(`\nCreating project in ${chalk.green(targetDir)}...`);

// 显示进度条
// 进度条
const spinner = ora("Cloning repository...").start();

try {
// 克隆仓库到本地
await emitter.clone(targetDir);
await git.clone(REPO_AddRESS, targetDir, [
"--branch",
branch,
"--single-branch",
]);

Logger.log(`\nCreating project in ${chalk.green(targetDir)}...`);
// 显示进度条
spinner.succeed("Repository cloned successfully!");
console.log(chalk.green(`\nProject created successfully!`));
console.log(chalk.yellow(`\nNext steps:`));
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { createProject } from "~/helpers/create-project.js";
async function main() {
try {
const program = new Command();

program
.name(APP_NAME)
.description("A CLI for creating web applications with the template")
Expand All @@ -17,7 +16,6 @@ async function main() {
await createProject(answer);
process.exit(1);
});

program.parse(process.argv);
} catch (error) {
console.log(chalk.red(error.message));
Expand Down

0 comments on commit 8b16dfe

Please sign in to comment.