@aeriajs/compiler 0.0.62 → 0.0.63

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/dist/semantic.js CHANGED
@@ -1,43 +1,7 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.analyze = void 0;
37
- const common_1 = require("@aeriajs/common");
38
- const parser_js_1 = require("./parser.js");
39
- const diagnostic_js_1 = require("./diagnostic.js");
40
- const AST = __importStar(require("./ast.js"));
1
+ import { isValidCollection } from '@aeriajs/common';
2
+ import { locationMap } from './parser.js';
3
+ import { Diagnostic } from './diagnostic.js';
4
+ import * as AST from './ast.js';
41
5
  const collectionHasProperty = async (collection, propName, options = {}) => {
42
6
  let hasProperty = propName in collection.properties;
43
7
  if (!hasProperty) {
@@ -46,8 +10,8 @@ const collectionHasProperty = async (collection, propName, options = {}) => {
46
10
  return true;
47
11
  }
48
12
  const { importPath, symbolName } = collection.extends;
49
- const { [symbolName]: importedCollection } = await Promise.resolve(`${importPath}`).then(s => __importStar(require(s)));
50
- if (!(0, common_1.isValidCollection)(importedCollection)) {
13
+ const { [symbolName]: importedCollection } = await import(importPath);
14
+ if (!isValidCollection(importedCollection)) {
51
15
  throw new Error;
52
16
  }
53
17
  hasProperty = propName in importedCollection.description.properties;
@@ -55,7 +19,7 @@ const collectionHasProperty = async (collection, propName, options = {}) => {
55
19
  }
56
20
  return hasProperty;
57
21
  };
58
- const analyze = async (ast, options, errors = []) => {
22
+ export const analyze = async (ast, options, errors = []) => {
59
23
  const checkCollectionForeignProperties = async (foreignCollection, property, attributeName) => {
60
24
  if (!property[attributeName]) {
61
25
  return;
@@ -64,9 +28,9 @@ const analyze = async (ast, options, errors = []) => {
64
28
  if (!await collectionHasProperty(foreignCollection, foreignPropName, options)) {
65
29
  let location;
66
30
  if (property[AST.LOCATION_SYMBOL]) {
67
- location = parser_js_1.locationMap.get(property[AST.LOCATION_SYMBOL].attributes[attributeName]);
31
+ location = locationMap.get(property[AST.LOCATION_SYMBOL].attributes[attributeName]);
68
32
  }
69
- errors.push(new diagnostic_js_1.Diagnostic(`collection "${foreignCollection.name}" hasn't such property "${foreignPropName}"`, location));
33
+ errors.push(new Diagnostic(`collection "${foreignCollection.name}" hasn't such property "${foreignPropName}"`, location));
70
34
  }
71
35
  }
72
36
  };
@@ -78,8 +42,8 @@ const analyze = async (ast, options, errors = []) => {
78
42
  const propName = node[attributeName][index];
79
43
  if (!await collectionHasProperty(node, propName, options)) {
80
44
  const symbol = node[AST.LOCATION_SYMBOL].arrays[attributeName][index];
81
- const location = parser_js_1.locationMap.get(symbol);
82
- errors.push(new diagnostic_js_1.Diagnostic(`collection "${node.name}" hasn't such property "${propName}"`, location));
45
+ const location = locationMap.get(symbol);
46
+ errors.push(new Diagnostic(`collection "${node.name}" hasn't such property "${propName}"`, location));
83
47
  }
84
48
  }
85
49
  };
@@ -91,9 +55,9 @@ const analyze = async (ast, options, errors = []) => {
91
55
  const propName = node.property[attributeName][index];
92
56
  if (!(propName in node.nestedProperties)) {
93
57
  const symbol = node.property[AST.LOCATION_SYMBOL].arrays[attributeName][index];
94
- const location = parser_js_1.locationMap.get(symbol);
58
+ const location = locationMap.get(symbol);
95
59
  console.log(JSON.stringify(node));
96
- errors.push(new diagnostic_js_1.Diagnostic(`object hasn't such property "${propName}"`, location));
60
+ errors.push(new Diagnostic(`object hasn't such property "${propName}"`, location));
97
61
  }
98
62
  }
99
63
  };
@@ -116,8 +80,8 @@ const analyze = async (ast, options, errors = []) => {
116
80
  const refName = node.property.$ref;
117
81
  const foreignCollection = ast.collections.find(({ name }) => name === refName);
118
82
  if (!foreignCollection) {
119
- const location = parser_js_1.locationMap.get(node.property[AST.LOCATION_SYMBOL].type);
120
- errors.push(new diagnostic_js_1.Diagnostic(`invalid reference "${refName}"`, location));
83
+ const location = locationMap.get(node.property[AST.LOCATION_SYMBOL].type);
84
+ errors.push(new Diagnostic(`invalid reference "${refName}"`, location));
121
85
  return;
122
86
  }
123
87
  await checkCollectionForeignProperties(foreignCollection, node.property, 'indexes');
@@ -126,8 +90,8 @@ const analyze = async (ast, options, errors = []) => {
126
90
  if (node.property.constraints) {
127
91
  for (const [name, symbol] of node.property[AST.LOCATION_SYMBOL].contraintTerms) {
128
92
  if (!await collectionHasProperty(foreignCollection, name, options)) {
129
- const location = parser_js_1.locationMap.get(symbol);
130
- errors.push(new diagnostic_js_1.Diagnostic(`left operand "${name}" does not exist on collection "${foreignCollection.name}"`, location));
93
+ const location = locationMap.get(symbol);
94
+ errors.push(new Diagnostic(`left operand "${name}" does not exist on collection "${foreignCollection.name}"`, location));
131
95
  }
132
96
  }
133
97
  }
@@ -153,8 +117,8 @@ const analyze = async (ast, options, errors = []) => {
153
117
  const propName = propNames[index];
154
118
  if (!(propName in node.properties)) {
155
119
  const symbol = node[AST.LOCATION_SYMBOL].required[index];
156
- const location = parser_js_1.locationMap.get(symbol);
157
- errors.push(new diagnostic_js_1.Diagnostic(`collection "${node.name}" hasn't such property "${propName}"`, location));
120
+ const location = locationMap.get(symbol);
121
+ errors.push(new Diagnostic(`collection "${node.name}" hasn't such property "${propName}"`, location));
158
122
  }
159
123
  }
160
124
  }
@@ -165,8 +129,8 @@ const analyze = async (ast, options, errors = []) => {
165
129
  if (node[AST.LOCATION_SYMBOL].requiredTerms) {
166
130
  for (const [name, symbol] of node[AST.LOCATION_SYMBOL].requiredTerms) {
167
131
  if (!(name in node.properties)) {
168
- const location = parser_js_1.locationMap.get(symbol);
169
- errors.push(new diagnostic_js_1.Diagnostic(`invalid left operand "${name}"`, location));
132
+ const location = locationMap.get(symbol);
133
+ errors.push(new Diagnostic(`invalid left operand "${name}"`, location));
170
134
  }
171
135
  }
172
136
  }
@@ -177,15 +141,15 @@ const analyze = async (ast, options, errors = []) => {
177
141
  if (Array.isArray(option)) {
178
142
  for (const [i, propName] of option.entries()) {
179
143
  if (!(propName in node.properties)) {
180
- const location = parser_js_1.locationMap.get(value[i]);
181
- errors.push(new diagnostic_js_1.Diagnostic(`invalid property "${propName}"`, location));
144
+ const location = locationMap.get(value[i]);
145
+ errors.push(new Diagnostic(`invalid property "${propName}"`, location));
182
146
  }
183
147
  }
184
148
  }
185
149
  else {
186
150
  if (!(option in node.properties)) {
187
- const location = parser_js_1.locationMap.get(value);
188
- errors.push(new diagnostic_js_1.Diagnostic(`invalid property "${option}"`, location));
151
+ const location = locationMap.get(value);
152
+ errors.push(new Diagnostic(`invalid property "${option}"`, location));
189
153
  }
190
154
  }
191
155
  }
@@ -195,16 +159,16 @@ const analyze = async (ast, options, errors = []) => {
195
159
  if (node.formLayout.fields) {
196
160
  for (const [name, value] of Object.entries(node.formLayout[AST.LOCATION_SYMBOL].fields)) {
197
161
  if (!(name in node.properties)) {
198
- const location = parser_js_1.locationMap.get(value.name);
199
- errors.push(new diagnostic_js_1.Diagnostic(`invalid property "${name}"`, location));
162
+ const location = locationMap.get(value.name);
163
+ errors.push(new Diagnostic(`invalid property "${name}"`, location));
200
164
  }
201
165
  }
202
166
  }
203
167
  if (node.formLayout[AST.LOCATION_SYMBOL].terms) {
204
168
  for (const [name, symbol] of node.formLayout[AST.LOCATION_SYMBOL].terms) {
205
169
  if (!(name in node.properties)) {
206
- const location = parser_js_1.locationMap.get(symbol);
207
- errors.push(new diagnostic_js_1.Diagnostic(`invalid left operand "${name}"`, location));
170
+ const location = locationMap.get(symbol);
171
+ errors.push(new Diagnostic(`invalid left operand "${name}"`, location));
208
172
  }
209
173
  }
210
174
  }
@@ -213,8 +177,8 @@ const analyze = async (ast, options, errors = []) => {
213
177
  for (const [i, symbol] of node[AST.LOCATION_SYMBOL].searchIndexes.entries()) {
214
178
  const propName = node.search.indexes[i];
215
179
  if (!(propName in node.properties)) {
216
- const location = parser_js_1.locationMap.get(symbol);
217
- errors.push(new diagnostic_js_1.Diagnostic(`invalid property "${propName}"`, location));
180
+ const location = locationMap.get(symbol);
181
+ errors.push(new Diagnostic(`invalid property "${propName}"`, location));
218
182
  }
219
183
  }
220
184
  }
@@ -241,4 +205,3 @@ const analyze = async (ast, options, errors = []) => {
241
205
  errors,
242
206
  };
243
207
  };
244
- exports.analyze = analyze;
package/dist/token.js CHANGED
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TokenType = void 0;
4
- exports.TokenType = {
1
+ export const TokenType = {
5
2
  LineBreak: 'LINE_BREAK',
6
3
  Comment: 'COMMENT',
7
4
  LeftBracket: 'LEFT_BRACKET',
package/dist/types.js CHANGED
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
package/dist/utils.js CHANGED
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_EXPORT_SYMBOLS = void 0;
4
- exports.DEFAULT_EXPORT_SYMBOLS = {
1
+ export const DEFAULT_EXPORT_SYMBOLS = {
5
2
  count: 'aeria',
6
3
  get: 'aeria',
7
4
  getAll: 'aeria',
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@aeriajs/compiler",
3
- "version": "0.0.62",
3
+ "type": "module",
4
+ "version": "0.0.63",
4
5
  "description": "",
5
6
  "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
8
8
  "publishConfig": {
9
9
  "access": "public"
@@ -11,7 +11,7 @@
11
11
  "exports": {
12
12
  ".": {
13
13
  "types": "./dist/index.d.ts",
14
- "import": "./dist/index.js"
14
+ "default": "./dist/index.js"
15
15
  }
16
16
  },
17
17
  "keywords": [],
@@ -21,8 +21,8 @@
21
21
  "dist"
22
22
  ],
23
23
  "peerDependencies": {
24
- "@aeriajs/common": "^0.0.157",
25
- "@aeriajs/types": "^0.0.135"
24
+ "@aeriajs/common": "^0.0.158",
25
+ "@aeriajs/types": "^0.0.136"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@aeriajs/common": "link:../common",
@@ -33,9 +33,6 @@
33
33
  "test": "echo skipping",
34
34
  "lint": "eslint .",
35
35
  "lint:fix": "eslint . --fix",
36
- "build": "pnpm build:cjs && pnpm build:esm",
37
- "build:cjs": "tsc",
38
- "build:esm": "esbuild './src/**/*.ts' --outdir=dist --out-extension:.js=.mjs && pnpm build:esm-transform",
39
- "build:esm-transform": "pnpm -w esm-transform $PWD/dist"
36
+ "build": "tsc"
40
37
  }
41
38
  }
package/dist/ast.mjs DELETED
@@ -1,25 +0,0 @@
1
- "use strict";
2
- export const LOCATION_SYMBOL = Symbol();
3
- export const PropertyType = {
4
- str: "string",
5
- int: "integer",
6
- num: "number",
7
- bool: "boolean",
8
- enum: "enum",
9
- date: "string",
10
- datetime: "string",
11
- objectid: "string",
12
- const: "const"
13
- };
14
- export const PropertyModifiers = {
15
- Error: {
16
- packageName: "aeria",
17
- importPath: "aeria",
18
- symbolName: "errorSchema"
19
- },
20
- Result: {
21
- packageName: "aeria",
22
- importPath: "aeria",
23
- symbolName: "resultSchema"
24
- }
25
- };
@@ -1,85 +0,0 @@
1
- "use strict";
2
- import { errorSchema, resultSchema } from "@aeriajs/types";
3
- import { recursivelyUnwrapPropertyNodes, unwrapPropertyNode, stringify, UnquotedSymbol } from "./utils.mjs";
4
- export const generateContracts = (ast) => {
5
- if (ast.contracts.length === 0) {
6
- return false;
7
- }
8
- return {
9
- js: makeJSContractsCode(ast),
10
- dts: makeTSContractsCode(ast)
11
- };
12
- };
13
- const makeJSContractsCode = (ast) => {
14
- const imports = /* @__PURE__ */ new Set(["defineContract"]);
15
- const getCodeForResponse = (responseProperty) => {
16
- const { kind, modifier, ...propertyNode } = responseProperty;
17
- if (!modifier) {
18
- return stringify(unwrapPropertyNode(propertyNode));
19
- }
20
- const modifierSymbol = responseProperty.modifier === "Result" ? "resultSchema" : "errorSchema";
21
- if (!imports.has(modifierSymbol)) {
22
- imports.add(modifierSymbol);
23
- }
24
- return `${modifierSymbol}(${stringify(unwrapPropertyNode(propertyNode))})`;
25
- };
26
- const declarations = ast.contracts.map((node) => {
27
- const { name, kind, roles, response, ...contractProperty } = node;
28
- let responseString;
29
- if (response) {
30
- responseString = "";
31
- if (Array.isArray(response)) {
32
- const responseArray = [];
33
- for (const responseElement of response) {
34
- responseArray.push({
35
- [UnquotedSymbol]: getCodeForResponse(responseElement)
36
- });
37
- }
38
- responseString = stringify(responseArray);
39
- } else {
40
- responseString = getCodeForResponse(response);
41
- }
42
- }
43
- const contractSchema = recursivelyUnwrapPropertyNodes(contractProperty);
44
- if (responseString) {
45
- contractSchema.response = {
46
- [UnquotedSymbol]: responseString
47
- };
48
- }
49
- if (roles) {
50
- contractSchema.roles = roles;
51
- }
52
- return `export const ${name} = defineContract(${stringify(contractSchema)})`;
53
- }).join("\n\n");
54
- return `import { ${Array.from(imports).join(", ")} } from 'aeria'
55
-
56
- ` + declarations;
57
- };
58
- const getResponseSchema = (response) => {
59
- const responseSchema = unwrapPropertyNode(response);
60
- if (!response.modifier) {
61
- return responseSchema;
62
- }
63
- return response.modifier === "Result" ? resultSchema(responseSchema) : errorSchema(responseSchema);
64
- };
65
- const makeTSContractsCode = (ast) => {
66
- return ast.contracts.map((node) => {
67
- const { name, kind, roles, ...contractSchema } = node;
68
- let responseSchema = null;
69
- if (contractSchema.response) {
70
- if (Array.isArray(contractSchema.response)) {
71
- responseSchema = contractSchema.response.map(getResponseSchema);
72
- } else {
73
- responseSchema = getResponseSchema(contractSchema.response);
74
- }
75
- }
76
- const contractProperties = recursivelyUnwrapPropertyNodes(contractSchema);
77
- if (responseSchema) {
78
- contractProperties.response = responseSchema;
79
- }
80
- if (roles) {
81
- contractProperties.roles = roles;
82
- }
83
- return `export declare const ${node.name}: ${stringify(contractProperties)}`;
84
- }).join("\n\n");
85
- };
@@ -1,42 +0,0 @@
1
- "use strict";
2
- import { resizeFirstChar, getExtendName, getCollectionId } from "./utils.mjs";
3
- export const generateExports = (ast, options = {
4
- hasContracts: false
5
- }) => {
6
- const symbolsToExport = Object.values(ast.collections.reduce((symbols, node) => {
7
- const id = getCollectionId(node.name);
8
- symbols[id] = {
9
- id,
10
- schema: resizeFirstChar(node.name, true),
11
- extend: getExtendName(node.name)
12
- };
13
- return symbols;
14
- }, {}));
15
- let indexJs = `export * as collections from './collections/index.mjs'
16
- export { ${symbolsToExport.map((symbol) => symbol.extend).join(", ")} } from './collections/collections.mjs'
17
- `;
18
- let indexDts = `export * as collections from './collections/index.mjs'
19
- export { ${symbolsToExport.map((symbol) => `${symbol.extend}, ${symbol.schema}`).join(", ")} } from './collections/collections.mjs'
20
- `;
21
- if (options.hasContracts) {
22
- indexJs += "export * as contracts from './contracts/index.mjs'\n";
23
- indexDts += "export * as contracts from './contracts/index.mjs'\n";
24
- }
25
- const exports = {
26
- collections: {
27
- js: `export { ${symbolsToExport.map((symbol) => `${symbol.id}`).join(", ")} } from './collections.mjs'`,
28
- dts: `export { ${symbolsToExport.map((symbol) => `${symbol.id}`).join(", ")} } from './collections.mjs'`
29
- },
30
- index: {
31
- js: indexJs,
32
- dts: indexDts
33
- }
34
- };
35
- if (options.hasContracts) {
36
- exports.contracts = {
37
- js: "export * from './contracts.mjs'",
38
- dts: "export * from './contracts.mjs'"
39
- };
40
- }
41
- return exports;
42
- };
@@ -1,103 +0,0 @@
1
- "use strict";
2
- import { unwrapNode, recursivelyUnwrapPropertyNodes, stringify, makeASTImports, getCollectionId, UnquotedSymbol, getExposedFunctions, getExtendName, PACKAGE_NAME } from "./utils.mjs";
3
- const initialImportedFunctions = [
4
- "extendCollection",
5
- "defineCollection"
6
- ];
7
- export const generateJSCollections = (ast) => {
8
- let javascriptCode = "";
9
- const importsResult = makeASTImports(ast.collections, {
10
- [PACKAGE_NAME]: new Set(initialImportedFunctions)
11
- }, {
12
- includeRuntimeOnlyImports: true
13
- });
14
- javascriptCode += importsResult.code.join("\n") + "\n\n";
15
- javascriptCode += makeJSCollections(ast, importsResult.modifiedSymbols) + "\n\n";
16
- return javascriptCode;
17
- };
18
- const makeJSCollections = (ast, modifiedSymbols) => {
19
- const collectionCodes = {};
20
- for (const collectionNode of ast.collections) {
21
- const id = getCollectionId(collectionNode.name);
22
- const extendCollectionName = getExtendName(collectionNode.name);
23
- const collectionDefinition = `export const ${id} = ${collectionNode.extends ? `extendCollection(${id in modifiedSymbols ? modifiedSymbols[id] : id}, ${makeJSCollectionSchema(ast, collectionNode, id)})` : `defineCollection(${makeJSCollectionSchema(ast, collectionNode, id)})`}`;
24
- const collectionDeclaration = `export const ${extendCollectionName} = (collection) => extendCollection(${id}, collection)`;
25
- collectionCodes[collectionNode.name] = [
26
- "//" + collectionNode.name,
27
- collectionDefinition,
28
- collectionDeclaration
29
- ].join("\n");
30
- }
31
- return Object.values(collectionCodes).join("\n\n");
32
- };
33
- const makeJSCollectionSchema = (ast, collectionNode, collectionId) => {
34
- const collectionSchema = {
35
- item: {},
36
- description: {
37
- $id: collectionId,
38
- properties: {}
39
- }
40
- };
41
- for (const key of Object.keys(collectionNode)) {
42
- if (collectionNode[key] === void 0) {
43
- continue;
44
- }
45
- switch (key) {
46
- case "properties":
47
- collectionSchema.description[key] = recursivelyUnwrapPropertyNodes(collectionNode[key]);
48
- break;
49
- case "owned":
50
- collectionSchema.description[key] = collectionNode[key];
51
- break;
52
- case "middlewares":
53
- collectionSchema.middlewares = {
54
- [UnquotedSymbol]: `[ ${collectionNode[key].join(", ")} ]`
55
- };
56
- break;
57
- case "functions":
58
- collectionSchema.functions = {
59
- [UnquotedSymbol]: `{ ${makeJSFunctions(collectionNode[key])} }`
60
- };
61
- collectionSchema.exposedFunctions = getExposedFunctions(collectionNode[key]);
62
- break;
63
- case "required":
64
- collectionSchema.description[key] = collectionNode[key];
65
- break;
66
- case "table":
67
- case "tableMeta":
68
- case "filters":
69
- case "indexes":
70
- case "form":
71
- collectionSchema.description[key] = collectionNode[key];
72
- break;
73
- case "actions":
74
- case "individualActions":
75
- collectionSchema.description[key] = collectionNode[key];
76
- break;
77
- case "icon":
78
- collectionSchema.description[key] = collectionNode[key];
79
- break;
80
- case "presets":
81
- collectionSchema.description[key] = collectionNode[key];
82
- break;
83
- case "search":
84
- collectionSchema.description[key] = collectionNode[key];
85
- break;
86
- case "layout":
87
- collectionSchema.description[key] = unwrapNode(collectionNode[key]);
88
- break;
89
- case "formLayout":
90
- collectionSchema.description[key] = unwrapNode(collectionNode[key]);
91
- break;
92
- }
93
- }
94
- return stringify(collectionSchema);
95
- };
96
- const makeJSFunctions = (functionNodes) => {
97
- let output = "";
98
- for (const functionNode of functionNodes) {
99
- output += functionNode.exportSymbol ? functionNode.exportSymbol.symbolName : `${functionNode.name}: () => { throw new Error('Function not implemented') }`;
100
- output += ", ";
101
- }
102
- return output;
103
- };
@@ -1,114 +0,0 @@
1
- "use strict";
2
- import { unwrapNode, recursivelyUnwrapPropertyNodes, stringify, makeASTImports, resizeFirstChar, getCollectionId, UnquotedSymbol, getExposedFunctions, PACKAGE_NAME } from "./utils.mjs";
3
- const initialImportedTypes = [
4
- "Collection",
5
- "SchemaWithId",
6
- "ExtendCollection",
7
- "Context"
8
- ];
9
- export const generateTSCollections = (ast) => {
10
- let code = "";
11
- code += `import type { ${initialImportedTypes.join(", ")} } from '${PACKAGE_NAME}'
12
- `;
13
- const importsResult = makeASTImports(ast.collections);
14
- code += importsResult.code.join("\n") + "\n\n";
15
- code += makeTSCollections(ast, importsResult.modifiedSymbols) + "\n";
16
- return code;
17
- };
18
- const makeTSCollections = (ast, modifiedSymbols) => {
19
- const collectionCodes = {};
20
- for (const collectionNode of ast.collections) {
21
- const id = getCollectionId(collectionNode.name);
22
- const schemaName = resizeFirstChar(collectionNode.name, true);
23
- const typeName = id + "Collection";
24
- const collectionType = `export declare type ${typeName} = ${id in modifiedSymbols ? `ExtendCollection<typeof ${modifiedSymbols[id]}, ${makeTSCollectionSchema(collectionNode, id)}>` : makeTSCollectionSchema(collectionNode, id)}`;
25
- const collectionDeclaration = `export declare const ${id}: ${typeName} & { item: SchemaWithId<${typeName}["description"]> }`;
26
- const collectionSchema = `export declare type ${schemaName} = SchemaWithId<typeof ${id}.description>`;
27
- const collectionExtend = `export declare const extend${schemaName}Collection: <
28
- const TCollection extends {
29
- [P in Exclude<keyof Collection, "functions">]?: Partial<Collection[P]>
30
- } & {
31
- functions?: {
32
- [F: string]: (payload: any, context: Context<typeof ${id}["description"]>) => unknown
33
- }
34
- }>(collection: TCollection) => ExtendCollection<typeof ${id}, TCollection>`;
35
- collectionCodes[collectionNode.name] = [
36
- "//" + collectionNode.name,
37
- collectionType,
38
- collectionDeclaration,
39
- collectionSchema,
40
- collectionExtend
41
- ].join("\n");
42
- }
43
- return Object.values(collectionCodes).join("\n\n");
44
- };
45
- const makeTSCollectionSchema = (collectionNode, collectionId) => {
46
- const collectionSchema = {
47
- item: {},
48
- description: {
49
- $id: collectionId,
50
- properties: {}
51
- }
52
- };
53
- for (const key of Object.keys(collectionNode)) {
54
- if (collectionNode[key] === void 0) {
55
- continue;
56
- }
57
- switch (key) {
58
- case "properties":
59
- collectionSchema.description.properties = recursivelyUnwrapPropertyNodes(collectionNode[key]);
60
- break;
61
- case "owned":
62
- collectionSchema.description.owned = collectionNode[key];
63
- break;
64
- case "middlewares":
65
- collectionSchema.middlewares = {
66
- [UnquotedSymbol]: "import('@aeriajs/types').CollectionMiddleware<unknown>[]"
67
- };
68
- break;
69
- case "functions":
70
- collectionSchema.functions = makeTSFunctions(collectionNode[key]);
71
- collectionSchema.exposedFunctions = getExposedFunctions(collectionNode[key]);
72
- break;
73
- case "required":
74
- collectionSchema.description[key] = collectionNode[key];
75
- break;
76
- case "table":
77
- case "tableMeta":
78
- case "filters":
79
- case "indexes":
80
- case "form":
81
- collectionSchema.description[key] = collectionNode[key];
82
- break;
83
- case "actions":
84
- case "individualActions":
85
- collectionSchema.description[key] = collectionNode[key];
86
- break;
87
- case "icon":
88
- collectionSchema.description[key] = collectionNode[key];
89
- break;
90
- case "presets":
91
- collectionSchema.description[key] = collectionNode[key];
92
- break;
93
- case "search":
94
- collectionSchema.description[key] = collectionNode[key];
95
- break;
96
- case "layout":
97
- collectionSchema.description[key] = unwrapNode(collectionNode[key]);
98
- break;
99
- case "formLayout":
100
- collectionSchema.description[key] = unwrapNode(collectionNode[key]);
101
- break;
102
- }
103
- }
104
- return stringify(collectionSchema);
105
- };
106
- const makeTSFunctions = (functionNodes) => {
107
- const funs = {};
108
- for (const functionNode of functionNodes) {
109
- funs[functionNode.name] = {
110
- [UnquotedSymbol]: functionNode.exportSymbol ? `typeof import('${functionNode.exportSymbol.importPath}').${functionNode.exportSymbol.symbolName}` : "unknown"
111
- };
112
- }
113
- return funs;
114
- };
@@ -1,5 +0,0 @@
1
- "use strict";
2
- export * from "./generateJSCollections.mjs";
3
- export * from "./generateTSCollections.mjs";
4
- export * from "./generateExports.mjs";
5
- export * from "./generateContracts.mjs";