Skip to content

Commit eec66b1

Browse files
committed
feat(example): add check stash repos
1 parent 51dd879 commit eec66b1

File tree

9 files changed

+3183
-1993
lines changed

9 files changed

+3183
-1993
lines changed

.eslintrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ignore-workspace-root-check=true
2+
shamefully-hoist=true

.vscode/settings.json

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,38 @@
66
"i18n-ally.keystyle": "nested",
77
"i18n-ally.localesPaths": "locales",
88
"i18n-ally.sortKeys": true,
9-
"prettier.enable": false,
10-
"editor.codeActionsOnSave": {
11-
"source.fixAll.eslint": true
12-
},
139
"files.associations": {
1410
"*.css": "postcss"
1511
},
12+
"typescript.tsdk": "node_modules/typescript/lib",
13+
14+
// Enable the ESlint flat config support
15+
"eslint.experimental.useFlatConfig": true,
16+
17+
// Disable the default formatter, use eslint instead
18+
"prettier.enable": false,
1619
"editor.formatOnSave": false,
17-
"typescript.tsdk": "node_modules/typescript/lib"
20+
21+
// Auto fix
22+
"editor.codeActionsOnSave": {
23+
"source.fixAll.eslint": "explicit",
24+
"source.organizeImports": "never"
25+
},
26+
27+
// Enable eslint for all supported languages
28+
"eslint.validate": [
29+
"javascript",
30+
"javascriptreact",
31+
"typescript",
32+
"typescriptreact",
33+
"vue",
34+
"html",
35+
"markdown",
36+
"json",
37+
"jsonc",
38+
"yaml",
39+
"toml",
40+
"gql",
41+
"graphql"
42+
]
1843
}

eslint.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import antfu from '@antfu/eslint-config'
2+
3+
export default antfu()

examples/clear-repos/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Clear Repos
2+
3+
清除没有待提交内容的仓库。

examples/clear-repos/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "module",
3+
"scripts": {
4+
"start": "tsx src/index.ts"
5+
}
6+
}

examples/clear-repos/src/index.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import path from 'node:path'
2+
import type { SimpleGit } from 'simple-git'
3+
import { simpleGit } from 'simple-git'
4+
import consola from 'consola'
5+
import fs from 'fs-extra'
6+
7+
export async function checkRepositoryStatus(git: SimpleGit) {
8+
try {
9+
// 获取当前仓库的状态
10+
const status = await git.status()
11+
const rootDir = await git.revparse(['--show-toplevel'])
12+
13+
// 检查是否有未提交的改动
14+
if (status.isClean()) {
15+
// consola.success('工作目录干净,没有待提交的内容。')
16+
}
17+
else {
18+
consola.warn(`工作目录 ${rootDir} 有待提交的内容。`)
19+
}
20+
// consola.warn('工作目录有待提交的内容:')
21+
// console.log('未跟踪的文件:', status.not_added)
22+
// console.log('修改的文件:', status.modified)
23+
// console.log('新建的文件:', status.created)
24+
// console.log('删除的文件:', status.deleted)
25+
// console.log('重命名的文件:', status.renamed)
26+
}
27+
catch (error) {
28+
consola.error('检查仓库状态时出错:', error)
29+
}
30+
}
31+
32+
export async function checkRemoteSync(git: SimpleGit) {
33+
try {
34+
// 获取仓库状态
35+
const status = await git.status()
36+
const rootDir = await git.revparse(['--show-toplevel'])
37+
38+
// 检查本地分支与远程分支的比较结果
39+
// consola.info('当前分支:', status.current)
40+
if (status.behind > 0)
41+
consola.warn(`${rootDir} 本地分支落后于远程分支 ${status.tracking}${status.behind} 个提交。`)
42+
43+
if (status.ahead > 0)
44+
consola.warn(`${rootDir} 本地分支领先于远程分支 ${status.tracking}${status.ahead} 个提交。`)
45+
46+
// if (status.ahead === 0 && status.behind === 0)
47+
// consola.success('本地分支与远程分支同步。')
48+
}
49+
catch (error) {
50+
consola.error('检查远程同步状态时出错:', error)
51+
}
52+
}
53+
54+
// checkRemoteSync()
55+
// checkRepositoryStatus()
56+
57+
export async function checkFolder(dir: string) {
58+
const folders = await fs.readdir(dir, { withFileTypes: true })
59+
for (const folder of folders) {
60+
if (!folder.isDirectory())
61+
continue
62+
63+
const baseDir = path.join(dir, folder.name)
64+
const git = simpleGit({
65+
baseDir,
66+
})
67+
const isRepo = await git.checkIsRepo().catch(() => false)
68+
69+
if (isRepo) {
70+
await checkRepositoryStatus(git)
71+
await checkRemoteSync(git)
72+
}
73+
else {
74+
checkFolder(path.join(dir, folder.name))
75+
}
76+
}
77+
}
78+
79+
async function main() {
80+
checkFolder('/Users/yunyou/repos')
81+
}
82+
83+
main()

package.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "scripts",
33
"version": "1.0.0",
4-
"packageManager": "[email protected]",
4+
"packageManager": "[email protected]",
5+
"type": "module",
56
"description": "",
67
"author": "",
78
"license": "MIT",
@@ -18,19 +19,20 @@
1819
"typecheck": "tsc --noEmit"
1920
},
2021
"devDependencies": {
21-
"@antfu/eslint-config": "^0.41.0",
22-
"@types/fs-extra": "^11.0.1",
23-
"@types/lodash": "^4.14.197",
24-
"@types/node": "^20.5.6",
22+
"@antfu/eslint-config": "^2.16.0",
23+
"@types/fs-extra": "^11.0.4",
24+
"@types/lodash": "^4.17.0",
25+
"@types/node": "^20.12.7",
2526
"chalk": "^5.3.0",
2627
"consola": "^3.2.3",
27-
"dotenv": "^16.3.1",
28-
"eslint": "^8.47.0",
29-
"fs-extra": "^11.1.1",
28+
"dotenv": "^16.4.5",
29+
"eslint": "^9.1.1",
30+
"fs-extra": "^11.2.0",
3031
"lodash": "^4.17.21",
3132
"picocolors": "^1.0.0",
32-
"tsx": "^3.12.7",
33-
"typescript": "^5.2.2",
34-
"vitest": "^0.34.2"
33+
"simple-git": "^3.24.0",
34+
"tsx": "^4.7.3",
35+
"typescript": "^5.4.5",
36+
"vitest": "^1.5.2"
3537
}
3638
}

0 commit comments

Comments
 (0)