From 8f16828432861d489ec22f369e3ee659ae75cbd4 Mon Sep 17 00:00:00 2001 From: Hector Castelli Zacharias Date: Tue, 9 Jan 2024 12:25:59 +0100 Subject: [PATCH] add content directory --- fixtures/commands/initialize/content.json | 6 ++++ fixtures/commands/initialize/content.yaml | 4 +++ src/commands/initialize.rs | 34 +++++++++++++++++++++-- 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 fixtures/commands/initialize/content.json create mode 100644 fixtures/commands/initialize/content.yaml diff --git a/fixtures/commands/initialize/content.json b/fixtures/commands/initialize/content.json new file mode 100644 index 0000000..1d64c95 --- /dev/null +++ b/fixtures/commands/initialize/content.json @@ -0,0 +1,6 @@ +{ + "schema": "sample", + "renderOrder": 1, + "name": "Sample", + "number": 10 +} \ No newline at end of file diff --git a/fixtures/commands/initialize/content.yaml b/fixtures/commands/initialize/content.yaml new file mode 100644 index 0000000..40ed249 --- /dev/null +++ b/fixtures/commands/initialize/content.yaml @@ -0,0 +1,4 @@ +schema: sample +renderOrder: 1 +name: Sample +number: 10 diff --git a/src/commands/initialize.rs b/src/commands/initialize.rs index a48a42f..1b9f457 100644 --- a/src/commands/initialize.rs +++ b/src/commands/initialize.rs @@ -32,7 +32,8 @@ impl Command for Initialize { self.initialize_module_file(&root, fs); self.initialize_authors(&root, fs); self.initialize_schema(&root, fs); - debug!("Create contents directory"); + self.initialize_content(&root, fs); + trace!("Finished initializing") } } @@ -111,11 +112,11 @@ impl Initialize { trace!("Creating sample schema"); let (file_name, contents) = match self.file_type { FileType::Json => ( - "schema.json", + "sample.json", include_str!("../../fixtures/commands/initialize/schema.json"), ), FileType::Yaml => ( - "schema.yaml", + "sample.yaml", include_str!("../../fixtures/commands/initialize/schema.yaml"), ), }; @@ -123,4 +124,31 @@ impl Initialize { .expect("Sample schema file could not be created"); } } + + fn initialize_content(&self, root: &PathBuf, fs: &impl FileSystem) { + trace!("Initializing content directory"); + let dir = fs.create_dir_if_not_exists(&root.join("content")).unwrap(); + + let dir_children = fs.get_dir_children(dir.as_ref()); + debug!( + "Content directory has {} files", + dir_children.clone().map_or(0, |v| v.len()) + ); + + if dir_children.is_none() { + trace!("Creating sample content"); + let (file_name, contents) = match self.file_type { + FileType::Json => ( + "content.json", + include_str!("../../fixtures/commands/initialize/content.json"), + ), + FileType::Yaml => ( + "content.yaml", + include_str!("../../fixtures/commands/initialize/content.yaml"), + ), + }; + fs.create_file(&dir.join(file_name), contents) + .expect("Sample content file could not be created"); + } + } }