@strapi/typescript-utils 4.2.0-beta.4 → 4.3.0
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/__tests__/generators/schemas/attributes.test.js +703 -0
- package/lib/__tests__/generators/schemas/global.test.js +108 -0
- package/lib/__tests__/generators/schemas/imports.test.js +54 -0
- package/lib/__tests__/generators/schemas/utils.test.js +362 -0
- package/lib/admin/create-tsconfig-file.js +37 -0
- package/lib/admin/index.js +5 -0
- package/lib/generators/index.js +7 -0
- package/lib/generators/schemas/attributes.js +281 -0
- package/lib/generators/schemas/global.js +68 -0
- package/lib/generators/schemas/imports.js +33 -0
- package/lib/generators/schemas/index.js +185 -0
- package/lib/generators/schemas/schema.js +87 -0
- package/lib/generators/schemas/utils.js +160 -0
- package/lib/index.js +4 -0
- package/lib/utils/resolve-outdir.js +1 -1
- package/package.json +5 -2
- /package/{lib/configs → tsconfigs}/admin.json +0 -0
- /package/{lib/configs → tsconfigs}/server.json +0 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const ts = require('typescript');
|
|
4
|
+
const { factory } = require('typescript');
|
|
5
|
+
const {
|
|
6
|
+
pipe,
|
|
7
|
+
replace,
|
|
8
|
+
camelCase,
|
|
9
|
+
upperFirst,
|
|
10
|
+
isUndefined,
|
|
11
|
+
isNull,
|
|
12
|
+
isString,
|
|
13
|
+
isNumber,
|
|
14
|
+
isDate,
|
|
15
|
+
isArray,
|
|
16
|
+
isBoolean,
|
|
17
|
+
propEq,
|
|
18
|
+
} = require('lodash/fp');
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Get all components and content-types in a Strapi application
|
|
22
|
+
*
|
|
23
|
+
* @param {Strapi} strapi
|
|
24
|
+
* @returns {object}
|
|
25
|
+
*/
|
|
26
|
+
const getAllStrapiSchemas = strapi => ({ ...strapi.contentTypes, ...strapi.components });
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Extract a valid interface name from a schema uid
|
|
30
|
+
*
|
|
31
|
+
* @param {string} uid
|
|
32
|
+
* @returns {string}
|
|
33
|
+
*/
|
|
34
|
+
const getSchemaInterfaceName = pipe(replace(/(:.)/, ' '), camelCase, upperFirst);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Get the parent type name to extend based on the schema's nature
|
|
38
|
+
*
|
|
39
|
+
* @param {object} schema
|
|
40
|
+
* @returns {string}
|
|
41
|
+
*/
|
|
42
|
+
const getSchemaExtendsTypeName = schema => {
|
|
43
|
+
const base = getSchemaModelType(schema);
|
|
44
|
+
|
|
45
|
+
return upperFirst(base) + 'Schema';
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const getSchemaModelType = schema => {
|
|
49
|
+
const { modelType, kind } = schema;
|
|
50
|
+
|
|
51
|
+
// Components
|
|
52
|
+
if (modelType === 'component') {
|
|
53
|
+
return 'component';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Content-Types
|
|
57
|
+
else if (modelType === 'contentType') {
|
|
58
|
+
return kind;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return null;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Get a type node based on a type and its params
|
|
66
|
+
*
|
|
67
|
+
* @param {string} typeName
|
|
68
|
+
* @param {ts.TypeNode[]} [params]
|
|
69
|
+
* @returns
|
|
70
|
+
*/
|
|
71
|
+
const getTypeNode = (typeName, params = []) => {
|
|
72
|
+
return factory.createTypeReferenceNode(factory.createIdentifier(typeName), params);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Transform a regular JavaScript object or scalar value into a literal expression
|
|
77
|
+
* @param data
|
|
78
|
+
* @returns {ts.TypeNode}
|
|
79
|
+
*/
|
|
80
|
+
const toTypeLiteral = data => {
|
|
81
|
+
if (isUndefined(data)) {
|
|
82
|
+
return factory.createLiteralTypeNode(ts.SyntaxKind.UndefinedKeyword);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (isNull(data)) {
|
|
86
|
+
return factory.createLiteralTypeNode(ts.SyntaxKind.NullKeyword);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (isString(data)) {
|
|
90
|
+
return factory.createStringLiteral(data, true);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (isNumber(data)) {
|
|
94
|
+
return factory.createNumericLiteral(data);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (isBoolean(data)) {
|
|
98
|
+
return data ? factory.createTrue() : factory.createFalse();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (isArray(data)) {
|
|
102
|
+
return factory.createTupleTypeNode(data.map(item => toTypeLiteral(item)));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (isDate(data)) {
|
|
106
|
+
return factory.createStringLiteral(data.toISOString());
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (typeof data !== 'object') {
|
|
110
|
+
throw new Error(`Cannot convert to object literal. Unknown type "${typeof data}"`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const entries = Object.entries(data);
|
|
114
|
+
|
|
115
|
+
const props = entries.reduce((acc, [key, value]) => {
|
|
116
|
+
// Handle keys such as content-type-builder & co.
|
|
117
|
+
const identifier = key.includes('-')
|
|
118
|
+
? factory.createStringLiteral(key, true)
|
|
119
|
+
: factory.createIdentifier(key);
|
|
120
|
+
|
|
121
|
+
return [
|
|
122
|
+
...acc,
|
|
123
|
+
factory.createPropertyDeclaration(
|
|
124
|
+
undefined,
|
|
125
|
+
undefined,
|
|
126
|
+
identifier,
|
|
127
|
+
undefined,
|
|
128
|
+
toTypeLiteral(value)
|
|
129
|
+
),
|
|
130
|
+
];
|
|
131
|
+
}, []);
|
|
132
|
+
|
|
133
|
+
return factory.createTypeLiteralNode(props);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Get the number of attributes generated for a given schema definition
|
|
138
|
+
*
|
|
139
|
+
* @param {ts.TypeNode} definition
|
|
140
|
+
* @returns {number | null}
|
|
141
|
+
*/
|
|
142
|
+
const getDefinitionAttributesCount = definition => {
|
|
143
|
+
const attributesNode = definition.members.find(propEq('name.escapedText', 'attributes'));
|
|
144
|
+
|
|
145
|
+
if (!attributesNode) {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return attributesNode.type.members.length;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
module.exports = {
|
|
153
|
+
getAllStrapiSchemas,
|
|
154
|
+
getSchemaInterfaceName,
|
|
155
|
+
getSchemaExtendsTypeName,
|
|
156
|
+
getSchemaModelType,
|
|
157
|
+
getDefinitionAttributesCount,
|
|
158
|
+
getTypeNode,
|
|
159
|
+
toTypeLiteral,
|
|
160
|
+
};
|
package/lib/index.js
CHANGED
|
@@ -2,11 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
const compile = require('./compile');
|
|
4
4
|
const compilers = require('./compilers');
|
|
5
|
+
const admin = require('./admin');
|
|
5
6
|
const utils = require('./utils');
|
|
7
|
+
const generators = require('./generators');
|
|
6
8
|
|
|
7
9
|
module.exports = {
|
|
8
10
|
compile,
|
|
9
11
|
compilers,
|
|
12
|
+
admin,
|
|
13
|
+
generators,
|
|
10
14
|
|
|
11
15
|
...utils,
|
|
12
16
|
};
|
|
@@ -8,7 +8,7 @@ const DEFAULT_TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
|
8
8
|
* Gets the outDir value from config file (tsconfig)
|
|
9
9
|
* @param {string} dir
|
|
10
10
|
* @param {string | undefined} configFilename
|
|
11
|
-
* @returns {string | undefined}
|
|
11
|
+
* @returns {Promise<string | undefined>}
|
|
12
12
|
*/
|
|
13
13
|
module.exports = async (dir, configFilename = DEFAULT_TS_CONFIG_FILENAME) => {
|
|
14
14
|
return (await isUsingTypescript(dir))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/typescript-utils",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"description": "Typescript support for Strapi",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"strapi",
|
|
@@ -24,13 +24,16 @@
|
|
|
24
24
|
"lib": "./lib"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
+
"chalk": "4.1.2",
|
|
28
|
+
"cli-table3": "0.6.2",
|
|
27
29
|
"fs-extra": "10.0.1",
|
|
28
30
|
"lodash": "4.17.21",
|
|
31
|
+
"prettier": "2.7.1",
|
|
29
32
|
"typescript": "4.6.2"
|
|
30
33
|
},
|
|
31
34
|
"engines": {
|
|
32
35
|
"node": ">=12.22.0 <=16.x.x",
|
|
33
36
|
"npm": ">=6.0.0"
|
|
34
37
|
},
|
|
35
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "74a2b908df75bc8001d72f9dc8571c4b7a2da337"
|
|
36
39
|
}
|
|
File without changes
|
|
File without changes
|