crankscript 0.5.1 → 0.7.0

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.
Files changed (63) hide show
  1. package/README.md +1 -11
  2. package/package.json +4 -2
  3. package/src/commands/CompileCommand/CompileCommand.d.ts +7 -0
  4. package/src/commands/CompileCommand/CompileCommand.js +49 -0
  5. package/src/commands/CompileCommand/CompileCommand.js.map +1 -0
  6. package/src/commands/CompileCommand/index.d.ts +1 -0
  7. package/src/commands/CompileCommand/index.js +3 -0
  8. package/src/commands/CompileCommand/index.js.map +1 -0
  9. package/src/commands/CompileCommand/plugin.js +168 -0
  10. package/src/commands/CompileCommand/plugin.js.map +1 -0
  11. package/src/commands/GenerateTypes/GenerateTypesCommand.d.ts +1 -0
  12. package/src/commands/GenerateTypes/GenerateTypesCommand.js +5 -1
  13. package/src/commands/GenerateTypes/GenerateTypesCommand.js.map +1 -1
  14. package/src/commands/GenerateTypes/components/GenerateTypes.d.ts +2 -1
  15. package/src/commands/GenerateTypes/components/GenerateTypes.js +11 -5
  16. package/src/commands/GenerateTypes/components/GenerateTypes.js.map +1 -1
  17. package/src/commands/GenerateTypes/fn/generateFunction.d.ts +4 -0
  18. package/src/commands/GenerateTypes/fn/generateFunction.js +28 -0
  19. package/src/commands/GenerateTypes/fn/generateFunction.js.map +1 -0
  20. package/src/commands/GenerateTypes/fn/generateNamespace.d.ts +4 -3
  21. package/src/commands/GenerateTypes/fn/generateNamespace.js +68 -21
  22. package/src/commands/GenerateTypes/fn/generateNamespace.js.map +1 -1
  23. package/src/commands/GenerateTypes/fn/getApiDefinitions.d.ts +2 -11
  24. package/src/commands/GenerateTypes/fn/getApiDefinitions.js +26 -9
  25. package/src/commands/GenerateTypes/fn/getApiDefinitions.js.map +1 -1
  26. package/src/commands/GenerateTypes/fn/getDescriptionsFromHtml.d.ts +5 -0
  27. package/src/commands/GenerateTypes/fn/getDescriptionsFromHtml.js +75 -0
  28. package/src/commands/GenerateTypes/fn/getDescriptionsFromHtml.js.map +1 -0
  29. package/src/commands/GenerateTypes/fn/parseFunctionSignature.d.ts +1 -1
  30. package/src/commands/GenerateTypes/fn/parseFunctionSignature.js +4 -6
  31. package/src/commands/GenerateTypes/fn/parseFunctionSignature.js.map +1 -1
  32. package/src/commands/GenerateTypes/hooks/useGenerateTypeFile.d.ts +2 -1
  33. package/src/commands/GenerateTypes/hooks/useGenerateTypeFile.js +14 -23
  34. package/src/commands/GenerateTypes/hooks/useGenerateTypeFile.js.map +1 -1
  35. package/src/commands/GenerateTypes/hooks/useGetVersion.d.ts +10 -0
  36. package/src/commands/GenerateTypes/hooks/useGetVersion.js +5 -1
  37. package/src/commands/GenerateTypes/hooks/useGetVersion.js.map +1 -1
  38. package/src/commands/GenerateTypes/hooks/useParseDocumentation.d.ts +0 -9
  39. package/src/commands/GenerateTypes/hooks/useParseDocumentation.js +3 -3
  40. package/src/commands/GenerateTypes/hooks/useParseDocumentation.js.map +1 -1
  41. package/src/commands/GenerateTypes/utils/createTypeProvider.d.ts +12 -0
  42. package/src/commands/GenerateTypes/utils/createTypeProvider.js +141 -0
  43. package/src/commands/GenerateTypes/utils/createTypeProvider.js.map +1 -0
  44. package/src/components/CheckList/CheckList.js +1 -2
  45. package/src/components/CheckList/CheckList.js.map +1 -1
  46. package/src/components/CheckList/Item.js +1 -0
  47. package/src/components/CheckList/Item.js.map +1 -1
  48. package/src/constants.d.ts +2 -0
  49. package/src/constants.js +4 -0
  50. package/src/constants.js.map +1 -1
  51. package/src/index.js +4 -2
  52. package/src/index.js.map +1 -1
  53. package/src/types.d.ts +34 -3
  54. package/src/types.js.map +1 -1
  55. package/src/utils/dirname.d.ts +1 -1
  56. package/src/utils/dirname.js +2 -3
  57. package/src/utils/dirname.js.map +1 -1
  58. package/src/commands/GenerateTypes/fn/getFunctionDescriptionsFromHtml.d.ts +0 -2
  59. package/src/commands/GenerateTypes/fn/getFunctionDescriptionsFromHtml.js +0 -37
  60. package/src/commands/GenerateTypes/fn/getFunctionDescriptionsFromHtml.js.map +0 -1
  61. package/src/commands/GenerateTypes/utils/playdateConstants.d.ts +0 -9
  62. package/src/commands/GenerateTypes/utils/playdateConstants.js +0 -134
  63. package/src/commands/GenerateTypes/utils/playdateConstants.js.map +0 -1
