Merge pull request #1 from webzard-io/runtime

init core package
This commit is contained in:
yz-yu 2021-07-02 17:59:32 +08:00 committed by GitHub
commit 21a29661b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 2953 additions and 41 deletions

5
.gitignore vendored
View File

@ -7,3 +7,8 @@
*.log.*
node_modules
# dist
lib
tsconfig.tsbuildinfo
typings

16
config/jest.config.js Normal file
View File

@ -0,0 +1,16 @@
module.exports = {
transform: {
".ts": "ts-jest",
},
globals: {
"ts-jest": {
diagnostics: false,
},
},
moduleFileExtensions: ["ts", "js"],
testMatch: ["<rootDir>/__tests__/**/**.spec.ts"],
testPathIgnorePatterns: ["/node_modules/", "/lib/", "<rootDir>/lib/"],
collectCoverage: Boolean(process.env.COVERAGE),
collectCoverageFrom: ["<rootDir>/src/**/*.ts"],
coveragePathIgnorePatterns: ["generated"],
};

11
config/tsconfig.base.json Normal file
View File

@ -0,0 +1,11 @@
{
"compilerOptions": {
"module": "commonjs",
"strict": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true
}
}

View File

@ -5,6 +5,22 @@
"lerna": "lerna"
},
"devDependencies": {
"lerna": "^4.0.0"
"husky": "^6.0.0",
"lerna": "^4.0.0",
"lint-staged": "^11.0.0"
},
"workspaces": [
"packages/*"
],
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"packages/**/!(generated)/*.{js,ts,tsx,yml,json}": [
"prettier --write",
"git add"
]
}
}

11
packages/core/README.md Normal file
View File

@ -0,0 +1,11 @@
# `@meta-ui/core`
> TODO: description
## Usage
```
const core = require('@meta-ui/core');
// TODO: DEMONSTRATE API
```

View File

@ -0,0 +1,68 @@
import { createApplication } from "../src/application";
describe("application", () => {
it("can create runtime application", () => {
expect(
createApplication({
version: "demo/v1",
metadata: {
name: "test-app",
description: "first application",
},
spec: {
components: [
{
id: "input1",
type: "core/v1/test-component",
properties: {
x: "foo",
},
traits: [
{
type: "core/v1/test-trait",
properties: {
width: 2,
},
},
],
},
],
},
})
).toMatchInlineSnapshot(`
Object {
"kind": "Application",
"metadata": Object {
"description": "first application",
"name": "test-app",
},
"parsedVersion": Object {
"category": "demo",
"value": "v1",
},
"spec": Object {
"components": Array [
Object {
"id": "input1",
"properties": Object {
"x": "foo",
},
"traits": Array [
Object {
"properties": Object {
"width": 2,
},
"type": "core/v1/test-trait",
},
],
"type": "core/v1/test-component",
},
],
},
"version": "demo/v1",
}
`);
});
});

View File

@ -0,0 +1,84 @@
import { createComponent } from "../src/component";
describe("component", () => {
it("can create runtime component", () => {
expect(
createComponent({
version: "core/v1",
metadata: {
name: "test-component",
},
spec: {
properties: [
{
name: "x",
type: "string",
},
],
acceptTraits: [
{
name: "t1",
},
],
state: {
type: "string",
},
methods: [
{
name: "reset",
},
{
name: "add",
parameters: {
type: "number",
},
},
],
},
})
).toMatchInlineSnapshot(`
Object {
"kind": "Component",
"metadata": Object {
"name": "test-component",
},
"parsedVersion": Object {
"category": "core",
"value": "v1",
},
"spec": Object {
"acceptTraits": Array [
Object {
"name": "t1",
},
],
"methods": Array [
Object {
"name": "reset",
},
Object {
"name": "add",
"parameters": Object {
"type": "number",
},
},
],
"properties": Array [
Object {
"name": "x",
"type": "string",
},
],
"state": Object {
"type": "string",
},
},
"version": "core/v1",
}
`);
});
});

View File

@ -0,0 +1,26 @@
import { createScope } from "../src/scope";
describe("scope", () => {
it("can create runtime scope", () => {
expect(
createScope({
version: "core/v1",
metadata: {
name: "test-scope",
},
})
).toMatchInlineSnapshot(`
Object {
"kind": "Scope",
"metadata": Object {
"name": "test-scope",
},
"parsedVersion": Object {
"category": "core",
"value": "v1",
},
"version": "core/v1",
}
`);
});
});

View File

@ -0,0 +1,61 @@
import { createTrait } from "../src/trait";
describe("trait", () => {
it("can create runtime trait", () => {
expect(
createTrait({
version: "core/v1",
metadata: {
name: "test-trait",
},
spec: {
properties: [{ name: "width", type: "number" }],
state: {
type: "string",
},
methods: [
{
name: "times",
parameters: {
type: "number",
},
},
],
},
})
).toMatchInlineSnapshot(`
Object {
"kind": "Trait",
"metadata": Object {
"name": "test-trait",
},
"parsedVersion": Object {
"category": "core",
"value": "v1",
},
"spec": Object {
"methods": Array [
Object {
"name": "times",
"parameters": Object {
"type": "number",
},
},
],
"properties": Array [
Object {
"name": "width",
"type": "number",
},
],
"state": Object {
"type": "string",
},
},
"version": "core/v1",
}
`);
});
});

View File

@ -0,0 +1,3 @@
module.exports = {
...require("../../config/jest.config"),
};

View File

