2024-01-01 14:58:21 -05:00
|
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-08-19 19:37:05 -04:00
|
|
|
|
|
|
|
|
|
use crate::args::InitFlags;
|
2022-12-08 00:34:28 -05:00
|
|
|
|
use crate::colors;
|
2022-08-29 14:19:54 -04:00
|
|
|
|
use crate::deno_std;
|
|
|
|
|
use deno_core::anyhow::Context;
|
|
|
|
|
use deno_core::error::AnyError;
|
2022-09-02 11:59:36 -04:00
|
|
|
|
use log::info;
|
2022-08-19 19:37:05 -04:00
|
|
|
|
use std::io::Write;
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
|
|
fn create_file(
|
|
|
|
|
dir: &Path,
|
|
|
|
|
filename: &str,
|
|
|
|
|
content: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2023-09-12 06:55:26 -04:00
|
|
|
|
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(())
|
|
|
|
|
}
|
2022-08-19 19:37:05 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn init_project(init_flags: InitFlags) -> Result<(), AnyError> {
|
|
|
|
|
let cwd =
|
|
|
|
|
std::env::current_dir().context("Can't read current working directory.")?;
|
|
|
|
|
let dir = if let Some(dir) = &init_flags.dir {
|
|
|
|
|
let dir = cwd.join(dir);
|
|
|
|
|
std::fs::create_dir_all(&dir)?;
|
|
|
|
|
dir
|
|
|
|
|
} else {
|
|
|
|
|
cwd
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let main_ts = include_str!("./templates/main.ts");
|
|
|
|
|
create_file(&dir, "main.ts", main_ts)?;
|
|
|
|
|
|
|
|
|
|
let main_test_ts = include_str!("./templates/main_test.ts")
|
2023-03-22 23:11:18 -04:00
|
|
|
|
.replace("{CURRENT_STD_URL}", deno_std::CURRENT_STD_URL_STR);
|
2022-08-19 19:37:05 -04:00
|
|
|
|
create_file(&dir, "main_test.ts", &main_test_ts)?;
|
2023-08-29 15:58:56 -04:00
|
|
|
|
create_file(&dir, "deno.json", include_str!("./templates/deno.json"))?;
|
2022-12-08 00:34:28 -05:00
|
|
|
|
|
|
|
|
|
info!("✅ {}", colors::green("Project initialized"));
|
|
|
|
|
info!("");
|
|
|
|
|
info!("{}", colors::gray("Run these commands to get started"));
|
|
|
|
|
info!("");
|
2022-08-19 19:37:05 -04:00
|
|
|
|
if let Some(dir) = init_flags.dir {
|
2022-09-02 11:59:36 -04:00
|
|
|
|
info!(" cd {}", dir);
|
2022-12-08 00:34:28 -05:00
|
|
|
|
info!("");
|
2022-08-19 19:37:05 -04:00
|
|
|
|
}
|
2022-12-17 17:26:25 -05:00
|
|
|
|
info!(" {}", colors::gray("# Run the program"));
|
2022-09-02 11:59:36 -04:00
|
|
|
|
info!(" deno run main.ts");
|
2022-12-08 00:34:28 -05:00
|
|
|
|
info!("");
|
|
|
|
|
info!(
|
|
|
|
|
" {}",
|
2022-12-17 17:26:25 -05:00
|
|
|
|
colors::gray("# Run the program and watch for file changes")
|
2022-12-08 00:34:28 -05:00
|
|
|
|
);
|
|
|
|
|
info!(" deno task dev");
|
|
|
|
|
info!("");
|
2022-12-17 17:26:25 -05:00
|
|
|
|
info!(" {}", colors::gray("# Run the tests"));
|
2022-09-02 11:59:36 -04:00
|
|
|
|
info!(" deno test");
|
2022-08-19 19:37:05 -04:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|