优化加载脚本逻辑

This commit is contained in:
YHH
2025-10-15 09:19:30 +08:00
parent cbfe09b5e9
commit 285279629e
3 changed files with 21 additions and 9 deletions

View File

@@ -46,17 +46,24 @@ async fn open_project_dialog(app: AppHandle) -> Result<Option<String>, String> {
#[tauri::command] #[tauri::command]
fn scan_directory(path: String, pattern: String) -> Result<Vec<String>, String> { fn scan_directory(path: String, pattern: String) -> Result<Vec<String>, String> {
use glob::glob; use glob::glob;
use std::path::Path; use std::path::{Path, MAIN_SEPARATOR};
let base_path = Path::new(&path); let base_path = Path::new(&path);
if !base_path.exists() { if !base_path.exists() {
return Err(format!("Directory does not exist: {}", path)); return Err(format!("Directory does not exist: {}", path));
} }
let glob_pattern = format!("{}/{}", path, pattern); let separator = if path.contains('\\') { '\\' } else { '/' };
let glob_pattern = format!("{}{}{}", path.trim_end_matches(&['/', '\\'][..]), separator, pattern);
let normalized_pattern = if cfg!(windows) {
glob_pattern.replace('/', "\\")
} else {
glob_pattern.replace('\\', "/")
};
let mut files = Vec::new(); let mut files = Vec::new();
match glob(&glob_pattern) { match glob(&normalized_pattern) {
Ok(entries) => { Ok(entries) => {
for entry in entries { for entry in entries {
match entry { match entry {

View File

@@ -169,7 +169,7 @@ function App() {
if (componentsPath) { if (componentsPath) {
const componentInfos = await discoveryService.scanComponents({ const componentInfos = await discoveryService.scanComponents({
basePath: componentsPath, basePath: componentsPath,
pattern: '**/*Component.ts', pattern: '**/*.ts',
scanFunction: TauriAPI.scanDirectory, scanFunction: TauriAPI.scanDirectory,
readFunction: TauriAPI.readFileContent readFunction: TauriAPI.readFileContent
}); });

View File

@@ -81,10 +81,14 @@ export class ProjectService implements IService {
} }
public getComponentsPath(): string | null { public getComponentsPath(): string | null {
if (!this.currentProject || !this.projectConfig?.componentsPath) { if (!this.currentProject) {
return null; return null;
} }
return `${this.currentProject.path}/${this.projectConfig.componentsPath}`; if (!this.projectConfig?.componentsPath) {
return this.currentProject.path;
}
const sep = this.currentProject.path.includes('\\') ? '\\' : '/';
return `${this.currentProject.path}${sep}${this.projectConfig.componentsPath}`;
} }
private async validateProject(projectPath: string): Promise<ProjectInfo> { private async validateProject(projectPath: string): Promise<ProjectInfo> {
@@ -96,7 +100,8 @@ export class ProjectService implements IService {
name: projectName name: projectName
}; };
const configPath = `${projectPath}/ecs-editor.config.json`; const sep = projectPath.includes('\\') ? '\\' : '/';
const configPath = `${projectPath}${sep}ecs-editor.config.json`;
try { try {
projectInfo.configPath = configPath; projectInfo.configPath = configPath;
@@ -111,8 +116,8 @@ export class ProjectService implements IService {
private async loadConfig(configPath: string): Promise<ProjectConfig> { private async loadConfig(configPath: string): Promise<ProjectConfig> {
return { return {
projectType: 'cocos', projectType: 'cocos',
componentsPath: 'assets/scripts/components', componentsPath: '',
componentPattern: '**/*Component.ts', componentPattern: '**/*.ts',
buildOutput: 'temp/editor-components' buildOutput: 'temp/editor-components'
}; };
} }