@oak-digital/types-4-strapi-2 0.2.3 → 0.2.6

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/README.md CHANGED
@@ -45,11 +45,18 @@ This can be done with the `--out` flag like in the following example.
45
45
 
46
46
  ### Planned features
47
47
 
48
+ * Support for localization
48
49
  * Support if you are using other plugins, such as `url-alias`, which should add extra fields for some interfaces.
49
50
 
50
- ## Help
51
+ ## Flags
51
52
 
52
- use `t4s --help` to display which options are available for you
53
+ | **flag** | **Description** |
54
+ |-----------------------------|------------------------------------------------------------------------------------|
55
+ | -i, --in <dir> | The src directory for strapi |
56
+ | -o, --out <dir> | The output directory to output the types to |
57
+ | --component-prefix <prefix> | A prefix for components |
58
+ | -D, --delete-old | CAUTION: This option is equivalent to running rm -rf on the output directory first |
59
+ | --prettier <file> | The prettier config file to use for formatting typescript interfaces |
53
60
 
54
61
  ## Building
55
62
 
package/lib/index.js CHANGED
@@ -11,13 +11,15 @@ commander_1.program
11
11
  .option('-i, --in <dir>', 'The src directory for strapi', './src')
12
12
  .option('-o, --out <dir>', 'The output directory to output the types to', './types')
13
13
  .option('--component-prefix <prefix>', 'A prefix for components', '')
14
+ .option('-D, --delete-old', 'CAUTION: This option is equivalent to running rm -rf on the output directory first')
14
15
  .option('--prettier <file>', 'The prettier config file to use for formatting typescript interfaces');
15
16
  commander_1.program.parse();
16
17
  var options = commander_1.program.opts();
17
- var input = options.in, out = options.out, componentPrefix = options.componentPrefix, prettierFile = options.prettier;
18
+ var input = options.in, out = options.out, componentPrefix = options.componentPrefix, prettierFile = options.prettier, deleteOld = options.deleteOld;
18
19
  var manager = new InterfaceManager_1.default(out, input, {
19
20
  componentPrefix: componentPrefix,
20
21
  prettierFile: prettierFile,
22
+ deleteOld: deleteOld,
21
23
  });
