Skip to content
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

fix(emptyOutDir): never remove .git #3043

Merged
merged 1 commit into from Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 30 additions & 28 deletions packages/vite/src/node/build.ts
Expand Up @@ -471,13 +471,8 @@ async function doBuild(
watcher.on('event', (event) => {
if (event.code === 'BUNDLE_START') {
config.logger.info(chalk.cyanBright(`\nbuild started...`))

// clean previous files
if (options.write) {
emptyDir(outDir)
if (fs.existsSync(config.publicDir)) {
copyDir(config.publicDir, outDir)
}
prepareOutDir(outDir, options.emptyOutDir, config)
}
} else if (event.code === 'BUNDLE_END') {
event.result.close()
Expand All @@ -504,28 +499,7 @@ async function doBuild(
}

if (options.write) {
// warn if outDir is outside of root
if (fs.existsSync(outDir)) {
const inferEmpty = options.emptyOutDir === null
if (
options.emptyOutDir ||
(inferEmpty && normalizePath(outDir).startsWith(config.root + '/'))
) {
emptyDir(outDir)
} else if (inferEmpty) {
config.logger.warn(
chalk.yellow(
`\n${chalk.bold(`(!)`)} outDir ${chalk.white.dim(
outDir
)} is not inside project root and will not be emptied.\n` +
`Use --emptyOutDir to override.\n`
)
)
}
}
if (fs.existsSync(config.publicDir)) {
copyDir(config.publicDir, outDir)
}
prepareOutDir(outDir, options.emptyOutDir, config)
}

if (Array.isArray(outputs)) {
Expand All @@ -543,6 +517,34 @@ async function doBuild(
}
}

function prepareOutDir(
outDir: string,
emptyOutDir: boolean | null,
config: ResolvedConfig
) {
if (fs.existsSync(outDir)) {
if (
emptyOutDir == null &&
!normalizePath(outDir).startsWith(config.root + '/')
) {
// warn if outDir is outside of root
config.logger.warn(
chalk.yellow(
`\n${chalk.bold(`(!)`)} outDir ${chalk.white.dim(
outDir
)} is not inside project root and will not be emptied.\n` +
`Use --emptyOutDir to override.\n`
)
)
} else if (emptyOutDir !== false) {
emptyDir(outDir, ['.git'])
}
}
if (fs.existsSync(config.publicDir)) {
copyDir(config.publicDir, outDir)
}
}

function getPkgName(root: string) {
const { name } = JSON.parse(lookupFile(root, ['package.json']) || `{}`)

Expand Down
12 changes: 8 additions & 4 deletions packages/vite/src/node/utils.ts
Expand Up @@ -323,11 +323,15 @@ export function writeFile(
fs.writeFileSync(filename, content)
}

export function emptyDir(dir: string): void {
if (!fs.existsSync(dir)) {
return
}
/**
* Delete every file and subdirectory. **The given directory must exist.**
* Pass an optional `skip` array to preserve files in the root directory.
*/
export function emptyDir(dir: string, skip?: string[]): void {
for (const file of fs.readdirSync(dir)) {
if (skip?.includes(file)) {
continue
}
const abs = path.resolve(dir, file)
// baseline is Node 12 so can't use rmSync :(
if (fs.lstatSync(abs).isDirectory()) {
Expand Down