From 2033acd5dd269468cc1e8bbfcbc02c10f6e382a4 Mon Sep 17 00:00:00 2001 From: wwsun Date: Thu, 1 Aug 2024 09:44:14 +0800 Subject: [PATCH] fix: update fileTypes --- packages/core/src/helpers/string.ts | 34 ++++++-------- .../src/models/abstract-code-workspace.ts | 22 +++++----- .../core/src/models/abstract-workspace.ts | 4 +- packages/core/src/models/workspace.ts | 5 +++ packages/core/src/types.ts | 44 +++++++------------ packages/core/tests/helpers.test.ts | 10 ++--- packages/designer/src/dnd/use-dnd.ts | 2 +- 7 files changed, 53 insertions(+), 68 deletions(-) diff --git a/packages/core/src/helpers/string.ts b/packages/core/src/helpers/string.ts index 392e3d0e..57367bca 100644 --- a/packages/core/src/helpers/string.ts +++ b/packages/core/src/helpers/string.ts @@ -8,65 +8,57 @@ import { FileType } from './../types'; export function inferFileType(filename: string): FileType { // 增加 tangoConfigJson Module if (/\/tango\.config\.json$/.test(filename)) { - return FileType.TangoConfigJson; + return FileType.TangoConfigJsonFile; } if (/\/appJson\.json$/.test(filename)) { - return FileType.AppJson; + return FileType.AppJsonFile; } if (/\/package\.json$/.test(filename)) { - return FileType.PackageJson; + return FileType.PackageJsonFile; } if (/\/routes\.js$/.test(filename)) { - return FileType.RouteModule; + return FileType.JsRouteConfigFile; } // 所有 pages 下的 js 文件均认为是有效的 viewModule if (/\/pages\/.+\.jsx?$/.test(filename)) { - return FileType.JsxViewModule; + return FileType.JsViewFile; } // 所有 pages 下的 js 文件均认为是有效的 viewModule if (/\/pages\/.+\.schema\.json?$/.test(filename)) { - return FileType.JsonViewModule; + return FileType.JsonViewFile; } if (/\/(blocks|components)\/index\.js/.test(filename)) { - return FileType.ComponentsEntryModule; + return FileType.JsLocalComponentsEntryFile; } if (/\/services\/.+\.js$/.test(filename)) { - return FileType.ServiceModule; + return FileType.JsServiceFile; } if (/service\.js$/.test(filename)) { - return FileType.ServiceModule; + return FileType.JsServiceFile; } if (/\/stores\/index\.js$/.test(filename)) { - return FileType.StoreEntryModule; + return FileType.JsStoreEntryFile; } if (/\/stores\/.+\.js$/.test(filename)) { - return FileType.StoreModule; + return FileType.JsStoreFile; } if (/\.jsx?$/.test(filename)) { - return FileType.Module; + return FileType.JsFile; } if (/\.json$/.test(filename)) { - return FileType.Json; - } - - if (/\.less$/.test(filename)) { - return FileType.Less; - } - - if (/\.scss$/.test(filename)) { - return FileType.Scss; + return FileType.JsonFile; } return FileType.File; diff --git a/packages/core/src/models/abstract-code-workspace.ts b/packages/core/src/models/abstract-code-workspace.ts index ab5de267..4182b6cf 100644 --- a/packages/core/src/models/abstract-code-workspace.ts +++ b/packages/core/src/models/abstract-code-workspace.ts @@ -69,15 +69,15 @@ export abstract class AbstractCodeWorkspace extends AbstractWorkspace { module = new JsAppEntryFile(this, props); this.jsAppEntryFile = module; break; - case FileType.StoreEntryModule: + case FileType.JsStoreEntryFile: module = new JsStoreEntryFile(this, props); this.storeEntryModule = module; break; - case FileType.ComponentsEntryModule: + case FileType.JsLocalComponentsEntryFile: module = new JsLocalComponentsEntryFile(this, props); this.componentsEntryModule = module; break; - case FileType.RouteModule: { + case FileType.JsRouteConfigFile: { module = new JsRouteConfigFile(this, props); this.routeModule = module; // check if activeRoute exists @@ -87,29 +87,29 @@ export abstract class AbstractCodeWorkspace extends AbstractWorkspace { } break; } - case FileType.JsxViewModule: + case FileType.JsViewFile: module = new JsViewFile(this, props); break; - case FileType.ServiceModule: + case FileType.JsServiceFile: module = new JsServiceFile(this, props); this.serviceModules[module.name] = module; break; - case FileType.StoreModule: + case FileType.JsStoreFile: module = new JsStoreFile(this, props); this.storeModules[module.name] = module; break; - case FileType.Module: + case FileType.JsFile: module = new JsFile(this, props); break; - case FileType.PackageJson: + case FileType.PackageJsonFile: module = new JsonFile(this, props); this.packageJson = module; break; - case FileType.TangoConfigJson: + case FileType.TangoConfigJsonFile: module = new JsonFile(this, props); this.tangoConfigJson = module; break; - case FileType.Json: + case FileType.JsonFile: module = new JsonFile(this, props); break; default: @@ -121,7 +121,7 @@ export abstract class AbstractCodeWorkspace extends AbstractWorkspace { addServiceFile(serviceName: string, code: string) { const filename = `/src/services/${serviceName}.js`; - this.addFile(filename, code, FileType.ServiceModule); + this.addFile(filename, code, FileType.JsServiceFile); const indexServiceModule = this.serviceModules.index; indexServiceModule?.addImportDeclaration(`./${serviceName}`, []).update(); } diff --git a/packages/core/src/models/abstract-workspace.ts b/packages/core/src/models/abstract-workspace.ts index c7977d4e..44d71b13 100644 --- a/packages/core/src/models/abstract-workspace.ts +++ b/packages/core/src/models/abstract-workspace.ts @@ -289,8 +289,8 @@ export abstract class AbstractWorkspace extends EventTarget { let filename = this.getFilenameByRoutePath(routePath); if (!filename) { // 没有找到 route 对应的文件,使用默认的 entry - for (const [key, file] of this.files) { - if (file.type === FileType.JsxViewModule) { + for (const [, file] of this.files) { + if (file.type === FileType.JsViewFile) { filename = file.filename; break; } diff --git a/packages/core/src/models/workspace.ts b/packages/core/src/models/workspace.ts index 1a24fa85..3d21fec8 100644 --- a/packages/core/src/models/workspace.ts +++ b/packages/core/src/models/workspace.ts @@ -1,6 +1,7 @@ import { action, computed, makeObservable, observable } from 'mobx'; import { IWorkspaceInitConfig } from './abstract-workspace'; import { AbstractCodeWorkspace } from './abstract-code-workspace'; +import { FileType } from '../types'; /** * 工作区 @@ -23,4 +24,8 @@ export class Workspace extends AbstractCodeWorkspace { removeFile: action, }); } + + addFile(filename: string, code: string, fileType?: FileType) { + super.addFile(filename, code, fileType); + } } diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 9f959d6b..a61692e5 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -6,40 +6,28 @@ export type SimulatorMode = 'desktop' | 'tablet' | 'phone'; * 文件类型枚举 */ export enum FileType { - // js 文件 - Module = 'module', + File = 'file', + + JsFile = 'jsFile', JsAppEntryFile = 'jsAppEntryFile', - StoreEntryModule = 'storeEntryModule', - RouteModule = 'routeModule', - ServiceModule = 'serviceModule', - StoreModule = 'storeModule', + JsRouteConfigFile = 'jsRouteConfigFile', + JsStoreEntryFile = 'jsStoreEntryFile', + JsStoreFile = 'jsStoreFile', + JsServiceFile = 'jsServiceFile', + JsLocalComponentsEntryFile = 'jsLocalComponentsEntryFile', + + JsViewFile = 'jsViewFile', + JsonViewFile = 'jsonViewFile', + + JsonFile = 'jsonFile', + PackageJsonFile = 'packageJsonFile', + TangoConfigJsonFile = 'tangoConfigJsonFile', + AppJsonFile = 'appJsonFile', // 组件配置文件 ComponentPrototypeModule = 'componentPrototypeModule', // 组件运行调试入口文件,一般为 `/app.js` ComponentDemoEntryModule = 'componentDemoEntryModule', - /** - * 本地组件目录的入口文件 - */ - ComponentsEntryModule = 'componentsEntryModule', - /** - * @deprecated 已废弃 - */ - BlockEntryModule = 'blockEntryModule', - - // jsx 类型视图文件 - JsxViewModule = 'jsxViewModule', - // json 类型视图文件 - JsonViewModule = 'jsonViewModule', - - // 非 js 文件 - PackageJson = 'packageJson', - TangoConfigJson = 'tangoConfigJson', - AppJson = 'appJson', - File = 'file', - Json = 'json', - Less = 'less', - Scss = 'scss', } export interface IFileConfig { diff --git a/packages/core/tests/helpers.test.ts b/packages/core/tests/helpers.test.ts index 2a1405ed..e1ba7618 100644 --- a/packages/core/tests/helpers.test.ts +++ b/packages/core/tests/helpers.test.ts @@ -179,12 +179,12 @@ describe('string helpers', () => { }); it('inferFileType', () => { - expect(inferFileType('/src/pages/template.js')).toBe(FileType.JsxViewModule); - expect(inferFileType('/src/pages/template.jsx')).toBe(FileType.JsxViewModule); + expect(inferFileType('/src/pages/template.js')).toBe(FileType.JsViewFile); + expect(inferFileType('/src/pages/template.jsx')).toBe(FileType.JsViewFile); expect(inferFileType('/src/pages/template.ejs')).toBe(FileType.File); - expect(inferFileType('/src/index.scss')).toBe(FileType.Scss); - expect(inferFileType('/src/index.less')).toBe(FileType.Less); - expect(inferFileType('/src/index.json')).toBe(FileType.Json); + expect(inferFileType('/src/index.scss')).toBe(FileType.File); + expect(inferFileType('/src/index.less')).toBe(FileType.File); + expect(inferFileType('/src/index.json')).toBe(FileType.JsonFile); }); }); diff --git a/packages/designer/src/dnd/use-dnd.ts b/packages/designer/src/dnd/use-dnd.ts index 9e1562f1..b52adfca 100644 --- a/packages/designer/src/dnd/use-dnd.ts +++ b/packages/designer/src/dnd/use-dnd.ts @@ -343,7 +343,7 @@ export function useDnd({ // 区块不能拖拽到区块中 if ( workspace.dragSource.prototype.type === 'block' && - closetDropTargetNode.file.type === FileType.BlockEntryModule + closetDropTargetNode.file.type === FileType.JsLocalComponentsEntryFile ) { return; }