22
24
  manager.run().catch(function (err) {
23
25
  console.error(err);
@@ -1,4 +1,4 @@
1
- import { RelationNames } from "./Interface";
1
+ import { RelationNames } from './Interface';
2
2
  export default class Attributes {
3
3
  Attrs: Record<string, Record<string, any>>;
4
4
  private RelationNames;
@@ -9,11 +9,11 @@ var Attributes = /** @class */ (function () {
9
9
  Attributes.prototype.isAttributeOptional = function (attr) {
10
10
  // If it is a component / relation / dynamiczone it is always optional due to population
11
11
  switch (attr.type) {
12
- case "nested":
12
+ case 'nested':
13
13
  return attr.nullable === true;
14
- case "component":
15
- case "dynamiczone":
16
- case "relation":
14
+ case 'component':
15
+ case 'dynamiczone':
16
+ case 'relation':
17
17
  return true;
18
18
  default:
19
19
  break;
@@ -26,18 +26,18 @@ var Attributes = /** @class */ (function () {
26
26
  var attr = this.Attrs[attrName];
27
27
  var dependencyNames = [];
28
28
  switch (attr.type) {
29
- case "nested":
29
+ case 'nested':
30
30
  var attrs = new Attributes(attr.fields, this.RelationNames);
31
31
  dependencyNames.push.apply(dependencyNames, attrs.getDependencies());
32
32
  break;
33
- case "relation":
33
+ case 'relation':
34
34
  dependencyNames.push(attr.target);
35
35
  break;
36
- case "component":
36
+ case 'component':
37
37
  dependencyNames.push(attr.component);
38
38
  break;
39
- case "media":
40
- dependencyNames.push("builtins::Media");
39
+ case 'media':
40
+ dependencyNames.push('builtins::Media');
41
41
  break;
42
42
  default:
43
43
  continue;
@@ -55,70 +55,70 @@ var Attributes = /** @class */ (function () {
55
55
  return dependencies;
56
56
  };
57
57
  Attributes.prototype.attributeToString = function (attrName, attr) {
58
- var _a, _b;
58
+ var _a;
59
59
  var optionalString = this.isAttributeOptional(attr) ? '?' : '';
60
60
  var str = " ".concat(attrName).concat(optionalString, ": ");
61
61
  var isArray = false;
62
62
  switch (attr.type) {
63
63
  // types-4-strapi-2 specific, used for builtin types
64
- case "nested":
64
+ case 'nested':
65
65
  // Be careful with recursion
66
66
  // console.log(attr);
67
67
  var newAttrs = new Attributes(attr.fields, this.RelationNames);
68
68
  str += newAttrs.toString();
69
69
  break;
70
- case "relation":
70
+ case 'relation':
71
71
  var apiName = attr.target;
72
72
  // console.log(this.RelationNames, apiName)
73
73
  var dependencyName = this.RelationNames[apiName].name;
74
- isArray = attr.relation.endsWith("ToMany");
75
- str += dependencyName;
74
+ var relationMultipleString = attr.relation.endsWith('ToMany') ? '[]' : ' | null';
75
+ str += "{ data: ".concat(dependencyName).concat(relationMultipleString, "; }");
76
76
  break;
77
- case "component":
77
+ case 'component':
78
78
  var componentName = attr.component;
79
79
  var relationNameObj = this.RelationNames[componentName];
80
80
  var dependencyComponentName = relationNameObj.name;
81
81
  isArray = (_a = attr.repeatable) !== null && _a !== void 0 ? _a : false;
82
82
  str += dependencyComponentName;
83
83
  break;
84
- case "media":
85
- var mediaOptional = attr.required !== true ? "?" : "";
86
- str += "{ data".concat(mediaOptional, ": ").concat(this.RelationNames["builtins::Media"].name, "; }");
87
- isArray = (_b = attr.multiple) !== null && _b !== void 0 ? _b : false;
84
+ case 'media':
85
+ var mediaOptional = attr.required !== true ? ' | null' : '';
86
+ var mediaMultipleString = attr.multiple ? '[]' : mediaOptional;
87
+ str += "{ data: ".concat(this.RelationNames['builtins::Media'].name).concat(mediaMultipleString, "; }");
88
88
  break;
89
- case "password":
89
+ case 'password':
90
90
  return null;
91
- case "enumeration":
92
- var hasDefault = "default" in attr;
91
+ case 'enumeration':
92
+ var hasDefault = 'default' in attr;
93
93
  var enums = attr.enum.map(function (en) { return "\"".concat(en, "\""); });
94
- enums.push("null");
95
- var typeString = enums.join(" | ");
94
+ enums.push('null');
95
+ var typeString = enums.join(' | ');
96
96
  str += typeString;
97
97
  break;
98
- case "string":
99
- case "text":
100
- case "richtext":
101
- case "email":
102
- case "uid":
103
- str += "string";
98
+ case 'string':
99
+ case 'text':
100
+ case 'richtext':
101
+ case 'email':
102
+ case 'uid':
103
+ str += 'string';
104
104
  break;
105
- case "integer":
106
- case "biginteger":
107
- case "decimal":
108
- case "float":
109
- str += "number";
105
+ case 'integer':
106
+ case 'biginteger':
107
+ case 'decimal':
108
+ case 'float':
109
+ str += 'number';
110
110
  break;
111
- case "date":
112
- case "datetime":
113
- case "time":
114
- str += "Date";
111
+ case 'date':
112
+ case 'datetime':
113
+ case 'time':
114
+ str += 'Date';
115
115
  break;
116
- case "boolean":
116
+ case 'boolean':
117
117
  str += attr.type;
118
118
  break;
119
- case "json":
119
+ case 'json':
120
120
  default:
121
- str += "any";
121
+ str += 'any';
122
122
  break;
123
123
  }
124
124
  var isArrayString = isArray ? '[]' : '';
@@ -135,13 +135,13 @@ var Attributes = /** @class */ (function () {
135
135
  }
136
136
  strings.push(attrString);
137
137
  }
138
- return strings.map(function (s) { return "".concat(s, "\n"); }).join("");
138
+ return strings.map(function (s) { return "".concat(s, "\n"); }).join('');
139
139
  };
140
140
  Attributes.prototype.toString = function () {
141
- var strings = ["{"];
141
+ var strings = ['{'];
142
142
  strings.push(this.toFieldsString());
143
- strings.push("}");
144
- return strings.join("\n");
143
+ strings.push('}');
144
+ return strings.join('\n');
145
145
  };
146
146
  return Attributes;
147
147
  }());
@@ -11,6 +11,7 @@ export default class InterfaceManager {
11
11
  componentPrefixOverridesPrefix: boolean;
12
12
  builtinsPrefix: string;
13
13
  builtinsPrefixOverridesPrefix: boolean;
14
+ deleteOld: boolean;
14
15
  prettierFile: any;
15
16
  };
16
17
  constructor(outRoot: string, strapiSrcRoot: string, options?: any);
@@ -18,6 +19,7 @@ export default class InterfaceManager {
18
19
  createInterfaces(): Promise<void>;
19
20
  createBuiltinInterfaces(): void;
20
21
  injectDependencies(): void;
22
+ deleteOldFolders(): Promise<void>;
21
23
  makeFolders(): Promise<void>;
22
24
  writeInterfaces(): Promise<void>;
23
25
  writeIndexFile(): Promise<void>;
@@ -140,23 +140,44 @@ var InterfaceManager = /** @class */ (function () {
140
140
  inter.setRelations(interfacesToInject);
141
141
  });
142
142
  };
143
+ InterfaceManager.prototype.deleteOldFolders = function () {
144
+ return __awaiter(this, void 0, void 0, function () {
145
+ return __generator(this, function (_a) {
146
+ switch (_a.label) {
147
+ case 0: return [4 /*yield*/, (0, promises_1.rm)(this.OutRoot, {
148
+ force: true,
149
+ recursive: true,
150
+ })];
151
+ case 1:
152
+ _a.sent();
153
+ return [2 /*return*/];
154
+ }
155
+ });
156
+ });
157
+ };
143
158
  InterfaceManager.prototype.makeFolders = function () {
144
159
  return __awaiter(this, void 0, void 0, function () {
145
160
  var componentCategories, promises, componentCategoriesPromises, builtinsPath;
146
161
  var _this = this;
147
162
  return __generator(this, function (_a) {
148
163
  switch (_a.label) {
149
- case 0: return [4 /*yield*/, (0, schemaReader_1.getComponentCategoryFolders)(this.StrapiSrcRoot)];
164
+ case 0:
165
+ if (!this.Options.deleteOld) return [3 /*break*/, 2];
166
+ return [4 /*yield*/, this.deleteOldFolders()];
150
167
  case 1:
168
+ _a.sent();
169
+ _a.label = 2;
170
+ case 2: return [4 /*yield*/, (0, schemaReader_1.getComponentCategoryFolders)(this.StrapiSrcRoot)];
171
+ case 3:
151
172
  componentCategories = _a.sent();
152
- if (!!(0, fs_1.existsSync)(this.OutRoot)) return [3 /*break*/, 3];
173
+ if (!!(0, fs_1.existsSync)(this.OutRoot)) return [3 /*break*/, 5];
153
174
  return [4 /*yield*/, (0, promises_1.mkdir)(this.OutRoot, {
154
175
  recursive: true,
155
176
  })];
156
- case 2:
177
+ case 4:
157
178
  _a.sent();
158
- _a.label = 3;
159
- case 3:
179
+ _a.label = 5;
180
+ case 5:
160
181
  promises = [];
161
182
  componentCategoriesPromises = componentCategories.map(function (category) { return __awaiter(_this, void 0, void 0, function () {
162
183
  var path;
@@ -180,7 +201,7 @@ var InterfaceManager = /** @class */ (function () {
180
201
  promises.push((0, promises_1.mkdir)((0, posix_1.join)(this.OutRoot, 'builtins')));
181
202
  }
182
203
  return [4 /*yield*/, Promise.all(promises)];
183
- case 4:
204
+ case 6:
184
205
  _a.sent();
185
206
  return [2 /*return*/];
186
207
  }
@@ -279,6 +300,7 @@ var InterfaceManager = /** @class */ (function () {
279
300
  componentPrefixOverridesPrefix: false,
280
301
  builtinsPrefix: '',
281
302
  builtinsPrefixOverridesPrefix: false,
303
+ deleteOld: false,
282
304
  prettierFile: null,
283
305
  };
284
306
  return InterfaceManager;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oak-digital/types-4-strapi-2",
3
- "version": "0.2.3",
3
+ "version": "0.2.6",
4
4
  "description": "Typescript interface generator for Strapi 4 models",
5
5
  "bin": {
6
6
  "t4s": "./bin/index.js"