Skip to content

Commit c4602f2

Browse files
feat(license): implement license detection
1 parent 7086c62 commit c4602f2

File tree

8 files changed

+381
-91
lines changed

8 files changed

+381
-91
lines changed

electron/ipc/project.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,70 @@ ipcMain.handle('project:analyze', async (event, folderPath): Promise<LinguistRes
5858
})
5959
})
6060

61+
// 读取项目许可证的前若干行
62+
ipcMain.handle('project:read-license', async (_evt, folderPath: string, maxLines = 20) => {
63+
try {
64+
if (!folderPath || typeof folderPath !== 'string')
65+
return { success: false, message: 'Invalid project path' }
66+
67+
const stat = await fs.stat(folderPath).catch(() => null)
68+
if (!stat || !stat.isDirectory())
69+
return { success: false, message: 'Project path does not exist or is not a directory' }
70+
71+
const entries = await fs.readdir(folderPath)
72+
const candidates = entries.filter((name) => {
73+
const lower = name.toLowerCase()
74+
return (
75+
// 常见文件名:license/licence/copying/unlicense,允许有扩展名
76+
lower.startsWith('license')
77+
|| lower.startsWith('licence')
78+
|| lower.startsWith('copying')
79+
|| lower.startsWith('unlicense')
80+
)
81+
})
82+
83+
if (!candidates.length)
84+
return { success: false, message: 'License file not found' }
85+
86+
const rank = (filename: string) => {
87+
const f = filename.toLowerCase()
88+
const ext = path.extname(f)
89+
const base = f.replace(ext, '')
90+
// 优先级:license > licence > copying > unlicense;无扩展名 > .md > .txt > 其他
91+
const baseScore = base.startsWith('license')
92+
? 0
93+
: base.startsWith('licence')
94+
? 1
95+
: base.startsWith('copying')
96+
? 2
97+
: base.startsWith('unlicense')
98+
? 3
99+
: 9
100+
const extScore = ext === '' ? 0 : ext === '.md' ? 1 : ext === '.txt' ? 2 : 3
101+
return baseScore * 10 + extScore
102+
}
103+
104+
const picked = candidates.sort((a, b) => rank(a) - rank(b))[0]
105+
const full = path.join(folderPath, picked)
106+
const fileStat = await fs.stat(full)
107+
if (!fileStat.isFile())
108+
return { success: false, message: 'License file is not a regular file' }
109+
110+
const content = await fs.readFile(full, 'utf8')
111+
const lines = content.split(/\r?\n/).slice(0, Math.max(1, Math.min(100, maxLines)))
112+
113+
return {
114+
success: true,
115+
filename: picked,
116+
snippet: lines.join('\n'),
117+
lines: lines.length,
118+
}
119+
}
120+
catch (e: any) {
121+
return { success: false, message: String(e?.message || e) }
122+
}
123+
})
124+
61125
// 使用IDE打开项目
62126
ipcMain.handle('project:open', async (_, idePath: string, projectPath: string): Promise<string> => {
63127
if (!idePath || !projectPath) {

electron/preload.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ contextBridge.exposeInMainWorld('api', {
1919
ipcRenderer.on('project:analyze:progress', handler)
2020
return () => ipcRenderer.removeListener('project:analyze:progress', handler)
2121
},
22+
readProjectLicense: (folderPath: string, maxLines = 20) => ipcRenderer.invoke('project:read-license', folderPath, maxLines),
2223
openProject: (idePath: string, projectPath: string) => ipcRenderer.invoke('project:open', idePath, projectPath),
2324
deleteProject: (projectPath: string) => ipcRenderer.invoke('project:delete', projectPath),
2425
importProject: () => ipcRenderer.invoke('project:import'),

src/components/ProjectCard/LicensePop/LicensePop.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ onClickOutside(popupRef, () => {
4343
flex="~ items-center" gap="5px"
4444
>
4545
<span size="0.9rem" i-custom="license-permission" />
46-
{{ t(permission) }}
46+
{{ t(`license.info.${permission}`) }}
4747
</li>
4848
</ul>
4949
</div>
@@ -61,7 +61,7 @@ onClickOutside(popupRef, () => {
6161
flex="~ items-center" gap="5px"
6262
>
6363
<span size="0.9rem" i-custom="license-limitation" />
64-
{{ t(limitation) }}
64+
{{ t(`license.info.${limitation}`) }}
6565
</li>
6666
</ul>
6767
</div>
@@ -79,7 +79,7 @@ onClickOutside(popupRef, () => {
7979
flex="~ items-center" gap="5px"
8080
>
8181
<span size="0.9rem" i-custom="license-condition" />
82-
{{ t(condition) }}
82+
{{ t(`license.info.${condition}`) }}
8383
</li>
8484
</ul>
8585
</div>

0 commit comments

Comments
 (0)