@oak-digital/types-4-strapi-2 0.5.5 → 1.0.0-beta.1

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 (84) hide show
  1. package/.eslintrc.json +2 -1
  2. package/CHANGELOG.md +7 -0
  3. package/README.md +17 -5
  4. package/lib/attributes/Attributes.d.ts +4 -3
  5. package/lib/attributes/Attributes.js +102 -78
  6. package/lib/content-types/reader.js +49 -145
  7. package/lib/extra-types/ExtraType.d.ts +2 -2
  8. package/lib/extra-types/ExtraType.js +19 -38
  9. package/lib/extra-types/createExtraTypes.d.ts +1 -1
  10. package/lib/extra-types/createExtraTypes.js +11 -12
  11. package/lib/file/File.d.ts +1 -1
  12. package/lib/file/File.js +44 -43
  13. package/lib/index.js +57 -19
  14. package/lib/interface/BuiltinComponentInterface.js +9 -27
  15. package/lib/interface/BuiltinInterface.d.ts +0 -1
  16. package/lib/interface/BuiltinInterface.js +5 -26
  17. package/lib/interface/ComponentInterface.d.ts +2 -1
  18. package/lib/interface/ComponentInterface.js +30 -48
  19. package/lib/interface/Interface.d.ts +6 -2
  20. package/lib/interface/Interface.js +69 -70
  21. package/lib/interface/builtinInterfaces.d.ts +1941 -2
  22. package/lib/interface/builtinInterfaces.js +56 -46
  23. package/lib/plugins/PluginManager.d.ts +8 -2
  24. package/lib/plugins/PluginManager.js +28 -35
  25. package/lib/plugins/draft-and-publish/index.d.ts +1 -1
  26. package/lib/plugins/draft-and-publish/index.js +17 -21
  27. package/lib/plugins/i18n/index.d.ts +1 -1
  28. package/lib/plugins/i18n/index.js +17 -22
  29. package/lib/plugins/index.js +4 -4
  30. package/lib/plugins/types.d.ts +1 -1
  31. package/lib/plugins/types.js +1 -4
  32. package/lib/plugins/url-alias/index.js +12 -15
  33. package/lib/plugins/url-alias/type.js +21 -32
  34. package/lib/program/InterfaceManager.d.ts +374 -24
  35. package/lib/program/InterfaceManager.js +128 -376
  36. package/lib/readers/by-file.d.ts +321 -0
  37. package/lib/readers/by-file.js +116 -0
  38. package/lib/readers/load-strapi/index.d.ts +319 -0
  39. package/lib/readers/load-strapi/index.js +106 -0
  40. package/lib/readers/types/attributes.d.ts +1639 -0
  41. package/lib/readers/types/attributes.js +144 -0
  42. package/lib/readers/types/component.d.ts +723 -0
  43. package/lib/readers/types/component.js +11 -0
  44. package/lib/readers/types/content-type-reader.d.ts +16 -0
  45. package/lib/readers/types/content-type-reader.js +2 -0
  46. package/lib/readers/types/content-type.d.ts +766 -0
  47. package/lib/readers/types/content-type.js +16 -0
  48. package/lib/utils/casing/index.js +2 -2
  49. package/lib/utils/index.js +5 -39
  50. package/lib/writers/basic-writer.d.ts +24 -0
  51. package/lib/writers/basic-writer.js +75 -0
  52. package/lib/writers/types/writer.d.ts +4 -0
  53. package/lib/writers/types/writer.js +2 -0
  54. package/package.json +4 -2
  55. package/tests/strapi-project/.editorconfig +16 -0
  56. package/tests/strapi-project/.env.example +6 -0
  57. package/tests/strapi-project/README.md +57 -0
  58. package/tests/strapi-project/config/admin.ts +13 -0
  59. package/tests/strapi-project/config/api.ts +7 -0
  60. package/tests/strapi-project/config/database.ts +93 -0
  61. package/tests/strapi-project/config/middlewares.ts +12 -0
  62. package/tests/strapi-project/config/server.ts +10 -0
  63. package/tests/strapi-project/database/migrations/.gitkeep +0 -0
  64. package/tests/strapi-project/favicon.png +0 -0
  65. package/tests/strapi-project/package-lock.json +16125 -0
  66. package/tests/strapi-project/package.json +30 -0
  67. package/tests/strapi-project/public/robots.txt +3 -0
  68. package/tests/strapi-project/public/uploads/.gitkeep +0 -0
  69. package/tests/strapi-project/src/admin/app.example.tsx +35 -0
  70. package/tests/strapi-project/src/admin/webpack.config.example.js +9 -0
  71. package/tests/strapi-project/src/api/.gitkeep +0 -0
  72. package/tests/strapi-project/src/api/collection-1/content-types/collection-1/schema.json +18 -0
  73. package/tests/strapi-project/src/api/collection-1/content-types/generated-type/schema.json +18 -0
  74. package/tests/strapi-project/src/api/collection-1/controllers/collection-1.ts +7 -0
  75. package/tests/strapi-project/src/api/collection-1/controllers/generated-type.ts +7 -0
  76. package/tests/strapi-project/src/api/collection-1/routes/collection-1.ts +7 -0
  77. package/tests/strapi-project/src/api/collection-1/routes/generated-type.ts +7 -0
  78. package/tests/strapi-project/src/api/collection-1/services/collection-1.ts +7 -0
  79. package/tests/strapi-project/src/api/collection-1/services/generated-type.ts +7 -0
  80. package/tests/strapi-project/src/api/standalone-controller/controllers/standalone-controller.ts +13 -0
  81. package/tests/strapi-project/src/components/my-category/all-types.json +88 -0
  82. package/tests/strapi-project/src/components/other-category/sub-component.json +12 -0
  83. package/tests/strapi-project/src/extensions/.gitkeep +0 -0
  84. package/tests/strapi-project/src/index.ts +18 -0
