zixulu 1.71.4 → 1.71.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/index.js CHANGED
@@ -15,8 +15,8 @@ import { createWriteStream, existsSync, readFileSync } from "fs";
15
15
  import { homedir, tmpdir } from "os";
16
16
  import { parse as toml_parse, stringify } from "@iarna/toml";
17
17
  import { writeFile as promises_writeFile } from "node:fs/promises";
18
- import json5 from "json5";
19
18
  import simple_git, { simpleGit } from "simple-git";
19
+ import json5 from "json5";
20
20
  import dayjs from "dayjs";
21
21
  import clipboardy from "clipboardy";
22
22
  import yaml_0 from "yaml";
@@ -1216,6 +1216,51 @@ async function addGitignore() {
1216
1216
  const message = await addRuleToGitIgnore(...addedRules);
1217
1217
  return message;
1218
1218
  }
1219
+ async function addHusky(params) {
1220
+ const options = params ?? {};
1221
+ const git = simple_git();
1222
+ const isRepo = await git.checkIsRepo();
1223
+ if (!isRepo) return void consola_0.info("当前目录不是 git 仓库,跳过 husky 配置");
1224
+ if (options.skipConfirm) consola_0.info("已根据外部指令跳过 husky 配置确认");
1225
+ else {
1226
+ const shouldSetupHooks = await shouldContinue("是否配置 husky hooks,在每次 commit 前自动格式化修改的文件?");
1227
+ if (!shouldSetupHooks) return void consola_0.info("已取消 husky 配置");
1228
+ }
1229
+ consola_0.start("开始配置 husky");
1230
+ const huskyConfig = {
1231
+ package: [
1232
+ "husky",
1233
+ "lint-staged"
1234
+ ],
1235
+ type: "devDependencies"
1236
+ };
1237
+ await addDependency(huskyConfig);
1238
+ await installDependceny();
1239
+ try {
1240
+ consola_0.start("初始化 husky");
1241
+ await spawnAsync("bunx husky init");
1242
+ consola_0.success("husky 初始化成功");
1243
+ } catch (error) {
1244
+ consola_0.error("husky 初始化失败", error);
1245
+ }
1246
+ try {
1247
+ consola_0.start("配置 pre-commit hook");
1248
+ const preCommitHook = "npx lint-staged";
1249
+ await writeFile(".husky/pre-commit", preCommitHook, "utf-8");
1250
+ consola_0.success("pre-commit hook 配置成功");
1251
+ } catch (error) {
1252
+ consola_0.error("pre-commit hook 配置失败", error);
1253
+ }
1254
+ const packageJson = await readPackageJson();
1255
+ packageJson["lint-staged"] = {
1256
+ "**/*": "prettier --write --ignore-unknown"
1257
+ };
1258
+ await writePackageJson({
1259
+ data: packageJson
1260
+ });
1261
+ consola_0.success("lint-staged 配置成功");
1262
+ consola_0.success("husky 配置完成");
1263
+ }
1219
1264
  async function getFiles_getFiles(options) {
1220
1265
  const { dir = ".", match, count, depth, exclude } = options;
1221
1266
  const result = [];
@@ -1450,48 +1495,21 @@ async function addPrettier() {
1450
1495
  await installDependceny();
1451
1496
  const git = simple_git();
1452
1497
  const isRepo = await git.checkIsRepo();
1453
- if (isRepo) {
1454
- consola_0.info("检测到 git 仓库");
1455
- const shouldSetupHooks = await shouldContinue("是否配置 git hooks,在每次 commit 前自动格式化修改的文件?");
1456
- if (!shouldSetupHooks) {
1457
- consola_0.info("跳过 git hooks 配置");
1458
- consola_0.success("添加 prettier 配置成功");
1459
- return;
1460
- }
1461
- consola_0.start("开始配置 pre-commit hooks");
1462
- const huskyConfig = {
1463
- package: [
1464
- "husky",
1465
- "lint-staged"
1466
- ],
1467
- type: "devDependencies"
1468
- };
1469
- await addDependency(huskyConfig);
1470
- await installDependceny();
1471
- try {
1472
- consola_0.start("初始化 husky");
1473
- await spawnAsync("bunx husky init");
1474
- consola_0.success("husky 初始化成功");
1475
- } catch (error) {
1476
- consola_0.error("husky 初始化失败", error);
1477
- }
1478
- try {
1479
- consola_0.start("配置 pre-commit hook");
1480
- const preCommitHook = "npx lint-staged";
1481
- await writeFile(".husky/pre-commit", preCommitHook, "utf-8");
1482
- consola_0.success("pre-commit hook 配置成功");
1483
- } catch (error) {
1484
- consola_0.error("pre-commit hook 配置失败", error);
1485
- }
1486
- const packageJson3 = await readPackageJson();
1487
- packageJson3["lint-staged"] = {
1488
- "**/*": "prettier --write --ignore-unknown"
1489
- };
1490
- await writePackageJson({
1491
- data: packageJson3
1492
- });
1493
- consola_0.success("lint-staged 配置成功");
1494
- } else consola_0.info("当前目录不是 git 仓库,跳过 git hooks 配置");
1498
+ if (!isRepo) {
1499
+ consola_0.info("当前目录不是 git 仓库,跳过 git hooks 配置");
1500
+ consola_0.success("添加 prettier 配置成功");
1501
+ return;
1502
+ }
1503
+ consola_0.info("检测到 git 仓库");
1504
+ const shouldSetupHooks = await shouldContinue("是否配置 git hooks,在每次 commit 前自动格式化修改的文件?");
1505
+ if (!shouldSetupHooks) {
1506
+ consola_0.info("跳过 git hooks 配置");
1507
+ consola_0.success("添加 prettier 配置成功");
1508
+ return;
1509
+ }
1510
+ await addHusky({
1511
+ skipConfirm: true
1512
+ });
1495
1513
  consola_0.success("添加 prettier 配置成功");
1496
1514
  }
1497
1515
  const prismaFile = `import { PrismaClient } from "@prisma/client"
@@ -4307,6 +4325,16 @@ async function getEditorExtensions({ source }) {
4307
4325
  data = data.filter((item)=>!item.startsWith("anysphere.") && "github.copilot" !== item && "github.copilot-chat" !== item);
4308
4326
  return new Set(data);
4309
4327
  }
4328
+ function hasCode() {
4329
+ const userDir = homedir();
4330
+ const path = join(userDir, "AppData/Roaming/Code");
4331
+ return existsSync(path);
4332
+ }
4333
+ function hasCursor() {
4334
+ const userDir = homedir();
4335
+ const path = join(userDir, "AppData/Roaming/Cursor");
4336
+ return existsSync(path);
4337
+ }
4310
4338
  const syncEditorSetting_userDir = homedir();
4311
4339
  const fileSourceMap = {
4312
4340
  settings: {
@@ -4329,8 +4357,12 @@ async function getFile(source) {
4329
4357
  }
4330
4358
  async function syncEditorFile({ source, target }) {
4331
4359
  const { dir, base } = parse(target);
4360
+ await mkdir(dir, {
4361
+ recursive: true
4362
+ });
4332
4363
  const setting = await readZixuluSetting();
4333
- const code = await getFile(source);
4364
+ let code = await getFile(source);
4365
+ if (target === fileSourceMap.settings.Code) code = code.replace(/\n^ *"extensions\.gallery\.serviceUrl":.+,?$/m, "");
4334
4366
  if (existsSync(target)) {
4335
4367
  const text = await readFile(target, "utf-8");
4336
4368
  if (text === code) return void consola_0.success(`${target} 已是最新`);
@@ -4354,36 +4386,31 @@ async function syncEditorFile({ source, target }) {
4354
4386
  }
4355
4387
  async function syncEditorSetting() {
4356
4388
  const setting = await readZixuluSetting();
4389
+ const sourceChoices = [
4390
+ "Online"
4391
+ ];
4392
+ if (hasCode()) sourceChoices.unshift("Code");
4393
+ if (hasCursor()) sourceChoices.unshift("Cursor");
4357
4394
  const { source } = await inquirer_0.prompt([
4358
4395
  {
4359
4396
  type: "list",
4360
4397
  name: "source",
4361
4398
  message: "选择同步来源",
4362
- choices: [
4363
- "Code",
4364
- "Cursor",
4365
- "Online"
4366
- ],
4399
+ choices: sourceChoices,
4367
4400
  default: setting.syncEditor?.source ?? "Cursor"
4368
4401
  }
4369
4402
  ]);
4370
4403
  setting.syncEditor ??= {};
4371
4404
  setting.syncEditor.source = source;
4405
+ const targetChoices = sourceChoices.filter((v)=>v !== source);
4406
+ if (0 === targetChoices.length) return consola_0.info("没有可同步的目标");
4372
4407
  const { targets, types } = await inquirer_0.prompt([
4373
4408
  {
4374
4409
  type: "checkbox",
4375
4410
  name: "targets",
4376
4411
  message: "选择同步目标",
4377
- choices: [
4378
- "Code",
4379
- "Cursor",
4380
- "Online"
4381
- ].filter((v)=>v !== source),
4382
- default: (setting.syncEditor?.targets ?? [
4383
- "Code",
4384
- "Cursor",
4385
- "Online"
4386
- ]).filter((v)=>v !== source)
4412
+ choices: targetChoices,
4413
+ default: setting.syncEditor?.targets?.filter((item)=>targetChoices.includes(item)) ?? targetChoices
4387
4414
  },
4388
4415
  {
4389
4416
  type: "checkbox",
@@ -4407,11 +4434,12 @@ async function syncEditorSetting() {
4407
4434
  const { onlinePath } = await inquirer_0.prompt({
4408
4435
  type: "input",
4409
4436
  name: "onlinePath",
4410
- message: "请输入 blog 文件夹的路径",
4411
- default: setting.syncEditor?.onlinePath ?? "C:\\Users\\lenovo\\Desktop\\workspace\\blog"
4437
+ message: "请输入 blog 文件夹的路径(留空则跳过)",
4438
+ default: setting.syncEditor?.onlinePath
4412
4439
  });
4413
- setting.syncEditor.onlinePath = onlinePath;
4440
+ setting.syncEditor.onlinePath = onlinePath.trim() ?? void 0;
4414
4441
  }
4442
+ if (!setting.syncEditor.onlinePath) setting.syncEditor.targets = targets.filter((v)=>"Online" !== v);
4415
4443
  const onlinePath = setting.syncEditor.onlinePath;
4416
4444
  const configs = types.filter((item)=>"extensions" !== item).map((fileType)=>targets.map((target)=>({
4417
4445
  source: fileSourceMap[fileType][source],
@@ -4464,6 +4492,9 @@ async function syncEditorSetting() {
4464
4492
  if (targets.includes("Online")) await writeFile(join(onlinePath, "static", "extensions.json"), JSON.stringify(Array.from(sourceExtensions), null, 4));
4465
4493
  }
4466
4494
  if (targets.includes("Online")) {
4495
+ await execAsync("npm run format", {
4496
+ cwd: onlinePath
4497
+ });
4467
4498
  if (await hasChangeNoCommit(onlinePath)) {
4468
4499
  await addGitCommit({
4469
4500
  message: getCommitMessage("feature", "sync editor setting"),
@@ -4548,7 +4579,7 @@ async function syncVscode() {
4548
4579
  if (0 === options.length) return;
4549
4580
  const userDir = homedir();
4550
4581
  const snippetSource = join(userDir, "AppData/Roaming/Code/User/snippets");
4551
- const setting = join(userDir, "AppData/Roaming/Code/User/settings.json");
4582
+ const setting = (await readFile(join(userDir, "AppData/Roaming/Code/User/settings.json"), "utf-8")).replace(/}[ \n\r]*$/, ` "chat.disableAIFeatures": true,\n}`);
4552
4583
  const dir = `vscode-${dayjs().format("YYYYMMDDHHmmss")}`;
4553
4584
  try {
4554
4585
  await mkdir(dir, {
@@ -4560,7 +4591,7 @@ async function syncVscode() {
4560
4591
  recursive: true
4561
4592
  });
4562
4593
  consola_0.start("开始下载最新 VSCode 配置");
4563
- await copyFile(setting, join(dir, "settings.json"));
4594
+ await writeFile(join(dir, "settings.json"), setting, "utf-8");
4564
4595
  const files = await readdir(snippetSource);
4565
4596
  for (const file of files)await copyFile(join(snippetSource, file), join(snippetTarget, file));
4566
4597
  consola_0.success("下载最新 VSCode 配置完成");
@@ -4941,6 +4972,7 @@ program.name("格数科技").version(src_pkg.version);
4941
4972
  console.log(chalk.redBright.bold(`zixulu ${src_pkg.version}`));
4942
4973
  program.command("eslint").description("添加 ESLint 相关配置").action(actionWithBackup(addEslint, getCommitMessage("feature", "添加 ESLint 相关配置")));
4943
4974
  program.command("prettier").description("添加 prettier 配置").action(actionWithBackup(addPrettier, getCommitMessage("feature", "添加 prettier 配置文件")));
4975
+ program.command("husky").description("添加 husky 配置").action(actionWithBackup(addHusky, getCommitMessage("feature", "添加 husky 配置")));
4944
4976
  program.command("vite").description("初始化 vite 配置").action(actionWithBackup(vite, getCommitMessage("feature", "初始化 vite 配置")));
4945
4977
  program.command("rsbuild").description("初始化 rsbuild 配置").action(actionWithBackup(rsbuild, getCommitMessage("feature", "初始化 rsbuild 配置")));
4946
4978
  program.command("next").description("初始化 next 配置").action(actionWithBackup(next_next, getCommitMessage("feature", "初始化 next 配置")));