@oak-digital/types-4-strapi-2 0.3.3 → 0.3.4

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.
@@ -0,0 +1,12 @@
1
+ export declare enum Events {
2
+ BeforeReadSchema = "BeforeReadSchema",
3
+ AfterReadSchema = "AfterReadSchema"
4
+ }
5
+ export declare type SchemasType = {
6
+ apiSchemas: Record<string, any>[];
7
+ componentSchemas: Record<string, any>[];
8
+ };
9
+ export declare type EventTypes = {
10
+ BeforeReadSchema: SchemasType;
11
+ AfterReadSchema: SchemasType;
12
+ };
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Events = void 0;
4
+ var Events;
5
+ (function (Events) {
6
+ Events["BeforeReadSchema"] = "BeforeReadSchema";
7
+ Events["AfterReadSchema"] = "AfterReadSchema";
8
+ })(Events = exports.Events || (exports.Events = {}));
package/lib/index.js CHANGED
@@ -15,10 +15,11 @@ commander_1.program
15
15
  .option('-D, --delete-old', 'CAUTION: This option is equivalent to running rm -rf on the output directory first')
16
16
  .option('--file-case <case>', 'Which case to use for generated files', 'pascal')
17
17
  .option('--folder-case <case>', 'Which case to use for generated folders', 'kebab')
18
+ .option('--plugins <plugins...>', 'A list of enabled plugins in the target strapi project that modify content-types')
18
19
  .option('--prettier <file>', 'The prettier config file to use for formatting typescript interfaces');
19
20
  commander_1.program.parse();
20
21
  var options = commander_1.program.opts();
