From 881c9a80785a3174cacf90250c7b45f64f2b5003 Mon Sep 17 00:00:00 2001 From: soul <104170577@qq.com> Date: Sat, 16 Dec 2023 20:25:38 +0800 Subject: [PATCH] feat: support service file name template --- .eslintignore | 13 - .eslintrc.cjs | 31 -- src-tauri/src/services/config.rs | 5 + src-tauri/src/structs/config.rs | 2 + src-tauri/src/structs/request.rs | 55 ++-- src/lib/components/ProcessingModal.svelte | 145 +++++----- src/lib/components/Prompt.svelte | 57 ++-- src/lib/images/github.svg | 16 -- src/lib/images/svelte-logo.svg | 1 - src/lib/images/svelte-welcome.png | Bin 360807 -> 0 bytes src/lib/images/svelte-welcome.webp | Bin 115470 -> 0 bytes src/lib/main.ts | 9 - src/lib/modules/ConfigModule/index.svelte | 253 +++++++++-------- .../components/ConfigModal.svelte | 153 +++++----- .../ServiceModule/components/Node.svelte | 185 ++++++------ .../ServiceModule/components/TypesTree.svelte | 144 +++++----- src/lib/modules/ServiceModule/index.svelte | 45 ++- .../TypeModule/components/ConfigModal.svelte | 158 +++++------ .../ProjectTab/AddProjectModal.svelte | 145 +++++----- .../components/ProjectTab/Category.svelte | 181 ++++++------ .../components/ProjectTab/Interface.svelte | 107 ++++--- .../components/ProjectTab/index.svelte | 267 ++++++++---------- src/lib/styles.css | 0 src/lib/types/public.ts | 1 + 24 files changed, 945 insertions(+), 1028 deletions(-) delete mode 100644 .eslintignore delete mode 100644 .eslintrc.cjs delete mode 100644 src/lib/images/github.svg delete mode 100644 src/lib/images/svelte-logo.svg delete mode 100644 src/lib/images/svelte-welcome.png delete mode 100644 src/lib/images/svelte-welcome.webp delete mode 100644 src/lib/main.ts delete mode 100644 src/lib/styles.css diff --git a/.eslintignore b/.eslintignore deleted file mode 100644 index 3897265..0000000 --- a/.eslintignore +++ /dev/null @@ -1,13 +0,0 @@ -.DS_Store -node_modules -/build -/.svelte-kit -/package -.env -.env.* -!.env.example - -# Ignore files for PNPM, NPM and YARN -pnpm-lock.yaml -package-lock.json -yarn.lock diff --git a/.eslintrc.cjs b/.eslintrc.cjs deleted file mode 100644 index 8304ad3..0000000 --- a/.eslintrc.cjs +++ /dev/null @@ -1,31 +0,0 @@ -/** @type { import("eslint").Linter.FlatConfig } */ -module.exports = { - root: true, - extends: [ - 'eslint:recommended', - 'plugin:@typescript-eslint/recommended', - 'plugin:svelte/recommended', - 'prettier' - ], - parser: '@typescript-eslint/parser', - plugins: ['@typescript-eslint'], - parserOptions: { - sourceType: 'module', - ecmaVersion: 2020, - extraFileExtensions: ['.svelte'] - }, - env: { - browser: true, - es2017: true, - node: true - }, - overrides: [ - { - files: ['*.svelte'], - parser: 'svelte-eslint-parser', - parserOptions: { - parser: '@typescript-eslint/parser' - } - } - ] -}; diff --git a/src-tauri/src/services/config.rs b/src-tauri/src/services/config.rs index c30a1d5..65b0b77 100644 --- a/src-tauri/src/services/config.rs +++ b/src-tauri/src/services/config.rs @@ -141,6 +141,7 @@ pub struct UpdateConfigRequest { pub request_template: Option, pub header_template: Option, pub request_path: Option, + pub file_name_template: Option } // 更新类型配置 @@ -193,6 +194,10 @@ pub async fn update_config( config_json.header_template = Some(header_template.clone()); } + if let Some(file_name_template) = &form.file_name_template { + config_json.file_name_template = Some(file_name_template.clone()); + } + if let Some(request_path) = &form.request_path { config_json.request_path = Some(PathBuf::from(request_path)); config_json.request_full_path = Some(join_path( diff --git a/src-tauri/src/structs/config.rs b/src-tauri/src/structs/config.rs index 8b1cc00..9c0a872 100644 --- a/src-tauri/src/structs/config.rs +++ b/src-tauri/src/structs/config.rs @@ -38,6 +38,7 @@ pub struct ConfigJson { pub request_full_path: Option, pub request_template: Option, pub header_template: Option, + pub file_name_template : Option } impl ConfigJson { @@ -54,6 +55,7 @@ impl ConfigJson { request_full_path:None, request_template: None, header_template: None, + file_name_template: None } } } diff --git a/src-tauri/src/structs/request.rs b/src-tauri/src/structs/request.rs index e720f37..dd48d37 100644 --- a/src-tauri/src/structs/request.rs +++ b/src-tauri/src/structs/request.rs @@ -34,6 +34,8 @@ pub struct Request { pub request_template: Option, pub header_template: Option, pub request_full_path: Option, + // My_$1 : dirname => My_dirname.ts + pub file_name_template: Option, } impl Request { @@ -47,6 +49,7 @@ impl Request { request_template: config_json.request_template, header_template: config_json.header_template, request_full_path: config_json.request_full_path, + file_name_template: config_json.file_name_template, } } @@ -87,15 +90,8 @@ impl Request { } pub fn write_ts(&self, path: &PathBuf) { - let types_root_path = self.context.types_full_path.clone().unwrap(); - let sub_path = match path == &types_root_path { - true => None, - false => Some(path.strip_prefix(&types_root_path).unwrap().to_path_buf()), - }; - - println!("sub_path: {:?}", sub_path); - - let read_res = fs::read_dir(path).unwrap(); + let sub_path = self.get_type_relative_path(path); + let write_path = self.get_write_path(&sub_path); let mut ts_string = match &self.header_template { Some(template) => format!("{}\n", template.clone()), @@ -105,14 +101,7 @@ impl Request { let mut import_list: Vec = vec![]; let mut export_list: Vec = vec![]; - let write_path = match &sub_path { - Some(sub_path) => { - self.request_full_path.clone().unwrap().join(&sub_path) - }, - None => self.request_full_path.clone().unwrap().join("index"), - }; - - for dirs in read_res.into_iter() { + for dirs in fs::read_dir(path).unwrap().into_iter() { let dir = dirs.unwrap(); let file_type = dir.file_type().unwrap(); let file_path = dir.path(); @@ -151,12 +140,40 @@ impl Request { let parent = write_path.parent().unwrap(); - println!("{:#?}", write_path); - fs::create_dir_all(parent).unwrap(); + fs::write(format!("{}.ts", write_path.to_str().unwrap()), ts_string).unwrap(); } + // 获取 type 文件的相对路径 + fn get_type_relative_path(&self, path: &PathBuf) -> Option { + let types_root_path = self.context.types_full_path.clone().unwrap(); + match path == &types_root_path { + true => None, + false => Some(path.strip_prefix(&types_root_path).unwrap().to_path_buf()), + } + } + + // 获取写入 service 文件的路径 + fn get_write_path(&self, sub_path: &Option) -> PathBuf { + match &sub_path { + Some(sub_path) => { + let mut write_path = self.request_full_path.clone().unwrap().join(&sub_path); + + if let Some(template) = self.file_name_template.clone() { + if let Some(file_name) = write_path.file_name() { + let real_file_name = template.replace("$1", file_name.to_str().unwrap()); + write_path.set_file_name(real_file_name); + } + } + + write_path + } + None => self.request_full_path.clone().unwrap().join("index"), + } + } + + // 检查生成service的 type 文件是否有 Request/Response interface fn check_file(&self, file_path: &PathBuf, file_name_without_ext: &String) -> bool { let req = format!("{}Request", file_name_without_ext); let resp = format!("{}Response", file_name_without_ext); diff --git a/src/lib/components/ProcessingModal.svelte b/src/lib/components/ProcessingModal.svelte index 0dd29b1..30ff4da 100644 --- a/src/lib/components/ProcessingModal.svelte +++ b/src/lib/components/ProcessingModal.svelte @@ -1,92 +1,89 @@ -
- {#each log_list as log} - {#if log.is_success} -

{log.msg}

- {:else} -

{log.msg}

- {/if} - {/each} -
+
+ {#each log_list as log} + {#if log.is_success} +

{log.msg}

+ {:else} +

{log.msg}

+ {/if} + {/each} +
- +
diff --git a/src/lib/modules/ServiceModule/components/ConfigModal.svelte b/src/lib/modules/ServiceModule/components/ConfigModal.svelte index 71aba15..4464e25 100644 --- a/src/lib/modules/ServiceModule/components/ConfigModal.svelte +++ b/src/lib/modules/ServiceModule/components/ConfigModal.svelte @@ -1,81 +1,96 @@ { - open = false; - }} - on:submit={on_submit} + bind:open + modalHeading="初始化配置" + preventCloseOnClickOutside + primaryButtonText="提交" + secondaryButtonText="取消" + on:click:button--secondary={() => { + open = false; + }} + on:submit={on_submit} > - (form.request_path = String(e.detail))} - labelText="Request 目录文件夹路径" - placeholder="你想要把定义 request 的 ts 文件放到哪个文件夹下?" - /> - (form.request_template = String(e.detail))} - labelText="请求模板字符串" - placeholder="请输入请求模板字符串" - /> - -

