@colyseus/schema 2.0.7 → 2.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/index.js +15 -5
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +14 -5
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +15 -5
- package/lib/Schema.d.ts +7 -1
- package/lib/Schema.js +14 -5
- package/lib/Schema.js.map +1 -1
- package/lib/codegen/api.js +19 -0
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/parser.js.map +1 -1
- package/package.json +2 -2
- package/src/Schema.ts +18 -4
- package/src/codegen/api.ts +20 -0
- package/src/codegen/parser.ts +5 -5
- package/lib/events/EventEmitter.d.ts +0 -13
- package/lib/events/EventEmitter.js +0 -31
- package/lib/events/EventEmitter.js.map +0 -1
- package/lib/types/index.d.ts +0 -6
- package/lib/types/index.js +0 -13
- package/lib/types/index.js.map +0 -1
package/src/codegen/api.ts
CHANGED
|
@@ -31,6 +31,16 @@ export function generate(targetId: string, options: GenerateOptions) {
|
|
|
31
31
|
*/
|
|
32
32
|
if (!options.decorator) { options.decorator = "type"; }
|
|
33
33
|
|
|
34
|
+
// resolve wildcard files
|
|
35
|
+
options.files = options.files.reduce((acc, cur) => {
|
|
36
|
+
if (cur.endsWith("*")) {
|
|
37
|
+
acc.push(...recursiveFiles(cur.slice(0, -1)).filter(filename => /\.(js|ts|mjs)$/.test(filename)));
|
|
38
|
+
} else {
|
|
39
|
+
acc.push(cur)
|
|
40
|
+
}
|
|
41
|
+
return acc;
|
|
42
|
+
}, [])
|
|
43
|
+
|
|
34
44
|
const structures = parseFiles(options.files, options.decorator);
|
|
35
45
|
|
|
36
46
|
// Post-process classes before generating
|
|
@@ -44,3 +54,13 @@ export function generate(targetId: string, options: GenerateOptions) {
|
|
|
44
54
|
console.log("generated:", file.name);
|
|
45
55
|
});
|
|
46
56
|
}
|
|
57
|
+
|
|
58
|
+
function recursiveFiles(dir: string): string[] {
|
|
59
|
+
const files = fs.readdirSync(dir, { withFileTypes: true });
|
|
60
|
+
let collect = [];
|
|
61
|
+
files.forEach(file => {
|
|
62
|
+
const filename = path.resolve(dir, file.name);
|
|
63
|
+
file.isDirectory() ? collect.push(...recursiveFiles(filename)) : collect.push(filename);
|
|
64
|
+
})
|
|
65
|
+
return collect;
|
|
66
|
+
}
|
package/src/codegen/parser.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as ts from "typescript";
|
|
2
2
|
import * as path from "path";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import {readFileSync} from "fs";
|
|
4
|
+
import {IStructure, Class, Interface, Property, Context, Enum} from "./types";
|
|
5
5
|
|
|
6
6
|
let currentStructure: IStructure;
|
|
7
7
|
let currentProperty: Property;
|
|
@@ -30,7 +30,7 @@ function inspectNode(node: ts.Node, context: Context, decoratorName: string) {
|
|
|
30
30
|
switch (node.kind) {
|
|
31
31
|
case ts.SyntaxKind.ImportClause:
|
|
32
32
|
const specifier = (node.parent as any).moduleSpecifier;
|
|
33
|
-
if (specifier && (specifier.text as string).startsWith('.'))
|
|
33
|
+
if (specifier && (specifier.text as string).startsWith('.')) {
|
|
34
34
|
const currentDir = path.dirname(node.getSourceFile().fileName);
|
|
35
35
|
const pathToImport = path.resolve(currentDir, specifier.text);
|
|
36
36
|
parseFiles([pathToImport], decoratorName, globalContext);
|
|
@@ -106,7 +106,7 @@ function inspectNode(node: ts.Node, context: Context, decoratorName: string) {
|
|
|
106
106
|
|
|
107
107
|
if (node.getText() === decoratorName) {
|
|
108
108
|
const prop: any = node.parent?.parent?.parent;
|
|
109
|
-
const propDecorator =
|
|
109
|
+
const propDecorator = getDecorators(prop);
|
|
110
110
|
const hasExpression = prop?.expression?.arguments;
|
|
111
111
|
const hasDecorator = (propDecorator?.length > 0);
|
|
112
112
|
|
|
@@ -172,7 +172,7 @@ function inspectNode(node: ts.Node, context: Context, decoratorName: string) {
|
|
|
172
172
|
currentStructure.name = className;
|
|
173
173
|
|
|
174
174
|
const types = callExpression.arguments[1] as any;
|
|
175
|
-
for (let i=0; i<types.properties.length; i++) {
|
|
175
|
+
for (let i = 0; i < types.properties.length; i++) {
|
|
176
176
|
const prop = types.properties[i];
|
|
177
177
|
|
|
178
178
|
const property = currentProperty || new Property();
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Extracted from https://www.npmjs.com/package/strong-events
|
|
3
|
-
*/
|
|
4
|
-
type ExtractFunctionParameters<T extends (...args: any[]) => any> = T extends (...args: infer P) => any ? P : never;
|
|
5
|
-
export declare class EventEmitter_<CallbackSignature extends (...args: any[]) => any> {
|
|
6
|
-
handlers: Array<CallbackSignature>;
|
|
7
|
-
register(cb: CallbackSignature, once?: boolean): this;
|
|
8
|
-
invoke(...args: ExtractFunctionParameters<CallbackSignature>): void;
|
|
9
|
-
invokeAsync(...args: ExtractFunctionParameters<CallbackSignature>): Promise<any[]>;
|
|
10
|
-
remove(cb: CallbackSignature): void;
|
|
11
|
-
clear(): void;
|
|
12
|
-
}
|
|
13
|
-
export {};
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Extracted from https://www.npmjs.com/package/strong-events
|
|
4
|
-
*/
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.EventEmitter_ = void 0;
|
|
7
|
-
class EventEmitter_ {
|
|
8
|
-
constructor() {
|
|
9
|
-
this.handlers = [];
|
|
10
|
-
}
|
|
11
|
-
register(cb, once = false) {
|
|
12
|
-
this.handlers.push(cb);
|
|
13
|
-
return this;
|
|
14
|
-
}
|
|
15
|
-
invoke(...args) {
|
|
16
|
-
this.handlers.forEach((handler) => handler(...args));
|
|
17
|
-
}
|
|
18
|
-
invokeAsync(...args) {
|
|
19
|
-
return Promise.all(this.handlers.map((handler) => handler(...args)));
|
|
20
|
-
}
|
|
21
|
-
remove(cb) {
|
|
22
|
-
const index = this.handlers.indexOf(cb);
|
|
23
|
-
this.handlers[index] = this.handlers[this.handlers.length - 1];
|
|
24
|
-
this.handlers.pop();
|
|
25
|
-
}
|
|
26
|
-
clear() {
|
|
27
|
-
this.handlers = [];
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
exports.EventEmitter_ = EventEmitter_;
|
|
31
|
-
//# sourceMappingURL=EventEmitter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"EventEmitter.js","sourceRoot":"","sources":["../../src/events/EventEmitter.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAIH,MAAa,aAAa;IAA1B;QACE,aAAQ,GAA6B,EAAE,CAAC;IAwB1C,CAAC;IAtBC,QAAQ,CAAC,EAAqB,EAAE,OAAgB,KAAK;QACnD,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACvB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,CAAC,GAAG,IAAkD;QAC1D,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;IACvD,CAAC;IAED,WAAW,CAAC,GAAG,IAAkD;QAC/D,OAAO,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;IACvE,CAAC;IAED,MAAM,CAAE,EAAqB;QAC3B,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACxC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAC/D,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC;IACtB,CAAC;IAED,KAAK;QACH,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;IACrB,CAAC;CACF;AAzBD,sCAyBC","sourcesContent":["/**\n * Extracted from https://www.npmjs.com/package/strong-events\n */\n\ntype ExtractFunctionParameters<T extends (...args: any[]) => any> = T extends (...args: infer P) => any ? P : never;\n\nexport class EventEmitter_<CallbackSignature extends (...args: any[]) => any> {\n handlers: Array<CallbackSignature> = [];\n\n register(cb: CallbackSignature, once: boolean = false) {\n this.handlers.push(cb);\n return this;\n }\n\n invoke(...args: ExtractFunctionParameters<CallbackSignature>) {\n this.handlers.forEach((handler) => handler(...args));\n }\n\n invokeAsync(...args: ExtractFunctionParameters<CallbackSignature>) {\n return Promise.all(this.handlers.map((handler) => handler(...args)));\n }\n\n remove (cb: CallbackSignature) {\n const index = this.handlers.indexOf(cb);\n this.handlers[index] = this.handlers[this.handlers.length - 1];\n this.handlers.pop();\n }\n\n clear() {\n this.handlers = [];\n }\n}\n"]}
|
package/lib/types/index.d.ts
DELETED
package/lib/types/index.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getType = exports.registerType = void 0;
|
|
4
|
-
const registeredTypes = {};
|
|
5
|
-
function registerType(identifier, definition) {
|
|
6
|
-
registeredTypes[identifier] = definition;
|
|
7
|
-
}
|
|
8
|
-
exports.registerType = registerType;
|
|
9
|
-
function getType(identifier) {
|
|
10
|
-
return registeredTypes[identifier];
|
|
11
|
-
}
|
|
12
|
-
exports.getType = getType;
|
|
13
|
-
//# sourceMappingURL=index.js.map
|
package/lib/types/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;AAUA,MAAM,eAAe,GAA4C,EAAE,CAAC;AAEpE,SAAgB,YAAY,CAAC,UAAkB,EAAE,UAA0B;IACvE,eAAe,CAAC,UAAU,CAAC,GAAG,UAAU,CAAC;AAC7C,CAAC;AAFD,oCAEC;AAED,SAAgB,OAAO,CAAC,UAAkB;IACtC,OAAO,eAAe,CAAC,UAAU,CAAC,CAAC;AACvC,CAAC;AAFD,0BAEC","sourcesContent":["export interface TypeDefinition {\n constructor: any,\n\n //\n // TODO: deprecate proxy on next version\n // the proxy is used for compatibility with versions <1.0.0 of @colyseus/schema\n //\n getProxy?: any,\n}\n\nconst registeredTypes: {[identifier: string] : TypeDefinition} = {};\n\nexport function registerType(identifier: string, definition: TypeDefinition) {\n registeredTypes[identifier] = definition;\n}\n\nexport function getType(identifier: string): TypeDefinition {\n return registeredTypes[identifier];\n}"]}
|