summaryrefslogtreecommitdiff
path: root/cli/tools/init
diff options
context:
space:
mode:
authorLeigh McCulloch <351529+leighmcculloch@users.noreply.github.com>2023-09-12 03:55:26 -0700
committerGitHub <noreply@github.com>2023-09-12 12:55:26 +0200
commit4a8b873111dbedde34b9bca702fb0b593fbf09a8 (patch)
treea33b88609f749e3dd62af244864d69aec1d2c07b /cli/tools/init
parentf32acb945e4f96b540c61705eb0d786fc4389d60 (diff)
fix(init): skip existing files instead of erroring (#20434)
### What Skip writing files from the template if the files already exist in the project directory. ### Why When I run deno init in a directory that already has a main.ts, or one of the other template files, I usually want to initialize a workspace around a file I've started working in. A hard error in this case seems counter productive. An informational message about what's being skipped seems sufficient. Close #20433
Diffstat (limited to 'cli/tools/init')
-rw-r--r--cli/tools/init/mod.rs23
1 files changed, 16 insertions, 7 deletions
diff --git a/cli/tools/init/mod.rs b/cli/tools/init/mod.rs
index f0da86006..9e46359c0 100644
--- a/cli/tools/init/mod.rs
+++ b/cli/tools/init/mod.rs
@@ -14,13 +14,22 @@ fn create_file(
filename: &str,
content: &str,
) -> Result<(), AnyError> {
- let mut file = std::fs::OpenOptions::new()
- .write(true)
- .create_new(true)
- .open(dir.join(filename))
- .with_context(|| format!("Failed to create {filename} file"))?;
- file.write_all(content.as_bytes())?;
- Ok(())
+ let path = dir.join(filename);
+ if path.exists() {
+ info!(
+ "ℹ️ {}",
+ colors::gray(format!("Skipped creating {filename} as it already exists"))
+ );
+ Ok(())
+ } else {
+ let mut file = std::fs::OpenOptions::new()
+ .write(true)
+ .create_new(true)
+ .open(path)
+ .with_context(|| format!("Failed to create {filename} file"))?;
+ file.write_all(content.as_bytes())?;
+ Ok(())
+ }
}
pub async fn init_project(init_flags: InitFlags) -> Result<(), AnyError> {