Skip to content

Commit d344aee

Browse files
committed
feat: add utility to remove empty directories and check file existence
1 parent 00b0e28 commit d344aee

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

.changeset/brave-poems-kneel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@mcp-tool-kit/shared': patch
3+
---
4+
5+
feat: add utility to remove empty directories and check file existence

packages/shared/src/projectSetup.ts

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { setTimeout as sleep } from 'timers/promises'
2-
import { readdir, readFile, rename, stat, writeFile, cp, mkdir, unlink } from 'fs/promises'
2+
import { readdir, readFile, rename, stat, writeFile, cp, mkdir, unlink, rmdir, access, constants } from 'fs/promises'
33
import { join } from 'path'
44
import { spawn } from 'child_process'
55
import Handlebars from 'handlebars'
@@ -56,6 +56,31 @@ async function compileTemplateFiles(
5656
}
5757
}
5858

59+
async function removeEmptyDirectories(currentDir: string) {
60+
const items = await readdir(currentDir, { recursive: true, withFileTypes: true })
61+
const directories = items
62+
.filter(item => item.isDirectory())
63+
.map(item => join(item.parentPath || currentDir, item.name))
64+
.sort((a, b) => b.split('/').length - a.split('/').length)
65+
for (const dirPath of directories) {
66+
try {
67+
const dirItems = await readdir(dirPath)
68+
if (dirItems.length === 0) {
69+
await rmdir(dirPath)
70+
}
71+
} catch {}
72+
}
73+
}
74+
75+
export async function fileExists(path: string) {
76+
try {
77+
await access(path, constants.F_OK)
78+
return true
79+
} catch {
80+
return false
81+
}
82+
}
83+
5984
export function installDependencies(currentDir: string) {
6085
return new Promise<void>((resolve, reject) => {
6186
const npm = spawn('npm', ['install'], {
@@ -89,41 +114,10 @@ export async function createProject(
89114
await mkdir(targetPath, { recursive: true })
90115
await cp(templatePath, targetPath, {
91116
recursive: true,
92-
filter: () => {
93-
// const relativePath = relative(templatePath, src)
94-
// const fileName = basename(src)
95-
// console.log('relativePath', relativePath)
96-
// console.log('fileName', fileName)
97-
// if (plugins === null) {
98-
// return true
99-
// }
100-
// if (!plugins.includes('github-action') && relativePath.startsWith('_github')) {
101-
// return false
102-
// }
103-
// if (
104-
// !plugins.includes('vitest') &&
105-
// (relativePath.startsWith('tests') ||
106-
// ['vitest.config.js', 'vitest.setup.js', 'vitest.config.ts', 'vitest.setup.ts'].includes(relativePath))
107-
// ) {
108-
// return false
109-
// }
110-
// if (
111-
// !plugins.includes('style') &&
112-
// ['eslint.config.js', '_prettierrc', 'lint-staged.config.js', '_husky/pre-commit'].includes(relativePath)
113-
// ) {
114-
// return false
115-
// }
116-
// if (!plugins.includes('commitlint') && ['commitlint.config.js', '_husky/commit-msg'].includes(relativePath)) {
117-
// return false
118-
// }
119-
// if (!plugins.includes('changelog') && ['changelog-option.js'].includes(relativePath)) {
120-
// return false
121-
// }
122-
return true
123-
},
124117
})
125118
await renameFiles(targetPath)
126119
await compileTemplateFiles(targetPath, templateData)
120+
await removeEmptyDirectories(targetPath)
127121
}
128122

129123
export { sleep }

0 commit comments

Comments
 (0)