package/README.md CHANGED
@@ -1,11 +1 @@
1
- # cli
2
-
3
- This library was generated with [Nx](https://nx.dev).
4
-
5
- ## Building
6
-
7
- Run `nx build cli` to build the library.
8
-
9
- ## Running unit tests
10
-
11
- Run `nx test cli` to execute the unit tests via [Jest](https://jestjs.io).
1
+ # Cli
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "crankscript",
3
- "version": "0.5.1",
3
+ "version": "0.7.0",
4
4
  "scripts": {
5
5
  "dev": "tsx src/index.ts",
6
6
  "post-build": "tsc-alias --project tsconfig.json"
@@ -17,7 +17,9 @@
17
17
  "ink": "^5.0.1",
18
18
  "react": "^18.3.1",
19
19
  "ts-morph": "^23.0.0",
20
- "typanion": "^3.14.0"
20
+ "typanion": "^3.14.0",
21
+ "typescript-to-lua": "^1.27.0",
22
+ "typescript": "~5.6.2"
21
23
  },
22
24
  "type": "module",
23
25
  "main": "./src/index.js",
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ import { RenderableCommand } from '../../commands/RenderableCommand.js';
3
+ export declare class CompileCommand extends RenderableCommand {
4
+ static paths: string[][];
5
+ projectPath: string;
6
+ render(): React.JSX.Element;
7
+ }
@@ -0,0 +1,49 @@
1
+ import { join } from 'node:path';
2
+ import process from 'node:process';
3
+ import { Option } from 'clipanion';
4
+ import React, { useEffect } from 'react';
5
+ import * as t from 'typanion';
6
+ import * as tstl from 'typescript-to-lua';
7
+ import { LuaTarget } from 'typescript-to-lua';
8
+ import { RenderableCommand } from '../../commands/RenderableCommand.js';
9
+ import { RootFolder } from '../../constants.js';
10
+ const compile = (path)=>{
11
+ const result = tstl.transpileProject(join(path, 'tsconfig.json'), {
12
+ luaTarget: LuaTarget.Lua54,
13
+ outDir: join(path, 'Source'),
14
+ luaBundle: 'game.lua',
15
+ luaBundleEntry: join(path, 'src', 'index.ts'),
16
+ luaPlugins: [
17
+ {
18
+ name: join(RootFolder, 'src', 'commands', 'CompileCommand', 'plugin.cts')
19
+ }
20
+ ]
21
+ });
22
+ };
23
+ const Compile = ({ path })=>{
24
+ useEffect(()=>{
25
+ compile(path);
26
+ }, []);
27
+ return null;
28
+ };
29
+ export class CompileCommand extends RenderableCommand {
30
+ render() {
31
+ return /*#__PURE__*/ React.createElement(Compile, {
32
+ path: this.projectPath
33
+ });
34
+ }
35
+ constructor(...args){
36
+ super(...args);
37
+ this.projectPath = Option.String('-p,--path', process.cwd(), {
38
+ description: `Where to find the project. Defaults to the current working directory ("${process.cwd()}")`,
39
+ validator: t.isString()
40
+ });
41
+ }
42
+ }
43
+ CompileCommand.paths = [
44
+ [
45
+ 'compile'
46
+ ]
47
+ ];
48
+
49
+ //# sourceMappingURL=CompileCommand.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../../libs/cli/src/commands/CompileCommand/CompileCommand.tsx"],"sourcesContent":["import { join } from 'node:path';\nimport process from 'node:process';\nimport { Option } from 'clipanion';\nimport React, { useEffect } from 'react';\nimport * as t from 'typanion';\nimport * as tstl from 'typescript-to-lua';\nimport { LuaTarget } from 'typescript-to-lua';\nimport { RenderableCommand } from '@/cli/commands/RenderableCommand.js';\nimport { RootFolder } from '@/cli/constants.js';\n\nconst compile = (path: string) => {\n const result = tstl.transpileProject(join(path, 'tsconfig.json'), {\n luaTarget: LuaTarget.Lua54,\n outDir: join(path, 'Source'),\n luaBundle: 'game.lua',\n luaBundleEntry: join(path, 'src', 'index.ts'),\n luaPlugins: [\n {\n name: join(\n RootFolder,\n 'src',\n 'commands',\n 'CompileCommand',\n 'plugin.cts'\n ),\n },\n ],\n });\n};\n\nconst Compile = ({ path }: { path: string }) => {\n useEffect(() => {\n compile(path);\n }, []);\n\n return null;\n};\n\nexport class CompileCommand extends RenderableCommand {\n static override paths = [['compile']];\n\n projectPath = Option.String('-p,--path', process.cwd(), {\n description: `Where to find the project. Defaults to the current working directory (\"${process.cwd()}\")`,\n validator: t.isString(),\n });\n\n override render() {\n return <Compile path={this.projectPath} />;\n }\n}\n"],"names":["join","process","Option","React","useEffect","t","tstl","LuaTarget","RenderableCommand","RootFolder","compile","path","result","transpileProject","luaTarget","Lua54","outDir","luaBundle","luaBundleEntry","luaPlugins","name","Compile","CompileCommand","render","projectPath","String","cwd","description","validator","isString","paths"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,SAASA,IAAI,QAAQ,YAAY;AACjC,OAAOC,aAAa,eAAe;AACnC,SAASC,MAAM,QAAQ,YAAY;AACnC,OAAOC,SAASC,SAAS,QAAQ,QAAQ;AACzC,YAAYC,OAAO,WAAW;AAC9B,YAAYC,UAAU,oBAAoB;AAC1C,SAASC,SAAS,QAAQ,oBAAoB;AAC9C,SAASC,iBAAiB,QAAQ,sCAAsC;AACxE,SAASC,UAAU,QAAQ,qBAAqB;AAEhD,MAAMC,UAAU,CAACC;IACb,MAAMC,SAASN,KAAKO,gBAAgB,CAACb,KAAKW,MAAM,kBAAkB;QAC9DG,WAAWP,UAAUQ,KAAK;QAC1BC,QAAQhB,KAAKW,MAAM;QACnBM,WAAW;QACXC,gBAAgBlB,KAAKW,MAAM,OAAO;QAClCQ,YAAY;YACR;gBACIC,MAAMpB,KACFS,YACA,OACA,YACA,kBACA;YAER;SACH;IACL;AACJ;AAEA,MAAMY,UAAU,CAAC,EAAEV,IAAI,EAAoB;IACvCP,UAAU;QACNM,QAAQC;IACZ,GAAG,EAAE;IAEL,OAAO;AACX;AAEA,OAAO,MAAMW,uBAAuBd;IAQvBe,SAAS;QACd,qBAAO,oBAACF;YAAQV,MAAM,IAAI,CAACa,WAAW;;IAC1C;;;aAPAA,cAActB,OAAOuB,MAAM,CAAC,aAAaxB,QAAQyB,GAAG,IAAI;YACpDC,aAAa,CAAC,uEAAuE,EAAE1B,QAAQyB,GAAG,GAAG,EAAE,CAAC;YACxGE,WAAWvB,EAAEwB,QAAQ;QACzB;;AAKJ;AAXaP,eACOQ,QAAQ;IAAC;QAAC;KAAU;CAAC"}
@@ -0,0 +1 @@
1
+ export * from './CompileCommand.js';
@@ -0,0 +1,3 @@
1
+ export * from './CompileCommand.js';
2
+
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../../libs/cli/src/commands/CompileCommand/index.ts"],"sourcesContent":["export * from './CompileCommand.js';\n"],"names":[],"rangeMappings":"","mappings":"AAAA,cAAc,sBAAsB"}
@@ -0,0 +1,168 @@
1
+ import * as ts from 'typescript';
2
+ import * as tstl from 'typescript-to-lua';
3
+ import * as lua from 'typescript-to-lua/dist/LuaAST';
4
+ import { ScopeType } from 'typescript-to-lua/dist/transformation/utils/scope';
5
+ import { transformCallAndArguments } from 'typescript-to-lua/dist/transformation/visitors/call';
6
+ import { transformClassInstanceFields } from 'typescript-to-lua/dist/transformation/visitors/class/members/fields';
7
+ import { getExtendedNode, isStaticNode } from 'typescript-to-lua/dist/transformation/visitors/class/utils';
8
+ import { transformFunctionBodyContent, transformFunctionToExpression, transformParameters } from 'typescript-to-lua/dist/transformation/visitors/function';
9
+ function createClassCall(context, className, extendsNode) {
10
+ // class('X')
11
+ const classCall = tstl.createCallExpression(tstl.createIdentifier('class'), [
12
+ tstl.createStringLiteral(className.text)
13
+ ]);
14
+ let classCreationExpression;
15
+ if (extendsNode) {
16
+ // class('X').extends(Blah)
17
+ classCreationExpression = tstl.createCallExpression(tstl.createTableIndexExpression(classCall, tstl.createStringLiteral('extends')), [
18
+ context.transformExpression(extendsNode.expression)
19
+ ]);
20
+ } else {
21
+ classCreationExpression = tstl.createCallExpression(tstl.createTableIndexExpression(classCall, tstl.createStringLiteral('extends')), [
22
+ tstl.createIdentifier('Object')
23
+ ]);
24
+ }
25
+ return tstl.createExpressionStatement(classCreationExpression);
26
+ }
27
+ export function transformPropertyName(context, node) {
28
+ if (ts.isComputedPropertyName(node)) {
29
+ return context.transformExpression(node.expression);
30
+ } else if (ts.isIdentifier(node)) {
31
+ return tstl.createStringLiteral(node.text);
32
+ } else if (ts.isPrivateIdentifier(node)) {
33
+ throw new Error('PrivateIdentifier is not supported');
34
+ } else {
35
+ return context.transformExpression(node);
36
+ }
37
+ }
38
+ function transformConstructor(context, className, instanceFields, constructor) {
39
+ const methodName = 'init';
40
+ context.pushScope(ScopeType.Function);
41
+ const bodyStatements = [];
42
+ let params;
43
+ if (constructor) {
44
+ [params] = transformParameters(context, constructor == null ? void 0 : constructor.parameters, tstl.createIdentifier('self'));
45
+ } else {
46
+ params = [
47
+ tstl.createIdentifier('self')
48
+ ];
49
+ }
50
+ bodyStatements.push(tstl.createExpressionStatement(tstl.createCallExpression(tstl.createTableIndexExpression(tstl.createTableIndexExpression(className, tstl.createStringLiteral('super')), tstl.createStringLiteral('init')), params)));
51
+ const classInstanceFields = transformClassInstanceFields(context, instanceFields);
52
+ // initializers have to come before any body of the constructor
53
+ bodyStatements.push(...classInstanceFields);
54
+ if (constructor == null ? void 0 : constructor.body) {
55
+ const body = transformFunctionBodyContent(context, constructor.body);
56
+ // if the first expression in the body is a super call, ignore it, because we have
57
+ // constructed our own super call.
58
+ // if it's not, make sure to include the entire body.
59
+ const firstStatement = constructor.body.statements[0];
60
+ if (firstStatement && ts.isExpressionStatement(firstStatement) && ts.isCallExpression(firstStatement.expression) && firstStatement.expression.expression.kind === ts.SyntaxKind.SuperKeyword) {
61
+ bodyStatements.push(...body.slice(1));
62
+ } else {
63
+ bodyStatements.push(...body);
64
+ }
65
+ }
66
+ context.popScope();
67
+ return tstl.createAssignmentStatement(tstl.createTableIndexExpression(className, tstl.createStringLiteral(methodName)), tstl.createFunctionExpression(tstl.createBlock(bodyStatements), params));
68
+ }
69
+ function transformMethodDeclaration(context, node, className) {
70
+ const [functionExpression] = transformFunctionToExpression(context, node);
71
+ return tstl.createAssignmentStatement(tstl.createTableIndexExpression(className, transformPropertyName(context, node.name)), functionExpression);
72
+ }
73
+ export const transformClassDeclaration = (declaration, context)=>{
74
+ let className;
75
+ if (declaration.name) {
76
+ className = tstl.createIdentifier(declaration.name.text);
77
+ } else {
78
+ className = tstl.createIdentifier(context.createTempName('class'), declaration);
79
+ }
80
+ const extension = getExtendedNode(declaration);
81
+ if (context.classSuperInfos) {
82
+ context.classSuperInfos.push({
83
+ className,
84
+ extendedTypeNode: extension
85
+ });
86
+ } else {
87
+ context.classSuperInfos = [
88
+ {
89
+ className,
90
+ extendedTypeNode: extension
91
+ }
92
+ ];
93
+ }
94
+ // Get all properties with value
95
+ const properties = declaration.members.filter(ts.isPropertyDeclaration).filter((member)=>member.initializer);
96
+ // Divide properties into static and non-static
97
+ const instanceFields = properties.filter((prop)=>!isStaticNode(prop));
98
+ const statements = [];
99
+ // class('X')
100
+ statements.push(createClassCall(context, className, extension));
101
+ // function X:init()
102
+ // X.super.init(self)
103
+ // end
104
+ const constructor = declaration.members.find((n)=>ts.isConstructorDeclaration(n) && n.body !== undefined);
105
+ const transformedConstructor = transformConstructor(context, className, instanceFields, constructor);
106
+ if (transformedConstructor) {
107
+ statements.push(transformedConstructor);
108
+ }
109
+ const methods = declaration.members.filter(ts.isMethodDeclaration).map((method)=>transformMethodDeclaration(context, method, className)).filter((method)=>method !== undefined);
110
+ statements.push(...methods);
111
+ return statements;
112
+ };
113
+ const transformNewExpression = (node, context)=>{
114
+ const signature = context.checker.getResolvedSignature(node);
115
+ var _node_arguments;
116
+ const [name, params] = transformCallAndArguments(context, node.expression, (_node_arguments = node.arguments) != null ? _node_arguments : [
117
+ ts.factory.createTrue()
118
+ ], signature);
119
+ return tstl.createCallExpression(name, params);
120
+ };
121
+ export const transformSuperExpression = (expression, context)=>{
122
+ const superInfos = context.classSuperInfos;
123
+ let superInfo = undefined;
124
+ if (superInfos) {
125
+ superInfo = superInfos[superInfos.length - 1];
126
+ }
127
+ if (!superInfo) return lua.createAnonymousIdentifier(expression);
128
+ const { className } = superInfo;
129
+ // Using `super` without extended type node is a TypeScript error
130
+ // const extendsExpression = extendedTypeNode?.expression;
131
+ // let baseClassName: lua.AssignmentLeftHandSideExpression | undefined;
132
+ // if (extendsExpression && ts.isIdentifier(extendsExpression)) {
133
+ // const symbol = context.checker.getSymbolAtLocation(extendsExpression);
134
+ // if (symbol && !isSymbolExported(context, symbol)) {
135
+ // // Use "baseClassName" if base is a simple identifier
136
+ // baseClassName = transformIdentifier(context, extendsExpression);
137
+ // }
138
+ // }
139
+ // if (!baseClassName) {
140
+ // // Use "className.____super" if the base is not a simple identifier
141
+ // baseClassName = lua.createTableIndexExpression(
142
+ // className,
143
+ // lua.createStringLiteral('____super'),
144
+ // expression
145
+ // );
146
+ // }
147
+ return lua.createTableIndexExpression(className, lua.createStringLiteral('super'));
148
+ };
149
+ const plugin = {
150
+ visitors: {
151
+ [ts.SyntaxKind.ClassDeclaration]: transformClassDeclaration,
152
+ [ts.SyntaxKind.SuperKeyword]: transformSuperExpression,
153
+ [ts.SyntaxKind.NewExpression]: transformNewExpression,
154
+ [ts.SyntaxKind.CallExpression]: (node, context)=>{
155
+ if (ts.isIdentifier(node.expression) && node.expression.escapedText === 'require') {
156
+ const normalNode = context.superTransformExpression(node);
157
+ normalNode.expression.text = 'import';
158
+ normalNode.expression.originalName = 'import';
159
+ return normalNode;
160
+ } else {
161
+ return context.superTransformExpression(node);
162
+ }
163
+ }
164
+ }
165
+ };
166
+ export default plugin;
167
+
168
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../../libs/cli/src/commands/CompileCommand/plugin.cts"],"sourcesContent":["import * as ts from 'typescript';\nimport * as tstl from 'typescript-to-lua';\nimport { FunctionVisitor, TransformationContext } from 'typescript-to-lua';\nimport * as lua from 'typescript-to-lua/dist/LuaAST';\nimport { ScopeType } from 'typescript-to-lua/dist/transformation/utils/scope';\nimport { transformCallAndArguments } from 'typescript-to-lua/dist/transformation/visitors/call';\nimport { transformClassInstanceFields } from 'typescript-to-lua/dist/transformation/visitors/class/members/fields';\nimport {\n getExtendedNode,\n isStaticNode,\n} from 'typescript-to-lua/dist/transformation/visitors/class/utils';\nimport {\n transformFunctionBodyContent,\n transformFunctionToExpression,\n transformParameters,\n} from 'typescript-to-lua/dist/transformation/visitors/function';\n\nfunction createClassCall(\n context: tstl.TransformationContext,\n className: tstl.Identifier,\n extendsNode?: ts.ExpressionWithTypeArguments\n): tstl.Statement {\n // class('X')\n const classCall = tstl.createCallExpression(\n tstl.createIdentifier('class'),\n [tstl.createStringLiteral(className.text)]\n );\n let classCreationExpression: tstl.Expression;\n if (extendsNode) {\n // class('X').extends(Blah)\n classCreationExpression = tstl.createCallExpression(\n tstl.createTableIndexExpression(\n classCall,\n tstl.createStringLiteral('extends')\n ),\n [context.transformExpression(extendsNode.expression)]\n );\n } else {\n classCreationExpression = tstl.createCallExpression(\n tstl.createTableIndexExpression(\n classCall,\n tstl.createStringLiteral('extends')\n ),\n [tstl.createIdentifier('Object')]\n );\n }\n return tstl.createExpressionStatement(classCreationExpression);\n}\n\nexport function transformPropertyName(\n context: TransformationContext,\n node: ts.PropertyName\n): tstl.Expression {\n if (ts.isComputedPropertyName(node)) {\n return context.transformExpression(node.expression);\n } else if (ts.isIdentifier(node)) {\n return tstl.createStringLiteral(node.text);\n } else if (ts.isPrivateIdentifier(node)) {\n throw new Error('PrivateIdentifier is not supported');\n } else {\n return context.transformExpression(node);\n }\n}\n\nfunction transformConstructor(\n context: TransformationContext,\n className: tstl.Identifier,\n instanceFields: ts.PropertyDeclaration[],\n constructor?: ts.ConstructorDeclaration\n): tstl.Statement | undefined {\n const methodName = 'init';\n context.pushScope(ScopeType.Function);\n const bodyStatements: tstl.Statement[] = [];\n let params: tstl.Identifier[];\n if (constructor) {\n [params] = transformParameters(\n context,\n constructor?.parameters,\n tstl.createIdentifier('self')\n );\n } else {\n params = [tstl.createIdentifier('self')];\n }\n bodyStatements.push(\n tstl.createExpressionStatement(\n tstl.createCallExpression(\n tstl.createTableIndexExpression(\n tstl.createTableIndexExpression(\n className,\n tstl.createStringLiteral('super')\n ),\n tstl.createStringLiteral('init')\n ),\n params\n )\n )\n );\n const classInstanceFields = transformClassInstanceFields(\n context,\n instanceFields\n );\n // initializers have to come before any body of the constructor\n bodyStatements.push(...classInstanceFields);\n if (constructor?.body) {\n const body = transformFunctionBodyContent(context, constructor.body);\n // if the first expression in the body is a super call, ignore it, because we have\n // constructed our own super call.\n // if it's not, make sure to include the entire body.\n const firstStatement = constructor.body.statements[0];\n if (\n firstStatement &&\n ts.isExpressionStatement(firstStatement) &&\n ts.isCallExpression(firstStatement.expression) &&\n firstStatement.expression.expression.kind ===\n ts.SyntaxKind.SuperKeyword\n ) {\n bodyStatements.push(...body.slice(1));\n } else {\n bodyStatements.push(...body);\n }\n }\n context.popScope();\n return tstl.createAssignmentStatement(\n tstl.createTableIndexExpression(\n className,\n tstl.createStringLiteral(methodName)\n ),\n tstl.createFunctionExpression(tstl.createBlock(bodyStatements), params)\n );\n}\n\nfunction transformMethodDeclaration(\n context: TransformationContext,\n node: ts.MethodDeclaration,\n className: tstl.Identifier\n): tstl.Statement | undefined {\n const [functionExpression] = transformFunctionToExpression(context, node);\n return tstl.createAssignmentStatement(\n tstl.createTableIndexExpression(\n className,\n transformPropertyName(context, node.name)\n ),\n functionExpression\n );\n}\ninterface ClassSuperInfo {\n className: lua.Identifier;\n extendedTypeNode?: ts.ExpressionWithTypeArguments;\n}\n\nexport const transformClassDeclaration: FunctionVisitor<\n ts.ClassLikeDeclaration\n> = (\n declaration,\n context: TransformationContext & { classSuperInfos?: [ClassSuperInfo] }\n) => {\n let className: tstl.Identifier;\n if (declaration.name) {\n className = tstl.createIdentifier(declaration.name.text);\n } else {\n className = tstl.createIdentifier(\n context.createTempName('class'),\n declaration\n );\n }\n\n const extension = getExtendedNode(declaration);\n if (context.classSuperInfos) {\n context.classSuperInfos.push({\n className,\n extendedTypeNode: extension,\n });\n } else {\n context.classSuperInfos = [{ className, extendedTypeNode: extension }];\n }\n\n // Get all properties with value\n const properties = declaration.members\n .filter(ts.isPropertyDeclaration)\n .filter((member) => member.initializer);\n\n // Divide properties into static and non-static\n const instanceFields = properties.filter((prop) => !isStaticNode(prop));\n\n const statements: tstl.Statement[] = [];\n\n // class('X')\n statements.push(createClassCall(context, className, extension));\n\n // function X:init()\n // X.super.init(self)\n // end\n const constructor = declaration.members.find(\n (n): n is ts.ConstructorDeclaration =>\n ts.isConstructorDeclaration(n) && n.body !== undefined\n );\n const transformedConstructor = transformConstructor(\n context,\n className,\n instanceFields,\n constructor\n );\n if (transformedConstructor) {\n statements.push(transformedConstructor);\n }\n\n const methods = declaration.members\n .filter(ts.isMethodDeclaration)\n .map((method) => transformMethodDeclaration(context, method, className))\n .filter((method): method is tstl.Statement => method !== undefined);\n statements.push(...methods);\n\n return statements;\n};\n\nconst transformNewExpression: FunctionVisitor<ts.NewExpression> = (\n node,\n context\n) => {\n const signature = context.checker.getResolvedSignature(node);\n const [name, params] = transformCallAndArguments(\n context,\n node.expression,\n node.arguments ?? [ts.factory.createTrue()],\n signature\n );\n return tstl.createCallExpression(name, params);\n};\n\nexport const transformSuperExpression: FunctionVisitor<ts.SuperExpression> = (\n expression,\n context: TransformationContext & { classSuperInfos?: ClassSuperInfo[] }\n) => {\n const superInfos = context.classSuperInfos;\n let superInfo: ClassSuperInfo | undefined = undefined;\n if (superInfos) {\n superInfo = superInfos[superInfos.length - 1];\n }\n if (!superInfo) return lua.createAnonymousIdentifier(expression);\n const { className } = superInfo;\n\n // Using `super` without extended type node is a TypeScript error\n // const extendsExpression = extendedTypeNode?.expression;\n // let baseClassName: lua.AssignmentLeftHandSideExpression | undefined;\n\n // if (extendsExpression && ts.isIdentifier(extendsExpression)) {\n // const symbol = context.checker.getSymbolAtLocation(extendsExpression);\n // if (symbol && !isSymbolExported(context, symbol)) {\n // // Use \"baseClassName\" if base is a simple identifier\n // baseClassName = transformIdentifier(context, extendsExpression);\n // }\n // }\n\n // if (!baseClassName) {\n // // Use \"className.____super\" if the base is not a simple identifier\n // baseClassName = lua.createTableIndexExpression(\n // className,\n // lua.createStringLiteral('____super'),\n // expression\n // );\n // }\n\n return lua.createTableIndexExpression(\n className,\n lua.createStringLiteral('super')\n );\n};\n\nconst plugin = {\n visitors: {\n [ts.SyntaxKind.ClassDeclaration]: transformClassDeclaration,\n [ts.SyntaxKind.SuperKeyword]: transformSuperExpression,\n [ts.SyntaxKind.NewExpression]: transformNewExpression,\n [ts.SyntaxKind.CallExpression]: (node, context) => {\n if (\n ts.isIdentifier(node.expression) &&\n node.expression.escapedText === 'require'\n ) {\n const normalNode = context.superTransformExpression(\n node\n ) as unknown as {\n expression: { text: string; originalName: string };\n };\n\n normalNode.expression.text = 'import';\n normalNode.expression.originalName = 'import';\n\n return normalNode as unknown as lua.Expression;\n } else {\n return context.superTransformExpression(node);\n }\n },\n },\n} satisfies tstl.Plugin;\n\nexport default plugin;\n"],"names":["ts","tstl","lua","ScopeType","transformCallAndArguments","transformClassInstanceFields","getExtendedNode","isStaticNode","transformFunctionBodyContent","transformFunctionToExpression","transformParameters","createClassCall","context","className","extendsNode","classCall","createCallExpression","createIdentifier","createStringLiteral","text","classCreationExpression","createTableIndexExpression","transformExpression","expression","createExpressionStatement","transformPropertyName","node","isComputedPropertyName","isIdentifier","isPrivateIdentifier","Error","transformConstructor","instanceFields","constructor","methodName","pushScope","Function","bodyStatements","params","parameters","push","classInstanceFields","body","firstStatement","statements","isExpressionStatement","isCallExpression","kind","SyntaxKind","SuperKeyword","slice","popScope","createAssignmentStatement","createFunctionExpression","createBlock","transformMethodDeclaration","functionExpression","name","transformClassDeclaration","declaration","createTempName","extension","classSuperInfos","extendedTypeNode","properties","members","filter","isPropertyDeclaration","member","initializer","prop","find","n","isConstructorDeclaration","undefined","transformedConstructor","methods","isMethodDeclaration","map","method","transformNewExpression","signature","checker","getResolvedSignature","arguments","factory","createTrue","transformSuperExpression","superInfos","superInfo","length","createAnonymousIdentifier","plugin","visitors","ClassDeclaration","NewExpression","CallExpression","escapedText","normalNode","superTransformExpression","originalName"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,YAAYA,QAAQ,aAAa;AACjC,YAAYC,UAAU,oBAAoB;AAE1C,YAAYC,SAAS,gCAAgC;AACrD,SAASC,SAAS,QAAQ,oDAAoD;AAC9E,SAASC,yBAAyB,QAAQ,sDAAsD;AAChG,SAASC,4BAA4B,QAAQ,sEAAsE;AACnH,SACIC,eAAe,EACfC,YAAY,QACT,6DAA6D;AACpE,SACIC,4BAA4B,EAC5BC,6BAA6B,EAC7BC,mBAAmB,QAChB,0DAA0D;AAEjE,SAASC,gBACLC,OAAmC,EACnCC,SAA0B,EAC1BC,WAA4C;IAE5C,aAAa;IACb,MAAMC,YAAYd,KAAKe,oBAAoB,CACvCf,KAAKgB,gBAAgB,CAAC,UACtB;QAAChB,KAAKiB,mBAAmB,CAACL,UAAUM,IAAI;KAAE;IAE9C,IAAIC;IACJ,IAAIN,aAAa;QACb,2BAA2B;QAC3BM,0BAA0BnB,KAAKe,oBAAoB,CAC/Cf,KAAKoB,0BAA0B,CAC3BN,WACAd,KAAKiB,mBAAmB,CAAC,aAE7B;YAACN,QAAQU,mBAAmB,CAACR,YAAYS,UAAU;SAAE;IAE7D,OAAO;QACHH,0BAA0BnB,KAAKe,oBAAoB,CAC/Cf,KAAKoB,0BAA0B,CAC3BN,WACAd,KAAKiB,mBAAmB,CAAC,aAE7B;YAACjB,KAAKgB,gBAAgB,CAAC;SAAU;IAEzC;IACA,OAAOhB,KAAKuB,yBAAyB,CAACJ;AAC1C;AAEA,OA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A,WAAWE,MAAM,GAAG,EAAE;IACjD;IACA,IAAI,CAACD,WAAW,OAAOvF,IAAIyF,yBAAyB,CAACpE;IACrD,MAAM,EAAEV,SAAS,EAAE,GAAG4E;IAEtB,iEAAiE;IACjE,0DAA0D;IAC1D,uEAAuE;IAEvE,iEAAiE;IACjE,6EAA6E;IAC7E,0DAA0D;IAC1D,gEAAgE;IAChE,2EAA2E;IAC3E,QAAQ;IACR,IAAI;IAEJ,wBAAwB;IACxB,0EAA0E;IAC1E,sDAAsD;IACtD,qBAAqB;IACrB,gDAAgD;IAChD,qBAAqB;IACrB,SAAS;IACT,IAAI;IAEJ,OAAOvF,IAAImB,0BAA0B,CACjCR,WACAX,IAAIgB,mBAAmB,CAAC;AAEhC,EAAE;AAEF,MAAM0E,SAAS;IACXC,UAAU;QACN,CAAC7F,GAAGgD,UAAU,CAAC8C,gBAAgB,CAAC,EAAEpC;QAClC,CAAC1D,GAAGgD,UAAU,CAACC,YAAY,CAAC,EAAEsC;QAC9B,CAACvF,GAAGgD,UAAU,CAAC+C,aAAa,CAAC,EAAEf;QAC/B,CAAChF,GAAGgD,UAAU,CAACgD,cAAc,CAAC,EAAE,CAACtE,MAAMd;YACnC,IACIZ,GAAG4B,YAAY,CAACF,KAAKH,UAAU,KAC/BG,KAAKH,UAAU,CAAC0E,WAAW,KAAK,WAClC;gBACE,MAAMC,aAAatF,QAAQuF,wBAAwB,CAC/CzE;gBAKJwE,WAAW3E,UAAU,CAACJ,IAAI,GAAG;gBAC7B+E,WAAW3E,UAAU,CAAC6E,YAAY,GAAG;gBAErC,OAAOF;YACX,OAAO;gBACH,OAAOtF,QAAQuF,wBAAwB,CAACzE;YAC5C;QACJ;IACJ;AACJ;AAEA,eAAekE,OAAO"}
@@ -4,6 +4,7 @@ export declare class GenerateTypesCommand extends RenderableCommand {
4
4
  static paths: string[][];
5
5
  static usage: import("clipanion").Usage;
6
6
  output: string;
7
+ overwriteJson: boolean;
7
8
  version: any;
8
9
  render(): React.JSX.Element;
9
10
  }
@@ -11,7 +11,8 @@ export class GenerateTypesCommand extends RenderableCommand {
11
11
  const output = this.output.endsWith('.d.ts') ? this.output : join(this.output, 'playdate.d.ts');
12
12
  return /*#__PURE__*/ React.createElement(GenerateTypes, {
13
13
  output: output,
14
- version: this.version
14
+ version: this.version,
15
+ overwriteJson: this.overwriteJson
15
16
  });
16
17
  }
17
18
  constructor(...args){
@@ -20,6 +21,9 @@ export class GenerateTypesCommand extends RenderableCommand {
20
21
  description: `Where to generate the playdate.d.ts file. Defaults to the current working directory ("${process.cwd()}")`,
21
22
  validator: t.isString()
22
23
  });
24
+ this.overwriteJson = Option.Boolean('-j,--overwrite-json', false, {
25
+ description: 'Overwrite the JSON data file with the new data'
26
+ });
23
27
  this.version = Option.String({
24
28
  name: 'version',
25
29
  validator: t.isOneOf([
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../libs/cli/src/commands/GenerateTypes/GenerateTypesCommand.tsx"],"sourcesContent":["import { join } from 'node:path';\nimport * as process from 'node:process';\nimport { Command, Option } from 'clipanion';\nimport React from 'react';\nimport * as t from 'typanion';\nimport { AnyStrictValidator } from 'typanion';\nimport { RenderableCommand } from '@/cli/commands/RenderableCommand.js';\nimport { PlaydateSdkVersionIdentifier } from '@/cli/types.js';\nimport { GenerateTypes } from './components/GenerateTypes.js';\n\nexport class GenerateTypesCommand extends RenderableCommand {\n static override paths = [['generate-types']];\n static override usage = Command.Usage({\n description: 'Generate types from the Playdate SDK documentation',\n });\n\n output = Option.String('-o,--output', process.cwd(), {\n description: `Where to generate the playdate.d.ts file. Defaults to the current working directory (\"${process.cwd()}\")`,\n validator: t.isString(),\n });\n\n version = Option.String({\n name: 'version',\n validator: t.isOneOf([\n t.isLiteral(PlaydateSdkVersionIdentifier.Latest),\n t.matchesRegExp(/^[0-9]+\\.[0-9]+\\.[0-9]+$/) as AnyStrictValidator,\n ]),\n });\n\n override render() {\n const output = this.output.endsWith('.d.ts')\n ? this.output\n : join(this.output, 'playdate.d.ts');\n\n return <GenerateTypes output={output} version={this.version} />;\n }\n}\n"],"names":["join","process","Command","Option","React","t","RenderableCommand","PlaydateSdkVersionIdentifier","GenerateTypes","GenerateTypesCommand","render","output","endsWith","version","String","cwd","description","validator","isString","name","isOneOf","isLiteral","Latest","matchesRegExp","paths","usage","Usage"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,SAASA,IAAI,QAAQ,YAAY;AACjC,YAAYC,aAAa,eAAe;AACxC,SAASC,OAAO,EAAEC,MAAM,QAAQ,YAAY;AAC5C,OAAOC,WAAW,QAAQ;AAC1B,YAAYC,OAAO,WAAW;AAE9B,SAASC,iBAAiB,QAAQ,sCAAsC;AACxE,SAASC,4BAA4B,QAAQ,iBAAiB;AAC9D,SAASC,aAAa,QAAQ,gCAAgC;AAE9D,OAAO,MAAMC,6BAA6BH;IAmB7BI,SAAS;QACd,MAAMC,SAAS,IAAI,CAACA,MAAM,CAACC,QAAQ,CAAC,WAC9B,IAAI,CAACD,MAAM,GACXX,KAAK,IAAI,CAACW,MAAM,EAAE;QAExB,qBAAO,oBAACH;YAAcG,QAAQA;YAAQE,SAAS,IAAI,CAACA,OAAO;;IAC/D;;;aAnBAF,SAASR,OAAOW,MAAM,CAAC,eAAeb,QAAQc,GAAG,IAAI;YACjDC,aAAa,CAAC,sFAAsF,EAAEf,QAAQc,GAAG,GAAG,EAAE,CAAC;YACvHE,WAAWZ,EAAEa,QAAQ;QACzB;aAEAL,UAAUV,OAAOW,MAAM,CAAC;YACpBK,MAAM;YACNF,WAAWZ,EAAEe,OAAO,CAAC;gBACjBf,EAAEgB,SAAS,CAACd,6BAA6Be,MAAM;gBAC/CjB,EAAEkB,aAAa,CAAC;aACnB;QACL;;AASJ;AA1Bad,qBACOe,QAAQ;IAAC;QAAC;KAAiB;CAAC;AADnCf,qBAEOgB,QAAQvB,QAAQwB,KAAK,CAAC;IAClCV,aAAa;AACjB"}
1
+ {"version":3,"sources":["../../../../../../libs/cli/src/commands/GenerateTypes/GenerateTypesCommand.tsx"],"sourcesContent":["import { join } from 'node:path';\nimport * as process from 'node:process';\nimport { Command, Option } from 'clipanion';\nimport React from 'react';\nimport * as t from 'typanion';\nimport { AnyStrictValidator } from 'typanion';\nimport { RenderableCommand } from '@/cli/commands/RenderableCommand.js';\nimport { PlaydateSdkVersionIdentifier } from '@/cli/types.js';\nimport { GenerateTypes } from './components/GenerateTypes.js';\n\nexport class GenerateTypesCommand extends RenderableCommand {\n static override paths = [['generate-types']];\n static override usage = Command.Usage({\n description: 'Generate types from the Playdate SDK documentation',\n });\n\n output = Option.String('-o,--output', process.cwd(), {\n description: `Where to generate the playdate.d.ts file. Defaults to the current working directory (\"${process.cwd()}\")`,\n validator: t.isString(),\n });\n\n overwriteJson = Option.Boolean('-j,--overwrite-json', false, {\n description: 'Overwrite the JSON data file with the new data',\n });\n\n version = Option.String({\n name: 'version',\n validator: t.isOneOf([\n t.isLiteral(PlaydateSdkVersionIdentifier.Latest),\n t.matchesRegExp(/^[0-9]+\\.[0-9]+\\.[0-9]+$/) as AnyStrictValidator,\n ]),\n });\n\n override render() {\n const output = this.output.endsWith('.d.ts')\n ? this.output\n : join(this.output, 'playdate.d.ts');\n\n return (\n <GenerateTypes\n output={output}\n version={this.version}\n overwriteJson={this.overwriteJson}\n />\n );\n }\n}\n"],"names":["join","process","Command","Option","React","t","RenderableCommand","PlaydateSdkVersionIdentifier","GenerateTypes","GenerateTypesCommand","render","output","endsWith","version","overwriteJson","String","cwd","description","validator","isString","Boolean","name","isOneOf","isLiteral","Latest","matchesRegExp","paths","usage","Usage"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,SAASA,IAAI,QAAQ,YAAY;AACjC,YAAYC,aAAa,eAAe;AACxC,SAASC,OAAO,EAAEC,MAAM,QAAQ,YAAY;AAC5C,OAAOC,WAAW,QAAQ;AAC1B,YAAYC,OAAO,WAAW;AAE9B,SAASC,iBAAiB,QAAQ,sCAAsC;AACxE,SAASC,4BAA4B,QAAQ,iBAAiB;AAC9D,SAASC,aAAa,QAAQ,gCAAgC;AAE9D,OAAO,MAAMC,6BAA6BH;IAuB7BI,SAAS;QACd,MAAMC,SAAS,IAAI,CAACA,MAAM,CAACC,QAAQ,CAAC,WAC9B,IAAI,CAACD,MAAM,GACXX,KAAK,IAAI,CAACW,MAAM,EAAE;QAExB,qBACI,oBAACH;YACGG,QAAQA;YACRE,SAAS,IAAI,CAACA,OAAO;YACrBC,eAAe,IAAI,CAACA,aAAa;;IAG7C;;;aA7BAH,SAASR,OAAOY,MAAM,CAAC,eAAed,QAAQe,GAAG,IAAI;YACjDC,aAAa,CAAC,sFAAsF,EAAEhB,QAAQe,GAAG,GAAG,EAAE,CAAC;YACvHE,WAAWb,EAAEc,QAAQ;QACzB;aAEAL,gBAAgBX,OAAOiB,OAAO,CAAC,uBAAuB,OAAO;YACzDH,aAAa;QACjB;aAEAJ,UAAUV,OAAOY,MAAM,CAAC;YACpBM,MAAM;YACNH,WAAWb,EAAEiB,OAAO,CAAC;gBACjBjB,EAAEkB,SAAS,CAAChB,6BAA6BiB,MAAM;gBAC/CnB,EAAEoB,aAAa,CAAC;aACnB;QACL;;AAeJ;AApCahB,qBACOiB,QAAQ;IAAC;QAAC;KAAiB;CAAC;AADnCjB,qBAEOkB,QAAQzB,QAAQ0B,KAAK,CAAC;IAClCX,aAAa;AACjB"}
@@ -3,6 +3,7 @@ import { PlaydateSdkVersion } from '../../../types.js';
3
3
  interface Props {
4
4
  output: string;
5
5
  version: PlaydateSdkVersion;
6
+ overwriteJson: boolean;
6
7
  }
7
- export declare const GenerateTypes: ({ output, version }: Props) => React.JSX.Element;
8
+ export declare const GenerateTypes: ({ output, version, overwriteJson }: Props) => React.JSX.Element;
8
9
  export {};
@@ -5,12 +5,12 @@ import { useGetVersion } from '../../../commands/GenerateTypes/hooks/useGetVersi
5
5
  import { useParseDocumentation } from '../../../commands/GenerateTypes/hooks/useParseDocumentation.js';
6
6
  import { CheckList } from '../../../components/CheckList/index.js';
7
7
  import { useQuitOnCtrlC } from '../../../hooks/useQuitOnCtrlC.js';
8
- export const GenerateTypes = ({ output, version })=>{
8
+ export const GenerateTypes = ({ output, version, overwriteJson })=>{
9
9
  useQuitOnCtrlC();
10
- const { fetchedVersion, getVersion } = useGetVersion(version);
10
+ const { typeProvider, fetchedVersion, getVersion } = useGetVersion(version);
11
11
  const { html, fetchHtml } = useFetchHtml(fetchedVersion);
12
12
  const { definitions, parseDocumentation } = useParseDocumentation(html, version);
13
- const { generateTypeFile } = useGenerateTypeFile(output, definitions);
13
+ const { generateTypeFile } = useGenerateTypeFile(output, definitions, typeProvider);
14
14
  const items = useMemo(()=>{
15
15
  return [
16
16
  getVersion,
@@ -21,11 +21,17 @@ export const GenerateTypes = ({ output, version })=>{
21
21
  }, [
22
22
  fetchedVersion,
23
23
  html,
24
- definitions
24
+ definitions,
25
+ typeProvider
25
26
  ]);
26
27
  return /*#__PURE__*/ React.createElement(CheckList, {
27
28
  items: items,
28
- onFinish: process.exit
29
+ onFinish: ()=>{
30
+ if (overwriteJson) {
31
+ typeProvider == null ? void 0 : typeProvider.save();
32
+ }
33
+ process.exit();
34
+ }
29
35
  });
30
36
  };
31
37
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../../libs/cli/src/commands/GenerateTypes/components/GenerateTypes.tsx"],"sourcesContent":["import React, { useMemo } from 'react';\nimport { useFetchHtml } from '@/cli/commands/GenerateTypes/hooks/useFetchHtml.js';\nimport { useGenerateTypeFile } from '@/cli/commands/GenerateTypes/hooks/useGenerateTypeFile.js';\nimport { useGetVersion } from '@/cli/commands/GenerateTypes/hooks/useGetVersion.js';\nimport { useParseDocumentation } from '@/cli/commands/GenerateTypes/hooks/useParseDocumentation.js';\nimport { CheckList } from '@/cli/components/CheckList/index.js';\nimport { useQuitOnCtrlC } from '@/cli/hooks/useQuitOnCtrlC.js';\nimport { CheckListItem, PlaydateSdkVersion } from '@/cli/types.js';\n\ninterface Props {\n output: string;\n version: PlaydateSdkVersion;\n}\n\nexport const GenerateTypes = ({ output, version }: Props) => {\n useQuitOnCtrlC();\n\n const { fetchedVersion, getVersion } = useGetVersion(version);\n const { html, fetchHtml } = useFetchHtml(fetchedVersion);\n const { definitions, parseDocumentation } = useParseDocumentation(\n html,\n version\n );\n const { generateTypeFile } = useGenerateTypeFile(output, definitions);\n\n const items = useMemo(() => {\n return [\n getVersion,\n fetchHtml,\n parseDocumentation,\n generateTypeFile,\n ] as CheckListItem<unknown>[];\n }, [fetchedVersion, html, definitions]);\n\n return <CheckList items={items} onFinish={process.exit} />;\n};\n"],"names":["React","useMemo","useFetchHtml","useGenerateTypeFile","useGetVersion","useParseDocumentation","CheckList","useQuitOnCtrlC","GenerateTypes","output","version","fetchedVersion","getVersion","html","fetchHtml","definitions","parseDocumentation","generateTypeFile","items","onFinish","process","exit"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,OAAOA,SAASC,OAAO,QAAQ,QAAQ;AACvC,SAASC,YAAY,QAAQ,qDAAqD;AAClF,SAASC,mBAAmB,QAAQ,4DAA4D;AAChG,SAASC,aAAa,QAAQ,sDAAsD;AACpF,SAASC,qBAAqB,QAAQ,8DAA8D;AACpG,SAASC,SAAS,QAAQ,sCAAsC;AAChE,SAASC,cAAc,QAAQ,gCAAgC;AAQ/D,OAAO,MAAMC,gBAAgB,CAAC,EAAEC,MAAM,EAAEC,OAAO,EAAS;IACpDH;IAEA,MAAM,EAAEI,cAAc,EAAEC,UAAU,EAAE,GAAGR,cAAcM;IACrD,MAAM,EAAEG,IAAI,EAAEC,SAAS,EAAE,GAAGZ,aAAaS;IACzC,MAAM,EAAEI,WAAW,EAAEC,kBAAkB,EAAE,GAAGX,sBACxCQ,MACAH;IAEJ,MAAM,EAAEO,gBAAgB,EAAE,GAAGd,oBAAoBM,QAAQM;IAEzD,MAAMG,QAAQjB,QAAQ;QAClB,OAAO;YACHW;YACAE;YACAE;YACAC;SACH;IACL,GAAG;QAACN;QAAgBE;QAAME;KAAY;IAEtC,qBAAO,oBAACT;QAAUY,OAAOA;QAAOC,UAAUC,QAAQC,IAAI;;AAC1D,EAAE"}
1
+ {"version":3,"sources":["../../../../../../../libs/cli/src/commands/GenerateTypes/components/GenerateTypes.tsx"],"sourcesContent":["import React, { useMemo } from 'react';\nimport { useFetchHtml } from '@/cli/commands/GenerateTypes/hooks/useFetchHtml.js';\nimport { useGenerateTypeFile } from '@/cli/commands/GenerateTypes/hooks/useGenerateTypeFile.js';\nimport { useGetVersion } from '@/cli/commands/GenerateTypes/hooks/useGetVersion.js';\nimport { useParseDocumentation } from '@/cli/commands/GenerateTypes/hooks/useParseDocumentation.js';\nimport { CheckList } from '@/cli/components/CheckList/index.js';\nimport { useQuitOnCtrlC } from '@/cli/hooks/useQuitOnCtrlC.js';\nimport { CheckListItem, PlaydateSdkVersion } from '@/cli/types.js';\n\ninterface Props {\n output: string;\n version: PlaydateSdkVersion;\n overwriteJson: boolean;\n}\n\nexport const GenerateTypes = ({ output, version, overwriteJson }: Props) => {\n useQuitOnCtrlC();\n\n const { typeProvider, fetchedVersion, getVersion } = useGetVersion(version);\n const { html, fetchHtml } = useFetchHtml(fetchedVersion);\n const { definitions, parseDocumentation } = useParseDocumentation(\n html,\n version\n );\n const { generateTypeFile } = useGenerateTypeFile(\n output,\n definitions,\n typeProvider\n );\n\n const items = useMemo(() => {\n return [\n getVersion,\n fetchHtml,\n parseDocumentation,\n generateTypeFile,\n ] as CheckListItem<unknown>[];\n }, [fetchedVersion, html, definitions, typeProvider]);\n\n return (\n <CheckList\n items={items}\n onFinish={() => {\n if (overwriteJson) {\n typeProvider?.save();\n }\n\n process.exit();\n }}\n />\n );\n};\n"],"names":["React","useMemo","useFetchHtml","useGenerateTypeFile","useGetVersion","useParseDocumentation","CheckList","useQuitOnCtrlC","GenerateTypes","output","version","overwriteJson","typeProvider","fetchedVersion","getVersion","html","fetchHtml","definitions","parseDocumentation","generateTypeFile","items","onFinish","save","process","exit"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,OAAOA,SAASC,OAAO,QAAQ,QAAQ;AACvC,SAASC,YAAY,QAAQ,qDAAqD;AAClF,SAASC,mBAAmB,QAAQ,4DAA4D;AAChG,SAASC,aAAa,QAAQ,sDAAsD;AACpF,SAASC,qBAAqB,QAAQ,8DAA8D;AACpG,SAASC,SAAS,QAAQ,sCAAsC;AAChE,SAASC,cAAc,QAAQ,gCAAgC;AAS/D,OAAO,MAAMC,gBAAgB,CAAC,EAAEC,MAAM,EAAEC,OAAO,EAAEC,aAAa,EAAS;IACnEJ;IAEA,MAAM,EAAEK,YAAY,EAAEC,cAAc,EAAEC,UAAU,EAAE,GAAGV,cAAcM;IACnE,MAAM,EAAEK,IAAI,EAAEC,SAAS,EAAE,GAAGd,aAAaW;IACzC,MAAM,EAAEI,WAAW,EAAEC,kBAAkB,EAAE,GAAGb,sBACxCU,MACAL;IAEJ,MAAM,EAAES,gBAAgB,EAAE,GAAGhB,oBACzBM,QACAQ,aACAL;IAGJ,MAAMQ,QAAQnB,QAAQ;QAClB,OAAO;YACHa;YACAE;YACAE;YACAC;SACH;IACL,GAAG;QAACN;QAAgBE;QAAME;QAAaL;KAAa;IAEpD,qBACI,oBAACN;QACGc,OAAOA;QACPC,UAAU;YACN,IAAIV,eAAe;gBACfC,gCAAAA,aAAcU,IAAI;YACtB;YAEAC,QAAQC,IAAI;QAChB;;AAGZ,EAAE"}
@@ -0,0 +1,4 @@
1
+ import { ModuleDeclaration, SourceFile } from 'ts-morph';
2
+ import { createTypeProvider } from '../../../commands/GenerateTypes/utils/createTypeProvider.js';
3
+ import { FunctionDescription } from '../../../types.js';
4
+ export declare const generateFunction: (func: FunctionDescription, subject: SourceFile | ModuleDeclaration, typeProvider: ReturnType<typeof createTypeProvider>) => void;
@@ -0,0 +1,28 @@
1
+ import { _ as _extends } from "@swc/helpers/_/_extends";
2
+ import { TypescriptReservedNamed } from '../../../constants.js';
3
+ export const generateFunction = (func, subject, typeProvider)=>{
4
+ const isReserved = TypescriptReservedNamed.includes(func.name);
5
+ const name = isReserved ? `_${func.name}` : func.name;
6
+ var _typeProvider_getFunctionOverrideOptions;
7
+ subject.addFunction(_extends({
8
+ name,
9
+ docs: [
10
+ func.docs
11
+ ],
12
+ isExported: !isReserved,
13
+ returnType: typeProvider.getFunctionReturnType(func),
14
+ parameters: typeProvider.getParameters(func)
15
+ }, (_typeProvider_getFunctionOverrideOptions = typeProvider.getFunctionOverrideOptions(func)) != null ? _typeProvider_getFunctionOverrideOptions : {}));
16
+ if (isReserved) {
17
+ subject.addExportDeclaration({
18
+ namedExports: [
19
+ {
20
+ name,
21
+ alias: func.name
22
+ }
23
+ ]
24
+ });
25
+ }
26
+ };
27
+
28
+ //# sourceMappingURL=generateFunction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../../../libs/cli/src/commands/GenerateTypes/fn/generateFunction.ts"],"sourcesContent":["import {\n FunctionDeclarationStructure,\n ModuleDeclaration,\n SourceFile,\n} from 'ts-morph';\nimport { createTypeProvider } from '@/cli/commands/GenerateTypes/utils/createTypeProvider.js';\nimport { TypescriptReservedNamed } from '@/cli/constants.js';\nimport { FunctionDescription } from '@/cli/types.js';\n\nexport const generateFunction = (\n func: FunctionDescription,\n subject: SourceFile | ModuleDeclaration,\n typeProvider: ReturnType<typeof createTypeProvider>\n) => {\n const isReserved = TypescriptReservedNamed.includes(func.name);\n\n const name = isReserved ? `_${func.name}` : func.name;\n\n subject.addFunction({\n name,\n docs: [func.docs],\n isExported: !isReserved,\n returnType: typeProvider.getFunctionReturnType(func),\n parameters: typeProvider.getParameters(func),\n ...((typeProvider.getFunctionOverrideOptions(\n func\n ) as Partial<FunctionDeclarationStructure>) ?? {}),\n });\n\n if (isReserved) {\n subject.addExportDeclaration({\n namedExports: [\n {\n name,\n alias: func.name,\n },\n ],\n });\n }\n};\n"],"names":["TypescriptReservedNamed","generateFunction","func","subject","typeProvider","isReserved","includes","name","addFunction","docs","isExported","returnType","getFunctionReturnType","parameters","getParameters","getFunctionOverrideOptions","addExportDeclaration","namedExports","alias"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;","mappings":";AAMA,SAASA,uBAAuB,QAAQ,qBAAqB;AAG7D,OAAO,MAAMC,mBAAmB,CAC5BC,MACAC,SACAC;IAEA,MAAMC,aAAaL,wBAAwBM,QAAQ,CAACJ,KAAKK,IAAI;IAE7D,MAAMA,OAAOF,aAAa,CAAC,CAAC,EAAEH,KAAKK,IAAI,CAAC,CAAC,GAAGL,KAAKK,IAAI;QAQ5CH;IANTD,QAAQK,WAAW,CAAC;QAChBD;QACAE,MAAM;YAACP,KAAKO,IAAI;SAAC;QACjBC,YAAY,CAACL;QACbM,YAAYP,aAAaQ,qBAAqB,CAACV;QAC/CW,YAAYT,aAAaU,aAAa,CAACZ;OACnC,CAACE,2CAAAA,aAAaW,0BAA0B,CACxCb,iBADCE,2CAE0C,CAAC;IAGpD,IAAIC,YAAY;QACZF,QAAQa,oBAAoB,CAAC;YACzBC,cAAc;gBACV;oBACIV;oBACAW,OAAOhB,KAAKK,IAAI;gBACpB;aACH;QACL;IACJ;AACJ,EAAE"}
@@ -1,3 +1,4 @@
1
- import { ModuleDeclaration, SourceFile } from 'ts-morph';
2
- import { PlaydateNamespace } from '../../../types.js';
3
- export declare const generateNamespace: (subject: SourceFile | ModuleDeclaration, namespace: string, namespaceDescription: PlaydateNamespace, nextNamespaces: string[]) => void;
1
+ import { InterfaceDeclaration, ModuleDeclaration, SourceFile } from 'ts-morph';
2
+ import { createTypeProvider } from '../../../commands/GenerateTypes/utils/createTypeProvider.js';
3
+ import { PlaydateNamespace, PlaydateType } from '../../../types.js';
4
+ export declare const generateNamespace: (namespaceDescription: PlaydateNamespace, namespaces: string[], subjects: Map<string, SourceFile | ModuleDeclaration>, typeSubjects: Map<string, InterfaceDeclaration>, typeProvider: ReturnType<typeof createTypeProvider>, types: Record<string, PlaydateType>) => void;
@@ -1,32 +1,79 @@
1
- import { TypescriptReservedNamed } from '../../../constants.js';
2
- export const generateNamespace = (subject, namespace, namespaceDescription, nextNamespaces)=>{
1
+ import { _ as _extends } from "@swc/helpers/_/_extends";
2
+ import { VariableDeclarationKind } from 'ts-morph';
3
+ import { generateFunction } from '../../../commands/GenerateTypes/fn/generateFunction.js';
4
+ export const generateNamespace = (namespaceDescription, namespaces, subjects, typeSubjects, typeProvider, types)=>{
5
+ const subjectName = namespaces.slice(0, -1).join('.');
6
+ const namespaceName = namespaces[namespaces.length - 1];
7
+ const isRoot = namespaces.length === 1 && namespaces[0] === 'playdate';
8
+ const subject = namespaces.length === 1 ? subjects.get('root') : subjects.get(subjectName);
9
+ if (!subject) {
10
+ return;
11
+ }
3
12
  const module = subject.addModule({
4
- name: namespace.trim()
13
+ name: namespaceName
5
14
  });
6
- if (nextNamespaces.length) {
7
- generateNamespace(module, nextNamespaces[0], namespaceDescription, nextNamespaces.slice(1));
8
- } else {
9
- for (const func of namespaceDescription.functions){
10
- const isReserved = TypescriptReservedNamed.includes(func.name);
11
- const name = isReserved ? `_${func.name}` : func.name;
12
- module.addFunction({
13
- name,
15
+ const addMethods = (typeName, subj, methods)=>{
16
+ const interfaceName = typeName.split('.').map((name)=>name[0].toUpperCase() + name.slice(1)).join('');
17
+ const typeInterface = subj.addInterface({
18
+ name: interfaceName
19
+ });
20
+ typeSubjects.set(typeName, typeInterface);
21
+ for (const func of methods){
22
+ const parameters = typeProvider.getParameters(func);
23
+ typeInterface.addMethod(_extends({
24
+ name: func.name,
14
25
  docs: [
15
26
  func.docs
16
27
  ],
17
- isExported: !isReserved
18
- });
19
- if (isReserved) {
20
- module.addExportDeclaration({
21
- namedExports: [
22
- {
23
- name,
24
- alias: func.name
25
- }
26
- ]
28
+ returnType: typeProvider.getFunctionReturnType(func),
29
+ parameters
30
+ }, typeProvider.getFunctionOverrideOptions(func)));
31
+ }
32
+ };
33
+ if (isRoot) {
34
+ module.addJsDoc({
35
+ description: 'Playdate SDK'
36
+ });
37
+ module.addStatements(typeProvider.getStatements());
38
+ Object.keys(types).forEach((eachType)=>{
39
+ addMethods(eachType, module, types[eachType].methods);
40
+ });
41
+ }
42
+ subjects.set(namespaces.join('.'), module);
43
+ if (namespaceDescription.methods.length > 0) {
44
+ addMethods(namespaces.join('.'), subjects.get('playdate'), namespaceDescription.methods);
45
+ }
46
+ for (const property of namespaceDescription.properties){
47
+ const propertyDetails = typeProvider.getPropertyDetails(property);
48
+ if (!propertyDetails.isStatic) {
49
+ const typeName = namespaces.join('.');
50
+ if (typeSubjects.has(typeName)) {
51
+ const typeInterface = typeSubjects.get(typeName);
52
+ typeInterface.addProperty({
53
+ name: property.name,
54
+ type: propertyDetails.type,
55
+ docs: [
56
+ property.docs
57
+ ],
58
+ isReadonly: propertyDetails.isReadOnly
27
59
  });
28
60
  }
61
+ continue;
29
62
  }
63
+ const propertyType = propertyDetails.type;
64
+ module.addVariableStatement({
65
+ isExported: true,
66
+ declarationKind: VariableDeclarationKind.Const,
67
+ declarations: [
68
+ {
69
+ name: property.name,
70
+ type: propertyType
71
+ }
72
+ ]
73
+ });
74
+ }
75
+ for (const func of namespaceDescription.functions){
76
+ generateFunction(func, module, typeProvider);
30
77
  }
31
78
  };
32
79