|
1 | 1 | 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' |
3 | 3 | import { join } from 'path'
|
4 | 4 | import { spawn } from 'child_process'
|
5 | 5 | import Handlebars from 'handlebars'
|
@@ -56,6 +56,31 @@ async function compileTemplateFiles(
|
56 | 56 | }
|
57 | 57 | }
|
58 | 58 |
|
| 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 | + |
59 | 84 | export function installDependencies(currentDir: string) {
|
60 | 85 | return new Promise<void>((resolve, reject) => {
|
61 | 86 | const npm = spawn('npm', ['install'], {
|
@@ -89,41 +114,10 @@ export async function createProject(
|
89 | 114 | await mkdir(targetPath, { recursive: true })
|
90 | 115 | await cp(templatePath, targetPath, {
|
91 | 116 | 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 |
| - }, |
124 | 117 | })
|
125 | 118 | await renameFiles(targetPath)
|
126 | 119 | await compileTemplateFiles(targetPath, templateData)
|
| 120 | + await removeEmptyDirectories(targetPath) |
127 | 121 | }
|
128 | 122 |
|
129 | 123 | export { sleep }
|
0 commit comments