@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.
@@ -0,0 +1,46 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ function writeInterface(inter) {
40
+ return __awaiter(this, void 0, void 0, function () {
41
+ return __generator(this, function (_a) {
42
+ return [2 /*return*/];
43
+ });
44
+ });
45
+ }
46
+ exports.default = writeInterface;
@@ -0,0 +1,14 @@
1
+ export declare function readSchema(schemaPath: string): Promise<any>;
2
+ export declare function getApiFolders(strapiSrcRoot: string): Promise<string[]>;
3
+ export declare function getComponentCategoryFolders(strapiSrcRoot: string): Promise<string[]>;
4
+ export declare function getComponentSchemas(strapiSrcRoot: string): Promise<{
5
+ category: string;
6
+ schemas: {
7
+ name: string;
8
+ attributes: any;
9
+ }[];
10
+ }[]>;
11
+ export declare function getApiSchemas(strapiSrcRoot: string): Promise<{
12
+ name: string;
13
+ attributes: any;
14
+ }[]>;
@@ -0,0 +1,168 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.getApiSchemas = exports.getComponentSchemas = exports.getComponentCategoryFolders = exports.getApiFolders = exports.readSchema = void 0;
40
+ var promises_1 = require("node:fs/promises");
41
+ var node_path_1 = require("node:path");
42
+ var utils_1 = require("../utils");
43
+ function readSchema(schemaPath) {
44
+ return __awaiter(this, void 0, void 0, function () {
45
+ var schemaData, e_1;
46
+ return __generator(this, function (_a) {
47
+ switch (_a.label) {
48
+ case 0:
49
+ _a.trys.push([0, 2, , 3]);
50
+ return [4 /*yield*/, (0, promises_1.readFile)(schemaPath)];
51
+ case 1:
52
+ schemaData = _a.sent();
53
+ return [2 /*return*/, JSON.parse(schemaData.toString()).attributes];
54
+ case 2:
55
+ e_1 = _a.sent();
56
+ return [2 /*return*/, null];
57
+ case 3: return [2 /*return*/];
58
+ }
59
+ });
60
+ });
61
+ }
62
+ exports.readSchema = readSchema;
63
+ function getApiFolders(strapiSrcRoot) {
64
+ return __awaiter(this, void 0, void 0, function () {
65
+ var path;
66
+ return __generator(this, function (_a) {
67
+ switch (_a.label) {
68
+ case 0:
69
+ path = (0, node_path_1.join)(strapiSrcRoot, 'api');
70
+ return [4 /*yield*/, (0, utils_1.readDirFiltered)(path)];
71
+ case 1: return [2 /*return*/, _a.sent()];
72
+ }
73
+ });
74
+ });
75
+ }
76
+ exports.getApiFolders = getApiFolders;
77
+ function getComponentCategoryFolders(strapiSrcRoot) {
78
+ return __awaiter(this, void 0, void 0, function () {
79
+ var path;
80
+ return __generator(this, function (_a) {
81
+ switch (_a.label) {
82
+ case 0:
83
+ path = (0, node_path_1.join)(strapiSrcRoot, "components");
84
+ return [4 /*yield*/, (0, utils_1.readDirFiltered)(path)];
85
+ case 1: return [2 /*return*/, _a.sent()];
86
+ }
87
+ });
88
+ });
89
+ }
90
+ exports.getComponentCategoryFolders = getComponentCategoryFolders;
91
+ function getComponentSchemas(strapiSrcRoot) {
92
+ return __awaiter(this, void 0, void 0, function () {
93
+ var categories, nestedSchemasPromises, nestedSchemasArr;
94
+ var _this = this;
95
+ return __generator(this, function (_a) {
96
+ switch (_a.label) {
97
+ case 0: return [4 /*yield*/, getComponentCategoryFolders(strapiSrcRoot)];
98
+ case 1:
99
+ categories = _a.sent();
100
+ nestedSchemasPromises = categories.map(function (category) { return __awaiter(_this, void 0, void 0, function () {
101
+ var schemaFilesPath, schemaFiles, schemaNamesWithAttributesPromises, schemaNamesWithAttributes;
102
+ var _this = this;
103
+ return __generator(this, function (_a) {
104
+ switch (_a.label) {
105
+ case 0:
106
+ schemaFilesPath = (0, node_path_1.join)(strapiSrcRoot, "components", category);
107
+ return [4 /*yield*/, (0, utils_1.readDirFiltered)(schemaFilesPath)];
108
+ case 1:
109
+ schemaFiles = _a.sent();
110
+ schemaNamesWithAttributesPromises = schemaFiles.map(function (file) { return __awaiter(_this, void 0, void 0, function () {
111
+ var schemaPath, attributes, name;
112
+ return __generator(this, function (_a) {
113
+ switch (_a.label) {
114
+ case 0:
115
+ schemaPath = (0, node_path_1.join)(schemaFilesPath, file);
116
+ return [4 /*yield*/, readSchema(schemaPath)];
117
+ case 1:
118
+ attributes = _a.sent();
119
+ name = file.split(".")[0];
120
+ return [2 /*return*/, { name: name, attributes: attributes }];
121
+ }
122
+ });
123
+ }); });
124
+ return [4 /*yield*/, Promise.all(schemaNamesWithAttributesPromises)];
125
+ case 2:
126
+ schemaNamesWithAttributes = _a.sent();
127
+ return [2 /*return*/, { category: category, schemas: schemaNamesWithAttributes }];
128
+ }
129
+ });
130
+ }); });
131
+ return [4 /*yield*/, Promise.all(nestedSchemasPromises)];
132
+ case 2:
133
+ nestedSchemasArr = _a.sent();
134
+ return [2 /*return*/, nestedSchemasArr];
135
+ }
136
+ });
137
+ });
138
+ }
139
+ exports.getComponentSchemas = getComponentSchemas;
140
+ function getApiSchemas(strapiSrcRoot) {
141
+ return __awaiter(this, void 0, void 0, function () {
142
+ var apiFolders, schemasWithAttributesPromises, schemasWithAttributes;
143
+ var _this = this;
144
+ return __generator(this, function (_a) {
145
+ switch (_a.label) {
146
+ case 0: return [4 /*yield*/, getApiFolders(strapiSrcRoot)];
147
+ case 1:
148
+ apiFolders = _a.sent();
149
+ schemasWithAttributesPromises = apiFolders.map(function (folder) { return __awaiter(_this, void 0, void 0, function () {
150
+ var schemaPath, attributes;
151
+ return __generator(this, function (_a) {
152
+ switch (_a.label) {
153
+ case 0:
154
+ schemaPath = (0, node_path_1.join)(strapiSrcRoot, "api", folder, "content-types", folder, "schema.json");
155
+ return [4 /*yield*/, readSchema(schemaPath)];
156
+ case 1:
157
+ attributes = _a.sent();
158
+ return [2 /*return*/, { name: folder, attributes: attributes }];
159
+ }
160
+ });
161
+ }); });
162
+ schemasWithAttributes = Promise.all(schemasWithAttributesPromises);
163
+ return [2 /*return*/, schemasWithAttributes];
164
+ }
165
+ });
166
+ });
167
+ }
168
+ exports.getApiSchemas = getApiSchemas;
@@ -0,0 +1,3 @@
1
+ export declare function pascalCase(name: string): string;
2
+ export declare function readDirFiltered(dir: string): Promise<string[]>;
3
+ export declare function prefixDotSlash(path: any): any;
@@ -0,0 +1,67 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.prefixDotSlash = exports.readDirFiltered = exports.pascalCase = void 0;
40
+ var promises_1 = require("fs/promises");
41
+ function pascalCase(name) {
42
+ var words = name.match(/[a-z]+/gi);
43
+ var pascalName = words.map(function (word) {
44
+ return word.charAt(0).toUpperCase() + word.substring(1).toLowerCase();
45
+ }).join("");
46
+ // console.log(pascalName)
47
+ return pascalName;
48
+ }
49
+ exports.pascalCase = pascalCase;
50
+ function readDirFiltered(dir) {
51
+ return __awaiter(this, void 0, void 0, function () {
52
+ var folders;
53
+ return __generator(this, function (_a) {
54
+ switch (_a.label) {
55
+ case 0: return [4 /*yield*/, (0, promises_1.readdir)(dir)];
56
+ case 1:
57
+ folders = _a.sent();
58
+ return [2 /*return*/, folders.filter(function (folder) { return !folder.startsWith("."); })];
59
+ }
60
+ });
61
+ });
62
+ }
63
+ exports.readDirFiltered = readDirFiltered;
64
+ function prefixDotSlash(path) {
65
+ return (/^\.?\.\//).test(path) ? path : "./" + path;
66
+ }
67
+ exports.prefixDotSlash = prefixDotSlash;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oak-digital/types-4-strapi-2",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Typescript interface generator for Strapi 4 models",
5
5
  "bin": {
6
6
  "t4s": "./bin/index.js"
package/src/index.ts DELETED
@@ -1,22 +0,0 @@
1
- import { program } from 'commander'
2
- import * as fs from 'node:fs/promises'
3
- import InterfaceManager from './interface/InterfaceManager'
4
-
5
- program
6
- .name("t4s")
7
-
8
- program
9
- .option('-i, --in <dir>', 'The src directory for strapi', './src')
10
- .option('-o, --out <dir>', 'The output directory to output the types to', './types')
11
-
12
- program.parse()
13
- const options = program.opts()
14
- const {
15
- in: input,
16
- out,
17
- } = options
18
-
19
- const manager = new InterfaceManager(out, input)
20
- manager.run().catch((err) => {
21
- console.error(err)
22
- });
@@ -1,27 +0,0 @@
1
- import Interface from "./Interface";
2
-
3
- export default class ComponentInterface extends Interface {
4
- protected Category: string;
5
-
6
- constructor(baseName: string, attributes: any, relativeDirectoryPath: string, category: string, prefix: string = "") {
7
- super(baseName, attributes, relativeDirectoryPath, prefix);
8
- this.Category = category;
9
- // this.Attributes.id = {
10
- // type: "number", // Components have a id field with a number
11
- // required: true,
12
- // };
13
- this.updateStrapiName();
14
- }
15
-
16
- updateStrapiName() {
17
- this.StrapiName = `${this.Category}.${this.getBaseName()}`
18
- }
19
-
20
- getInterfaceFieldsString() {
21
- const attrs = this.getAttributes();
22
- let str = '';
23
- str += ` id: number;\n`;
24
- str += ` __component: "${this.getStrapiName()}";\n`
25
- return str + attrs.toFieldsString();
26
- }
27
- }
@@ -1,123 +0,0 @@
1
- import { dirname, join, parse, relative } from "path";
2
- import { pascalCase, prefixDotSlash } from "../utils";
3
- import Attributes from "./Attributes";
4
-
5
- export default class Interface {
6
- protected BaseName: string;
7
- private Relations: Interface[] = []; // Components and relations
8
- private RelationNames: Record<string, [string, Interface]> = {};
9
- private RelationNamesCounter: Record<string, number> = {};
10
- private NamePrefix: string = "";
11
- protected Attributes: any;
12
- private RelativeDirectoryPath: string;
13
- protected StrapiName: string;
14
-
15
- constructor(baseName: string, attributes: any, relativeDirectoryPath: string, prefix: string = "") {
16
- this.BaseName = baseName;
17
- this.updateStrapiName()
18
- this.NamePrefix = prefix;
19
- this.Attributes = attributes;
20
- this.RelativeDirectoryPath = relativeDirectoryPath;
21
- }
22
-
23
- protected updateStrapiName() {
24
- this.StrapiName = `api::${this.BaseName}.${this.BaseName}`;
25
- }
26
-
27
- getBaseName() {
28
- return this.BaseName;
29
- }
30
-
31
- getStrapiName() {
32
- return this.StrapiName;
33
- }
34
-
35
- getDependencies() {
36
- const attrs = new Attributes(this.Attributes, this.RelationNames);
37
- return attrs.getDependencies(this.getStrapiName());
38
- }
39
-
40
- getFullInterfaceName() {
41
- const pascalName = pascalCase(this.BaseName);
42
- return `${this.NamePrefix}${pascalName}`;
43
- }
44
-
45
- // For typescript import from index file
46
- getRelativeRootPath() {
47
- const path = join(this.RelativeDirectoryPath, this.getBaseName());
48
- return prefixDotSlash(path);
49
- }
50
-
51
- getRelativeRootDir() {
52
- const path = dirname(this.getRelativeRootPathFile());
53
- return path;
54
- }
55
-
56
- getRelativeRootPathFile() {
57
- return `${this.getRelativeRootPath()}.ts`
58
- }
59
-
60
- setRelations(relations: Interface[]) {
61
- this.Relations = relations;
62
- this.RelationNames = {};
63
- this.Relations.forEach((inter: Interface) => {
64
- let name = inter.getFullInterfaceName();
65
- // FIXME: clean up this mess...
66
- if (inter.getStrapiName() === this.getStrapiName()) {
67
- } else {
68
- // Avoid duplicate names
69
- if (name in this.RelationNamesCounter) {
70
- name += ++this.RelationNamesCounter[name];
71
- } else {
72
- this.RelationNamesCounter[name] = 0;
73
- }
74
- }
75
- this.RelationNames[inter.getStrapiName()] = [name, inter];
76
- })
77
- }
78
-
79
- private getTsImports() {
80
- return Object.keys(this.RelationNames).map((strapiName: string) => {
81
- if (strapiName === this.getStrapiName()) {
82
- return "";
83
- }
84
- const relationName = this.RelationNames[strapiName][0];
85
- const inter = this.RelationNames[strapiName][1];
86
- const importPath = prefixDotSlash(relative(this.getRelativeRootDir(), inter.getRelativeRootPath()));
87
- const fullName = inter.getFullInterfaceName();
88
- const importNameString = fullName === relationName ? fullName : `${fullName} as ${relationName}`;
89
- return `import { ${importNameString} } from '${importPath}';`;
90
- }).filter(s => s).join("\n");
91
- }
92
-
93
- getAttributes() : Attributes {
94
- return new Attributes(this.Attributes, this.RelationNames);
95
- }
96
-
97
- attributesToString() {
98
- const attrs = this.getAttributes();
99
- return attrs.toString();
100
- }
101
-
102
- getInerfaceString() {
103
- let str = `export interface ${this.getFullInterfaceName()} {\n`;
104
- str += this.getInterfaceFieldsString();
105
- str += `}`
106
- return str;
107
- }
108
-
109
- getInterfaceFieldsString() {
110
- let str = '';
111
- str += ` id: number;\n`;
112
- str += ` attributes: ${this.attributesToString()}\n`;
113
- return str;
114
- }
115
-
116
- toString() {
117
- const strings = [
118
- this.getTsImports(),
119
- this.getInerfaceString()
120
- ];
121
- return strings.join("\n")
122
- }
123
- }
@@ -1,116 +0,0 @@
1
- import { existsSync } from "fs";
2
- import { mkdir, rm, writeFile } from "fs/promises";
3
- import { join } from "path";
4
- import { pascalCase } from "../utils";
5
- import ComponentInterface from "./ComponentInterface";
6
- import Interface from "./Interface";
7
- import { getApiSchemas, getComponentCategoryFolders, getComponentSchemas } from "./schemaReader";
8
-
9
- export default class InterfaceManager {
10
- private Interfaces: Record<string, Interface> = {}; // string = strapi name
11
- private OutRoot: string;
12
- private StrapiSrcRoot: string;
13
- private Options: any;
14
-
15
- static BaseOptions = {
16
- prefix: "I",
17
- useCategoryPrefix: true,
18
- componentPrefix: "",
19
- componentPrefixOverridesPrefix: false,
20
- };
21
-
22
- constructor(outRoot: string, strapiSrcRoot: string, options: any = {}) {
23
- this.OutRoot = outRoot;
24
- this.StrapiSrcRoot = strapiSrcRoot;
25
- this.Options = Object.assign({}, InterfaceManager.BaseOptions, options);
26
- }
27
-
28
- async createInterfaces() {
29
- const apiSchemasPromise = getApiSchemas(this.StrapiSrcRoot);
30
- const componentSchemasPromise = getComponentSchemas(this.StrapiSrcRoot);
31
- const apiSchemas = await apiSchemasPromise;
32
- apiSchemas.forEach((schema) => {
33
- const { name, attributes } = schema;
34
- const strapiName = `api::${name}.${name}`;
35
- const inter = new Interface(name, attributes, "./", this.Options.prefix);
36
- this.Interfaces[strapiName] = inter;
37
- })
38
-
39
- const componentSchemas = await componentSchemasPromise;
40
- componentSchemas.forEach((category) => {
41
- const categoryName : string = category.category;
42
- category.schemas.forEach((schema) => {
43
- const componentName = schema.name;
44
- const strapiName = `${categoryName}.${schema.name}`
45
- const componentPrefix = `${this.Options.componentPrefix}${this.Options.useCategoryPrefix ? pascalCase(categoryName) : ""}`;
46
- const prefix = this.Options.componentPrefixOverridesPrefix ? componentPrefix : this.Options.prefix + componentPrefix;
47
- // TODO: make component interface
48
- const inter = new ComponentInterface(componentName, schema.attributes, `./${categoryName}`, categoryName, prefix);
49
- this.Interfaces[strapiName] = inter;
50
- })
51
- })
52
- }
53
-
54
- // Inject dependencies into all interfaces
55
- injectDependencies() {
56
- // console.log("Injecting dependencies")
57
- Object.keys(this.Interfaces).forEach((strapiName: string) => {
58
- const inter = this.Interfaces[strapiName];
59
- const dependencies = inter.getDependencies();
60
- // console.log(`Interfaces for ${inter.getStrapiName()} are`)
61
- const interfacesToInject = dependencies.map((dependencyStrapiName: string) => {
62
- return this.Interfaces[dependencyStrapiName];
63
- }).filter((inter) => inter);
64
- inter.setRelations(interfacesToInject);
65
- })
66
- }
67
-
68
- async makeFolders() {
69
- const componentCategories = await getComponentCategoryFolders(this.StrapiSrcRoot);
70
- if (!existsSync(this.OutRoot)) {
71
- await mkdir(this.OutRoot, {
72
- recursive: true,
73
- });
74
- }
75
- await Promise.all(componentCategories.map(async (category) => {
76
- const path = join(this.OutRoot, category);
77
- if (existsSync(path)) {
78
- return;
79
- }
80
- await mkdir(path);
81
- }));
82
- }
83
-
84
- async writeInterfaces() {
85
- const writePromises = Object.keys(this.Interfaces).map(async (strapiName) => {
86
- const inter = this.Interfaces[strapiName];
87
- const fileData = inter.toString();
88
- const filePath = join(this.OutRoot, inter.getRelativeRootPathFile());
89
- await writeFile(filePath, fileData)
90
- });
91
- await Promise.all(writePromises);
92
- }
93
-
94
- async writeIndexFile() {
95
- const strings = Object.keys(this.Interfaces).map((strapiName: string) => {
96
- const inter = this.Interfaces[strapiName];
97
- return `export * from '${inter.getRelativeRootPath()}'`;
98
- });
99
- const fileData = strings.join("\n");
100
- const filePath = join(this.OutRoot, "index.ts");
101
- await writeFile(filePath, fileData);
102
- }
103
-
104
- async run() {
105
- try {
106
- const createInterfacesPromise = this.createInterfaces();
107
- const makeFoldersPromise = this.makeFolders();
108
- await createInterfacesPromise;
109
- this.injectDependencies();
110
- await makeFoldersPromise;
111
- await Promise.all([this.writeInterfaces(), this.writeIndexFile()])
112
- } catch (err) {
113
- console.error(err);
114
- }
115
- }
116
- }
@@ -1,5 +0,0 @@
1
- import type Interface from "./Interface"
2
-
3
- export default async function writeInterface(inter: Interface) {
4
-
5
- }
@@ -1,51 +0,0 @@
1
- import { readFile } from 'node:fs/promises';
2
- import { join } from 'node:path';
3
- import { readDirFiltered } from '../utils';
4
-
5
- export async function readSchema(schemaPath: string) {
6
- try {
7
- const schemaData = await readFile(schemaPath);
8
- return JSON.parse(schemaData.toString()).attributes;
9
- } catch (e) {
10
- return null;
11
- }
12
- }
13
-
14
- export async function getApiFolders(strapiSrcRoot: string) {
15
- const path = join(strapiSrcRoot, 'api');
16
- return await readDirFiltered(path)
17
- }
18
-
19
- export async function getComponentCategoryFolders(strapiSrcRoot: string) {
20
- const path = join(strapiSrcRoot, "components")
21
- return await readDirFiltered(path)
22
- }
23
-
24
- export async function getComponentSchemas(strapiSrcRoot: string) {
25
- const categories = await getComponentCategoryFolders(strapiSrcRoot);
26
- const nestedSchemasPromises = categories.map(async (category: string) => {
27
- const schemaFilesPath = join(strapiSrcRoot, "components", category);
28
- const schemaFiles = await readDirFiltered(schemaFilesPath);
29
- const schemaNamesWithAttributesPromises = schemaFiles.map(async (file: string) => {
30
- const schemaPath = join(schemaFilesPath, file);
31
- const attributes = await readSchema(schemaPath);
32
- const name = file.split(".")[0];
33
- return { name, attributes }
34
- })
35
- const schemaNamesWithAttributes = await Promise.all(schemaNamesWithAttributesPromises);
36
- return { category, schemas: schemaNamesWithAttributes };
37
- });
38
- const nestedSchemasArr = await Promise.all(nestedSchemasPromises);
39
- return nestedSchemasArr;
40
- }
41
-
42
- export async function getApiSchemas(strapiSrcRoot: string) {
43
- const apiFolders = await getApiFolders(strapiSrcRoot);
44
- const schemasWithAttributesPromises = apiFolders.map(async (folder: string) => {
45
- const schemaPath = join(strapiSrcRoot, "api", folder, "content-types", folder, "schema.json")
46
- const attributes = await readSchema(schemaPath);
47
- return { name: folder, attributes };
48
- })
49
- const schemasWithAttributes = Promise.all(schemasWithAttributesPromises);
50
- return schemasWithAttributes;
51
- }