-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
92 lines (85 loc) · 2.79 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { IConfig } from "./generateConfig.js";
import chalkAnimation from "chalk-animation";
import fetch from "node-fetch";
import figlet from "figlet";
import fs from "fs";
import { globalConfig } from "./config.js";
import gradient from "gradient-string";
import path from "path";
import { showErrorMessage } from "./tips";
export const covertPath = (projectName: string, fileName: string) => {
const currentDirName = path.basename(process.cwd());
// @ts-ignore
if (projectName === currentDirName && globalThis?._sameDir_ !== true) {
return fileName;
}
return `./${projectName}/${fileName}`;
};
export const getExecPackageInstallPath = (projectName: string) => {
const currentDirName = path.basename(process.cwd());
// @ts-ignore
if (projectName === currentDirName && globalThis?._sameDir_ !== true) {
return ".";
}
return `./${projectName}`;
};
export const getDownloadTemplateDir = (projectName: string) => {
const currentDirName = path.basename(process.cwd());
// @ts-ignore
if (projectName === currentDirName && globalThis?._sameDir_ !== true) {
return ".";
}
return `./${projectName}`;
};
export const resolveProjectName = (projectName?: string) => {
if (!projectName) return;
if (projectName === ".") {
// @ts-ignore
globalThis._sameDir_ = false;
return path.basename(process.cwd());
}
// check if the file or directory is already exist
const isExistSameNameFileOrDir = fs.existsSync(projectName);
if (isExistSameNameFileOrDir) {
showErrorMessage(
`当前目录下存在 ${projectName} 的同名文件夹或文件,无法创建项目!`
);
process.exit(0);
}
return projectName;
};
export const showSuccessSlogan = async (answer: IConfig) => {
const { projectName } = answer;
const rainbowTitle = chalkAnimation.rainbow(
`\n\n\tEnjoy this project: ${projectName}!\n\n\tLet's do it. 🚀🚀🚀\n\n`
);
rainbowTitle.start();
await (() => new Promise((resolve) => setTimeout(resolve, 1000 * 2)));
rainbowTitle.stop();
};
export function createCustomFiglet(text: string): Promise<string> {
return new Promise((resolve, reject) => {
figlet(text, (err, data) => {
if (err) {
reject(err);
} else {
const res = gradient.pastel(data);
resolve(res);
}
});
});
}
// 获取 github 仓库的分支模板选项
export const getBranches = async () => {
const { githubName: owner, repo } = globalConfig;
const options = {
headers: {
Accept: "application/vnd.github.v3+json",
"User-Agent": "Github API Client",
},
};
const url = `https://api.github.com/repos/${owner}/${repo}/branches`;
const res = await fetch(url, options);
const data = (await res.json()) as Array<{ name: string }>;
return data.filter(({ name }) => name !== "main").map(({ name }) => name);
};