-
Notifications
You must be signed in to change notification settings - Fork 11
优化 vue 模板 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xiaou66
wants to merge
6
commits into
ZToolsCenter:main
Choose a base branch
from
xiaou66:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
优化 vue 模板 #5
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,5 @@ dist/ | |
| *.log | ||
| .DS_Store | ||
| *.tsbuildinfo | ||
| templates/**/*-lock.yaml | ||
| .idea | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import fs from 'node:fs' | ||
| import path from 'node:path' | ||
| import type { DiscoveredPluginProject, PluginConfig, PluginLayout } from './types.js' | ||
|
|
||
| interface PluginPathCandidate { | ||
| layout: PluginLayout | ||
| pluginRoot: string | ||
| pluginJsonPath: string | ||
| } | ||
|
|
||
| /** | ||
| * 插件项目发现模块。 | ||
| * | ||
| * 该模块集中维护 CLI 对插件根目录的判断规则,避免 publish、pull 等入口各自 | ||
| * 手写路径候选列表后产生结构兼容差异。最新的 src-ztools 结构优先,旧结构继续 | ||
| * 兼容以避免破坏已有插件项目。 | ||
| */ | ||
| export function discoverPluginProject(cwd: string = process.cwd()): DiscoveredPluginProject { | ||
| const projectRoot = path.resolve(cwd) | ||
| const candidates = buildPluginPathCandidates(projectRoot) | ||
| const existing = candidates.filter((candidate) => fs.existsSync(candidate.pluginJsonPath)) | ||
|
|
||
| if (existing.length === 0) { | ||
| throw new Error( | ||
| '未找到 plugin.json,请确保在插件项目根目录下执行此命令\n' + | ||
| '支持的路径:./src-ztools/plugin.json, ./plugin.json, ./public/plugin.json' | ||
| ) | ||
| } | ||
|
|
||
| const selected = existing[0] | ||
| const config = readPluginConfig(selected.pluginJsonPath) | ||
| const warnings = buildMultipleConfigWarnings(existing) | ||
|
|
||
| return { | ||
| projectRoot, | ||
| pluginRoot: selected.pluginRoot, | ||
| pluginJsonPath: selected.pluginJsonPath, | ||
| layout: selected.layout, | ||
| config, | ||
| warnings | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 按优先级构造候选路径。 | ||
| * | ||
| * src-ztools 是新模板结构,必须优先于根目录和旧 public 结构;否则同一项目中残留 | ||
| * 旧配置时,CLI 会错误读取过期配置。 | ||
| */ | ||
| function buildPluginPathCandidates(projectRoot: string): PluginPathCandidate[] { | ||
| return [ | ||
| { | ||
| layout: 'src-ztools', | ||
| pluginRoot: path.join(projectRoot, 'src-ztools'), | ||
| pluginJsonPath: path.join(projectRoot, 'src-ztools', 'plugin.json') | ||
| }, | ||
| { | ||
| layout: 'root', | ||
| pluginRoot: projectRoot, | ||
| pluginJsonPath: path.join(projectRoot, 'plugin.json') | ||
| }, | ||
| { | ||
| layout: 'public', | ||
| pluginRoot: path.join(projectRoot, 'public'), | ||
| pluginJsonPath: path.join(projectRoot, 'public', 'plugin.json') | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| /** | ||
| * 读取插件配置。 | ||
| * | ||
| * 只负责 JSON 解析,不做业务字段校验;字段校验仍由 publish 等调用方根据命令场景 | ||
| * 执行,避免解析器承担过多职责。 | ||
| */ | ||
| function readPluginConfig(pluginJsonPath: string): PluginConfig { | ||
| try { | ||
| const content = fs.readFileSync(pluginJsonPath, 'utf-8') | ||
| return JSON.parse(content) as PluginConfig | ||
| } catch (error) { | ||
| throw new Error(`读取 plugin.json 失败: ${(error as Error).message}`) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 构造多配置文件提示。 | ||
| * | ||
| * 多个 plugin.json 同时存在通常意味着迁移残留。这里不直接失败,是为了保留旧项目 | ||
| * 的渐进迁移能力;调用方负责把 warning 展示给用户。 | ||
| */ | ||
| function buildMultipleConfigWarnings(existing: PluginPathCandidate[]): string[] { | ||
| if (existing.length <= 1) { | ||
| return [] | ||
| } | ||
|
|
||
| const selected = existing[0] | ||
| const ignored = existing.slice(1).map((candidate) => candidate.pluginJsonPath) | ||
|
|
||
| return [ | ||
| [ | ||
| `检测到多个 plugin.json,已使用 ${selected.pluginJsonPath}`, | ||
| '被忽略的配置:', | ||
| ...ignored.map((item) => ` - ${item}`) | ||
| ].join('\n') | ||
| ] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
2 changes: 1 addition & 1 deletion
2
templates/vue-vite/public/plugin.json → templates/vue-vite/src-ztools/plugin.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "extends": "@vue/tsconfig/tsconfig.dom.json", | ||
| "include": ["env.d.ts", "src/**/*", "src/**/*.vue"], | ||
| "exclude": ["src/**/__tests__/*", "src-ztools/**"], | ||
| "compilerOptions": { | ||
| "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo", | ||
|
|
||
| "paths": { | ||
| "@/*": ["./src/*"] | ||
| }, | ||
| "types": ["@ztools-center/ztools-api-types"] | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在解析
plugin.json时,JSON.parse可能会返回null、数组或非对象类型(例如,如果文件内容是null或非对象字符串)。建议在返回前添加防御性检查,确保解析后的内容是一个非空的键值对对象,以避免后续属性访问时抛出运行时错误。