Skip to content

Commit

Permalink
fix: some bugs fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
1ncounter committed May 29, 2024
1 parent d632e7f commit 42a53b5
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/react-renderer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './api/app';
export * from './api/component';
export { definePlugin } from './plugin';
export { defineRendererPlugin } from './plugin';
export * from './context/render';
export * from './context/router';

Expand Down
2 changes: 1 addition & 1 deletion packages/react-renderer/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ export const rendererExtends: RendererExtends = {
},
};

export function definePlugin(plugin: Plugin<RendererExtends>) {
export function defineRendererPlugin(plugin: Plugin<RendererExtends>) {
return plugin;
}
3 changes: 3 additions & 0 deletions packages/renderer-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"devDependencies": {
"@types/lodash-es": "^4.17.12"
},
"peerDependencies": {
"@alilc/lowcode-shared": "workspace:*"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
Expand Down
2 changes: 1 addition & 1 deletion packages/renderer-core/src/apiCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function createRenderer<Render = IRender>(
const rendererMain = instantiationService.createInstance(RendererMain);

return async (options) => {
rendererMain.initialize(options);
await rendererMain.initialize(options);

return rendererMain.startup<Render>(renderAdapter);
};
Expand Down
3 changes: 3 additions & 0 deletions packages/renderer-core/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ export class RendererMain {
this.runtimeUtilService.add(util);
}

const constants = this.schemaService.get('constants') ?? {};

const globalScope = this.codeRuntimeService.getScope();
globalScope.setValue({
constants,
utils: this.runtimeUtilService.toExpose(),
...this.runtimeIntlService.toExpose(),
});
Expand Down
2 changes: 1 addition & 1 deletion packages/renderer-core/src/parts/code-runtime/codeScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class CodeScope implements ICodeScope {
if (this.__node.current[name] && !force) {
return;
}
this.__node.current.value[name] = value;
this.__node.current[name] = value;
}

setValue(value: PlainObject, replace = false) {
Expand Down
2 changes: 1 addition & 1 deletion packages/renderer-core/src/parts/schema/schemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class SchemaService implements ISchemaService {

initialize(schema: unknown): void {
if (!isObject(schema)) {
throw Error('schema muse a object');
throw Error('schema must a object');
}

Object.keys(schema).forEach((key) => {
Expand Down
53 changes: 42 additions & 11 deletions packages/renderer-core/src/parts/schema/validation.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,54 @@
import { type Spec } from '@alilc/lowcode-shared';

const SCHEMA_VALIDATIONS_OPTIONS: Partial<
Record<
keyof Spec.Project,
{
valid: (value: any) => boolean;
description: string;
}
>
> = {};
interface ValidationRule<T> {
valid: (value: T) => boolean;
description: string;
}

type ValidOptionRecord = {
[K in keyof Spec.Project]: ValidationRule<Spec.Project[K]>;
};

const SCHEMA_KEYS = [
'version',
'componentsMap',
'componentsTree',
'utils',
'i18n',
'constants',
'css',
'config',
'meta',
'router',
'pages',
];

const SCHEMA_VALIDATIONS_OPTIONS: Partial<ValidOptionRecord> = {
componentsMap: {
valid(value) {
return Array.isArray(value);
},
description: 'componentsMap 必须是一个数组',
},
componentsTree: {
valid(value) {
return Array.isArray(value);
},
description: 'componentsTree 必须是一个数组',
},
};

export function schemaValidation<K extends keyof Spec.Project>(key: K, value: Spec.Project[K]) {
if (!SCHEMA_KEYS.includes(key)) {
return `schema 的字段名必须是${JSON.stringify(SCHEMA_KEYS)}中的一个`;
}

const validOption = SCHEMA_VALIDATIONS_OPTIONS[key];

if (validOption) {
const result = validOption.valid(value);

if (!result) {
throw Error(validOption.description);
return validOption.description;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@alilc/lowcode-shared",
"version": "1.0.0-alpha.0",
"type": "module",
"main": "dist/low-code-shared.js",
"main": "dist/low-code-shared.cjs",
"module": "dist/low-code-shared.js",
"types": "dist/index.d.ts",
"files": [
Expand Down
9 changes: 4 additions & 5 deletions packages/shared/src/abilities/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ export class KeyValueStore<O = PlainObject, K extends keyof O = keyof O> {
if (this.setterValidation) {
const valid = this.setterValidation(key, value);

invariant(
valid === false || typeof valid === 'string',
`failed to config ${key.toString()}, only predefined options can be set under strict mode, predefined options: ${valid ? valid : ''}`,
'KeyValueStore',
);
if (valid !== true) {
console.warn(`failed to config ${key.toString()}, validation error: ${valid ? valid : ''}`);
return;
}
}

this.store.set(key, value);
Expand Down
11 changes: 7 additions & 4 deletions packages/shared/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { defineConfig } from 'vite';
import baseConfigFn from '../../vite.base.config'
import baseConfigFn from '../../vite.base.config';

export default defineConfig(async () => baseConfigFn({
name: 'LowCodeShared',
}));
export default defineConfig(async () =>
baseConfigFn({
name: 'LowCodeShared',
defaultFormats: ['es', 'cjs'],
}),
);

0 comments on commit 42a53b5

Please sign in to comment.