Finish off init command

This commit is contained in:
SkyfallWasTaken 2024-07-15 20:04:33 +01:00
parent 6164542183
commit c681c7b125
6 changed files with 242 additions and 15 deletions

178
Cargo.lock generated
View file

@ -26,6 +26,21 @@ dependencies = [
"memchr",
]
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "anstream"
version = "0.6.14"
@ -161,6 +176,19 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401"
dependencies = [
"android-tzdata",
"iana-time-zone",
"num-traits",
"serde",
"windows-targets 0.52.6",
]
[[package]]
name = "clap"
version = "4.5.9"
@ -263,6 +291,41 @@ version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
[[package]]
name = "darling"
version = "0.20.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.20.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim",
"syn",
]
[[package]]
name = "darling_macro"
version = "0.20.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
dependencies = [
"darling_core",
"quote",
"syn",
]
[[package]]
name = "deranged"
version = "0.3.11"
@ -270,6 +333,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4"
dependencies = [
"powerfmt",
"serde",
]
[[package]]
@ -298,8 +362,10 @@ dependencies = [
"exitcode",
"gix-config",
"log",
"maplit",
"owo-colors 4.0.0",
"reqwest",
"serde_json",
"tokio",
]
@ -309,6 +375,7 @@ version = "0.1.0"
dependencies = [
"serde",
"serde_json",
"serde_with",
"thiserror",
"tokio",
]
@ -717,13 +784,19 @@ dependencies = [
"futures-core",
"futures-sink",
"http",
"indexmap",
"indexmap 2.2.6",
"slab",
"tokio",
"tokio-util",
"tracing",
]
[[package]]
name = "hashbrown"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "hashbrown"
version = "0.14.5"
@ -742,6 +815,12 @@ version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "home"
version = "0.5.9"
@ -870,6 +949,35 @@ dependencies = [
"tracing",
]
[[package]]
name = "iana-time-zone"
version = "0.1.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "idna"
version = "0.5.0"
@ -886,6 +994,17 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]]
name = "indexmap"
version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
dependencies = [
"autocfg",
"hashbrown 0.12.3",
"serde",
]
[[package]]
name = "indexmap"
version = "2.2.6"
@ -893,7 +1012,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
dependencies = [
"equivalent",
"hashbrown",
"hashbrown 0.14.5",
"serde",
]
[[package]]
@ -957,6 +1077,12 @@ version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
[[package]]
name = "maplit"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d"
[[package]]
name = "memchr"
version = "2.7.4"
@ -1021,6 +1147,15 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.16.0"
@ -1459,6 +1594,36 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_with"
version = "3.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cecfa94848272156ea67b2b1a53f20fc7bc638c4a46d2f8abde08f05f4b857"
dependencies = [
"base64",
"chrono",
"hex",
"indexmap 1.9.3",
"indexmap 2.2.6",
"serde",
"serde_derive",
"serde_json",
"serde_with_macros",
"time",
]
[[package]]
name = "serde_with_macros"
version = "3.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8fee4991ef4f274617a51ad4af30519438dacb2f56ac773b08a1922ff743350"
dependencies = [
"darling",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "sha1_smol"
version = "1.0.0"
@ -1975,6 +2140,15 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "windows-core"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-sys"
version = "0.48.0"

View file

@ -22,3 +22,5 @@ env_logger = "0.11.3"
dialoguer = "0.11.0"
camino = "1.1.7"
gix-config = "0.37.0"
maplit = "1.0.2"
serde_json = "1.0.120"

View file

@ -24,5 +24,6 @@ pub enum Command {
Test,
/// Create a package.json file
#[command(aliases = ["create", "innit"])]
Init,
}

View file

@ -3,7 +3,11 @@ use std::env;
use camino::Utf8PathBuf;
use color_eyre::Result;
use dialoguer::{theme::ColorfulTheme, Confirm, Input};
use dinopkg_package_json::PackageJson;
use gix_config::File as GitConfigFile;
use maplit::hashmap;
use owo_colors::OwoColorize;
use tokio::fs;
pub async fn init() -> Result<()> {
// Get some project/env specific info to make the defaults more relevant
@ -11,7 +15,9 @@ pub async fn init() -> Result<()> {
let current_dir_name = current_dir.file_name().unwrap_or("package");
// FIXME: this blocks the event loop
let git_config_file = GitConfigFile::from_git_dir(current_dir.join(".git").into());
let git_repo_url = git_config_file.map(|config| config
let git_repo_url = git_config_file
.map(|config| {
config
.section("remote", Some("origin".into()))
.ok()
.and_then(|remote_section| {
@ -19,7 +25,8 @@ pub async fn init() -> Result<()> {
.body()
.value("url")
.map(|url| url.to_string())
}))
})
})
.ok()
.flatten()
.map(|url| url.replace("git@github.com", "https://github.com/"));
@ -30,9 +37,13 @@ pub async fn init() -> Result<()> {
.default(current_dir_name.into())
.interact_text()?;
let version: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Version")
.with_prompt("Package version")
.default("1.0.0".into())
.interact_text()?;
let private = Confirm::with_theme(&ColorfulTheme::default())
.with_prompt("Is this a private package?")
.default(true)
.interact()?;
let description: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Description")
.allow_empty(true)
@ -58,15 +69,28 @@ pub async fn init() -> Result<()> {
.default("MIT".into())
.interact_text()?;
dbg!(
package_name,
let package_json = PackageJson {
name: package_name,
version,
description,
entry_point,
test_command,
git_repository,
author,
license
author: Some(author),
repository: Some(git_repository),
license: Some(license),
description: Some(description),
private,
main: Some(entry_point),
scripts: Some(hashmap! {
"test".into() => test_command,
}),
dependencies: None,
dev_dependencies: None,
};
let output = serde_json::to_string_pretty(&package_json)?;
println!(
"\nI will write the following output to {}:\n{output}\n",
"`package.json`".purple()
);
let yes = Confirm::with_theme(&ColorfulTheme::default())
@ -74,9 +98,14 @@ pub async fn init() -> Result<()> {
.default(true)
.interact()?;
if yes {
println!("Writing.")
fs::write("package.json", output).await?;
println!(
"Successfully wrote new {} for {}!",
"`package.json`".purple(),
package_json.name.purple()
);
} else {
println!("Cancelled.")
println!("{}", "Cancelled.".bold().red())
}
Ok(())

View file

@ -6,5 +6,6 @@ edition = "2021"
[dependencies]
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
serde_with = "3.9.0"
thiserror = "1.0.61"
tokio = { version = "1.38.0", features = ["fs"], optional = true }

View file

@ -2,21 +2,41 @@ use std::collections::HashMap;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, skip_serializing_none};
mod util;
#[serde_as]
#[skip_serializing_none]
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PackageJson {
pub name: String,
pub version: String,
pub author: Option<String>,
#[serde(default = "default_as_false")]
#[serde(skip_serializing_if = "is_false")]
pub private: bool,
pub license: Option<String>,
pub description: Option<String>,
pub main: Option<String>,
pub repository: Option<String>,
pub scripts: Option<Scripts>,
pub dependencies: Option<Dependencies>,
pub dev_dependencies: Option<Dependencies>,
}
// serde :/
fn is_false(value: &bool) -> bool {
!*value
}
fn default_as_false() -> bool {
false
}
pub type Scripts = HashMap<String, String>;
pub type Dependencies = HashMap<String, String>;