21
- var input = options.in, out = options.out, componentPrefix = options.componentPrefix, prefix = options.prefix, prettierFile = options.prettier, deleteOld = options.deleteOld, fileCaseType = options.fileCase, folderCaseType = options.folderCase;
22
+ var input = options.in, out = options.out, componentPrefix = options.componentPrefix, prefix = options.prefix, prettierFile = options.prettier, deleteOld = options.deleteOld, fileCaseType = options.fileCase, folderCaseType = options.folderCase, plugins = options.plugins;
22
23
  var manager = new InterfaceManager_1.default(out, input, {
23
24
  componentPrefix: componentPrefix,
24
25
  prefix: prefix,
@@ -26,6 +27,7 @@ var manager = new InterfaceManager_1.default(out, input, {
26
27
  deleteOld: deleteOld,
27
28
  fileCaseType: fileCaseType,
28
29
  folderCaseType: folderCaseType,
30
+ enabledPlugins: plugins,
29
31
  });
30
32
  manager.run().catch(function (err) {
31
33
  console.error(err);
@@ -1,9 +1,12 @@
1
+ /// <reference types="node" />
2
+ import EventEmitter from 'events';
1
3
  export default class InterfaceManager {
2
4
  private Interfaces;
3
5
  private OutRoot;
4
6
  private StrapiSrcRoot;
5
7
  private Options;
6
8
  private PrettierOptions;
9
+ eventEmitter: EventEmitter;
7
10
  static BaseOptions: {
8
11
  prefix: string;
9
12
  useCategoryPrefix: boolean;
@@ -13,12 +16,18 @@ export default class InterfaceManager {
13
16
  builtinsPrefixOverridesPrefix: boolean;
14
17
  deleteOld: boolean;
15
18
  prettierFile: any;
16
- fileCaseType: "camel" | "capital" | "dot" | "snake" | "pascal" | "constant" | "kebab";
17
- folderCaseType: "camel" | "capital" | "dot" | "snake" | "pascal" | "constant" | "kebab";
19
+ fileCaseType: "pascal" | "camel" | "capital" | "dot" | "snake" | "constant" | "kebab";
20
+ folderCaseType: "pascal" | "camel" | "capital" | "dot" | "snake" | "constant" | "kebab";
21
+ enabledPlugins: any[];
18
22
  };
19
23
  constructor(outRoot: string, strapiSrcRoot: string, options?: any);
24
+ registerPlugins(): void;
20
25
  validateOptions(): void;
21
26
  loadPrettierConfig(): Promise<void>;
27
+ readSchemas(): Promise<{
28
+ apiSchemas: any[];
29
+ componentSchemas: any[];
30
+ }>;
22
31
  createInterfaces(): Promise<void>;
23
32
  createBuiltinInterfaces(): void;
24
33
  injectDependencies(): void;
@@ -35,6 +35,15 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
36
  }
37
37
  };
38
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
40
+ if (ar || !(i in from)) {
41
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
42
+ ar[i] = from[i];
43
+ }
44
+ }
45
+ return to.concat(ar || Array.prototype.slice.call(from));
46
+ };
38
47
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
48
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
49
  };
@@ -49,15 +58,25 @@ var schemaReader_1 = require("./schemaReader");
49
58
  var prettier_1 = __importDefault(require("prettier"));
50
59
  var pascal_case_1 = require("pascal-case");
51
60
  var case_1 = require("../case");
61
+ var events_1 = __importDefault(require("events"));
62
+ var events_2 = require("../events");
63
+ var plugins_1 = require("../plugins");
64
+ var types_1 = require("../plugins/types");
52
65
  var InterfaceManager = /** @class */ (function () {
53
66
  function InterfaceManager(outRoot, strapiSrcRoot, options) {
54
67
  if (options === void 0) { options = {}; }
55
68
  this.Interfaces = {}; // string = strapi name
69
+ this.eventEmitter = new events_1.default();
56
70
  this.OutRoot = outRoot;
57
71
  this.StrapiSrcRoot = strapiSrcRoot;
58
72
  this.Options = Object.assign({}, InterfaceManager.BaseOptions, options);
59
73
  this.validateOptions();
74
+ this.registerPlugins();
60
75
  }
76
+ InterfaceManager.prototype.registerPlugins = function () {
77
+ var pluginNames = new Set(this.Options.enabledPlugins);
78
+ (0, plugins_1.registerPlugins)(pluginNames, this.eventEmitter);
79
+ };
61
80
  InterfaceManager.prototype.validateOptions = function () {
62
81
  if (!(0, case_1.checkCaseType)(this.Options.fileCaseType)) {
63
82
  throw new Error("".concat(this.Options.fileCaseType, " is not a supported type, please use one of the following ").concat(case_1.caseTypesArray.join(', ')));
@@ -65,6 +84,11 @@ var InterfaceManager = /** @class */ (function () {
65
84
  if (!(0, case_1.checkCaseType)(this.Options.folderCaseType)) {
66
85
  throw new Error("".concat(this.Options.folderCaseType, " is not a supported type, please use one of the following ").concat(case_1.caseTypesArray.join(', ')));
67
86
  }
87
+ this.Options.enabledPlugins.forEach(function (enabledPlugin) {
88
+ if (!types_1.supportedPluginNames.includes(enabledPlugin)) {
89
+ throw new Error("".concat(enabledPlugin, " is not a supported plugin, please open an issue on https://github.com/Oak-Digital/types-4-strapi-2 or only use the following plugins [").concat(types_1.supportedPluginNames.join(', '), "]"));
90
+ }
91
+ });
68
92
  };
69
93
  InterfaceManager.prototype.loadPrettierConfig = function () {
70
94
  return __awaiter(this, void 0, void 0, function () {
@@ -90,27 +114,48 @@ var InterfaceManager = /** @class */ (function () {
90
114
  });
91
115
  });
92
116
  };
93
- InterfaceManager.prototype.createInterfaces = function () {
117
+ InterfaceManager.prototype.readSchemas = function () {
94
118
  return __awaiter(this, void 0, void 0, function () {
95
- var apiSchemasPromise, componentSchemasPromise, apiSchemas, componentSchemas;
96
- var _this = this;
97
- return __generator(this, function (_a) {
98
- switch (_a.label) {
119
+ var apiSchemasPre, componentSchemasPre, apiSchemasPromise, componentSchemasPromise, _a, apiSchemas, componentSchemas, newObject;
120
+ return __generator(this, function (_b) {
121
+ switch (_b.label) {
99
122
  case 0:
123
+ apiSchemasPre = [];
124
+ componentSchemasPre = [];
100
125
  apiSchemasPromise = (0, schemaReader_1.getApiSchemas)(this.StrapiSrcRoot);
101
126
  componentSchemasPromise = (0, schemaReader_1.getComponentSchemas)(this.StrapiSrcRoot);
102
- return [4 /*yield*/, apiSchemasPromise];
127
+ this.eventEmitter.emit(events_2.Events.BeforeReadSchema, {
128
+ apiSchemas: apiSchemasPre,
129
+ componentSchemas: componentSchemasPre,
130
+ });
131
+ return [4 /*yield*/, Promise.all([apiSchemasPromise, componentSchemasPromise])];
103
132
  case 1:
104
- apiSchemas = _a.sent();
133
+ _a = _b.sent(), apiSchemas = _a[0], componentSchemas = _a[1];
134
+ newObject = {
135
+ apiSchemas: __spreadArray(__spreadArray([], apiSchemasPre, true), apiSchemas, true),
136
+ componentSchemas: __spreadArray(__spreadArray([], componentSchemasPre, true), componentSchemas, true),
137
+ };
138
+ this.eventEmitter.emit(events_2.Events.AfterReadSchema, newObject);
139
+ return [2 /*return*/, newObject];
140
+ }
141
+ });
142
+ });
143
+ };
144
+ InterfaceManager.prototype.createInterfaces = function () {
145
+ return __awaiter(this, void 0, void 0, function () {
146
+ var _a, apiSchemas, componentSchemas;
147
+ var _this = this;
148
+ return __generator(this, function (_b) {
149
+ switch (_b.label) {
150
+ case 0: return [4 /*yield*/, this.readSchemas()];
151
+ case 1:
152
+ _a = _b.sent(), apiSchemas = _a.apiSchemas, componentSchemas = _a.componentSchemas;
105
153
  apiSchemas.forEach(function (schema) {
106
154
  var name = schema.name, attributes = schema.attributes;
107
155
  var strapiName = "api::".concat(name, ".").concat(name);
108
156
  var inter = new Interface_1.default(name, attributes, './', _this.Options.fileCaseType, _this.Options.prefix);
109
157
  _this.Interfaces[strapiName] = inter;
110
158
  });
111
- return [4 /*yield*/, componentSchemasPromise];
112
- case 2:
113
- componentSchemas = _a.sent();
114
159
  componentSchemas.forEach(function (category) {
115
160
  var categoryName = category.category;
116
161
  category.schemas.forEach(function (schema) {
@@ -326,6 +371,7 @@ var InterfaceManager = /** @class */ (function () {
326
371
  prettierFile: null,
327
372
  fileCaseType: 'pascal',
328
373
  folderCaseType: 'kebab',
374
+ enabledPlugins: [],
329
375
  };
330
376
  return InterfaceManager;
331
377
  }());
@@ -1,2 +1,3 @@
1
+ import EventEmitter from "events";
1
2
  import { SupportedPluginNamesType } from "./types";
2
- export declare const registerPlugins: (pluginNames: Set<SupportedPluginNamesType>) => void;
3
+ export declare const registerPlugins: (pluginNames: Set<SupportedPluginNamesType>, eventEmitter: EventEmitter) => void;
@@ -1,8 +1,13 @@
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
6
  exports.registerPlugins = void 0;
4
- var registerPlugins = function (pluginNames) {
7
+ var url_alias_1 = __importDefault(require("./url-alias"));
8
+ var registerPlugins = function (pluginNames, eventEmitter) {
5
9
  if (pluginNames.has('url-alias')) {
10
+ (0, url_alias_1.default)(eventEmitter);
6
11
  }
7
12
  };
8
13
  exports.registerPlugins = registerPlugins;
@@ -0,0 +1,3 @@
1
+ import EventEmitter from "events";
2
+ declare const register: (eventEmitter: EventEmitter) => void;
3
+ export default register;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var events_1 = require("../../events");
4
+ var addUrlAliasToAllContentTypes = function (_a) {
5
+ var apiSchemas = _a.apiSchemas;
6
+ apiSchemas.forEach(function (schema) {
7
+ var attributes = schema.attributes;
8
+ attributes.url_path = {
9
+ type: 'string',
10
+ required: true,
11
+ };
12
+ // console.log('updated schema', schema)
13
+ // TODO: add this with possibly undefined instead of null
14
+ // attributes.url_path_id = {
15
+ // type: 'string',
16
+ // };
17
+ });
18
+ };
19
+ var register = function (eventEmitter) {
20
+ eventEmitter.on(events_1.Events.AfterReadSchema, addUrlAliasToAllContentTypes);
21
+ };
22
+ exports.default = register;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oak-digital/types-4-strapi-2",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "Typescript interface generator for Strapi 4 models",
5
5
  "bin": {
6
6
  "t4s": "./bin/index.js"