@oak-digital/types-4-strapi-2 0.1.1 → 0.1.2
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/lib/index.d.ts +1 -0
- package/lib/index.js +19 -0
- package/lib/interface/Attributes.d.ts +11 -0
- package/{src/interface/Attributes.ts → lib/interface/Attributes.js} +45 -50
- package/lib/interface/ComponentInterface.d.ts +7 -0
- package/lib/interface/ComponentInterface.js +47 -0
- package/lib/interface/Interface.d.ts +27 -0
- package/lib/interface/Interface.js +114 -0
- package/lib/interface/InterfaceManager.d.ts +19 -0
- package/lib/interface/InterfaceManager.js +236 -0
- package/lib/interface/interfaceCreator.d.ts +0 -0
- package/{src/interface/interfaceCreator.ts → lib/interface/interfaceCreator.js} +0 -0
- package/lib/interface/interfaceWriter.d.ts +2 -0
- package/lib/interface/interfaceWriter.js +46 -0
- package/lib/interface/schemaReader.d.ts +14 -0
- package/lib/interface/schemaReader.js +168 -0
- package/lib/utils/index.d.ts +3 -0
- package/lib/utils/index.js +67 -0
- package/package.json +1 -1
- package/src/index.ts +0 -22
- package/src/interface/ComponentInterface.ts +0 -27
- package/src/interface/Interface.ts +0 -123
- package/src/interface/InterfaceManager.ts +0 -116
- package/src/interface/interfaceWriter.ts +0 -5
- package/src/interface/schemaReader.ts +0 -51
- package/src/utils/index.ts +0 -19
- package/tsconfig.jsonc +0 -15
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
var commander_1 = require("commander");
|
|
7
|
+
var InterfaceManager_1 = __importDefault(require("./interface/InterfaceManager"));
|
|
8
|
+
commander_1.program
|
|
9
|
+
.name("t4s");
|
|
10
|
+
commander_1.program
|
|
11
|
+
.option('-i, --in <dir>', 'The src directory for strapi', './src')
|
|
12
|
+
.option('-o, --out <dir>', 'The output directory to output the types to', './types');
|
|
13
|
+
commander_1.program.parse();
|
|
14
|
+
var options = commander_1.program.opts();
|
|
15
|
+
var input = options.in, out = options.out;
|
|
16
|
+
var manager = new InterfaceManager_1.default(out, input);
|
|
17
|
+
manager.run().catch(function (err) {
|
|
18
|
+
console.error(err);
|
|
19
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import Interface from "./Interface";
|
|
2
|
+
export default class Attributes {
|
|
3
|
+
Attrs: Record<string, Record<string, any>>;
|
|
4
|
+
private RelationNames;
|
|
5
|
+
constructor(attr: Record<string, Record<string, any>>, relationNames: Record<string, [string, Interface]>);
|
|
6
|
+
isAttributeOptional(attr: any): boolean;
|
|
7
|
+
getDependencies(strapiName: string): any[];
|
|
8
|
+
attributeToString(attrName: string, attr: any): string;
|
|
9
|
+
toFieldsString(): string;
|
|
10
|
+
toString(): string;
|
|
11
|
+
}
|
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
constructor(attr: Record<string, Record<string, any>>, relationNames: Record<string, [string, Interface]>) {
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var Attributes = /** @class */ (function () {
|
|
4
|
+
function Attributes(attr, relationNames) {
|
|
5
|
+
this.RelationNames = {};
|
|
8
6
|
this.Attrs = attr;
|
|
9
7
|
this.RelationNames = relationNames;
|
|
10
8
|
}
|
|
11
|
-
|
|
12
|
-
isAttributeOptional(attr: any) {
|
|
9
|
+
Attributes.prototype.isAttributeOptional = function (attr) {
|
|
13
10
|
// If it is a component / relation / dynamiczone it is always optional due to population
|
|
14
11
|
switch (attr.type) {
|
|
15
12
|
case "component":
|
|
@@ -19,15 +16,13 @@ export default class Attributes {
|
|
|
19
16
|
default:
|
|
20
17
|
break;
|
|
21
18
|
}
|
|
22
|
-
|
|
23
19
|
return false;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
let dependencyName : string;
|
|
20
|
+
};
|
|
21
|
+
Attributes.prototype.getDependencies = function (strapiName) {
|
|
22
|
+
var dependencies = [];
|
|
23
|
+
for (var attrName in this.Attrs) {
|
|
24
|
+
var attr = this.Attrs[attrName];
|
|
25
|
+
var dependencyName = void 0;
|
|
31
26
|
switch (attr.type) {
|
|
32
27
|
case "relation":
|
|
33
28
|
dependencyName = attr.target;
|
|
@@ -45,34 +40,34 @@ export default class Attributes {
|
|
|
45
40
|
dependencies.push(dependencyName);
|
|
46
41
|
}
|
|
47
42
|
return dependencies;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
43
|
+
};
|
|
44
|
+
Attributes.prototype.attributeToString = function (attrName, attr) {
|
|
45
|
+
var _a;
|
|
46
|
+
var optionalString = this.isAttributeOptional(attr) ? '?' : '';
|
|
47
|
+
var str = " ".concat(attrName).concat(optionalString, ": ");
|
|
48
|
+
var isArray = false;
|
|
54
49
|
switch (attr.type) {
|
|
55
50
|
case "relation":
|
|
56
|
-
|
|
51
|
+
var apiName = attr.target;
|
|
57
52
|
// console.log(attrName)
|
|
58
53
|
// console.log(this.RelationNames, apiName)
|
|
59
|
-
|
|
54
|
+
var dependencyName = this.RelationNames[apiName][0];
|
|
60
55
|
isArray = attr.relation.endsWith("ToMany");
|
|
61
56
|
str += dependencyName;
|
|
62
57
|
break;
|
|
63
58
|
case "component":
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
isArray = attr.repeatable
|
|
59
|
+
var componentName = attr.component;
|
|
60
|
+
var dependencyComponentName = this.RelationNames[componentName][0];
|
|
61
|
+
isArray = (_a = attr.repeatable) !== null && _a !== void 0 ? _a : false;
|
|
67
62
|
str += dependencyComponentName;
|
|
68
63
|
break;
|
|
69
64
|
case "password":
|
|
70
65
|
return null;
|
|
71
66
|
case "enumeration":
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
enums.push("null")
|
|
75
|
-
|
|
67
|
+
var hasDefault = "default" in attr;
|
|
68
|
+
var enums = attr.enum.map(function (en) { return "\"".concat(en, "\""); });
|
|
69
|
+
enums.push("null");
|
|
70
|
+
var typeString = enums.join(" | ");
|
|
76
71
|
str += typeString;
|
|
77
72
|
break;
|
|
78
73
|
case "string":
|
|
@@ -101,28 +96,28 @@ export default class Attributes {
|
|
|
101
96
|
str += "any";
|
|
102
97
|
break;
|
|
103
98
|
}
|
|
104
|
-
|
|
105
|
-
str +=
|
|
99
|
+
var isArrayString = isArray ? '[]' : '';
|
|
100
|
+
str += "".concat(isArrayString, ";");
|
|
106
101
|
return str;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const attrString = this.attributeToString(attrName, attr);
|
|
102
|
+
};
|
|
103
|
+
Attributes.prototype.toFieldsString = function () {
|
|
104
|
+
var strings = [];
|
|
105
|
+
for (var attrName in this.Attrs) {
|
|
106
|
+
var attr = this.Attrs[attrName];
|
|
107
|
+
var attrString = this.attributeToString(attrName, attr);
|
|
114
108
|
if (attrString === null) {
|
|
115
109
|
continue;
|
|
116
110
|
}
|
|
117
|
-
strings.push(attrString)
|
|
111
|
+
strings.push(attrString);
|
|
118
112
|
}
|
|
119
|
-
return strings.map(s
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const strings = [ "{" ];
|
|
113
|
+
return strings.map(function (s) { return "".concat(s, "\n"); }).join("");
|
|
114
|
+
};
|
|
115
|
+
Attributes.prototype.toString = function () {
|
|
116
|
+
var strings = ["{"];
|
|
124
117
|
strings.push(this.toFieldsString());
|
|
125
|
-
strings.push("}")
|
|
118
|
+
strings.push("}");
|
|
126
119
|
return strings.join("\n");
|
|
127
|
-
}
|
|
128
|
-
|
|
120
|
+
};
|
|
121
|
+
return Attributes;
|
|
122
|
+
}());
|
|
123
|
+
exports.default = Attributes;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import Interface from "./Interface";
|
|
2
|
+
export default class ComponentInterface extends Interface {
|
|
3
|
+
protected Category: string;
|
|
4
|
+
constructor(baseName: string, attributes: any, relativeDirectoryPath: string, category: string, prefix?: string);
|
|
5
|
+
updateStrapiName(): void;
|
|
6
|
+
getInterfaceFieldsString(): string;
|
|
7
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
+
};
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
var Interface_1 = __importDefault(require("./Interface"));
|
|
22
|
+
var ComponentInterface = /** @class */ (function (_super) {
|
|
23
|
+
__extends(ComponentInterface, _super);
|
|
24
|
+
function ComponentInterface(baseName, attributes, relativeDirectoryPath, category, prefix) {
|
|
25
|
+
if (prefix === void 0) { prefix = ""; }
|
|
26
|
+
var _this = _super.call(this, baseName, attributes, relativeDirectoryPath, prefix) || this;
|
|
27
|
+
_this.Category = category;
|
|
28
|
+
// this.Attributes.id = {
|
|
29
|
+
// type: "number", // Components have a id field with a number
|
|
30
|
+
// required: true,
|
|
31
|
+
// };
|
|
32
|
+
_this.updateStrapiName();
|
|
33
|
+
return _this;
|
|
34
|
+
}
|
|
35
|
+
ComponentInterface.prototype.updateStrapiName = function () {
|
|
36
|
+
this.StrapiName = "".concat(this.Category, ".").concat(this.getBaseName());
|
|
37
|
+
};
|
|
38
|
+
ComponentInterface.prototype.getInterfaceFieldsString = function () {
|
|
39
|
+
var attrs = this.getAttributes();
|
|
40
|
+
var str = '';
|
|
41
|
+
str += " id: number;\n";
|
|
42
|
+
str += " __component: \"".concat(this.getStrapiName(), "\";\n");
|
|
43
|
+
return str + attrs.toFieldsString();
|
|
44
|
+
};
|
|
45
|
+
return ComponentInterface;
|
|
46
|
+
}(Interface_1.default));
|
|
47
|
+
exports.default = ComponentInterface;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import Attributes from "./Attributes";
|
|
2
|
+
export default class Interface {
|
|
3
|
+
protected BaseName: string;
|
|
4
|
+
private Relations;
|
|
5
|
+
private RelationNames;
|
|
6
|
+
private RelationNamesCounter;
|
|
7
|
+
private NamePrefix;
|
|
8
|
+
protected Attributes: any;
|
|
9
|
+
private RelativeDirectoryPath;
|
|
10
|
+
protected StrapiName: string;
|
|
11
|
+
constructor(baseName: string, attributes: any, relativeDirectoryPath: string, prefix?: string);
|
|
12
|
+
protected updateStrapiName(): void;
|
|
13
|
+
getBaseName(): string;
|
|
14
|
+
getStrapiName(): string;
|
|
15
|
+
getDependencies(): any[];
|
|
16
|
+
getFullInterfaceName(): string;
|
|
17
|
+
getRelativeRootPath(): any;
|
|
18
|
+
getRelativeRootDir(): string;
|
|
19
|
+
getRelativeRootPathFile(): string;
|
|
20
|
+
setRelations(relations: Interface[]): void;
|
|
21
|
+
private getTsImports;
|
|
22
|
+
getAttributes(): Attributes;
|
|
23
|
+
attributesToString(): string;
|
|
24
|
+
getInerfaceString(): string;
|
|
25
|
+
getInterfaceFieldsString(): string;
|
|
26
|
+
toString(): string;
|
|
27
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
var path_1 = require("path");
|
|
7
|
+
var utils_1 = require("../utils");
|
|
8
|
+
var Attributes_1 = __importDefault(require("./Attributes"));
|
|
9
|
+
var Interface = /** @class */ (function () {
|
|
10
|
+
function Interface(baseName, attributes, relativeDirectoryPath, prefix) {
|
|
11
|
+
if (prefix === void 0) { prefix = ""; }
|
|
12
|
+
this.Relations = []; // Components and relations
|
|
13
|
+
this.RelationNames = {};
|
|
14
|
+
this.RelationNamesCounter = {};
|
|
15
|
+
this.NamePrefix = "";
|
|
16
|
+
this.BaseName = baseName;
|
|
17
|
+
this.updateStrapiName();
|
|
18
|
+
this.NamePrefix = prefix;
|
|
19
|
+
this.Attributes = attributes;
|
|
20
|
+
this.RelativeDirectoryPath = relativeDirectoryPath;
|
|
21
|
+
}
|
|
22
|
+
Interface.prototype.updateStrapiName = function () {
|
|
23
|
+
this.StrapiName = "api::".concat(this.BaseName, ".").concat(this.BaseName);
|
|
24
|
+
};
|
|
25
|
+
Interface.prototype.getBaseName = function () {
|
|
26
|
+
return this.BaseName;
|
|
27
|
+
};
|
|
28
|
+
Interface.prototype.getStrapiName = function () {
|
|
29
|
+
return this.StrapiName;
|
|
30
|
+
};
|
|
31
|
+
Interface.prototype.getDependencies = function () {
|
|
32
|
+
var attrs = new Attributes_1.default(this.Attributes, this.RelationNames);
|
|
33
|
+
return attrs.getDependencies(this.getStrapiName());
|
|
34
|
+
};
|
|
35
|
+
Interface.prototype.getFullInterfaceName = function () {
|
|
36
|
+
var pascalName = (0, utils_1.pascalCase)(this.BaseName);
|
|
37
|
+
return "".concat(this.NamePrefix).concat(pascalName);
|
|
38
|
+
};
|
|
39
|
+
// For typescript import from index file
|
|
40
|
+
Interface.prototype.getRelativeRootPath = function () {
|
|
41
|
+
var path = (0, path_1.join)(this.RelativeDirectoryPath, this.getBaseName());
|
|
42
|
+
return (0, utils_1.prefixDotSlash)(path);
|
|
43
|
+
};
|
|
44
|
+
Interface.prototype.getRelativeRootDir = function () {
|
|
45
|
+
var path = (0, path_1.dirname)(this.getRelativeRootPathFile());
|
|
46
|
+
return path;
|
|
47
|
+
};
|
|
48
|
+
Interface.prototype.getRelativeRootPathFile = function () {
|
|
49
|
+
return "".concat(this.getRelativeRootPath(), ".ts");
|
|
50
|
+
};
|
|
51
|
+
Interface.prototype.setRelations = function (relations) {
|
|
52
|
+
var _this = this;
|
|
53
|
+
this.Relations = relations;
|
|
54
|
+
this.RelationNames = {};
|
|
55
|
+
this.Relations.forEach(function (inter) {
|
|
56
|
+
var name = inter.getFullInterfaceName();
|
|
57
|
+
// FIXME: clean up this mess...
|
|
58
|
+
if (inter.getStrapiName() === _this.getStrapiName()) {
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
// Avoid duplicate names
|
|
62
|
+
if (name in _this.RelationNamesCounter) {
|
|
63
|
+
name += ++_this.RelationNamesCounter[name];
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
_this.RelationNamesCounter[name] = 0;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
_this.RelationNames[inter.getStrapiName()] = [name, inter];
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
Interface.prototype.getTsImports = function () {
|
|
73
|
+
var _this = this;
|
|
74
|
+
return Object.keys(this.RelationNames).map(function (strapiName) {
|
|
75
|
+
if (strapiName === _this.getStrapiName()) {
|
|
76
|
+
return "";
|
|
77
|
+
}
|
|
78
|
+
var relationName = _this.RelationNames[strapiName][0];
|
|
79
|
+
var inter = _this.RelationNames[strapiName][1];
|
|
80
|
+
var importPath = (0, utils_1.prefixDotSlash)((0, path_1.relative)(_this.getRelativeRootDir(), inter.getRelativeRootPath()));
|
|
81
|
+
var fullName = inter.getFullInterfaceName();
|
|
82
|
+
var importNameString = fullName === relationName ? fullName : "".concat(fullName, " as ").concat(relationName);
|
|
83
|
+
return "import { ".concat(importNameString, " } from '").concat(importPath, "';");
|
|
84
|
+
}).filter(function (s) { return s; }).join("\n");
|
|
85
|
+
};
|
|
86
|
+
Interface.prototype.getAttributes = function () {
|
|
87
|
+
return new Attributes_1.default(this.Attributes, this.RelationNames);
|
|
88
|
+
};
|
|
89
|
+
Interface.prototype.attributesToString = function () {
|
|
90
|
+
var attrs = this.getAttributes();
|
|
91
|
+
return attrs.toString();
|
|
92
|
+
};
|
|
93
|
+
Interface.prototype.getInerfaceString = function () {
|
|
94
|
+
var str = "export interface ".concat(this.getFullInterfaceName(), " {\n");
|
|
95
|
+
str += this.getInterfaceFieldsString();
|
|
96
|
+
str += "}";
|
|
97
|
+
return str;
|
|
98
|
+
};
|
|
99
|
+
Interface.prototype.getInterfaceFieldsString = function () {
|
|
100
|
+
var str = '';
|
|
101
|
+
str += " id: number;\n";
|
|
102
|
+
str += " attributes: ".concat(this.attributesToString(), "\n");
|
|
103
|
+
return str;
|
|
104
|
+
};
|
|
105
|
+
Interface.prototype.toString = function () {
|
|
106
|
+
var strings = [
|
|
107
|
+
this.getTsImports(),
|
|
108
|
+
this.getInerfaceString()
|
|
109
|
+
];
|
|
110
|
+
return strings.join("\n");
|
|
111
|
+
};
|
|
112
|
+
return Interface;
|
|
113
|
+
}());
|
|
114
|
+
exports.default = Interface;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export default class InterfaceManager {
|
|
2
|
+
private Interfaces;
|
|
3
|
+
private OutRoot;
|
|
4
|
+
private StrapiSrcRoot;
|
|
5
|
+
private Options;
|
|
6
|
+
static BaseOptions: {
|
|
7
|
+
prefix: string;
|
|
8
|
+
useCategoryPrefix: boolean;
|
|
9
|
+
componentPrefix: string;
|
|
10
|
+
componentPrefixOverridesPrefix: boolean;
|
|
11
|
+
};
|
|
12
|
+
constructor(outRoot: string, strapiSrcRoot: string, options?: any);
|
|
13
|
+
createInterfaces(): Promise<void>;
|
|
14
|
+
injectDependencies(): void;
|
|
15
|
+
makeFolders(): Promise<void>;
|
|
16
|
+
writeInterfaces(): Promise<void>;
|
|
17
|
+
writeIndexFile(): Promise<void>;
|
|
18
|
+
run(): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
var fs_1 = require("fs");
|
|
43
|
+
var promises_1 = require("fs/promises");
|
|
44
|
+
var path_1 = require("path");
|
|
45
|
+
var utils_1 = require("../utils");
|
|
46
|
+
var ComponentInterface_1 = __importDefault(require("./ComponentInterface"));
|
|
47
|
+
var Interface_1 = __importDefault(require("./Interface"));
|
|
48
|
+
var schemaReader_1 = require("./schemaReader");
|
|
49
|
+
var InterfaceManager = /** @class */ (function () {
|
|
50
|
+
function InterfaceManager(outRoot, strapiSrcRoot, options) {
|
|
51
|
+
if (options === void 0) { options = {}; }
|
|
52
|
+
this.Interfaces = {}; // string = strapi name
|
|
53
|
+
this.OutRoot = outRoot;
|
|
54
|
+
this.StrapiSrcRoot = strapiSrcRoot;
|
|
55
|
+
this.Options = Object.assign({}, InterfaceManager.BaseOptions, options);
|
|
56
|
+
}
|
|
57
|
+
InterfaceManager.prototype.createInterfaces = function () {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
59
|
+
var apiSchemasPromise, componentSchemasPromise, apiSchemas, componentSchemas;
|
|
60
|
+
var _this = this;
|
|
61
|
+
return __generator(this, function (_a) {
|
|
62
|
+
switch (_a.label) {
|
|
63
|
+
case 0:
|
|
64
|
+
apiSchemasPromise = (0, schemaReader_1.getApiSchemas)(this.StrapiSrcRoot);
|
|
65
|
+
componentSchemasPromise = (0, schemaReader_1.getComponentSchemas)(this.StrapiSrcRoot);
|
|
66
|
+
return [4 /*yield*/, apiSchemasPromise];
|
|
67
|
+
case 1:
|
|
68
|
+
apiSchemas = _a.sent();
|
|
69
|
+
apiSchemas.forEach(function (schema) {
|
|
70
|
+
var name = schema.name, attributes = schema.attributes;
|
|
71
|
+
var strapiName = "api::".concat(name, ".").concat(name);
|
|
72
|
+
var inter = new Interface_1.default(name, attributes, "./", _this.Options.prefix);
|
|
73
|
+
_this.Interfaces[strapiName] = inter;
|
|
74
|
+
});
|
|
75
|
+
return [4 /*yield*/, componentSchemasPromise];
|
|
76
|
+
case 2:
|
|
77
|
+
componentSchemas = _a.sent();
|
|
78
|
+
componentSchemas.forEach(function (category) {
|
|
79
|
+
var categoryName = category.category;
|
|
80
|
+
category.schemas.forEach(function (schema) {
|
|
81
|
+
var componentName = schema.name;
|
|
82
|
+
var strapiName = "".concat(categoryName, ".").concat(schema.name);
|
|
83
|
+
var componentPrefix = "".concat(_this.Options.componentPrefix).concat(_this.Options.useCategoryPrefix ? (0, utils_1.pascalCase)(categoryName) : "");
|
|
84
|
+
var prefix = _this.Options.componentPrefixOverridesPrefix ? componentPrefix : _this.Options.prefix + componentPrefix;
|
|
85
|
+
// TODO: make component interface
|
|
86
|
+
var inter = new ComponentInterface_1.default(componentName, schema.attributes, "./".concat(categoryName), categoryName, prefix);
|
|
87
|
+
_this.Interfaces[strapiName] = inter;
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
return [2 /*return*/];
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
// Inject dependencies into all interfaces
|
|
96
|
+
InterfaceManager.prototype.injectDependencies = function () {
|
|
97
|
+
var _this = this;
|
|
98
|
+
// console.log("Injecting dependencies")
|
|
99
|
+
Object.keys(this.Interfaces).forEach(function (strapiName) {
|
|
100
|
+
var inter = _this.Interfaces[strapiName];
|
|
101
|
+
var dependencies = inter.getDependencies();
|
|
102
|
+
// console.log(`Interfaces for ${inter.getStrapiName()} are`)
|
|
103
|
+
var interfacesToInject = dependencies.map(function (dependencyStrapiName) {
|
|
104
|
+
return _this.Interfaces[dependencyStrapiName];
|
|
105
|
+
}).filter(function (inter) { return inter; });
|
|
106
|
+
inter.setRelations(interfacesToInject);
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
InterfaceManager.prototype.makeFolders = function () {
|
|
110
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
111
|
+
var componentCategories;
|
|
112
|
+
var _this = this;
|
|
113
|
+
return __generator(this, function (_a) {
|
|
114
|
+
switch (_a.label) {
|
|
115
|
+
case 0: return [4 /*yield*/, (0, schemaReader_1.getComponentCategoryFolders)(this.StrapiSrcRoot)];
|
|
116
|
+
case 1:
|
|
117
|
+
componentCategories = _a.sent();
|
|
118
|
+
if (!!(0, fs_1.existsSync)(this.OutRoot)) return [3 /*break*/, 3];
|
|
119
|
+
return [4 /*yield*/, (0, promises_1.mkdir)(this.OutRoot, {
|
|
120
|
+
recursive: true,
|
|
121
|
+
})];
|
|
122
|
+
case 2:
|
|
123
|
+
_a.sent();
|
|
124
|
+
_a.label = 3;
|
|
125
|
+
case 3: return [4 /*yield*/, Promise.all(componentCategories.map(function (category) { return __awaiter(_this, void 0, void 0, function () {
|
|
126
|
+
var path;
|
|
127
|
+
return __generator(this, function (_a) {
|
|
128
|
+
switch (_a.label) {
|
|
129
|
+
case 0:
|
|
130
|
+
path = (0, path_1.join)(this.OutRoot, category);
|
|
131
|
+
if ((0, fs_1.existsSync)(path)) {
|
|
132
|
+
return [2 /*return*/];
|
|
133
|
+
}
|
|
134
|
+
return [4 /*yield*/, (0, promises_1.mkdir)(path)];
|
|
135
|
+
case 1:
|
|
136
|
+
_a.sent();
|
|
137
|
+
return [2 /*return*/];
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}); }))];
|
|
141
|
+
case 4:
|
|
142
|
+
_a.sent();
|
|
143
|
+
return [2 /*return*/];
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
InterfaceManager.prototype.writeInterfaces = function () {
|
|
149
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
150
|
+
var writePromises;
|
|
151
|
+
var _this = this;
|
|
152
|
+
return __generator(this, function (_a) {
|
|
153
|
+
switch (_a.label) {
|
|
154
|
+
case 0:
|
|
155
|
+
writePromises = Object.keys(this.Interfaces).map(function (strapiName) { return __awaiter(_this, void 0, void 0, function () {
|
|
156
|
+
var inter, fileData, filePath;
|
|
157
|
+
return __generator(this, function (_a) {
|
|
158
|
+
switch (_a.label) {
|
|
159
|
+
case 0:
|
|
160
|
+
inter = this.Interfaces[strapiName];
|
|
161
|
+
fileData = inter.toString();
|
|
162
|
+
filePath = (0, path_1.join)(this.OutRoot, inter.getRelativeRootPathFile());
|
|
163
|
+
return [4 /*yield*/, (0, promises_1.writeFile)(filePath, fileData)];
|
|
164
|
+
case 1:
|
|
165
|
+
_a.sent();
|
|
166
|
+
return [2 /*return*/];
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}); });
|
|
170
|
+
return [4 /*yield*/, Promise.all(writePromises)];
|
|
171
|
+
case 1:
|
|
172
|
+
_a.sent();
|
|
173
|
+
return [2 /*return*/];
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
InterfaceManager.prototype.writeIndexFile = function () {
|
|
179
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
180
|
+
var strings, fileData, filePath;
|
|
181
|
+
var _this = this;
|
|
182
|
+
return __generator(this, function (_a) {
|
|
183
|
+
switch (_a.label) {
|
|
184
|
+
case 0:
|
|
185
|
+
strings = Object.keys(this.Interfaces).map(function (strapiName) {
|
|
186
|
+
var inter = _this.Interfaces[strapiName];
|
|
187
|
+
return "export * from '".concat(inter.getRelativeRootPath(), "'");
|
|
188
|
+
});
|
|
189
|
+
fileData = strings.join("\n");
|
|
190
|
+
filePath = (0, path_1.join)(this.OutRoot, "index.ts");
|
|
191
|
+
return [4 /*yield*/, (0, promises_1.writeFile)(filePath, fileData)];
|
|
192
|
+
case 1:
|
|
193
|
+
_a.sent();
|
|
194
|
+
return [2 /*return*/];
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
};
|
|
199
|
+
InterfaceManager.prototype.run = function () {
|
|
200
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
201
|
+
var createInterfacesPromise, makeFoldersPromise, err_1;
|
|
202
|
+
return __generator(this, function (_a) {
|
|
203
|
+
switch (_a.label) {
|
|
204
|
+
case 0:
|
|
205
|
+
_a.trys.push([0, 4, , 5]);
|
|
206
|
+
createInterfacesPromise = this.createInterfaces();
|
|
207
|
+
makeFoldersPromise = this.makeFolders();
|
|
208
|
+
return [4 /*yield*/, createInterfacesPromise];
|
|
209
|
+
case 1:
|
|
210
|
+
_a.sent();
|
|
211
|
+
this.injectDependencies();
|
|
212
|
+
return [4 /*yield*/, makeFoldersPromise];
|
|
213
|
+
case 2:
|
|
214
|
+
_a.sent();
|
|
215
|
+
return [4 /*yield*/, Promise.all([this.writeInterfaces(), this.writeIndexFile()])];
|
|
216
|
+
case 3:
|
|
217
|
+
_a.sent();
|
|
218
|
+
return [3 /*break*/, 5];
|
|
219
|
+
case 4:
|
|
220
|
+
err_1 = _a.sent();
|
|
221
|
+
console.error(err_1);
|
|
222
|
+
return [3 /*break*/, 5];
|
|
223
|
+
case 5: return [2 /*return*/];
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
};
|
|
228
|
+
InterfaceManager.BaseOptions = {
|
|
229
|
+
prefix: "I",
|
|
230
|
+
useCategoryPrefix: true,
|
|
231
|
+
componentPrefix: "",
|
|
232
|
+
componentPrefixOverridesPrefix: false,
|
|
233
|
+
};
|
|
234
|
+
return InterfaceManager;
|
|
235
|
+
}());
|
|
236
|
+
exports.default = InterfaceManager;
|
|
File without changes
|
|
File without changes
|