@colyseus/schema 2.0.7 → 2.0.8

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.
@@ -22,6 +22,16 @@ function generate(targetId, options) {
22
22
  if (!options.decorator) {
23
23
  options.decorator = "type";
24
24
  }
25
+ // resolve wildcard files
26
+ options.files = options.files.reduce((acc, cur) => {
27
+ if (cur.endsWith("*")) {
28
+ acc.push(...recursiveFiles(cur.slice(0, -1)).filter(filename => /\.(js|ts|mjs)$/.test(filename)));
29
+ }
30
+ else {
31
+ acc.push(cur);
32
+ }
33
+ return acc;
34
+ }, []);
25
35
  const structures = (0, parser_1.parseFiles)(options.files, options.decorator);
26
36
  // Post-process classes before generating
27
37
  structures.classes.forEach(klass => klass.postProcessing());
@@ -33,4 +43,13 @@ function generate(targetId, options) {
33
43
  });
34
44
  }
35
45
  exports.generate = generate;
46
+ function recursiveFiles(dir) {
47
+ const files = fs.readdirSync(dir, { withFileTypes: true });
48
+ let collect = [];
49
+ files.forEach(file => {
50
+ const filename = path.resolve(dir, file.name);
51
+ file.isDirectory() ? collect.push(...recursiveFiles(filename)) : collect.push(filename);
52
+ });
53
+ return collect;
54
+ }
36
55
  //# sourceMappingURL=api.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"api.js","sourceRoot":"","sources":["../../src/codegen/api.ts"],"names":[],"mappings":";;;AAAA,yBAAyB;AACzB,6BAA6B;AAG7B,qCAAsC;AAStC,SAAgB,QAAQ,CAAC,QAAgB,EAAE,OAAwB;IAC/D,IAAI,SAAmB,CAAC;IAExB,IAAI;QACA,SAAS,GAAG,OAAO,CAAC,cAAc,GAAG,QAAQ,CAAC,CAAC,QAAQ,CAAC;KAE3D;IAAC,OAAO,CAAC,EAAE;QACR,MAAM,IAAI,KAAK,CAAC,oFAAoF,CAAC,CAAC;KACzG;IAED,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QAChC,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;QACrD,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;KAChC;IAED;;OAEG;IACH,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE;QAAE,OAAO,CAAC,SAAS,GAAG,MAAM,CAAC;KAAE;IAEvD,MAAM,UAAU,GAAG,IAAA,mBAAU,EAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC;IAEhE,yCAAyC;IACzC,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,cAAc,EAAE,CAAC,CAAC;IAE5D,MAAM,KAAK,GAAG,SAAS,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAE7C,KAAK,CAAC,OAAO,CAAC,CAAC,IAAU,EAAE,EAAE;QACzB,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;QAC3D,EAAE,CAAC,aAAa,CAAC,UAAU,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAC3C,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;AACP,CAAC;AAhCD,4BAgCC","sourcesContent":["import * as fs from \"fs\";\nimport * as path from \"path\";\n\nimport { File } from \"./types\";\nimport { parseFiles } from \"./parser\";\n\nexport interface GenerateOptions {\n files: string[],\n output: string;\n decorator?: string;\n namespace?: string;\n}\n\nexport function generate(targetId: string, options: GenerateOptions) {\n let generator: Function;\n\n try {\n generator = require('./languages/' + targetId).generate;\n\n } catch (e) {\n throw new Error(\"You must provide a valid generator as argument, such as: --csharp, --haxe or --cpp\");\n }\n\n if (!fs.existsSync(options.output)) {\n console.log(\"Creating\", options.output, \"directory\");\n fs.mkdirSync(options.output);\n }\n\n /**\n * Default `@type()` decorator name\n */\n if (!options.decorator) { options.decorator = \"type\"; }\n\n const structures = parseFiles(options.files, options.decorator);\n\n // Post-process classes before generating\n structures.classes.forEach(klass => klass.postProcessing());\n\n const files = generator(structures, options);\n\n files.forEach((file: File) => {\n const outputPath = path.resolve(options.output, file.name);\n fs.writeFileSync(outputPath, file.content);\n console.log(\"generated:\", file.name);\n });\n}\n"]}
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../../src/codegen/api.ts"],"names":[],"mappings":";;;AAAA,yBAAyB;AACzB,6BAA6B;AAG7B,qCAAsC;AAStC,SAAgB,QAAQ,CAAC,QAAgB,EAAE,OAAwB;IAC/D,IAAI,SAAmB,CAAC;IAExB,IAAI;QACA,SAAS,GAAG,OAAO,CAAC,cAAc,GAAG,QAAQ,CAAC,CAAC,QAAQ,CAAC;KAE3D;IAAC,OAAO,CAAC,EAAE;QACR,MAAM,IAAI,KAAK,CAAC,oFAAoF,CAAC,CAAC;KACzG;IAED,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QAChC,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;QACrD,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;KAChC;IAED;;OAEG;IACH,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE;QAAE,OAAO,CAAC,SAAS,GAAG,MAAM,CAAC;KAAE;IAEvD,yBAAyB;IACzB,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAC9C,IAAI,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;YACnB,GAAG,CAAC,IAAI,CAAC,GAAG,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,MAA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C,GAAG,cAAc,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC5F,CAAC,CAAC,CAAA;IACF,OAAO,OAAO,CAAC;AACnB,CAAC","sourcesContent":["import * as fs from \"fs\";\nimport * as path from \"path\";\n\nimport { File } from \"./types\";\nimport { parseFiles } from \"./parser\";\n\nexport interface GenerateOptions {\n files: string[],\n output: string;\n decorator?: string;\n namespace?: string;\n}\n\nexport function generate(targetId: string, options: GenerateOptions) {\n let generator: Function;\n\n try {\n generator = require('./languages/' + targetId).generate;\n\n } catch (e) {\n throw new Error(\"You must provide a valid generator as argument, such as: --csharp, --haxe or --cpp\");\n }\n\n if (!fs.existsSync(options.output)) {\n console.log(\"Creating\", options.output, \"directory\");\n fs.mkdirSync(options.output);\n }\n\n /**\n * Default `@type()` decorator name\n */\n if (!options.decorator) { options.decorator = \"type\"; }\n\n // resolve wildcard files\n options.files = options.files.reduce((acc, cur) => {\n if (cur.endsWith(\"*\")) {\n acc.push(...recursiveFiles(cur.slice(0, -1)).filter(filename => /\\.(js|ts|mjs)$/.test(filename)));\n } else {\n acc.push(cur)\n }\n return acc;\n }, [])\n\n const structures = parseFiles(options.files, options.decorator);\n\n // Post-process classes before generating\n structures.classes.forEach(klass => klass.postProcessing());\n\n const files = generator(structures, options);\n\n files.forEach((file: File) => {\n const outputPath = path.resolve(options.output, file.name);\n fs.writeFileSync(outputPath, file.content);\n console.log(\"generated:\", file.name);\n });\n}\n\nfunction recursiveFiles(dir: string): string[] {\n const files = fs.readdirSync(dir, { withFileTypes: true });\n let collect = [];\n files.forEach(file => {\n const filename = path.resolve(dir, file.name);\n file.isDirectory() ? collect.push(...recursiveFiles(filename)) : collect.push(filename);\n })\n return collect;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"parser.js","sourceRoot":"","sources":["../../src/codegen/parser.ts"],"names":[],"mappings":";;;AAAA,iCAAiC;AACjC,6BAA6B;AA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O,IAAI,CAAC,UAAU,CAAC;KAAE;IAEhD,2BAA2B;IAC3B,aAAa;IACb,IAAI,EAAE,CAAC,iBAAiB,IAAI,EAAE,CAAC,iBAAiB,CAAC,IAAI,CAAC,EAAE;QACpD,aAAa;QACb,MAAM,UAAU,GAAG,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;QAC1C,OAAO,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;KAC1D;IAED,aAAa;IACb,OAAO,IAAI,CAAC,SAAS,EAAE,MAAM,CAAC,EAAE,CAAC,WAAW,CAAC,CAAC;AAClD,CAAC;AAjBD,sCAiBC","sourcesContent":["import * as ts from \"typescript\";\nimport * as path from \"path\";\nimport { readFileSync } from \"fs\";\nimport { IStructure, Class, Interface, Property, Context, Enum } from \"./types\";\n\nlet currentStructure: IStructure;\nlet currentProperty: Property;\n\nlet globalContext: Context;\n\nfunction defineProperty(property: Property, initializer: any) {\n if (ts.isIdentifier(initializer)) {\n property.type = \"ref\";\n property.childType = initializer.text;\n\n } else if (initializer.kind == ts.SyntaxKind.ObjectLiteralExpression) {\n property.type = initializer.properties[0].name.text;\n property.childType = initializer.properties[0].initializer.text;\n\n } else if (initializer.kind == ts.SyntaxKind.ArrayLiteralExpression) {\n property.type = \"array\";\n property.childType = initializer.elements[0].text;\n\n } else {\n property.type = initializer.text;\n }\n}\n\nfunction inspectNode(node: ts.Node, context: Context, decoratorName: string) {\n switch (node.kind) {\n case ts.SyntaxKind.ImportClause:\n const specifier = (node.parent as any).moduleSpecifier;\n if (specifier && (specifier.text as string).startsWith('.')) {\n const currentDir = path.dirname(node.getSourceFile().fileName);\n const pathToImport = path.resolve(currentDir, specifier.text);\n parseFiles([pathToImport], decoratorName, globalContext);\n }\n break;\n\n case ts.SyntaxKind.ClassDeclaration:\n currentStructure = new Class();\n\n const heritageClauses = (node as ts.ClassLikeDeclarationBase).heritageClauses;\n if (heritageClauses && heritageClauses.length > 0) {\n (currentStructure as Class).extends = heritageClauses[0].types[0].expression.getText();\n }\n\n context.addStructure(currentStructure);\n break;\n\n case ts.SyntaxKind.InterfaceDeclaration:\n //\n // Only generate Interfaces if it has \"Message\" on its name.\n // Example: MyMessage\n //\n const interfaceName = (node as ts.TypeParameterDeclaration).name.escapedText.toString();\n if (interfaceName.indexOf(\"Message\") !== -1) {\n currentStructure = new Interface();\n currentStructure.name = interfaceName;\n\n context.addStructure(currentStructure);\n }\n break;\n\n case ts.SyntaxKind.EnumDeclaration:\n const enumName = (\n node as ts.EnumDeclaration\n ).name.escapedText.toString();\n currentStructure = new Enum();\n currentStructure.name = enumName;\n context.addStructure(currentStructure);\n break;\n\n case ts.SyntaxKind.ExtendsKeyword:\n // console.log(node.getText());\n break;\n\n case ts.SyntaxKind.PropertySignature:\n if (currentStructure instanceof Interface) {\n const interfaceDeclaration = node.parent;\n\n if (\n currentStructure.name !== (interfaceDeclaration as ts.TypeParameterDeclaration).name.escapedText.toString()\n ) {\n // skip if property if for a another interface than the one we're interested in.\n break;\n }\n\n // define a property of an interface\n const property = new Property();\n property.name = (node as any).name.escapedText.toString();\n property.type = (node as any).type.getText();\n currentStructure.addProperty(property);\n }\n break;\n\n case ts.SyntaxKind.Identifier:\n if (\n node.getText() === \"deprecated\" &&\n node.parent.kind !== ts.SyntaxKind.ImportSpecifier\n ) {\n currentProperty = new Property();\n currentProperty.deprecated = true;\n break;\n }\n\n if (node.getText() === decoratorName) {\n const prop: any = node.parent?.parent?.parent;\n const propDecorator = getDecorators(prop);\n const hasExpression = prop?.expression?.arguments;\n const hasDecorator = (propDecorator?.length > 0);\n\n /**\n * neither a `@type()` decorator or `type()` call. skip.\n */\n if (!hasDecorator && !hasExpression) {\n break;\n }\n\n // using as decorator\n if (propDecorator) {\n /**\n * Calling `@type()` as decorator\n */\n const typeDecorator: any = propDecorator.find((decorator => {\n return (decorator.expression as any).expression.escapedText === decoratorName;\n })).expression;\n\n const property = currentProperty || new Property();\n property.name = prop.name.escapedText;\n currentStructure.addProperty(property);\n\n const typeArgument = typeDecorator.arguments[0];\n defineProperty(property, typeArgument);\n\n } else if (\n prop.expression.arguments?.[1] &&\n prop.expression.expression.arguments?.[0]\n ) {\n /**\n * Calling `type()` as a regular method\n */\n const property = currentProperty || new Property();\n property.name = prop.expression.arguments[1].text;\n currentStructure.addProperty(property);\n\n const typeArgument = prop.expression.expression.arguments[0];\n defineProperty(property, typeArgument);\n }\n\n\n } else if (\n node.getText() === \"defineTypes\" &&\n (\n node.parent.kind === ts.SyntaxKind.CallExpression ||\n node.parent.kind === ts.SyntaxKind.PropertyAccessExpression\n )\n ) {\n /**\n * JavaScript source file (`.js`)\n * Using `defineTypes()`\n */\n const callExpression = (node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)\n ? node.parent.parent as ts.CallExpression\n : node.parent as ts.CallExpression;\n\n if (callExpression.kind !== ts.SyntaxKind.CallExpression) {\n break;\n }\n\n const className = callExpression.arguments[0].getText()\n currentStructure.name = className;\n\n const types = callExpression.arguments[1] as any;\n for (let i=0; i<types.properties.length; i++) {\n const prop = types.properties[i];\n\n const property = currentProperty || new Property();\n property.name = prop.name.escapedText;\n currentStructure.addProperty(property);\n\n defineProperty(property, prop.initializer);\n }\n\n }\n\n if (node.parent.kind === ts.SyntaxKind.ClassDeclaration) {\n currentStructure.name = node.getText();\n }\n\n currentProperty = undefined;\n\n break;\n\n case ts.SyntaxKind.EnumMember:\n if (currentStructure instanceof Enum) {\n const initializer = (node as any).initializer?.getText();\n const name = node.getFirstToken().getText();\n const property = currentProperty || new Property();\n property.name = name;\n if (initializer !== undefined) {\n property.type = initializer;\n }\n currentStructure.addProperty(property);\n currentProperty = undefined;\n }\n break;\n }\n\n ts.forEachChild(node, (n) => inspectNode(n, context, decoratorName));\n}\n\nlet parsedFiles: { [filename: string]: boolean };\n\nexport function parseFiles(\n fileNames: string[],\n decoratorName: string = \"type\",\n context: Context = new Context()\n) {\n /**\n * Re-set globalContext for each test case\n */\n if (globalContext !== context) {\n parsedFiles = {};\n globalContext = context;\n }\n\n fileNames.forEach((fileName) => {\n let sourceFile: ts.Node;\n let sourceFileName: string;\n\n const fileNameAlternatives = [];\n\n if (\n !fileName.endsWith(\".ts\") &&\n !fileName.endsWith(\".js\") &&\n !fileName.endsWith(\".mjs\")\n ) {\n fileNameAlternatives.push(`${fileName}.ts`);\n fileNameAlternatives.push(`${fileName}/index.ts`);\n\n } else {\n fileNameAlternatives.push(fileName);\n }\n\n for (let i = 0; i < fileNameAlternatives.length; i++) {\n try {\n sourceFileName = path.resolve(fileNameAlternatives[i]);\n\n if (parsedFiles[sourceFileName]) {\n break;\n }\n\n sourceFile = ts.createSourceFile(\n sourceFileName,\n readFileSync(sourceFileName).toString(),\n ts.ScriptTarget.Latest,\n true\n );\n\n parsedFiles[sourceFileName] = true;\n\n break;\n } catch (e) {\n // console.log(`${fileNameAlternatives[i]} => ${e.message}`);\n }\n }\n\n if (sourceFile) {\n inspectNode(sourceFile, context, decoratorName);\n }\n });\n\n return context.getStructures();\n}\n\n/**\n * TypeScript 4.8+ has introduced a change on how to access decorators.\n * - https://github.com/microsoft/TypeScript/pull/49089\n * - https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#decorators-are-placed-on-modifiers-on-typescripts-syntax-trees\n */\nexport function getDecorators(node: ts.Node | null | undefined,): undefined | ts.Decorator[] {\n if (node == undefined) { return undefined; }\n\n // TypeScript 4.7 and below\n // @ts-ignore\n if (node.decorators) { return node.decorators; }\n\n // TypeScript 4.8 and above\n // @ts-ignore\n if (ts.canHaveDecorators && ts.canHaveDecorators(node)) {\n // @ts-ignore\n const decorators = ts.getDecorators(node);\n return decorators ? Array.from(decorators) : undefined;\n }\n\n // @ts-ignore\n return node.modifiers?.filter(ts.isDecorator);\n}\n"]}
1
+ {"version":3,"file":"parser.js","sourceRoot":"","sources":["../../src/codegen/parser.ts"],"names":[],"mappings":";;;AAAA,iCAAiC;AACjC,6BAA6B;A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O,IAAI,CAAC,UAAU,CAAC;KAAE;IAEhD,2BAA2B;IAC3B,aAAa;IACb,IAAI,EAAE,CAAC,iBAAiB,IAAI,EAAE,CAAC,iBAAiB,CAAC,IAAI,CAAC,EAAE;QACpD,aAAa;QACb,MAAM,UAAU,GAAG,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;QAC1C,OAAO,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;KAC1D;IAED,aAAa;IACb,OAAO,IAAI,CAAC,SAAS,EAAE,MAAM,CAAC,EAAE,CAAC,WAAW,CAAC,CAAC;AAClD,CAAC;AAjBD,sCAiBC","sourcesContent":["import * as ts from \"typescript\";\nimport * as path from \"path\";\nimport {readFileSync} from \"fs\";\nimport {IStructure, Class, Interface, Property, Context, Enum} from \"./types\";\n\nlet currentStructure: IStructure;\nlet currentProperty: Property;\n\nlet globalContext: Context;\n\nfunction defineProperty(property: Property, initializer: any) {\n if (ts.isIdentifier(initializer)) {\n property.type = \"ref\";\n property.childType = initializer.text;\n\n } else if (initializer.kind == ts.SyntaxKind.ObjectLiteralExpression) {\n property.type = initializer.properties[0].name.text;\n property.childType = initializer.properties[0].initializer.text;\n\n } else if (initializer.kind == ts.SyntaxKind.ArrayLiteralExpression) {\n property.type = \"array\";\n property.childType = initializer.elements[0].text;\n\n } else {\n property.type = initializer.text;\n }\n}\n\nfunction inspectNode(node: ts.Node, context: Context, decoratorName: string) {\n switch (node.kind) {\n case ts.SyntaxKind.ImportClause:\n const specifier = (node.parent as any).moduleSpecifier;\n if (specifier && (specifier.text as string).startsWith('.')) {\n const currentDir = path.dirname(node.getSourceFile().fileName);\n const pathToImport = path.resolve(currentDir, specifier.text);\n parseFiles([pathToImport], decoratorName, globalContext);\n }\n break;\n\n case ts.SyntaxKind.ClassDeclaration:\n currentStructure = new Class();\n\n const heritageClauses = (node as ts.ClassLikeDeclarationBase).heritageClauses;\n if (heritageClauses && heritageClauses.length > 0) {\n (currentStructure as Class).extends = heritageClauses[0].types[0].expression.getText();\n }\n\n context.addStructure(currentStructure);\n break;\n\n case ts.SyntaxKind.InterfaceDeclaration:\n //\n // Only generate Interfaces if it has \"Message\" on its name.\n // Example: MyMessage\n //\n const interfaceName = (node as ts.TypeParameterDeclaration).name.escapedText.toString();\n if (interfaceName.indexOf(\"Message\") !== -1) {\n currentStructure = new Interface();\n currentStructure.name = interfaceName;\n\n context.addStructure(currentStructure);\n }\n break;\n\n case ts.SyntaxKind.EnumDeclaration:\n const enumName = (\n node as ts.EnumDeclaration\n ).name.escapedText.toString();\n currentStructure = new Enum();\n currentStructure.name = enumName;\n context.addStructure(currentStructure);\n break;\n\n case ts.SyntaxKind.ExtendsKeyword:\n // console.log(node.getText());\n break;\n\n case ts.SyntaxKind.PropertySignature:\n if (currentStructure instanceof Interface) {\n const interfaceDeclaration = node.parent;\n\n if (\n currentStructure.name !== (interfaceDeclaration as ts.TypeParameterDeclaration).name.escapedText.toString()\n ) {\n // skip if property if for a another interface than the one we're interested in.\n break;\n }\n\n // define a property of an interface\n const property = new Property();\n property.name = (node as any).name.escapedText.toString();\n property.type = (node as any).type.getText();\n currentStructure.addProperty(property);\n }\n break;\n\n case ts.SyntaxKind.Identifier:\n if (\n node.getText() === \"deprecated\" &&\n node.parent.kind !== ts.SyntaxKind.ImportSpecifier\n ) {\n currentProperty = new Property();\n currentProperty.deprecated = true;\n break;\n }\n\n if (node.getText() === decoratorName) {\n const prop: any = node.parent?.parent?.parent;\n const propDecorator = getDecorators(prop);\n const hasExpression = prop?.expression?.arguments;\n const hasDecorator = (propDecorator?.length > 0);\n\n /**\n * neither a `@type()` decorator or `type()` call. skip.\n */\n if (!hasDecorator && !hasExpression) {\n break;\n }\n\n // using as decorator\n if (propDecorator) {\n /**\n * Calling `@type()` as decorator\n */\n const typeDecorator: any = propDecorator.find((decorator => {\n return (decorator.expression as any).expression.escapedText === decoratorName;\n })).expression;\n\n const property = currentProperty || new Property();\n property.name = prop.name.escapedText;\n currentStructure.addProperty(property);\n\n const typeArgument = typeDecorator.arguments[0];\n defineProperty(property, typeArgument);\n\n } else if (\n prop.expression.arguments?.[1] &&\n prop.expression.expression.arguments?.[0]\n ) {\n /**\n * Calling `type()` as a regular method\n */\n const property = currentProperty || new Property();\n property.name = prop.expression.arguments[1].text;\n currentStructure.addProperty(property);\n\n const typeArgument = prop.expression.expression.arguments[0];\n defineProperty(property, typeArgument);\n }\n\n\n } else if (\n node.getText() === \"defineTypes\" &&\n (\n node.parent.kind === ts.SyntaxKind.CallExpression ||\n node.parent.kind === ts.SyntaxKind.PropertyAccessExpression\n )\n ) {\n /**\n * JavaScript source file (`.js`)\n * Using `defineTypes()`\n */\n const callExpression = (node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)\n ? node.parent.parent as ts.CallExpression\n : node.parent as ts.CallExpression;\n\n if (callExpression.kind !== ts.SyntaxKind.CallExpression) {\n break;\n }\n\n const className = callExpression.arguments[0].getText()\n currentStructure.name = className;\n\n const types = callExpression.arguments[1] as any;\n for (let i = 0; i < types.properties.length; i++) {\n const prop = types.properties[i];\n\n const property = currentProperty || new Property();\n property.name = prop.name.escapedText;\n currentStructure.addProperty(property);\n\n defineProperty(property, prop.initializer);\n }\n\n }\n\n if (node.parent.kind === ts.SyntaxKind.ClassDeclaration) {\n currentStructure.name = node.getText();\n }\n\n currentProperty = undefined;\n\n break;\n\n case ts.SyntaxKind.EnumMember:\n if (currentStructure instanceof Enum) {\n const initializer = (node as any).initializer?.getText();\n const name = node.getFirstToken().getText();\n const property = currentProperty || new Property();\n property.name = name;\n if (initializer !== undefined) {\n property.type = initializer;\n }\n currentStructure.addProperty(property);\n currentProperty = undefined;\n }\n break;\n }\n\n ts.forEachChild(node, (n) => inspectNode(n, context, decoratorName));\n}\n\nlet parsedFiles: { [filename: string]: boolean };\n\nexport function parseFiles(\n fileNames: string[],\n decoratorName: string = \"type\",\n context: Context = new Context()\n) {\n /**\n * Re-set globalContext for each test case\n */\n if (globalContext !== context) {\n parsedFiles = {};\n globalContext = context;\n }\n\n fileNames.forEach((fileName) => {\n let sourceFile: ts.Node;\n let sourceFileName: string;\n\n const fileNameAlternatives = [];\n\n if (\n !fileName.endsWith(\".ts\") &&\n !fileName.endsWith(\".js\") &&\n !fileName.endsWith(\".mjs\")\n ) {\n fileNameAlternatives.push(`${fileName}.ts`);\n fileNameAlternatives.push(`${fileName}/index.ts`);\n\n } else {\n fileNameAlternatives.push(fileName);\n }\n\n for (let i = 0; i < fileNameAlternatives.length; i++) {\n try {\n sourceFileName = path.resolve(fileNameAlternatives[i]);\n\n if (parsedFiles[sourceFileName]) {\n break;\n }\n\n sourceFile = ts.createSourceFile(\n sourceFileName,\n readFileSync(sourceFileName).toString(),\n ts.ScriptTarget.Latest,\n true\n );\n\n parsedFiles[sourceFileName] = true;\n\n break;\n } catch (e) {\n // console.log(`${fileNameAlternatives[i]} => ${e.message}`);\n }\n }\n\n if (sourceFile) {\n inspectNode(sourceFile, context, decoratorName);\n }\n });\n\n return context.getStructures();\n}\n\n/**\n * TypeScript 4.8+ has introduced a change on how to access decorators.\n * - https://github.com/microsoft/TypeScript/pull/49089\n * - https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#decorators-are-placed-on-modifiers-on-typescripts-syntax-trees\n */\nexport function getDecorators(node: ts.Node | null | undefined,): undefined | ts.Decorator[] {\n if (node == undefined) { return undefined; }\n\n // TypeScript 4.7 and below\n // @ts-ignore\n if (node.decorators) { return node.decorators; }\n\n // TypeScript 4.8 and above\n // @ts-ignore\n if (ts.canHaveDecorators && ts.canHaveDecorators(node)) {\n // @ts-ignore\n const decorators = ts.getDecorators(node);\n return decorators ? Array.from(decorators) : undefined;\n }\n\n // @ts-ignore\n return node.modifiers?.filter(ts.isDecorator);\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colyseus/schema",
3
- "version": "2.0.7",
3
+ "version": "2.0.8",
4
4
  "description": "Binary state serializer with delta encoding for games",
5
5
  "bin": {
6
6
  "schema-codegen": "./bin/schema-codegen"
@@ -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
+ }
@@ -1,7 +1,7 @@
1
1
  import * as ts from "typescript";
2
2
  import * as path from "path";
3
- import { readFileSync } from "fs";
4
- import { IStructure, Class, Interface, Property, Context, Enum } from "./types";
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 = getDecorators(prop);
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"]}
@@ -1,6 +0,0 @@
1
- export interface TypeDefinition {
2
- constructor: any;
3
- getProxy?: any;
4
- }
5
- export declare function registerType(identifier: string, definition: TypeDefinition): void;
6
- export declare function getType(identifier: string): TypeDefinition;
@@ -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
@@ -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}"]}