@ -0,0 +1,44 @@
{
"name": "@meta-ui/core",
"version": "0.1.0",
"description": "meta-ui core runtime",
"author": "Yanzhen Yu <yanzhen@smartx.com>",
"homepage": "https://github.com/webzard-io/meta-ui#readme",
"license": "MIT",
"main": "lib/core.js",
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"lib"
],
"repository": {
"type": "git",
"url": "git+https://github.com/webzard-io/meta-ui.git"
},
"scripts": {
"build": "tsc -b .",
"typings": "tsc -d --emitDeclarationOnly --declarationDir typings",
"test": "jest",
"lint": "eslint src --ext .ts",
"prepublish": "npm run build && npm run typings"
},
"bugs": {
"url": "https://github.com/webzard-io/meta-ui/issues"
},
"devDependencies": {
"@types/jest": "^26.0.23",
"@typescript-eslint/eslint-plugin": "^4.28.1",
"@typescript-eslint/parser": "^4.28.1",
"eslint": "^7.29.0",
"eslint-config-prettier": "^8.3.0",
"jest": "^27.0.6",
"prettier": "^2.3.2",
"ts-jest": "^27.0.3",
"typescript": "^4.3.5"
},
"dependencies": {
"@types/json-schema": "^7.0.7"
}
}

View File

@ -0,0 +1,45 @@
import { Metadata } from "./metadata";
import { parseVersion, Version } from "./version";
// spec
export type Application = {
version: string;
kind: "Application";
metadata: Metadata;
spec: ApplicationSpec;
};
type ApplicationSpec = {
components: ApplicationComponent[];
};
type ApplicationComponent = {
id: string;
type: string;
// do runtime type check
properties: object;
traits: ComponentTrait[];
// scopes TBD
};
type ComponentTrait = {
type: string;
// do runtime type check
properties: object;
};
// extended runtime
export type RuntimeApplication = Application & {
parsedVersion: Version;
};
export function createApplication(
options: Omit<Application, "kind">
): RuntimeApplication {
return {
...options,
kind: "Application",
parsedVersion: parseVersion(options.version),
};
}

View File

@ -0,0 +1,40 @@
import { JSONSchema4 } from "json-schema";
import { parseVersion } from "./version";
import { Metadata } from "./metadata";
import { MethodSchema } from "./method";
import { Version } from "./version";
// spec
export type Component = {
version: string;
kind: "Component";
metadata: Metadata;
spec: ComponentSpec;
};
type ComponentSpec = {
properties: Array<JSONSchema4 & { name: string }>;
acceptTraits: TraitSchema[];
state: JSONSchema4;
methods: MethodSchema[];
};
type TraitSchema = {
name: string;
};
// extended runtime
export type RuntimeComponent = Component & {
parsedVersion: Version;
};
export function createComponent(
options: Omit<Component, "kind">
): RuntimeComponent {
return {
...options,
kind: "Component",
parsedVersion: parseVersion(options.version),
};
}

View File

View File

@ -0,0 +1,4 @@
export type Metadata = {
name: string;
description?: string;
};

View File

@ -0,0 +1,6 @@
import { JSONSchema4 } from "json-schema";
export type MethodSchema = {
name: string;
parameters?: JSONSchema4;
};

View File

@ -0,0 +1,23 @@
import { Metadata } from "./metadata";
import { parseVersion, Version } from "./version";
// spec
export type Scope = {
version: string;
kind: "Scope";
metadata: Metadata;
};
// extended runtime
export type RuntimeScope = Scope & {
parsedVersion: Version;
};
export function createScope(options: Omit<Scope, "kind">): RuntimeScope {
return {
...options,
kind: "Scope",
parsedVersion: parseVersion(options.version),
};
}

View File

@ -0,0 +1,32 @@
import { JSONSchema4 } from "json-schema";
import { Metadata } from "./metadata";
import { MethodSchema } from "./method";
import { parseVersion, Version } from "./version";
// spec
export type Trait = {
version: string;
kind: "Trait";
metadata: Metadata;
spec: TraitSpec;
};
type TraitSpec = {
properties: Array<JSONSchema4 & { name: string }>;
state: JSONSchema4;
methods: MethodSchema[];
};
// extended runtime
export type RuntimeTrait = Trait & {
parsedVersion: Version;
};
export function createTrait(options: Omit<Trait, "kind">): RuntimeTrait {
return {
...options,
kind: "Trait",
parsedVersion: parseVersion(options.version),
};
}

View File

@ -0,0 +1,22 @@
const VERSION_REG = /^([a-zA-Z-_\d]+)\/([a-zA-Z-_\d]+)$/;
export function isValidVersion(v: string): boolean {
return VERSION_REG.test(v);
}
export type Version = {
category: string;
value: string;
};
export function parseVersion(v: string): Version {
if (!isValidVersion(v)) {
throw new Error(`Invalid version string: "${v}"`);
}
const [, category, value] = v.match(VERSION_REG)!;
return {
category,
value,
};
}

View File

@ -0,0 +1,12 @@
{
"extends": "../../config/tsconfig.base.json",
"compilerOptions": {
"target": "es6",
"outDir": "lib",
"lib": ["es6"],
"composite": true,
"rootDir": "src"
},
"include": ["src/**/*.ts"],
"exclude": ["__tests__/**/*.ts"]
}

8
tsconfig.json Normal file
View File

@ -0,0 +1,8 @@
{
"extends": "./config/tsconfig.base.json",
"references": [
{
"path": "./packages/core"
}
]
}

2455
yarn.lock

File diff suppressed because it is too large Load Diff