Skip to content

Commit

Permalink
✨ feat(generate-code-workspace): 实现settings.json文件的读取与识别,并自动整合到配置文件内。
Browse files Browse the repository at this point in the history
  • Loading branch information
ruan-cat committed Nov 16, 2024
1 parent 3cc2b97 commit e89918d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
13 changes: 13 additions & 0 deletions packages/generate-code-workspace/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

- 初始化项目。
12 changes: 8 additions & 4 deletions packages/generate-code-workspace/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -26,7 +26,8 @@
"./src/*": "./src/*"
},
"keywords": [
"vuepress"
"code-workspace",
"vscode"
],
"author": {
"name": "ruan-cat",
Expand All @@ -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"
},
Expand Down
35 changes: 35 additions & 0 deletions packages/generate-code-workspace/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
26 changes: 23 additions & 3 deletions packages/generate-code-workspace/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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/**",
});
Expand Down Expand Up @@ -151,6 +170,7 @@ const folders = [...defFolders, ...getFolders()];

const codeWorkspaceContent = {
folders,
settings: getVscodeSettings() || {},
};

/**
Expand Down
1 change: 0 additions & 1 deletion scripts/generate.code-workspace.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
// 全量导入此文件
import { generateCodeWorkspace } from "@ruan-cat/generate-code-workspace";
generateCodeWorkspace("monorepo单仓");

0 comments on commit e89918d

Please sign in to comment.