@lonewolfyx/setup 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.mjs +213 -91
- package/dist/cli.mjs.map +1 -1
- package/package.json +1 -1
package/dist/cli.mjs
CHANGED
|
@@ -1,21 +1,12 @@
|
|
|
1
1
|
import { access, mkdir, readdir, rm, writeFile } from "node:fs/promises";
|
|
2
|
-
import path from "node:path";
|
|
3
2
|
import { confirm, intro, isCancel, outro, spinner } from "@clack/prompts";
|
|
4
3
|
import { createMain, defineCommand } from "citty";
|
|
5
4
|
import { readPackageJSON, writePackageJSON, writeTSConfig } from "pkg-types";
|
|
5
|
+
import path, { resolve } from "node:path";
|
|
6
6
|
import { x } from "tinyexec";
|
|
7
|
-
//#region package.json
|
|
8
|
-
var name = "@lonewolfyx/setup";
|
|
9
|
-
var version = "0.0.5";
|
|
10
|
-
var description = "Instant project boilerplate setup";
|
|
11
|
-
//#endregion
|
|
12
7
|
//#region src/task/addGitHooksConfig.ts
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
* @param cwd 当前工作目录
|
|
16
|
-
* @param packageJsonPath package.json 文件路径
|
|
17
|
-
*/
|
|
18
|
-
async function addGitHooksConfig(cwd, packageJsonPath) {
|
|
8
|
+
async function addGitHooksConfig(config) {
|
|
9
|
+
const { cwd, packageJsonPath } = config;
|
|
19
10
|
const newPackageJson = await readPackageJSON(cwd);
|
|
20
11
|
newPackageJson.simpleGitHooks = {
|
|
21
12
|
"pre-commit": "npx lint-staged",
|
|
@@ -44,12 +35,8 @@ async function clearDirectory(dirPath) {
|
|
|
44
35
|
}
|
|
45
36
|
//#endregion
|
|
46
37
|
//#region src/task/configPackageJson.ts
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
* @param cwd 当前工作目录
|
|
50
|
-
* @param packageJsonPath package.json 文件路径
|
|
51
|
-
*/
|
|
52
|
-
async function configPackageJson(cwd, packageJsonPath) {
|
|
38
|
+
async function configPackageJson(config) {
|
|
39
|
+
const { cwd, packageJsonPath } = config;
|
|
53
40
|
const packageJson = await readPackageJSON(cwd);
|
|
54
41
|
if (packageJson.main) delete packageJson.main;
|
|
55
42
|
packageJson.type = "module";
|
|
@@ -63,12 +50,8 @@ async function configPackageJson(cwd, packageJsonPath) {
|
|
|
63
50
|
}
|
|
64
51
|
//#endregion
|
|
65
52
|
//#region src/task/createEslintConfig.ts
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
* @param cwd 当前工作目录
|
|
69
|
-
*/
|
|
70
|
-
async function createEslintConfig(cwd) {
|
|
71
|
-
await writeFile(path.join(cwd, "eslint.config.ts"), `import type { Linter } from 'eslint'
|
|
53
|
+
async function createEslintConfig(config) {
|
|
54
|
+
await writeFile(path.join(config.cwd, "eslint.config.ts"), `import type { Linter } from 'eslint'
|
|
72
55
|
import antfu from '@antfu/eslint-config'
|
|
73
56
|
|
|
74
57
|
const config = antfu({
|
|
@@ -94,47 +77,163 @@ export default config`);
|
|
|
94
77
|
}
|
|
95
78
|
//#endregion
|
|
96
79
|
//#region src/task/createGitHooks.ts
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
* @param cwd 当前工作目录
|
|
100
|
-
*/
|
|
101
|
-
async function createGitHooks(cwd) {
|
|
102
|
-
const scriptsDir = path.join(cwd, "scripts");
|
|
80
|
+
async function createGitHooks(config) {
|
|
81
|
+
const scriptsDir = path.join(config.cwd, "scripts");
|
|
103
82
|
await mkdir(scriptsDir, { recursive: true });
|
|
104
83
|
await writeFile(path.join(scriptsDir, "verify-commit.js"), "import { readFileSync } from 'node:fs'\nimport path from 'node:path'\n// @ts-check\nimport pico from 'picocolors'\n\nconst msgPath = path.resolve('.git/COMMIT_EDITMSG')\nconst msg = readFileSync(msgPath, 'utf-8').trim()\n\nconst commitRE\n = /^(revert: )?(feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release)(\\(.+\\))?(!)?: .{1,50}/\n\nif (!commitRE.test(msg)) {\n console.log()\n console.error(\n ` ${pico.white(pico.bgRed(' ERROR '))} ${pico.red(\n `invalid commit message format.`,\n )}\\n\\n${\n pico.red(\n ` Proper commit message format is required for automated changelog generation. Examples:\\n\\n`,\n )\n } ${pico.green(`feat(compiler): add 'comments' option`)}\\n`\n + ` ${pico.green(\n `fix(v-model): handle events on blur (close #28)`,\n )}\\n`\n + ` ${pico.green(\n `feat(api)!: remove legacy token endpoint`,\n )}\\n\\n${\n pico.red(` See .github/commit-convention.md for more details.\\n`)}`,\n )\n process.exit(1)\n}\n");
|
|
105
84
|
}
|
|
106
85
|
//#endregion
|
|
107
|
-
//#region src/task/
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
86
|
+
//#region src/task/createGithubWorkflows.ts
|
|
87
|
+
const workflows = [
|
|
88
|
+
{
|
|
89
|
+
filename: "autofix.yml",
|
|
90
|
+
content: `name: autofix.ci
|
|
91
|
+
|
|
92
|
+
on:
|
|
93
|
+
pull_request:
|
|
94
|
+
permissions:
|
|
95
|
+
contents: write
|
|
96
|
+
|
|
97
|
+
jobs:
|
|
98
|
+
autofix:
|
|
99
|
+
runs-on: ubuntu-latest
|
|
100
|
+
timeout-minutes: 10
|
|
101
|
+
|
|
102
|
+
steps:
|
|
103
|
+
- uses: actions/checkout@v4
|
|
104
|
+
|
|
105
|
+
- name: Use Node.js lts/*
|
|
106
|
+
uses: actions/setup-node@v4
|
|
107
|
+
with:
|
|
108
|
+
node-version: lts/*
|
|
109
|
+
|
|
110
|
+
- name: Install pnpm
|
|
111
|
+
uses: pnpm/action-setup@v4
|
|
112
|
+
|
|
113
|
+
- name: Install
|
|
114
|
+
run: pnpm i
|
|
115
|
+
|
|
116
|
+
- name: Lint
|
|
117
|
+
run: pnpm run lint:fix
|
|
118
|
+
|
|
119
|
+
- uses: autofix-ci/action@635ffb0c9798bd160680f18fd73371e355b85f27
|
|
120
|
+
`
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
filename: "ci.yml",
|
|
124
|
+
content: `name: CI
|
|
125
|
+
|
|
126
|
+
on:
|
|
127
|
+
pull_request:
|
|
128
|
+
|
|
129
|
+
jobs:
|
|
130
|
+
lint:
|
|
131
|
+
runs-on: ubuntu-latest
|
|
132
|
+
steps:
|
|
133
|
+
- uses: actions/checkout@v4
|
|
134
|
+
|
|
135
|
+
- name: Install pnpm
|
|
136
|
+
uses: pnpm/action-setup@v4
|
|
137
|
+
|
|
138
|
+
- name: Set node
|
|
139
|
+
uses: actions/setup-node@v4
|
|
140
|
+
with:
|
|
141
|
+
node-version: lts/*
|
|
142
|
+
cache: pnpm
|
|
143
|
+
|
|
144
|
+
- name: Install
|
|
145
|
+
run: pnpm i
|
|
146
|
+
|
|
147
|
+
- name: Lint
|
|
148
|
+
run: pnpm run lint
|
|
149
|
+
`
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
filename: "pr-contributor-welcome.yml",
|
|
153
|
+
content: `name: PullRequest Contributor Welcome
|
|
154
|
+
|
|
155
|
+
on:
|
|
156
|
+
pull_request_target:
|
|
157
|
+
types:
|
|
158
|
+
- closed
|
|
159
|
+
# paths:
|
|
160
|
+
# - 'components/**'
|
|
161
|
+
|
|
162
|
+
permissions:
|
|
163
|
+
contents: read
|
|
164
|
+
|
|
165
|
+
jobs:
|
|
166
|
+
comment:
|
|
167
|
+
permissions:
|
|
168
|
+
issues: write # for actions-cool/maintain-one-comment to modify or create issue comments
|
|
169
|
+
pull-requests: write # for actions-cool/maintain-one-comment to modify or create PR comments
|
|
170
|
+
if: github.event.pull_request.merged == true && github.repository == 'lonewolfyx/xxxx'
|
|
171
|
+
runs-on: ubuntu-latest
|
|
172
|
+
steps:
|
|
173
|
+
- name: get commit count
|
|
174
|
+
id: get_commit_count
|
|
175
|
+
run: |
|
|
176
|
+
PR_AUTHOR=$(echo "\${{ github.event.pull_request.user.login }}")
|
|
177
|
+
RESULT_DATA=$(curl -s "https://api.github.com/repos/\${{ github.repository }}/commits?author=\${PR_AUTHOR}&per_page=5")
|
|
178
|
+
DATA_LENGTH=$(echo \$RESULT_DATA | jq 'if type == "array" then length else 0 end')
|
|
179
|
+
echo "COUNT=\$DATA_LENGTH" >> \$GITHUB_OUTPUT
|
|
180
|
+
- name: Comment on PR
|
|
181
|
+
if: steps.get_commit_count.outputs.COUNT < 3
|
|
182
|
+
uses: actions-cool/maintain-one-comment@v3
|
|
183
|
+
with:
|
|
184
|
+
token: \${{ secrets.GITHUB_TOKEN }}
|
|
185
|
+
body: |
|
|
186
|
+
🎉 Thx for the PR @\${{ github.event.pull_request.user.login }}
|
|
187
|
+
|
|
188
|
+
<!-- WELCOME_CONTRIBUTION -->
|
|
189
|
+
body-include: <!-- WELCOME_CONTRIBUTION -->
|
|
190
|
+
`
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
filename: "release.yml",
|
|
194
|
+
content: `name: Release
|
|
195
|
+
|
|
196
|
+
permissions:
|
|
197
|
+
contents: write
|
|
198
|
+
|
|
199
|
+
on:
|
|
200
|
+
push:
|
|
201
|
+
tags:
|
|
202
|
+
- 'v*'
|
|
203
|
+
|
|
204
|
+
jobs:
|
|
205
|
+
release:
|
|
206
|
+
runs-on: ubuntu-latest
|
|
207
|
+
steps:
|
|
208
|
+
- uses: actions/checkout@v4
|
|
209
|
+
with:
|
|
210
|
+
fetch-depth: 0
|
|
211
|
+
|
|
212
|
+
- uses: actions/setup-node@v4
|
|
213
|
+
with:
|
|
214
|
+
node-version: lts/*
|
|
215
|
+
|
|
216
|
+
- run: npx genereleaselog
|
|
217
|
+
env:
|
|
218
|
+
GITHUB_TOKEN: \${{secrets.GITHUB_TOKEN}}
|
|
219
|
+
`
|
|
118
220
|
}
|
|
221
|
+
];
|
|
222
|
+
async function createGithubWorkflows(config) {
|
|
223
|
+
const workflowsDir = path.join(config.cwd, ".github", "workflows");
|
|
224
|
+
await mkdir(workflowsDir, { recursive: true });
|
|
225
|
+
for (const { filename, content } of workflows) await writeFile(path.join(workflowsDir, filename), content);
|
|
119
226
|
}
|
|
120
227
|
//#endregion
|
|
121
228
|
//#region src/task/createPackageJson.ts
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
*/
|
|
126
|
-
async function createPackageJson(cwd) {
|
|
127
|
-
const hasPnpm = await hasPackageManager("pnpm");
|
|
128
|
-
await x(hasPnpm ? "pnpm" : "npm", hasPnpm ? ["init"] : ["init", "-y"], { nodeOptions: { cwd } });
|
|
229
|
+
async function createPackageJson(config) {
|
|
230
|
+
const { cwd, packageManager } = config;
|
|
231
|
+
await x(packageManager, packageManager === "pnpm" ? ["init"] : ["init", "-y"], { nodeOptions: { cwd } });
|
|
129
232
|
}
|
|
130
233
|
//#endregion
|
|
131
234
|
//#region src/task/createTsConfig.ts
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
* @param cwd 当前工作目录
|
|
135
|
-
*/
|
|
136
|
-
async function createTsConfig(cwd) {
|
|
137
|
-
await writeTSConfig(path.resolve(cwd, "tsconfig.json"), {
|
|
235
|
+
async function createTsConfig(config) {
|
|
236
|
+
await writeTSConfig(resolve(config.cwd, "tsconfig.json"), {
|
|
138
237
|
extends: "@lonewolfyx/tsconfig/tsconfig.lib.json",
|
|
139
238
|
compilerOptions: {
|
|
140
239
|
baseUrl: "./",
|
|
@@ -152,11 +251,8 @@ async function createTsConfig(cwd) {
|
|
|
152
251
|
}
|
|
153
252
|
//#endregion
|
|
154
253
|
//#region src/task/installDevDeps.ts
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
* @param cwd 当前工作目录
|
|
158
|
-
*/
|
|
159
|
-
async function installDevDeps(cwd) {
|
|
254
|
+
async function installDevDeps(config) {
|
|
255
|
+
const { cwd, packageManager } = config;
|
|
160
256
|
const devDeps = [
|
|
161
257
|
"eslint",
|
|
162
258
|
"@antfu/eslint-config",
|
|
@@ -170,8 +266,7 @@ async function installDevDeps(cwd) {
|
|
|
170
266
|
"tsx",
|
|
171
267
|
"tsdown"
|
|
172
268
|
];
|
|
173
|
-
|
|
174
|
-
await x(hasPnpm ? "pnpm" : "npm", hasPnpm ? [
|
|
269
|
+
await x(packageManager, packageManager === "pnpm" ? [
|
|
175
270
|
"add",
|
|
176
271
|
"-D",
|
|
177
272
|
...devDeps
|
|
@@ -182,6 +277,59 @@ async function installDevDeps(cwd) {
|
|
|
182
277
|
], { nodeOptions: { cwd } });
|
|
183
278
|
}
|
|
184
279
|
//#endregion
|
|
280
|
+
//#region package.json
|
|
281
|
+
var name = "@lonewolfyx/setup";
|
|
282
|
+
var version = "0.0.6";
|
|
283
|
+
var description = "Instant project boilerplate setup";
|
|
284
|
+
//#endregion
|
|
285
|
+
//#region src/utils.ts
|
|
286
|
+
/**
|
|
287
|
+
* 检测包管理器是否存在
|
|
288
|
+
* @param command 包管理器命令名称
|
|
289
|
+
*/
|
|
290
|
+
async function hasPackageManager(command) {
|
|
291
|
+
try {
|
|
292
|
+
await x(command, ["--version"], { throwOnError: true });
|
|
293
|
+
return true;
|
|
294
|
+
} catch {
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
//#endregion
|
|
299
|
+
//#region src/config.ts
|
|
300
|
+
async function resolveConfig(cwd) {
|
|
301
|
+
const hasPnpm = await hasPackageManager("pnpm");
|
|
302
|
+
return {
|
|
303
|
+
cwd,
|
|
304
|
+
packageJsonPath: path.join(cwd, "package.json"),
|
|
305
|
+
packageManager: hasPnpm ? "pnpm" : "npm"
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
//#endregion
|
|
309
|
+
//#region src/schedule.ts
|
|
310
|
+
function schedule() {
|
|
311
|
+
const s = spinner();
|
|
312
|
+
const tasks = [];
|
|
313
|
+
const chain = {
|
|
314
|
+
step(startMessage, fn, doneMessage) {
|
|
315
|
+
tasks.push({
|
|
316
|
+
startMessage,
|
|
317
|
+
fn,
|
|
318
|
+
doneMessage
|
|
319
|
+
});
|
|
320
|
+
return chain;
|
|
321
|
+
},
|
|
322
|
+
async done() {
|
|
323
|
+
for (const { startMessage, fn, doneMessage } of tasks) {
|
|
324
|
+
s.start(startMessage);
|
|
325
|
+
await fn();
|
|
326
|
+
s.stop(doneMessage);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
return chain;
|
|
331
|
+
}
|
|
332
|
+
//#endregion
|
|
185
333
|
//#region src/cli.ts
|
|
186
334
|
createMain(defineCommand({
|
|
187
335
|
meta: {
|
|
@@ -203,42 +351,16 @@ createMain(defineCommand({
|
|
|
203
351
|
} },
|
|
204
352
|
async run({ args }) {
|
|
205
353
|
const cwd = args.cwd;
|
|
206
|
-
const
|
|
207
|
-
|
|
208
|
-
const progress = spinner();
|
|
209
|
-
if (packageJsonExists) {
|
|
354
|
+
const config = await resolveConfig(cwd);
|
|
355
|
+
if (await access(config.packageJsonPath).then(() => true).catch(() => false)) {
|
|
210
356
|
const shouldOverwrite = await confirm({ message: "检测到当前目录已存在项目,是否覆盖创建新项目?(将清空当前目录下所有文件)" });
|
|
211
357
|
if (isCancel(shouldOverwrite) || !shouldOverwrite) {
|
|
212
358
|
outro("已取消操作");
|
|
213
359
|
return;
|
|
214
360
|
}
|
|
215
|
-
|
|
216
|
-
await clearDirectory(cwd);
|
|
217
|
-
progress.stop("目录已清空");
|
|
361
|
+
await clearDirectory(config.cwd);
|
|
218
362
|
}
|
|
219
|
-
|
|
220
|
-
await hasPackageManager("pnpm");
|
|
221
|
-
progress.message("项目初始化...");
|
|
222
|
-
await createPackageJson(cwd);
|
|
223
|
-
progress.stop("package.json 创建完成");
|
|
224
|
-
progress.start("正在配置 package.json...");
|
|
225
|
-
await configPackageJson(cwd, packageJsonPath);
|
|
226
|
-
progress.stop("package.json 配置完成");
|
|
227
|
-
progress.start("正在安装开发依赖...");
|
|
228
|
-
await installDevDeps(cwd);
|
|
229
|
-
progress.stop("开发依赖安装完成");
|
|
230
|
-
progress.start("正在创建 git hooks 配置文件...");
|
|
231
|
-
await createGitHooks(cwd);
|
|
232
|
-
progress.stop("git hooks 配置文件创建完成");
|
|
233
|
-
progress.start("正在创建 tsconfig.json...");
|
|
234
|
-
await createTsConfig(cwd);
|
|
235
|
-
progress.stop("tsconfig.json 创建完成");
|
|
236
|
-
progress.start("正在添加 git hooks 配置...");
|
|
237
|
-
await addGitHooksConfig(cwd, packageJsonPath);
|
|
238
|
-
progress.stop("git hooks 配置添加完成");
|
|
239
|
-
progress.start("正在创建 ESLint 配置文件...");
|
|
240
|
-
await createEslintConfig(cwd);
|
|
241
|
-
progress.stop("ESLint 配置文件创建完成");
|
|
363
|
+
await schedule().step("正在创建 package.json...", () => createPackageJson(config), "package.json 创建完成").step("正在配置 package.json...", () => configPackageJson(config), "package.json 配置完成").step("正在安装开发依赖...", () => installDevDeps(config), "开发依赖安装完成").step("正在创建 git hooks 配置文件...", () => createGitHooks(config), "git hooks 配置文件创建完成").step("正在创建 tsconfig.json...", () => createTsConfig(config), "tsconfig.json 创建完成").step("正在添加 git hooks 配置...", () => addGitHooksConfig(config), "git hooks 配置添加完成").step("正在创建 ESLint 配置文件...", () => createEslintConfig(config), "ESLint 配置文件创建完成").step("正在创建 GitHub Workflows...", () => createGithubWorkflows(config), "GitHub Workflows 创建完成").done();
|
|
242
364
|
}
|
|
243
365
|
}))({});
|
|
244
366
|
//#endregion
|
package/dist/cli.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.mjs","names":[],"sources":["../package.json","../src/task/addGitHooksConfig.ts","../src/task/clearDirectory.ts","../src/task/configPackageJson.ts","../src/task/createEslintConfig.ts","../src/task/createGitHooks.ts","../src/task/utils.ts","../src/task/createPackageJson.ts","../src/task/createTsConfig.ts","../src/task/installDevDeps.ts","../src/cli.ts"],"sourcesContent":["","import { readPackageJSON, writePackageJSON } from 'pkg-types'\n\n/**\n * 添加 git hooks 配置到 package.json\n * @param cwd 当前工作目录\n * @param packageJsonPath package.json 文件路径\n */\nexport async function addGitHooksConfig(cwd: string, packageJsonPath: string): Promise<void> {\n const newPackageJson = await readPackageJSON(cwd)\n newPackageJson.simpleGitHooks = {\n 'pre-commit': 'npx lint-staged',\n 'commit-msg': 'node scripts/verify-commit.js',\n }\n newPackageJson.lintStaged = {\n '*': 'eslint --fix',\n }\n\n await writePackageJSON(packageJsonPath, newPackageJson)\n}\n","import { readdir, rm } from 'node:fs/promises'\nimport path from 'node:path'\n\n/**\n * 清空目录内容,保留 .git 目录\n * @param dirPath 目录路径\n */\nexport async function clearDirectory(dirPath: string): Promise<void> {\n const entries = await readdir(dirPath, { withFileTypes: true })\n\n for (const entry of entries) {\n // 保留 .git 目录\n if (entry.name === '.git') {\n continue\n }\n\n const fullPath = path.join(dirPath, entry.name)\n\n if (entry.isDirectory()) {\n await rm(fullPath, { recursive: true, force: true })\n }\n else {\n await rm(fullPath, { force: true })\n }\n }\n}\n","import { readPackageJSON, writePackageJSON } from 'pkg-types'\n\n/**\n * 配置 package.json,添加 \"type\": \"module\"\n * @param cwd 当前工作目录\n * @param packageJsonPath package.json 文件路径\n */\nexport async function configPackageJson(cwd: string, packageJsonPath: string): Promise<void> {\n const packageJson = await readPackageJSON(cwd)\n\n if (packageJson.main) {\n delete packageJson.main\n }\n\n packageJson.type = 'module'\n packageJson.scripts = {\n ...packageJson.scripts,\n 'lint': 'eslint .',\n 'lint:fix': 'eslint --fix',\n 'prepare': 'simple-git-hooks',\n }\n\n await writePackageJSON(packageJsonPath, packageJson)\n}\n","import { writeFile } from 'node:fs/promises'\nimport path from 'node:path'\n\n/**\n * 创建 ESLint 配置文件\n * @param cwd 当前工作目录\n */\nexport async function createEslintConfig(cwd: string): Promise<void> {\n const eslintConfigContent = `import type { Linter } from 'eslint'\nimport antfu from '@antfu/eslint-config'\n\nconst config = antfu({\n type: 'lib',\n stylistic: {\n indent: 4,\n quotes: 'single',\n },\n rules: {\n 'no-console': 'off',\n 'node/prefer-global/process': 'off',\n 'antfu/top-level-function': 'off',\n 'regexp/no-unused-capturing-group': 'off',\n },\n yaml: {\n overrides: {\n 'yaml/indent': ['error', 2],\n },\n },\n}) as Linter.Config\n\nexport default config`\n\n const eslintConfigPath = path.join(cwd, 'eslint.config.ts')\n await writeFile(eslintConfigPath, eslintConfigContent)\n}\n","import { mkdir, writeFile } from 'node:fs/promises'\nimport path from 'node:path'\n\n/**\n * 创建 git hooks 配置文件\n * @param cwd 当前工作目录\n */\nexport async function createGitHooks(cwd: string): Promise<void> {\n const scriptsDir = path.join(cwd, 'scripts')\n await mkdir(scriptsDir, { recursive: true })\n\n const verifyCommitPath = path.join(scriptsDir, 'verify-commit.js')\n const verifyCommitContent = 'import { readFileSync } from \\'node:fs\\'\\n'\n + 'import path from \\'node:path\\'\\n'\n + '// @ts-check\\n'\n + 'import pico from \\'picocolors\\'\\n'\n + '\\n'\n + 'const msgPath = path.resolve(\\'.git/COMMIT_EDITMSG\\')\\n'\n + 'const msg = readFileSync(msgPath, \\'utf-8\\').trim()\\n'\n + '\\n'\n + 'const commitRE\\n'\n + ' = /^(revert: )?(feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release)(\\\\(.+\\\\))?(!)?: .{1,50}/\\n'\n + '\\n'\n + 'if (!commitRE.test(msg)) {\\n'\n + ' console.log()\\n'\n + ' console.error(\\n'\n // eslint-disable-next-line no-template-curly-in-string\n + ' ` ${pico.white(pico.bgRed(\\' ERROR \\'))} ${pico.red(\\n'\n + ' `invalid commit message format.`,\\n'\n + ' )}\\\\n\\\\n${\\n'\n + ' pico.red(\\n'\n + ' ` Proper commit message format is required for automated changelog generation. Examples:\\\\n\\\\n`,\\n'\n + ' )\\n'\n // eslint-disable-next-line no-template-curly-in-string\n + ' } ${pico.green(`feat(compiler): add \\'comments\\' option`)}\\\\n`\\n'\n + ' + ` ${pico.green(\\n'\n + ' `fix(v-model): handle events on blur (close #28)`,\\n'\n + ' )}\\\\n`\\n'\n + ' + ` ${pico.green(\\n'\n + ' `feat(api)!: remove legacy token endpoint`,\\n'\n + ' )}\\\\n\\\\n${\\n'\n + ' pico.red(` See .github/commit-convention.md for more details.\\\\n`)}`,\\n'\n + ' )\\n'\n + ' process.exit(1)\\n'\n + '}\\n'\n\n await writeFile(verifyCommitPath, verifyCommitContent)\n}\n","import { x } from 'tinyexec'\n\n/**\n * 检测包管理器是否存在\n * @param command 包管理器命令名称\n */\nexport async function hasPackageManager(command: string): Promise<boolean> {\n try {\n await x(command, ['--version'], { throwOnError: true })\n return true\n }\n catch {\n return false\n }\n}\n","import { x } from 'tinyexec'\nimport { hasPackageManager } from './utils'\n\n/**\n * 创建 package.json 文件\n * @param cwd 当前工作目录\n */\nexport async function createPackageJson(cwd: string): Promise<void> {\n const hasPnpm = await hasPackageManager('pnpm')\n\n await x(hasPnpm ? 'pnpm' : 'npm', hasPnpm ? ['init'] : ['init', '-y'], { nodeOptions: { cwd } })\n}\n","import path from 'node:path'\nimport { writeTSConfig } from 'pkg-types'\n\n/**\n * 创建 tsconfig.json 文件\n * @param cwd 当前工作目录\n */\nexport async function createTsConfig(cwd: string): Promise<void> {\n const tsconfigConfig = {\n extends: '@lonewolfyx/tsconfig/tsconfig.lib.json',\n compilerOptions: {\n baseUrl: './',\n paths: {\n '@/*': [\n './src/*',\n ],\n '#/*': [\n './src/types/*',\n './types/*',\n ],\n },\n resolveJsonModule: true,\n types: [\n 'node',\n ],\n noUnusedLocals: false,\n noUnusedParameters: false,\n declarationMap: false,\n },\n }\n\n await writeTSConfig(path.resolve(cwd, 'tsconfig.json'), tsconfigConfig)\n}\n","import { x } from 'tinyexec'\nimport { hasPackageManager } from './utils'\n\n/**\n * 安装开发依赖\n * @param cwd 当前工作目录\n */\nexport async function installDevDeps(cwd: string): Promise<void> {\n const devDeps = [\n 'eslint',\n '@antfu/eslint-config',\n 'jiti',\n 'typescript',\n '@lonewolfyx/tsconfig',\n '@types/node',\n 'lint-staged',\n 'simple-git-hooks',\n 'picocolors',\n 'tsx',\n 'tsdown',\n ]\n\n const hasPnpm = await hasPackageManager('pnpm')\n const addCmd = hasPnpm ? 'pnpm' : 'npm'\n const addArgs = hasPnpm\n ? ['add', '-D', ...devDeps]\n : ['install', '-D', ...devDeps]\n\n await x(addCmd, addArgs, { nodeOptions: { cwd } })\n}\n","import { access } from 'node:fs/promises'\nimport path from 'node:path'\nimport { confirm, intro, isCancel, outro, spinner } from '@clack/prompts'\nimport { createMain, defineCommand } from 'citty'\nimport { description, name, version } from '../package.json'\nimport { addGitHooksConfig } from './task/addGitHooksConfig'\nimport { clearDirectory } from './task/clearDirectory'\nimport { configPackageJson } from './task/configPackageJson'\nimport { createEslintConfig } from './task/createEslintConfig'\nimport { createGitHooks } from './task/createGitHooks'\nimport { createPackageJson } from './task/createPackageJson'\nimport { createTsConfig } from './task/createTsConfig'\nimport { installDevDeps } from './task/installDevDeps'\nimport { hasPackageManager } from './task/utils'\n\nconst command = defineCommand({\n meta: {\n name,\n version,\n description,\n },\n setup() {\n intro('项目初始化')\n },\n cleanup() {\n outro('Done. 项目初始化完成')\n },\n args: {\n cwd: {\n type: 'string',\n description: 'Current working directory',\n alias: 'c',\n default: process.cwd(),\n },\n },\n async run({ args }) {\n const cwd = args.cwd as string\n const packageJsonPath = path.join(cwd, 'package.json')\n\n // 检测 package.json 是否存在\n const packageJsonExists = await access(packageJsonPath)\n .then(() => true)\n .catch(() => false)\n\n const progress = spinner()\n\n if (packageJsonExists) {\n const shouldOverwrite = await confirm({\n message: '检测到当前目录已存在项目,是否覆盖创建新项目?(将清空当前目录下所有文件)',\n })\n\n if (isCancel(shouldOverwrite) || !shouldOverwrite) {\n outro('已取消操作')\n return\n }\n\n // 清空目录内容\n progress.start('正在清空目录...')\n await clearDirectory(cwd)\n progress.stop('目录已清空')\n }\n\n // 创建 package.json 文件\n progress.start('正在检测包管理器...')\n\n const hasPnpm = await hasPackageManager('pnpm')\n progress.message('项目初始化...')\n\n await createPackageJson(cwd)\n\n progress.stop('package.json 创建完成')\n\n // 添加 \"type\": \"module\" 配置\n progress.start('正在配置 package.json...')\n\n await configPackageJson(cwd, packageJsonPath)\n\n progress.stop('package.json 配置完成')\n\n // 安装开发依赖\n progress.start('正在安装开发依赖...')\n\n await installDevDeps(cwd)\n\n progress.stop('开发依赖安装完成')\n\n // 创建 scripts/verify-commit.js 文件\n progress.start('正在创建 git hooks 配置文件...')\n\n await createGitHooks(cwd)\n\n progress.stop('git hooks 配置文件创建完成')\n\n // 创建 tsconfig.json 文件\n progress.start('正在创建 tsconfig.json...')\n\n await createTsConfig(cwd)\n\n progress.stop('tsconfig.json 创建完成')\n\n // 添加 git hooks 配置到 package.json\n progress.start('正在添加 git hooks 配置...')\n\n await addGitHooksConfig(cwd, packageJsonPath)\n\n progress.stop('git hooks 配置添加完成')\n\n // 创建 eslint.config.ts 文件\n progress.start('正在创建 ESLint 配置文件...')\n\n await createEslintConfig(cwd)\n\n progress.stop('ESLint 配置文件创建完成')\n },\n})\n\ncreateMain(command)({})\n"],"mappings":";;;;;;;;;;;;;;;;;ACOA,eAAsB,kBAAkB,KAAa,iBAAwC;CACzF,MAAM,iBAAiB,MAAM,gBAAgB,IAAI;AACjD,gBAAe,iBAAiB;EAC5B,cAAc;EACd,cAAc;EACjB;AACD,gBAAe,aAAa,EACxB,KAAK,gBACR;AAED,OAAM,iBAAiB,iBAAiB,eAAe;;;;;;;;ACV3D,eAAsB,eAAe,SAAgC;CACjE,MAAM,UAAU,MAAM,QAAQ,SAAS,EAAE,eAAe,MAAM,CAAC;AAE/D,MAAK,MAAM,SAAS,SAAS;AAEzB,MAAI,MAAM,SAAS,OACf;EAGJ,MAAM,WAAW,KAAK,KAAK,SAAS,MAAM,KAAK;AAE/C,MAAI,MAAM,aAAa,CACnB,OAAM,GAAG,UAAU;GAAE,WAAW;GAAM,OAAO;GAAM,CAAC;MAGpD,OAAM,GAAG,UAAU,EAAE,OAAO,MAAM,CAAC;;;;;;;;;;ACf/C,eAAsB,kBAA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AM,eAAe,IAAI;AAEzB,WAAS,KAAK,qBAAqB;AAGnC,WAAS,MAAM,wBAAwB;AAEvC,QAAM,eAAe,IAAI;AAEzB,WAAS,KAAK,qBAAqB;AAGnC,WAAS,MAAM,uBAAuB;AAEtC,QAAM,kBAAkB,KAAK,gBAAgB;AAE7C,WAAS,KAAK,mBAAmB;AAGjC,WAAS,MAAM,sBAAsB;AAErC,QAAM,mBAAmB,IAAI;AAE7B,WAAS,KAAK,kBAAkB;;CAEvC,CAEiB,CAAC,CAAC,EAAE,CAAC"}
|
|
1
|
+
{"version":3,"file":"cli.mjs","names":[],"sources":["../src/task/addGitHooksConfig.ts","../src/task/clearDirectory.ts","../src/task/configPackageJson.ts","../src/task/createEslintConfig.ts","../src/task/createGitHooks.ts","../src/task/createGithubWorkflows.ts","../src/task/createPackageJson.ts","../src/task/createTsConfig.ts","../src/task/installDevDeps.ts","../package.json","../src/utils.ts","../src/config.ts","../src/schedule.ts","../src/cli.ts"],"sourcesContent":["import type { IConfig } from '../types'\nimport { readPackageJSON, writePackageJSON } from 'pkg-types'\n\nexport async function addGitHooksConfig(config: IConfig): Promise<void> {\n const { cwd, packageJsonPath } = config\n const newPackageJson = await readPackageJSON(cwd)\n newPackageJson.simpleGitHooks = {\n 'pre-commit': 'npx lint-staged',\n 'commit-msg': 'node scripts/verify-commit.js',\n }\n newPackageJson.lintStaged = {\n '*': 'eslint --fix',\n }\n\n await writePackageJSON(packageJsonPath, newPackageJson)\n}\n","import { readdir, rm } from 'node:fs/promises'\nimport path from 'node:path'\n\n/**\n * 清空目录内容,保留 .git 目录\n * @param dirPath 目录路径\n */\nexport async function clearDirectory(dirPath: string): Promise<void> {\n const entries = await readdir(dirPath, { withFileTypes: true })\n\n for (const entry of entries) {\n // 保留 .git 目录\n if (entry.name === '.git') {\n continue\n }\n\n const fullPath = path.join(dirPath, entry.name)\n\n if (entry.isDirectory()) {\n await rm(fullPath, { recursive: true, force: true })\n }\n else {\n await rm(fullPath, { force: true })\n }\n }\n}\n","import type { IConfig } from '../types'\nimport { readPackageJSON, writePackageJSON } from 'pkg-types'\n\nexport async function configPackageJson(config: IConfig): Promise<void> {\n const { cwd, packageJsonPath } = config\n const packageJson = await readPackageJSON(cwd)\n\n if (packageJson.main) {\n delete packageJson.main\n }\n\n packageJson.type = 'module'\n packageJson.scripts = {\n ...packageJson.scripts,\n 'lint': 'eslint .',\n 'lint:fix': 'eslint --fix',\n 'prepare': 'simple-git-hooks',\n }\n\n await writePackageJSON(packageJsonPath, packageJson)\n}\n","import type { IConfig } from '../types'\nimport { writeFile } from 'node:fs/promises'\nimport path from 'node:path'\n\nexport async function createEslintConfig(config: IConfig): Promise<void> {\n const eslintConfigContent = `import type { Linter } from 'eslint'\nimport antfu from '@antfu/eslint-config'\n\nconst config = antfu({\n type: 'lib',\n stylistic: {\n indent: 4,\n quotes: 'single',\n },\n rules: {\n 'no-console': 'off',\n 'node/prefer-global/process': 'off',\n 'antfu/top-level-function': 'off',\n 'regexp/no-unused-capturing-group': 'off',\n },\n yaml: {\n overrides: {\n 'yaml/indent': ['error', 2],\n },\n },\n}) as Linter.Config\n\nexport default config`\n\n const eslintConfigPath = path.join(config.cwd, 'eslint.config.ts')\n await writeFile(eslintConfigPath, eslintConfigContent)\n}\n","import type { IConfig } from '../types'\nimport { mkdir, writeFile } from 'node:fs/promises'\nimport path from 'node:path'\n\nexport async function createGitHooks(config: IConfig): Promise<void> {\n const scriptsDir = path.join(config.cwd, 'scripts')\n await mkdir(scriptsDir, { recursive: true })\n\n const verifyCommitPath = path.join(scriptsDir, 'verify-commit.js')\n const verifyCommitContent = 'import { readFileSync } from \\'node:fs\\'\\n'\n + 'import path from \\'node:path\\'\\n'\n + '// @ts-check\\n'\n + 'import pico from \\'picocolors\\'\\n'\n + '\\n'\n + 'const msgPath = path.resolve(\\'.git/COMMIT_EDITMSG\\')\\n'\n + 'const msg = readFileSync(msgPath, \\'utf-8\\').trim()\\n'\n + '\\n'\n + 'const commitRE\\n'\n + ' = /^(revert: )?(feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release)(\\\\(.+\\\\))?(!)?: .{1,50}/\\n'\n + '\\n'\n + 'if (!commitRE.test(msg)) {\\n'\n + ' console.log()\\n'\n + ' console.error(\\n'\n // eslint-disable-next-line no-template-curly-in-string\n + ' ` ${pico.white(pico.bgRed(\\' ERROR \\'))} ${pico.red(\\n'\n + ' `invalid commit message format.`,\\n'\n + ' )}\\\\n\\\\n${\\n'\n + ' pico.red(\\n'\n + ' ` Proper commit message format is required for automated changelog generation. Examples:\\\\n\\\\n`,\\n'\n + ' )\\n'\n // eslint-disable-next-line no-template-curly-in-string\n + ' } ${pico.green(`feat(compiler): add \\'comments\\' option`)}\\\\n`\\n'\n + ' + ` ${pico.green(\\n'\n + ' `fix(v-model): handle events on blur (close #28)`,\\n'\n + ' )}\\\\n`\\n'\n + ' + ` ${pico.green(\\n'\n + ' `feat(api)!: remove legacy token endpoint`,\\n'\n + ' )}\\\\n\\\\n${\\n'\n + ' pico.red(` See .github/commit-convention.md for more details.\\\\n`)}`,\\n'\n + ' )\\n'\n + ' process.exit(1)\\n'\n + '}\\n'\n\n await writeFile(verifyCommitPath, verifyCommitContent)\n}\n","import type { IConfig } from '../types'\nimport { mkdir, writeFile } from 'node:fs/promises'\nimport path from 'node:path'\n\nconst workflows: Array<{ filename: string, content: string }> = [\n {\n filename: 'autofix.yml',\n content: `name: autofix.ci\n\non:\n pull_request:\npermissions:\n contents: write\n\njobs:\n autofix:\n runs-on: ubuntu-latest\n timeout-minutes: 10\n\n steps:\n - uses: actions/checkout@v4\n\n - name: Use Node.js lts/*\n uses: actions/setup-node@v4\n with:\n node-version: lts/*\n\n - name: Install pnpm\n uses: pnpm/action-setup@v4\n\n - name: Install\n run: pnpm i\n\n - name: Lint\n run: pnpm run lint:fix\n\n - uses: autofix-ci/action@635ffb0c9798bd160680f18fd73371e355b85f27\n`,\n },\n {\n filename: 'ci.yml',\n content: `name: CI\n\non:\n pull_request:\n\njobs:\n lint:\n runs-on: ubuntu-latest\n steps:\n - uses: actions/checkout@v4\n\n - name: Install pnpm\n uses: pnpm/action-setup@v4\n\n - name: Set node\n uses: actions/setup-node@v4\n with:\n node-version: lts/*\n cache: pnpm\n\n - name: Install\n run: pnpm i\n\n - name: Lint\n run: pnpm run lint\n`,\n },\n {\n filename: 'pr-contributor-welcome.yml',\n content: `name: PullRequest Contributor Welcome\n\non:\n pull_request_target:\n types:\n - closed\n# paths:\n# - 'components/**'\n\npermissions:\n contents: read\n\njobs:\n comment:\n permissions:\n issues: write # for actions-cool/maintain-one-comment to modify or create issue comments\n pull-requests: write # for actions-cool/maintain-one-comment to modify or create PR comments\n if: github.event.pull_request.merged == true && github.repository == 'lonewolfyx/xxxx'\n runs-on: ubuntu-latest\n steps:\n - name: get commit count\n id: get_commit_count\n run: |\n PR_AUTHOR=$(echo \"\\${{ github.event.pull_request.user.login }}\")\n RESULT_DATA=$(curl -s \"https://api.github.com/repos/\\${{ github.repository }}/commits?author=\\${PR_AUTHOR}&per_page=5\")\n DATA_LENGTH=$(echo \\$RESULT_DATA | jq 'if type == \"array\" then length else 0 end')\n echo \"COUNT=\\$DATA_LENGTH\" >> \\$GITHUB_OUTPUT\n - name: Comment on PR\n if: steps.get_commit_count.outputs.COUNT < 3\n uses: actions-cool/maintain-one-comment@v3\n with:\n token: \\${{ secrets.GITHUB_TOKEN }}\n body: |\n 🎉 Thx for the PR @\\${{ github.event.pull_request.user.login }}\n\n <!-- WELCOME_CONTRIBUTION -->\n body-include: <!-- WELCOME_CONTRIBUTION -->\n`,\n },\n {\n filename: 'release.yml',\n content: `name: Release\n\npermissions:\n contents: write\n\non:\n push:\n tags:\n - 'v*'\n\njobs:\n release:\n runs-on: ubuntu-latest\n steps:\n - uses: actions/checkout@v4\n with:\n fetch-depth: 0\n\n - uses: actions/setup-node@v4\n with:\n node-version: lts/*\n\n - run: npx genereleaselog\n env:\n GITHUB_TOKEN: \\${{secrets.GITHUB_TOKEN}}\n`,\n },\n]\n\nexport async function createGithubWorkflows(config: IConfig): Promise<void> {\n const workflowsDir = path.join(config.cwd, '.github', 'workflows')\n await mkdir(workflowsDir, { recursive: true })\n\n for (const { filename, content } of workflows) {\n const filePath = path.join(workflowsDir, filename)\n await writeFile(filePath, content)\n }\n}\n","import type { IConfig } from '../types'\nimport { x } from 'tinyexec'\n\nexport async function createPackageJson(config: IConfig): Promise<void> {\n const { cwd, packageManager } = config\n\n await x(packageManager, packageManager === 'pnpm' ? ['init'] : ['init', '-y'], { nodeOptions: { cwd } })\n}\n","import type { IConfig } from '../types'\nimport { resolve } from 'node:path'\nimport { writeTSConfig } from 'pkg-types'\n\nexport async function createTsConfig(config: IConfig): Promise<void> {\n const tsconfigConfig = {\n extends: '@lonewolfyx/tsconfig/tsconfig.lib.json',\n compilerOptions: {\n baseUrl: './',\n paths: {\n '@/*': [\n './src/*',\n ],\n '#/*': [\n './src/types/*',\n './types/*',\n ],\n },\n resolveJsonModule: true,\n types: [\n 'node',\n ],\n noUnusedLocals: false,\n noUnusedParameters: false,\n declarationMap: false,\n },\n }\n\n await writeTSConfig(resolve(config.cwd, 'tsconfig.json'), tsconfigConfig)\n}\n","import type { IConfig } from '../types'\nimport { x } from 'tinyexec'\n\nexport async function installDevDeps(config: IConfig): Promise<void> {\n const { cwd, packageManager } = config\n const devDeps = [\n 'eslint',\n '@antfu/eslint-config',\n 'jiti',\n 'typescript',\n '@lonewolfyx/tsconfig',\n '@types/node',\n 'lint-staged',\n 'simple-git-hooks',\n 'picocolors',\n 'tsx',\n 'tsdown',\n ]\n\n const addArgs = packageManager === 'pnpm'\n ? ['add', '-D', ...devDeps]\n : ['install', '-D', ...devDeps]\n\n await x(packageManager, addArgs, { nodeOptions: { cwd } })\n}\n","","import { x } from 'tinyexec'\n\n/**\n * 检测包管理器是否存在\n * @param command 包管理器命令名称\n */\nexport async function hasPackageManager(command: string): Promise<boolean> {\n try {\n await x(command, ['--version'], { throwOnError: true })\n return true\n }\n catch {\n return false\n }\n}\n","import type { IConfig } from './types'\nimport path from 'node:path'\nimport { hasPackageManager } from './utils'\n\nexport async function resolveConfig(cwd: string): Promise<IConfig> {\n const hasPnpm = await hasPackageManager('pnpm')\n\n return {\n cwd,\n packageJsonPath: path.join(cwd, 'package.json'),\n packageManager: hasPnpm ? 'pnpm' : 'npm',\n }\n}\n","import { spinner } from '@clack/prompts'\n\ninterface Schedule {\n step: (startMessage: string, fn: () => Promise<void>, doneMessage: string) => Schedule\n done: () => Promise<void>\n}\n\nexport function schedule(): Schedule {\n const s = spinner()\n const tasks: Array<{ startMessage: string, fn: () => Promise<void>, doneMessage: string }> = []\n\n const chain: Schedule = {\n step(startMessage, fn, doneMessage) {\n tasks.push({ startMessage, fn, doneMessage })\n return chain\n },\n async done() {\n for (const { startMessage, fn, doneMessage } of tasks) {\n s.start(startMessage)\n await fn()\n s.stop(doneMessage)\n }\n },\n }\n\n return chain\n}\n","import { access } from 'node:fs/promises'\nimport { confirm, intro, isCancel, outro } from '@clack/prompts'\nimport { createMain, defineCommand } from 'citty'\nimport { addGitHooksConfig, clearDirectory, configPackageJson, createEslintConfig, createGitHooks, createGithubWorkflows, createPackageJson, createTsConfig, installDevDeps } from '@/task'\nimport { description, name, version } from '../package.json'\nimport { resolveConfig } from './config'\nimport { schedule } from './schedule.ts'\n\nconst command = defineCommand({\n meta: {\n name,\n version,\n description,\n },\n setup() {\n intro('项目初始化')\n },\n cleanup() {\n outro('Done. 项目初始化完成')\n },\n args: {\n cwd: {\n type: 'string',\n description: 'Current working directory',\n alias: 'c',\n default: process.cwd(),\n },\n },\n async run({ args }) {\n const cwd = args.cwd as string\n const config = await resolveConfig(cwd)\n\n // 检测 package.json 是否存在\n const packageJsonExists = await access(config.packageJsonPath)\n .then(() => true)\n .catch(() => false)\n\n if (packageJsonExists) {\n const shouldOverwrite = await confirm({\n message: '检测到当前目录已存在项目,是否覆盖创建新项目?(将清空当前目录下所有文件)',\n })\n\n if (isCancel(shouldOverwrite) || !shouldOverwrite) {\n outro('已取消操作')\n return\n }\n\n await clearDirectory(config.cwd)\n }\n\n await schedule()\n .step('正在创建 package.json...', () => createPackageJson(config), 'package.json 创建完成')\n .step('正在配置 package.json...', () => configPackageJson(config), 'package.json 配置完成')\n .step('正在安装开发依赖...', () => installDevDeps(config), '开发依赖安装完成')\n .step('正在创建 git hooks 配置文件...', () => createGitHooks(config), 'git hooks 配置文件创建完成')\n .step('正在创建 tsconfig.json...', () => createTsConfig(config), 'tsconfig.json 创建完成')\n .step('正在添加 git hooks 配置...', () => addGitHooksConfig(config), 'git hooks 配置添加完成')\n .step('正在创建 ESLint 配置文件...', () => createEslintConfig(config), 'ESLint 配置文件创建完成')\n .step('正在创建 GitHub Workflows...', () => createGithubWorkflows(config), 'GitHub Workflows 创建完成')\n .done()\n },\n})\n\ncreateMain(command)({})\n"],"mappings":";;;;;;;AAGA,eAAsB,kBAAkB,QAAgC;CACpE,MAAM,EAAE,KAAK,oBAAoB;CACjC,MAAM,iBAAiB,MAAM,gBAAgB,IAAI;AACjD,gBAAe,iBAAiB;EAC5B,cAAc;EACd,cAAc;EACjB;AACD,gBAAe,aAAa,EACxB,KAAK,gBACR;AAED,OAAM,iBAAiB,iBAAiB,eAAe;;;;;;;;ACP3D,eAAsB,eAAe,SAAgC;CACjE,MAAM,UAAU,MAAM,QAAQ,SAAS,EAAE,eAAe,MAAM,CAAC;AAE/D,MAAK,MAAM,SAAS,SAAS;AAEzB,MAAI,MAAM,SAAS,OACf;EAGJ,MAAM,WAAW,KAAK,KAAK,SAAS,MAAM,KAAK;AAE/C,MAAI,MAAM,aAAa,CACnB,OAAM,GAAG,UAAU;GAAE,WAAW;GAAM,OAAO;GAAM,CAAC;MAGpD,OAAM,GAAG,UAAU,EAAE,OAAO,MAAM,CAAC;;;;;ACnB/C,eAAsB,kBAAkB,QAAgC;CACpE,MAAM,EAAE,KAAK,oBAAoB;CACjC,MAAM,cAAc,MAAM,gBAAgB,IAAI;AAE9C,KAAI,YAA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