package/.eslintrc.json CHANGED
@@ -38,7 +38,8 @@
38
38
  ],
39
39
  "quotes": [
40
40
  "error",
41
- "single"
41
+ "single",
42
+ { "allowTemplateLiterals": true }
42
43
  ],
43
44
  "semi": [
44
45
  "error",
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## v1
2
+
3
+ * Generated files and folders will be seperated by admin, plugin, api and builtin.
4
+ * Added readers and cli reader option.
5
+ * Added `load-strapi` reader, which can find all content types.
6
+ * Better type safety (removed some uses of any).
7
+ * targets es6 instead of es5.
package/README.md CHANGED
@@ -47,27 +47,35 @@ This can be done with the `--out` flag like in the following example.
47
47
 
48
48
  * Generate TypeScript interfaces for all your api content-types and components
49
49
  * Generate TypeScript interfaces for builtin types such as `Media` and `MediaFormat`
50
+ * Generate TypeScript interfaces for plugins' content types
50
51
  * Select input and output directory
51
52
  * Prettier formatting and ability to use your own `.prettierrc`.
52
53
  * Generate types for plugins such as [url-alias](https://github.com/strapi-community/strapi-plugin-url-alias)
53
54
  * Population by generics
54
55
 
55
- ### Planned features
56
-
57
- * Support for localization
58
-
59
56
  ## Flags
60
57
 
61
58
  | **Flag** | **Description** | **Default** |
62
59
  |-----------------------------|--------------------------------------------------------------------------------------|-------------|
63
- | -i, --in <dir> | The src directory for strapi | `./src` |
60
+ | -i, --in <dir> | The root directory for strapi | `./` |
64
61
  | -o, --out <dir> | The output directory to output the types to | `./types` |
62
+ | -r, --reader <reader> | The reader to use, see reader section | `by-file` |
65
63
  | --prefix <prefix> | A prefix for all generated interfaces | `I` |
66
64
  | --component-prefix <prefix> | A prefix for components | none |
67
65
  | -D, --delete-old | CAUTION: This option is equivalent to running `rm -rf` on the output directory first | `false` |
68
66
  | --prettier <file> | The prettier config file to use for formatting TypeScript interfaces | none |
69
67
  | --plugins <plugins...> | The plugins to use | none |
70
68
 
69
+ ## Readers
70
+
71
+ To fetch the content types from strapi, we need to read it in some way.
72
+ In version 0.x.x it was done by simply reading the generated files.
73
+ In version 1.x.x the default will be to read the generated files with the `by-file` reader.
74
+
75
+ You can select which reader you want to use in the cli with the `--reader` flag.
76
+ The `by-file` reader is very fast, but it will not work in some cases and may not provide all the desired types.
77
+ The `load-strapi` reader is slow, but gets all types, it works by creating a strapi instance that would then have loaded all the content types, which can then be etracted.
78
+
71
79
  ## Using plugins
72
80
 
73
81
  It is possible to generate types for plugins, for example url-alias gives a new field on your content types, so that plugin will automatically add that field to your types.
@@ -149,6 +157,10 @@ export default (plugin: any) => {
149
157
  }
150
158
  ```
151
159
 
160
+ ## API
161
+
162
+ The api is not finished, so it may change over time.
163
+
152
164
  ## Building
153
165
 
154
166
  To build this project, use the following command
@@ -1,13 +1,14 @@
1
1
  import { RelationNames } from '../file/File';
2
+ import { AttributeWithNested } from '../interface/builtinInterfaces';
2
3
  export default class Attributes {
3
- Attrs: Record<string, Record<string, any>>;
4
+ Attrs: Record<string, AttributeWithNested>;
4
5
  private RelationNames;
5
6
  constructor(attr: Record<string, Record<string, any>>, relationNames: RelationNames);
6
- isAttributePopulatable(attr: any): boolean;
7
+ isAttributePopulatable(attr: AttributeWithNested): boolean;
7
8
  hasPopulatableAttributes(): boolean;
8
9
  getPopulatableAttributes(): Set<string>;
9
10
  getDependencies(): string[];
10
- attributeToString(attrName: string, attr: any): string;
11
+ attributeToString(attrName: string, attr: AttributeWithNested): string;
11
12
  toFieldsString(): string;
12
13
  toString(): string;
13
14
  }
@@ -1,13 +1,17 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var constants_1 = require("../constants");
4
- var Attributes = /** @class */ (function () {
5
- function Attributes(attr, relationNames) {
6
+ const constants_1 = require("../constants");
7
+ const Interface_1 = __importDefault(require("../interface/Interface"));
8
+ class Attributes {
9
+ constructor(attr, relationNames) {
6
10
  this.RelationNames = {};
7
11
  this.Attrs = attr;
8
12
  this.RelationNames = relationNames;
9
13
  }
10
- Attributes.prototype.isAttributePopulatable = function (attr) {
14
+ isAttributePopulatable(attr) {
11
15
  // If it is a component / relation / dynamiczone it is always optional due to population
12
16
  switch (attr.type) {
13
17
  case 'nested':
@@ -22,38 +26,41 @@ var Attributes = /** @class */ (function () {
22
26
  break;
23
27
  }
24
28
  return false;
25
- };
26
- Attributes.prototype.hasPopulatableAttributes = function () {
27
- for (var attrName in this.Attrs) {
28
- var attr = this.Attrs[attrName];
29
+ }
30
+ hasPopulatableAttributes() {
31
+ for (const attrName in this.Attrs) {
32
+ const attr = this.Attrs[attrName];
29
33
  if (this.isAttributePopulatable(attr)) {
30
34
  return true;
31
35
  }
32
36
  }
33
37
  return false;
34
- };
35
- Attributes.prototype.getPopulatableAttributes = function () {
36
- var populatableAttributes = new Set();
37
- for (var attrName in this.Attrs) {
38
- var attr = this.Attrs[attrName];
38
+ }
39
+ getPopulatableAttributes() {
40
+ const populatableAttributes = new Set();
41
+ for (const attrName in this.Attrs) {
42
+ const attr = this.Attrs[attrName];
39
43
  if (this.isAttributePopulatable(attr)) {
40
44
  populatableAttributes.add(attrName);
41
45
  }
42
46
  }
43
47
  return populatableAttributes;
44
- };
45
- Attributes.prototype.getDependencies = function () {
48
+ }
49
+ getDependencies() {
46
50
  var _a;
47
- var dependencies = new Set();
48
- for (var attrName in this.Attrs) {
49
- var attr = this.Attrs[attrName];
51
+ const dependencies = new Set();
52
+ for (const attrName in this.Attrs) {
53
+ const attr = this.Attrs[attrName];
50
54
  switch (attr.type) {
51
55
  case 'nested':
52
- var attrs = new Attributes(attr.fields, this.RelationNames);
53
- var deps = attrs.getDependencies();
54
- deps.forEach(function (dep) { return dependencies.add(dep); });
56
+ const attrs = new Attributes(attr.fields, this.RelationNames);
57
+ const deps = attrs.getDependencies();
58
+ deps.forEach((dep) => dependencies.add(dep));
55
59
  break;
56
60
  case 'relation':
61
+ if (attr.relation === 'morphToMany') {
62
+ break;
63
+ }
57
64
  dependencies.add(attr.target);
58
65
  break;
59
66
  case 'component':
@@ -63,8 +70,8 @@ var Attributes = /** @class */ (function () {
63
70
  dependencies.add('builtins::Media');
64
71
  break;
65
72
  case 'dynamiczone':
66
- var componentDeps = (_a = attr.components) !== null && _a !== void 0 ? _a : [];
67
- componentDeps.forEach(function (dep) { return dependencies.add(dep); });
73
+ const componentDeps = (_a = attr.components) !== null && _a !== void 0 ? _a : [];
74
+ componentDeps.forEach((dep) => dependencies.add(dep));
68
75
  break;
69
76
  default:
70
77
  continue;
@@ -80,60 +87,72 @@ var Attributes = /** @class */ (function () {
80
87
  dependencies.add('builtins::RequiredBy');
81
88
  }
82
89
  return Array.from(dependencies);
83
- };
84
- Attributes.prototype.attributeToString = function (attrName, attr) {
85
- var _this = this;
90
+ }
91
+ attributeToString(attrName, attr) {
86
92
  var _a, _b, _c;
87
- var isPopulatable = this.isAttributePopulatable(attr);
88
- var isOptional = isPopulatable;
89
- var optionalString = isOptional ? '?' : '';
90
- var orNull = ' | null';
93
+ const isPopulatable = this.isAttributePopulatable(attr);
94
+ const isOptional = isPopulatable;
95
+ const optionalString = isOptional ? '?' : '';
96
+ const orNull = ' | null';
91
97
  // TODO: only add this in non paranoid mode
92
98
  /* const requiredString = attr.required !== true ? orNull : ''; */
93
- var requiredString = (attr === null || attr === void 0 ? void 0 : attr[constants_1.CERTAINLY_REQUIRED_KEY]) === true ? '' : orNull;
94
- var str = " ".concat(attrName).concat(optionalString, ": ");
95
- var isArray = false;
99
+ const requiredString = (attr === null || attr === void 0 ? void 0 : attr[constants_1.CERTAINLY_REQUIRED_KEY]) === true ? '' : orNull;
100
+ let str = ` ${attrName}${optionalString}: `;
101
+ let isArray = false;
96
102
  switch (attr.type) {
97
103
  // types-4-strapi-2 specific, used for builtin types
98
104
  case 'nested':
99
105
  // Be careful with recursion
100
106
  // console.log(attr);
101
- var nullableString = ((_a = attr === null || attr === void 0 ? void 0 : attr.nullable) !== null && _a !== void 0 ? _a : false) ? ' | null' : '';
102
- var newAttrs = new Attributes(attr.fields, this.RelationNames);
107
+ const nullableString = ((_a = attr === null || attr === void 0 ? void 0 : attr.nullable) !== null && _a !== void 0 ? _a : false) ? ' | null' : '';
108
+ const newAttrs = new Attributes(attr.fields, this.RelationNames);
103
109
  str += newAttrs.toString() + nullableString;
104
110
  break;
105
111
  case 'relation':
106
- var apiName = attr.target;
112
+ if (attr.relation === 'morphToMany') {
113
+ str += 'any';
114
+ break;
115
+ }
116
+ const apiName = attr.target;
107
117
  // console.log(this.RelationNames, apiName)
108
- var dependencyName = (_c = (_b = this.RelationNames[apiName]) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : 'any';
109
- var relationMultipleString = attr.relation.endsWith('ToMany')
118
+ const dependencyName = (_c = (_b = this.RelationNames[apiName]) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : 'any';
119
+ const relationMultipleString = attr.relation.endsWith('ToMany')
110
120
  ? '[]'
111
121
  : orNull;
112
- str += "{ data: ";
122
+ str += `{ data: `;
113
123
  str += dependencyName;
114
- // TODO: assert correctly
115
- var relationInterface = this.RelationNames[apiName].file;
124
+ const relationInterfaceWrapped = this.RelationNames[apiName];
125
+ if (!relationInterfaceWrapped) {
126
+ throw new Error(`Could not find relation ${apiName}, consider using another reader.`);
127
+ }
128
+ const relationInterface = relationInterfaceWrapped.file;
129
+ if (!(relationInterface instanceof Interface_1.default)) {
130
+ throw new Error(`t4s internal error: Expected relation ${apiName} to be an interface, but it was not. Please open an issue on the GitHub repository.`);
131
+ }
116
132
  if (relationInterface.hasPopulatableAttributes()) {
117
133
  str += '<';
118
- str += "".concat(this.RelationNames['builtins::ExtractNested'].name, "<").concat(constants_1.POPULATE_GENERIC_NAME, ", '").concat(attrName, "'>");
134
+ str += `${this.RelationNames['builtins::ExtractNested'].name}<${constants_1.POPULATE_GENERIC_NAME}, '${attrName}'>`;
119
135
  str += '>';
120
136
  }
121
137
  str += relationMultipleString;
122
- str += "; }";
138
+ str += `; }`;
123
139
  break;
124
140
  case 'component':
125
- var componentName = attr.component;
126
- var relationNameObj = this.RelationNames[componentName];
141
+ const componentName = attr.component;
142
+ const relationNameObj = this.RelationNames[componentName];
127
143
  // TODO: assert correctly
128
- var componentInterface = relationNameObj.file;
144
+ const componentInterface = relationNameObj.file;
145
+ if (!(componentInterface instanceof Interface_1.default)) {
146
+ throw new Error(`t4s internal error: Expected component ${componentName} to be an interface, but it was not. Please open an issue on the GitHub repository.`);
147
+ }
129
148
  /* console.log(this.RelationNames); */
130
- var dependencyComponentName = relationNameObj.name;
149
+ const dependencyComponentName = relationNameObj.name;
131
150
  /* isArray = attr.repeatable ?? false; */
132
151
  isArray = false; // we already handle this here.
133
152
  str += dependencyComponentName;
134
153
  if (componentInterface.hasPopulatableAttributes()) {
135
154
  str += '<';
136
- str += "".concat(this.RelationNames['builtins::ExtractNested'].name, "<").concat(constants_1.POPULATE_GENERIC_NAME, ", '").concat(attrName, "'>");
155
+ str += `${this.RelationNames['builtins::ExtractNested'].name}<${constants_1.POPULATE_GENERIC_NAME}, '${attrName}'>`;
137
156
  str += '>';
138
157
  }
139
158
  if (attr.repeatable) {
@@ -144,40 +163,46 @@ var Attributes = /** @class */ (function () {
144
163
  }
145
164
  break;
146
165
  case 'media':
147
- var mediaMultipleString = attr.multiple
166
+ const mediaMultipleString = attr.multiple
148
167
  ? '[]'
149
168
  : requiredString;
150
- str += "{ data: ";
169
+ str += `{ data: `;
151
170
  str += this.RelationNames['builtins::Media'].name;
152
171
  str += mediaMultipleString;
153
- str += "; }";
172
+ str += `; }`;
154
173
  break;
155
174
  case 'password':
156
175
  return null;
157
176
  case 'enumeration':
158
- var hasDefault = 'default' in attr;
159
- var enums = attr.enum.map(function (en) { return "\"".concat(en, "\""); });
177
+ const hasDefault = 'default' in attr;
178
+ const enums = attr.enum.map((en) => `"${en}"`);
160
179
  enums.push('null');
161
- var typeString = enums.join(' | ');
180
+ const typeString = enums.join(' | ');
162
181
  str += typeString;
163
182
  break;
164
183
  case 'dynamiczone':
165
184
  // console.log(attr.components);
166
- var relations = attr.components.map(function (componentName) {
167
- var component = _this.RelationNames[componentName];
185
+ const relations = attr.components.map((componentName) => {
186
+ const component = this.RelationNames[componentName];
187
+ if (!component) {
188
+ throw new Error(`Could not find component ${componentName}, consider using another reader.`);
189
+ }
168
190
  // in this context file should always be an interface
169
- var file = component.file;
170
- var populatable = file.hasPopulatableAttributes();
191
+ const file = component.file;
192
+ if (!(file instanceof Interface_1.default)) {
193
+ throw new Error(`t4s internal error: Expected component ${componentName} to be an interface, but it was not. Please open an issue on the GitHub repository.`);
194
+ }
195
+ const populatable = file.hasPopulatableAttributes();
171
196
  /* false; */
172
- var populatableString = populatable
173
- ? "".concat(_this.RelationNames[componentName].name, "<").concat(_this.RelationNames['builtins::ExtractNested'].name, "<").concat(constants_1.POPULATE_GENERIC_NAME, ", '").concat(attrName, "'>>")
174
- : _this.RelationNames[componentName].name;
197
+ const populatableString = populatable
198
+ ? `${this.RelationNames[componentName].name}<${this.RelationNames['builtins::ExtractNested'].name}<${constants_1.POPULATE_GENERIC_NAME}, '${attrName}'>>`
199
+ : this.RelationNames[componentName].name;
175
200
  return populatableString;
176
201
  });
177
202
  // console.log(relations);
178
- var relationsString = relations.join(' | ');
203
+ const relationsString = relations.join(' | ');
179
204
  // console.log(relationsString);
180
- str += "Array<".concat(relationsString, ">");
205
+ str += `Array<${relationsString}>`;
181
206
  break;
182
207
  case 'string':
183
208
  case 'text':
@@ -209,28 +234,27 @@ var Attributes = /** @class */ (function () {
209
234
  str += 'any';
210
235
  break;
211
236
  }
212
- var isArrayString = isArray ? '[]' : '';
213
- str += "".concat(isArrayString, ";");
237
+ const isArrayString = isArray ? '[]' : '';
238
+ str += `${isArrayString};`;
214
239
  return str;
215
- };
216
- Attributes.prototype.toFieldsString = function () {
217
- var strings = [];
218
- for (var attrName in this.Attrs) {
219
- var attr = this.Attrs[attrName];
220
- var attrString = this.attributeToString(attrName, attr);
240
+ }
241
+ toFieldsString() {
242
+ const strings = [];
243
+ for (const attrName in this.Attrs) {
244
+ const attr = this.Attrs[attrName];
245
+ const attrString = this.attributeToString(attrName, attr);
221
246
  if (attrString === null) {
222
247
  continue;
223
248
  }
224
249
  strings.push(attrString);
225
250
  }
226
- return strings.map(function (s) { return "".concat(s, "\n"); }).join('');
227
- };
228
- Attributes.prototype.toString = function () {
229
- var strings = ['{'];
251
+ return strings.map((s) => `${s}\n`).join('');
252
+ }
253
+ toString() {
254
+ const strings = ['{'];
230
255
  strings.push(this.toFieldsString());
231
256
  strings.push('}');
232
257
  return strings.join('\n');
233
- };
234
- return Attributes;
235
- }());
258
+ }
259
+ }
236
260
  exports.default = Attributes;
@@ -8,170 +8,74 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
11
  Object.defineProperty(exports, "__esModule", { value: true });
39
12
  exports.getApiSchemas = exports.getComponentSchemas = exports.getComponentCategoryFolders = exports.getApiFolders = exports.readSchema = void 0;
40
- var node_fs_1 = require("node:fs");
41
- var promises_1 = require("node:fs/promises");
42
- var posix_1 = require("node:path/posix");
43
- var utils_1 = require("../utils");
13
+ const node_fs_1 = require("node:fs");
14
+ const promises_1 = require("node:fs/promises");
15
+ const posix_1 = require("node:path/posix");
16
+ const utils_1 = require("../utils");
44
17
  function readSchema(schemaPath) {
45
- return __awaiter(this, void 0, void 0, function () {
46
- var schemaData, e_1;
47
- return __generator(this, function (_a) {
48
- switch (_a.label) {
49
- case 0:
50
- _a.trys.push([0, 2, , 3]);
51
- return [4 /*yield*/, (0, promises_1.readFile)(schemaPath)];
52
- case 1:
53
- schemaData = _a.sent();
54
- return [2 /*return*/, JSON.parse(schemaData.toString())];
55
- case 2:
56
- e_1 = _a.sent();
57
- return [2 /*return*/, null];
58
- case 3: return [2 /*return*/];
59
- }
60
- });
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ try {
20
+ const schemaData = yield (0, promises_1.readFile)(schemaPath);
21
+ return JSON.parse(schemaData.toString());
22
+ }
23
+ catch (e) {
24
+ return null;
25
+ }
61
26
  });
62
27
  }
63
28
  exports.readSchema = readSchema;
64
29
  function getApiFolders(strapiSrcRoot) {
65
- return __awaiter(this, void 0, void 0, function () {
66
- var path, folders;
67
- return __generator(this, function (_a) {
68
- switch (_a.label) {
69
- case 0:
70
- path = (0, posix_1.join)(strapiSrcRoot, 'api');
71
- return [4 /*yield*/, (0, utils_1.readDirFiltered)(path)];
72
- case 1:
73
- folders = _a.sent();
74
- return [2 /*return*/, folders];
75
- }
76
- });
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ const path = (0, posix_1.join)(strapiSrcRoot, 'api');
32
+ const folders = yield (0, utils_1.readDirFiltered)(path);
33
+ return folders;
77
34
  });
78
35
  }
79
36
  exports.getApiFolders = getApiFolders;
80
37
  function getComponentCategoryFolders(strapiSrcRoot) {
81
- return __awaiter(this, void 0, void 0, function () {
82
- var path, folders;
83
- return __generator(this, function (_a) {
84
- switch (_a.label) {
85
- case 0:
86
- path = (0, posix_1.join)(strapiSrcRoot, 'components');
87
- // If there exists no components, just fallback to an empty array.
88
- if (!(0, node_fs_1.existsSync)(path)) {
89
- return [2 /*return*/, []];
90
- }
91
- return [4 /*yield*/, (0, utils_1.readDirFiltered)(path)];
92
- case 1:
93
- folders = _a.sent();
94
- return [2 /*return*/, folders];
95
- }
96
- });
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ const path = (0, posix_1.join)(strapiSrcRoot, 'components');
40
+ // If there exists no components, just fallback to an empty array.
41
+ if (!(0, node_fs_1.existsSync)(path)) {
42
+ return [];
43
+ }
44
+ const folders = yield (0, utils_1.readDirFiltered)(path);
45
+ return folders;
97
46
  });
98
47
  }
99
48
  exports.getComponentCategoryFolders = getComponentCategoryFolders;
100
49
  function getComponentSchemas(strapiSrcRoot) {
101
- return __awaiter(this, void 0, void 0, function () {
102
- var categories, nestedSchemasPromises, nestedSchemasArr;
103
- var _this = this;
104
- return __generator(this, function (_a) {
105
- switch (_a.label) {
106
- case 0: return [4 /*yield*/, getComponentCategoryFolders(strapiSrcRoot)];
107
- case 1:
108
- categories = _a.sent();
109
- nestedSchemasPromises = categories.map(function (category) { return __awaiter(_this, void 0, void 0, function () {
110
- var schemaFilesPath, schemaFiles, schemaNamesWithAttributesPromises, schemaNamesWithAttributes;
111
- var _this = this;
112
- return __generator(this, function (_a) {
113
- switch (_a.label) {
114
- case 0:
115
- schemaFilesPath = (0, posix_1.join)(strapiSrcRoot, 'components', category);
116
- return [4 /*yield*/, (0, utils_1.readDirFiltered)(schemaFilesPath)];
117
- case 1:
118
- schemaFiles = _a.sent();
119
- schemaNamesWithAttributesPromises = schemaFiles.map(function (file) { return __awaiter(_this, void 0, void 0, function () {
120
- var schemaPath, schema, name;
121
- return __generator(this, function (_a) {
122
- switch (_a.label) {
123
- case 0:
124
- schemaPath = (0, posix_1.join)(schemaFilesPath, file);
125
- return [4 /*yield*/, readSchema(schemaPath)];
126
- case 1:
127
- schema = _a.sent();
128
- name = file.split('.')[0];
129
- return [2 /*return*/, { name: name, schema: schema }];
130
- }
131
- });
132
- }); });
133
- return [4 /*yield*/, Promise.all(schemaNamesWithAttributesPromises)];
134
- case 2:
135
- schemaNamesWithAttributes = _a.sent();
136
- return [2 /*return*/, { category: category, schemas: schemaNamesWithAttributes }];
137
- }
138
- });
139
- }); });
140
- return [4 /*yield*/, Promise.all(nestedSchemasPromises)];
141
- case 2:
142
- nestedSchemasArr = _a.sent();
143
- return [2 /*return*/, nestedSchemasArr];
144
- }
145
- });
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ const categories = yield getComponentCategoryFolders(strapiSrcRoot);
52
+ const nestedSchemasPromises = categories.map((category) => __awaiter(this, void 0, void 0, function* () {
53
+ const schemaFilesPath = (0, posix_1.join)(strapiSrcRoot, 'components', category);
54
+ const schemaFiles = yield (0, utils_1.readDirFiltered)(schemaFilesPath);
55
+ const schemaNamesWithAttributesPromises = schemaFiles.map((file) => __awaiter(this, void 0, void 0, function* () {
56
+ const schemaPath = (0, posix_1.join)(schemaFilesPath, file);
57
+ const schema = yield readSchema(schemaPath);
58
+ const name = file.split('.')[0];
59
+ return { name, schema };
60
+ }));
61
+ const schemaNamesWithAttributes = yield Promise.all(schemaNamesWithAttributesPromises);
62
+ return { category, schemas: schemaNamesWithAttributes };
63
+ }));
64
+ const nestedSchemasArr = yield Promise.all(nestedSchemasPromises);
65
+ return nestedSchemasArr;
146
66
  });
147
67
  }
148
68
  exports.getComponentSchemas = getComponentSchemas;
149
69
  function getApiSchemas(strapiSrcRoot) {
150
- return __awaiter(this, void 0, void 0, function () {
151
- var apiFolders, schemasWithAttributesPromises, schemasWithAttributes;
152
- var _this = this;
153
- return __generator(this, function (_a) {
154
- switch (_a.label) {
155
- case 0: return [4 /*yield*/, getApiFolders(strapiSrcRoot)];
156
- case 1:
157
- apiFolders = _a.sent();
158
- schemasWithAttributesPromises = apiFolders.map(function (folder) { return __awaiter(_this, void 0, void 0, function () {
159
- var schemaPath, schema;
160
- return __generator(this, function (_a) {
161
- switch (_a.label) {
162
- case 0:
163
- schemaPath = (0, posix_1.join)(strapiSrcRoot, 'api', folder, 'content-types', folder, 'schema.json');
164
- return [4 /*yield*/, readSchema(schemaPath)];
165
- case 1:
166
- schema = _a.sent();
167
- return [2 /*return*/, { name: folder, schema: schema }];
168
- }
169
- });
170
- }); });
171
- schemasWithAttributes = Promise.all(schemasWithAttributesPromises);
172
- return [2 /*return*/, schemasWithAttributes];
173
- }
174
- });
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ const apiFolders = yield getApiFolders(strapiSrcRoot);
72
+ const schemasWithAttributesPromises = apiFolders.map((folder) => __awaiter(this, void 0, void 0, function* () {
73
+ const schemaPath = (0, posix_1.join)(strapiSrcRoot, 'api', folder, 'content-types', folder, 'schema.json');
74
+ const schema = yield readSchema(schemaPath);
75
+ return { name: folder, schema };
76
+ }));
77
+ const schemasWithAttributes = Promise.all(schemasWithAttributesPromises);
78
+ return schemasWithAttributes;
175
79
  });
176
80
  }
177
81
  exports.getApiSchemas = getApiSchemas;
@@ -1,5 +1,5 @@
1
- import { File } from "../file/File";
2
- import { caseType } from "../utils/casing";
1
+ import { File } from '../file/File';
2
+ import { caseType } from '../utils/casing';
3
3
  export declare class ExtraType extends File {
4
4
  protected typeString: string;
5
5
  constructor(typeString: string, baseName: string, relativeDirectoryPath: string, fileCaseType?: caseType);