diff --git a/packages/generate-code-workspace/CHANGELOG.md b/packages/generate-code-workspace/CHANGELOG.md new file mode 100644 index 0000000..c8e7c46 --- /dev/null +++ b/packages/generate-code-workspace/CHANGELOG.md @@ -0,0 +1,13 @@ +# @ruan-cat/generate-code-workspace + +## 0.1.0 + +### Minor Changes + +- 读取工作区的 settings.json 文件,实现配置同步。将 `.vscode\settings.json` 的文件读取并整合到工作区 `.code-workspace` 文件内。 + +## 0.0.1 + +### Patch Changes + +- 初始化项目。 diff --git a/packages/generate-code-workspace/package.json b/packages/generate-code-workspace/package.json index c02cab3..533dda9 100644 --- a/packages/generate-code-workspace/package.json +++ b/packages/generate-code-workspace/package.json @@ -1,6 +1,6 @@ { "name": "@ruan-cat/generate-code-workspace", - "version": "0.0.0", + "version": "0.1.0", "description": "根据 pnpm-workspace.yaml 配置,生成vscode的code-workspace工作区文件。", "type": "module", "main": "./src/index.ts", @@ -26,7 +26,8 @@ "./src/*": "./src/*" }, "keywords": [ - "vuepress" + "code-workspace", + "vscode" ], "author": { "name": "ruan-cat", @@ -49,12 +50,15 @@ }, "files": [ "dist", - "src" + "src", + "CHANGELOG.md", + "readme.md" ], "dependencies": { - "commander": "^12.0.0", + "commander": "^12.1.0", "consola": "^3.2.3", "glob": "^11.0.0", + "jsonc-parser": "^3.3.1", "lodash-es": "^4.17.21", "yaml": "^2.6.0" }, diff --git a/packages/generate-code-workspace/readme.md b/packages/generate-code-workspace/readme.md index 44d79cd..7b91a24 100644 --- a/packages/generate-code-workspace/readme.md +++ b/packages/generate-code-workspace/readme.md @@ -3,3 +3,38 @@ 本库用于生成工作区文件。 ## 使用方式 + +### 函数用法 + +```ts +// scripts/generate.code-workspace.ts +import { generateCodeWorkspace } from "@ruan-cat/generate-code-workspace"; +generateCodeWorkspace("monorepo单仓"); +``` + +```json +{ + "scripts": { + "code-workspace:create": "node --import=tsx ./scripts/generate.code-workspace.ts" + } +} +``` + +### 命令行传参 + +```ts +// scripts/generate.code-workspace.ts +import "@ruan-cat/generate-code-workspace"; +``` + +```json +{ + "scripts": { + "code-workspace:create": "node --import=tsx ./scripts/generate.code-workspace.ts --name=monorepo单仓" + } +} +``` + +## 更新日志 + +[CHANGELOG.md](./CHANGELOG.md) diff --git a/packages/generate-code-workspace/src/index.ts b/packages/generate-code-workspace/src/index.ts index 5f6bc9c..5a291ee 100644 --- a/packages/generate-code-workspace/src/index.ts +++ b/packages/generate-code-workspace/src/index.ts @@ -7,6 +7,7 @@ import { merge, isNil } from "lodash-es"; import { program } from "commander"; import { sync } from "glob"; import yaml from "js-yaml"; +import { parse as parseJSONC } from "jsonc-parser"; import { type PackageJson } from "pkg-types"; import { type PnpmWorkspace } from "./types/pnpm-workspace.yaml.shim"; @@ -38,6 +39,26 @@ function pathChange(path: string) { return path.replace(/\\/g, "/"); } +/** + * 获取 vscode 的配置 + * @description + * 读取 .vscode/settings.json 文件 + * + * 如果不存在则返回 false + */ +function getVscodeSettings() { + const vscodeSettingsPath = join(process.cwd(), ".vscode/settings.json"); + if (fs.existsSync(vscodeSettingsPath)) { + const vscodeSettings = fs.readFileSync(vscodeSettingsPath, "utf8"); + const res = parseJSONC(vscodeSettings); + // console.log("????", res); + return res; + } else { + consola.warn(`${vscodeSettingsPath} does not exist.`); + return false; + } +} + /** * 根据 pnpm-workspace.yaml 生成工作区配置数组 * @description @@ -69,9 +90,7 @@ function getFolders() { // 根据每个模式匹配相应的目录 pkgPatterns!.map((pkgPattern) => { const matchedPath = pathChange(join(process.cwd(), pkgPattern, "package.json")); - - console.log(" 检查拼接出来的路径: ", matchedPath); - + // console.log(" 检查拼接出来的路径: ", matchedPath); const matchedPaths = sync(matchedPath, { ignore: "**/node_modules/**", }); @@ -151,6 +170,7 @@ const folders = [...defFolders, ...getFolders()]; const codeWorkspaceContent = { folders, + settings: getVscodeSettings() || {}, }; /** diff --git a/scripts/generate.code-workspace.ts b/scripts/generate.code-workspace.ts index 32e7bbb..40dd54b 100644 --- a/scripts/generate.code-workspace.ts +++ b/scripts/generate.code-workspace.ts @@ -1,3 +1,2 @@ -// 全量导入此文件 import { generateCodeWorkspace } from "@ruan-cat/generate-code-workspace"; generateCodeWorkspace("monorepo单仓");