$1: 请求名

-

$2: 请求类型

-

$3: 返回类型

-

$4: 接口地址

-
- (form.header_template = String(e.detail))} - labelText="请求文件首部字符串" - placeholder="请输入请求文件首部字符串" - /> + (form.request_path = String(e.detail))} + labelText="Request 目录文件夹路径" + placeholder="你想要把定义 request 的 ts 文件放到哪个文件夹下?" + /> + (form.request_template = String(e.detail))} + labelText="请求模板字符串" + placeholder="请输入请求模板字符串" + /> + +

$1: 请求名

+

$2: 请求类型

+

$3: 返回类型

+

$4: 接口地址

+
+ (form.header_template = String(e.detail))} + labelText="请求文件首部字符串" + placeholder="请输入请求文件首部字符串" + /> + (form.file_name_template = String(e.detail))} + labelText="文件名模板字符串" + placeholder="请输入文件名模板字符串" + /> + +

$1: 文件名

+
diff --git a/src/lib/modules/ServiceModule/components/TypesTree.svelte b/src/lib/modules/ServiceModule/components/TypesTree.svelte index 034c2a1..27289bf 100644 --- a/src/lib/modules/ServiceModule/components/TypesTree.svelte +++ b/src/lib/modules/ServiceModule/components/TypesTree.svelte @@ -1,91 +1,91 @@
-
-
对应的接口树:
- -
-
- - -
+
+
对应的接口树:
+ +
+
+ + +
- {#each list as item} - - {/each} + {#each list as item} + + {/each}
diff --git a/src/lib/modules/ServiceModule/index.svelte b/src/lib/modules/ServiceModule/index.svelte index 619d3a8..f3441c5 100644 --- a/src/lib/modules/ServiceModule/index.svelte +++ b/src/lib/modules/ServiceModule/index.svelte @@ -1,33 +1,32 @@ {#if need_init} - + {/if} {#if load_types} - + {/if} - diff --git a/src/lib/modules/TypeModule/components/ConfigModal.svelte b/src/lib/modules/TypeModule/components/ConfigModal.svelte index c2bb4ec..532d591 100644 --- a/src/lib/modules/TypeModule/components/ConfigModal.svelte +++ b/src/lib/modules/TypeModule/components/ConfigModal.svelte @@ -1,92 +1,90 @@ { - open = false; - }} - on:submit={on_submit} + bind:open + modalHeading="初始化配置" + preventCloseOnClickOutside + selectorPrimaryFocus="#source-code" + primaryButtonText="提交" + secondaryButtonText="取消" + on:click:button--secondary={() => { + open = false; + }} + on:submit={on_submit} > - (form.base_url = String(e.detail))} - labelText="Yapi地址根路径" - placeholder="请输入Yapi地址根路径" - /> - (form.types_path = String(e.detail))} - labelText="项目类型目录文件夹路径" - placeholder="你想要把接口ts文件放到哪个文件夹下?" - /> - (form.rate_limit = Number(e.detail))} - labelText="请求yapi-openapi最大并行请求数" - placeholder="请输入请求yapi-openapi最大并行请求数" - type="number" - /> - (form.break_seconds = Number(e.detail))} - labelText="请求yapi-openapi时间间隔" - placeholder="请输入请求yapi-openapi时间间隔" - type="number" - /> + (form.base_url = String(e.detail))} + labelText="Yapi地址根路径" + placeholder="请输入Yapi地址根路径" + /> + (form.types_path = String(e.detail))} + labelText="项目类型目录文件夹路径" + placeholder="你想要把接口ts文件放到哪个文件夹下?" + /> + (form.rate_limit = Number(e.detail))} + labelText="请求yapi-openapi最大并行请求数" + placeholder="请输入请求yapi-openapi最大并行请求数" + type="number" + /> + (form.break_seconds = Number(e.detail))} + labelText="请求yapi-openapi时间间隔" + placeholder="请输入请求yapi-openapi时间间隔" + type="number" + /> diff --git a/src/lib/modules/TypeModule/components/ProjectTab/Interface.svelte b/src/lib/modules/TypeModule/components/ProjectTab/Interface.svelte index 1015656..82364bb 100644 --- a/src/lib/modules/TypeModule/components/ProjectTab/Interface.svelte +++ b/src/lib/modules/TypeModule/components/ProjectTab/Interface.svelte @@ -1,68 +1,65 @@
-
- {data.name} -
-
-
update_interface(data.id)} - on:keypress|stopPropagation={() => update_interface(data.id)} - role="button" - tabindex="0" - > - 更新接口 -
-
+
+ {data.name} +
+
+
update_interface(data.id)} + on:keypress|stopPropagation={() => update_interface(data.id)} + role="button" + tabindex="0" + > + 更新接口 +
+
diff --git a/src/lib/modules/TypeModule/components/ProjectTab/index.svelte b/src/lib/modules/TypeModule/components/ProjectTab/index.svelte index c1a6910..b5ada18 100644 --- a/src/lib/modules/TypeModule/components/ProjectTab/index.svelte +++ b/src/lib/modules/TypeModule/components/ProjectTab/index.svelte @@ -1,156 +1,133 @@ - +
- - - + + +
- (banInitModal = false)} /> - + (banInitModal = false)} /> +
- {#each project_list as project} - - {/each} - - - {#each project_list as project} - -
-
- - -
- {#each project.categories as category} - - {/each} -
-
- {/each} -
+ {#each project_list as project} + + {/each} + + + {#each project_list as project} + +
+
+ + +
+ {#each project.categories as category} + + {/each} +
+
+ {/each} +
diff --git a/src/lib/styles.css b/src/lib/styles.css deleted file mode 100644 index e69de29..0000000 diff --git a/src/lib/types/public.ts b/src/lib/types/public.ts index c344e5b..85d7e97 100644 --- a/src/lib/types/public.ts +++ b/src/lib/types/public.ts @@ -9,6 +9,7 @@ export interface Config { request_path?: string, request_template?: string, header_template?: string + file_name_template?: string }