Skip to content

Commit

Permalink
fix: update fileTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
wwsun committed Aug 1, 2024
1 parent 6f27c61 commit 2033acd
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 68 deletions.
34 changes: 13 additions & 21 deletions packages/core/src/helpers/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 11 additions & 11 deletions packages/core/src/models/abstract-code-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/models/abstract-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/models/workspace.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
* 工作区
Expand All @@ -23,4 +24,8 @@ export class Workspace extends AbstractCodeWorkspace {
removeFile: action,
});
}

addFile(filename: string, code: string, fileType?: FileType) {
super.addFile(filename, code, fileType);
}
}
44 changes: 16 additions & 28 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions packages/core/tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/designer/src/dnd/use-dnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export function useDnd({
// 区块不能拖拽到区块中
if (
workspace.dragSource.prototype.type === 'block' &&
closetDropTargetNode.file.type === FileType.BlockEntryModule
closetDropTargetNode.file.type === FileType.JsLocalComponentsEntryFile
) {
return;
}
Expand Down

0 comments on commit 2033acd

Please sign in to comment.