@strapi/typescript-utils 0.0.0-experimental.abc → 0.0.0-experimental.ac6c76c235167c88527fdfe0ec095479acc4ed86
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/.eslintrc.cjs +15 -0
- package/lib/__tests__/generators/schemas/attributes.test.js +18 -23
- package/lib/__tests__/generators/schemas/utils.test.js +37 -31
- package/lib/compilers/basic.js +1 -1
- package/lib/generators/common/imports.js +3 -3
- package/lib/generators/common/models/schema.js +5 -3
- package/lib/generators/common/models/utils.js +7 -2
- package/lib/generators/components/index.js +20 -4
- package/lib/generators/content-types/index.js +20 -4
- package/lib/utils/index.js +2 -0
- package/lib/utils/is-using-typescript-sync.js +3 -1
- package/lib/utils/resolve-outdir-sync.js +18 -0
- package/package.json +13 -10
- package/.eslintignore +0 -3
- package/.eslintrc.js +0 -4
package/.eslintrc.cjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/** @type {import('eslint').Linter.Config} */
|
|
4
|
+
const config = {
|
|
5
|
+
root: true,
|
|
6
|
+
extends: ['eslint-config-custom/back'],
|
|
7
|
+
ignorePatterns: [
|
|
8
|
+
'node_modules/',
|
|
9
|
+
'.eslintrc.cjs',
|
|
10
|
+
'index.d.ts',
|
|
11
|
+
'coverage/',
|
|
12
|
+
],
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
module.exports = config;
|
|
@@ -680,18 +680,18 @@ describe('Attributes', () => {
|
|
|
680
680
|
expect(definition.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
681
681
|
expect(definition.members).toHaveLength(2);
|
|
682
682
|
|
|
683
|
-
const [
|
|
684
|
-
|
|
685
|
-
expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
686
|
-
expect(min.name.escapedText).toBe('min');
|
|
687
|
-
expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
|
|
688
|
-
expect(min.type.text).toBe('4');
|
|
683
|
+
const [max, min] = definition.members;
|
|
689
684
|
|
|
690
685
|
expect(max.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
691
686
|
expect(max.name.escapedText).toBe('max');
|
|
692
687
|
expect(max.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
|
|
693
688
|
expect(max.type.text).toBe('12');
|
|
694
689
|
|
|
690
|
+
expect(min.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
691
|
+
expect(min.name.escapedText).toBe('min');
|
|
692
|
+
expect(min.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
|
|
693
|
+
expect(min.type.text).toBe('4');
|
|
694
|
+
|
|
695
695
|
// Check for number keyword on the second typeArgument
|
|
696
696
|
expect(typeofMinMax.kind).toBe(ts.SyntaxKind.NumberKeyword);
|
|
697
697
|
});
|
|
@@ -830,24 +830,19 @@ describe('Attributes', () => {
|
|
|
830
830
|
expect(modifiers[0].typeArguments[0].kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
831
831
|
expect(modifiers[0].typeArguments[0].members).toHaveLength(2);
|
|
832
832
|
|
|
833
|
-
|
|
834
|
-
expect(modifiers[0].typeArguments[0].members[0].kind).toBe(
|
|
835
|
-
ts.SyntaxKind.PropertyDeclaration
|
|
836
|
-
);
|
|
837
|
-
expect(modifiers[0].typeArguments[0].members[0].name.escapedText).toBe('minLength');
|
|
838
|
-
expect(modifiers[0].typeArguments[0].members[0].type.kind).toBe(
|
|
839
|
-
ts.SyntaxKind.NumericLiteral
|
|
840
|
-
);
|
|
841
|
-
expect(modifiers[0].typeArguments[0].members[0].type.text).toBe('4');
|
|
833
|
+
const [maxLength, minLength] = modifiers[0].typeArguments[0].members;
|
|
842
834
|
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
);
|
|
846
|
-
expect(
|
|
847
|
-
expect(
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
expect(
|
|
835
|
+
// Max
|
|
836
|
+
expect(maxLength.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
837
|
+
expect(maxLength.name.escapedText).toBe('maxLength');
|
|
838
|
+
expect(maxLength.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
|
|
839
|
+
expect(maxLength.type.text).toBe('12');
|
|
840
|
+
|
|
841
|
+
// Min
|
|
842
|
+
expect(minLength.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
843
|
+
expect(minLength.name.escapedText).toBe('minLength');
|
|
844
|
+
expect(minLength.type.kind).toBe(ts.SyntaxKind.NumericLiteral);
|
|
845
|
+
expect(minLength.type.text).toBe('4');
|
|
851
846
|
});
|
|
852
847
|
});
|
|
853
848
|
|
|
@@ -168,10 +168,16 @@ describe('Utils', () => {
|
|
|
168
168
|
});
|
|
169
169
|
|
|
170
170
|
test('Number', () => {
|
|
171
|
-
const
|
|
171
|
+
const nodePositive = toTypeLiteral(42);
|
|
172
|
+
const nodeNegative = toTypeLiteral(-42);
|
|
172
173
|
|
|
173
|
-
expect(
|
|
174
|
-
expect(
|
|
174
|
+
expect(nodePositive.kind).toBe(ts.SyntaxKind.FirstLiteralToken);
|
|
175
|
+
expect(nodePositive.text).toBe('42');
|
|
176
|
+
|
|
177
|
+
expect(nodeNegative.kind).toBe(ts.SyntaxKind.PrefixUnaryExpression);
|
|
178
|
+
expect(nodeNegative.operator).toBe(ts.SyntaxKind.MinusToken);
|
|
179
|
+
expect(nodeNegative.operand.kind).toBe(ts.SyntaxKind.FirstLiteralToken);
|
|
180
|
+
expect(nodeNegative.operand.text).toBe('42');
|
|
175
181
|
});
|
|
176
182
|
|
|
177
183
|
test('Boolean', () => {
|
|
@@ -247,13 +253,13 @@ describe('Utils', () => {
|
|
|
247
253
|
expect(objectNode.members).toHaveLength(2);
|
|
248
254
|
|
|
249
255
|
expect(objectNode.members[0].kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
250
|
-
expect(objectNode.members[0].name.escapedText).toBe('
|
|
251
|
-
expect(objectNode.members[0].type.kind).toBe(ts.SyntaxKind.
|
|
252
|
-
expect(objectNode.members[0].type.text).toBe('bar');
|
|
256
|
+
expect(objectNode.members[0].name.escapedText).toBe('bar');
|
|
257
|
+
expect(objectNode.members[0].type.kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
253
258
|
|
|
254
259
|
expect(objectNode.members[1].kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
255
|
-
expect(objectNode.members[1].name.escapedText).toBe('
|
|
256
|
-
expect(objectNode.members[1].type.kind).toBe(ts.SyntaxKind.
|
|
260
|
+
expect(objectNode.members[1].name.escapedText).toBe('foo');
|
|
261
|
+
expect(objectNode.members[1].type.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
262
|
+
expect(objectNode.members[1].type.text).toBe('bar');
|
|
257
263
|
});
|
|
258
264
|
|
|
259
265
|
test('Object', () => {
|
|
@@ -262,20 +268,20 @@ describe('Utils', () => {
|
|
|
262
268
|
expect(node.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
263
269
|
expect(node.members).toHaveLength(2);
|
|
264
270
|
|
|
265
|
-
const [
|
|
271
|
+
const [barMember, fooMember] = node.members;
|
|
266
272
|
|
|
267
|
-
expect(
|
|
268
|
-
expect(
|
|
269
|
-
expect(
|
|
270
|
-
expect(
|
|
271
|
-
expect(firstMember.type.elements[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
272
|
-
expect(firstMember.type.elements[1].kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
273
|
-
expect(firstMember.type.elements[2].kind).toBe(ts.SyntaxKind.FirstLiteralToken);
|
|
273
|
+
expect(barMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
274
|
+
expect(barMember.name.escapedText).toBe('bar');
|
|
275
|
+
expect(barMember.type.kind).toBe(ts.SyntaxKind.LiteralType);
|
|
276
|
+
expect(barMember.type.literal).toBe(ts.SyntaxKind.NullKeyword);
|
|
274
277
|
|
|
275
|
-
expect(
|
|
276
|
-
expect(
|
|
277
|
-
expect(
|
|
278
|
-
expect(
|
|
278
|
+
expect(fooMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
279
|
+
expect(fooMember.name.escapedText).toBe('foo');
|
|
280
|
+
expect(fooMember.type.kind).toBe(ts.SyntaxKind.TupleType);
|
|
281
|
+
expect(fooMember.type.elements).toHaveLength(3);
|
|
282
|
+
expect(fooMember.type.elements[0].kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
283
|
+
expect(fooMember.type.elements[1].kind).toBe(ts.SyntaxKind.TrueKeyword);
|
|
284
|
+
expect(fooMember.type.elements[2].kind).toBe(ts.SyntaxKind.FirstLiteralToken);
|
|
279
285
|
});
|
|
280
286
|
|
|
281
287
|
test('Object with complex keys', () => {
|
|
@@ -284,19 +290,19 @@ describe('Utils', () => {
|
|
|
284
290
|
expect(node.kind).toBe(ts.SyntaxKind.TypeLiteral);
|
|
285
291
|
expect(node.members).toHaveLength(2);
|
|
286
292
|
|
|
287
|
-
const [
|
|
293
|
+
const [fooBar, fooDashBar] = node.members;
|
|
288
294
|
|
|
289
|
-
expect(
|
|
290
|
-
expect(
|
|
291
|
-
expect(
|
|
292
|
-
expect(
|
|
293
|
-
expect(
|
|
295
|
+
expect(fooBar.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
296
|
+
expect(fooBar.name.kind).toBe(ts.SyntaxKind.Identifier);
|
|
297
|
+
expect(fooBar.name.escapedText).toBe('foo');
|
|
298
|
+
expect(fooBar.type.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
299
|
+
expect(fooBar.type.text).toBe('bar');
|
|
294
300
|
|
|
295
|
-
expect(
|
|
296
|
-
expect(
|
|
297
|
-
expect(
|
|
298
|
-
expect(
|
|
299
|
-
expect(
|
|
301
|
+
expect(fooDashBar.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
|
|
302
|
+
expect(fooDashBar.name.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
303
|
+
expect(fooDashBar.name.text).toBe('foo-bar');
|
|
304
|
+
expect(fooDashBar.type.kind).toBe(ts.SyntaxKind.StringLiteral);
|
|
305
|
+
expect(fooDashBar.type.text).toBe('foobar');
|
|
300
306
|
});
|
|
301
307
|
|
|
302
308
|
test('Invalid data type supplied (function)', () => {
|
package/lib/compilers/basic.js
CHANGED
|
@@ -18,9 +18,9 @@ module.exports = {
|
|
|
18
18
|
},
|
|
19
19
|
|
|
20
20
|
generateImportDefinition() {
|
|
21
|
-
const formattedImports = imports
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
const formattedImports = imports
|
|
22
|
+
.sort()
|
|
23
|
+
.map((key) => factory.createImportSpecifier(false, undefined, factory.createIdentifier(key)));
|
|
24
24
|
|
|
25
25
|
return [
|
|
26
26
|
factory.createImportDeclaration(
|
|
@@ -22,9 +22,11 @@ const { addImport } = require('../imports');
|
|
|
22
22
|
const generateAttributePropertySignature = (schema) => {
|
|
23
23
|
const { attributes } = schema;
|
|
24
24
|
|
|
25
|
-
const properties = Object.entries(attributes)
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
const properties = Object.entries(attributes)
|
|
26
|
+
.sort((a, b) => a[0].localeCompare(b[0]))
|
|
27
|
+
.map(([attributeName, attribute]) => {
|
|
28
|
+
return attributeToPropertySignature(schema, attributeName, attribute);
|
|
29
|
+
});
|
|
28
30
|
|
|
29
31
|
return factory.createPropertySignature(
|
|
30
32
|
undefined,
|
|
@@ -92,7 +92,12 @@ const toTypeLiteral = (data) => {
|
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
if (isNumber(data)) {
|
|
95
|
-
return
|
|
95
|
+
return data < 0
|
|
96
|
+
? factory.createPrefixUnaryExpression(
|
|
97
|
+
ts.SyntaxKind.MinusToken,
|
|
98
|
+
factory.createNumericLiteral(-data)
|
|
99
|
+
)
|
|
100
|
+
: factory.createNumericLiteral(data);
|
|
96
101
|
}
|
|
97
102
|
|
|
98
103
|
if (isBoolean(data)) {
|
|
@@ -111,7 +116,7 @@ const toTypeLiteral = (data) => {
|
|
|
111
116
|
throw new Error(`Cannot convert to object literal. Unknown type "${typeof data}"`);
|
|
112
117
|
}
|
|
113
118
|
|
|
114
|
-
const entries = Object.entries(data);
|
|
119
|
+
const entries = Object.entries(data).sort((a, b) => a[0].localeCompare(b[0]));
|
|
115
120
|
|
|
116
121
|
const props = entries.reduce((acc, [key, value]) => {
|
|
117
122
|
// Handle keys such as content-type-builder & co.
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const { factory } = require('typescript');
|
|
4
|
+
const { pipe, values, sortBy, map } = require('lodash/fp');
|
|
4
5
|
|
|
5
6
|
const { models } = require('../common');
|
|
6
7
|
const { emitDefinitions, format, generateSharedExtensionDefinition } = require('../utils');
|
|
7
8
|
|
|
9
|
+
const NO_COMPONENT_PLACEHOLDER_COMMENT = `/*
|
|
10
|
+
* The app doesn't have any components yet.
|
|
11
|
+
*/
|
|
12
|
+
`;
|
|
13
|
+
|
|
8
14
|
/**
|
|
9
15
|
* Generate type definitions for Strapi Components
|
|
10
16
|
*
|
|
@@ -18,10 +24,20 @@ const generateComponentsDefinitions = async (options = {}) => {
|
|
|
18
24
|
|
|
19
25
|
const { components } = strapi;
|
|
20
26
|
|
|
21
|
-
const componentsDefinitions =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
const componentsDefinitions = pipe(
|
|
28
|
+
values,
|
|
29
|
+
sortBy('uid'),
|
|
30
|
+
map((component) => ({
|
|
31
|
+
uid: component.uid,
|
|
32
|
+
definition: models.schema.generateSchemaDefinition(component),
|
|
33
|
+
}))
|
|
34
|
+
)(components);
|
|
35
|
+
|
|
36
|
+
options.logger.debug(`Found ${componentsDefinitions.length} components.`);
|
|
37
|
+
|
|
38
|
+
if (componentsDefinitions.length === 0) {
|
|
39
|
+
return { output: NO_COMPONENT_PLACEHOLDER_COMMENT, stats: {} };
|
|
40
|
+
}
|
|
25
41
|
|
|
26
42
|
const formattedSchemasDefinitions = componentsDefinitions.reduce((acc, def) => {
|
|
27
43
|
acc.push(
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const { factory } = require('typescript');
|
|
4
|
+
const { values, pipe, map, sortBy } = require('lodash/fp');
|
|
4
5
|
|
|
5
6
|
const { models } = require('../common');
|
|
6
7
|
const { emitDefinitions, format, generateSharedExtensionDefinition } = require('../utils');
|
|
7
8
|
|
|
9
|
+
const NO_CONTENT_TYPE_PLACEHOLDER_COMMENT = `/*
|
|
10
|
+
* The app doesn't have any content-types yet.
|
|
11
|
+
*/
|
|
12
|
+
`;
|
|
13
|
+
|
|
8
14
|
/**
|
|
9
15
|
* Generate type definitions for Strapi Content-Types
|
|
10
16
|
*
|
|
@@ -18,10 +24,20 @@ const generateContentTypesDefinitions = async (options = {}) => {
|
|
|
18
24
|
|
|
19
25
|
const { contentTypes } = strapi;
|
|
20
26
|
|
|
21
|
-
const contentTypesDefinitions =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
const contentTypesDefinitions = pipe(
|
|
28
|
+
values,
|
|
29
|
+
sortBy('uid'),
|
|
30
|
+
map((contentType) => ({
|
|
31
|
+
uid: contentType.uid,
|
|
32
|
+
definition: models.schema.generateSchemaDefinition(contentType),
|
|
33
|
+
}))
|
|
34
|
+
)(contentTypes);
|
|
35
|
+
|
|
36
|
+
options.logger.debug(`Found ${contentTypesDefinitions.length} content-types.`);
|
|
37
|
+
|
|
38
|
+
if (contentTypesDefinitions.length === 0) {
|
|
39
|
+
return { output: NO_CONTENT_TYPE_PLACEHOLDER_COMMENT, stats: {} };
|
|
40
|
+
}
|
|
25
41
|
|
|
26
42
|
const formattedSchemasDefinitions = contentTypesDefinitions.reduce((acc, def) => {
|
|
27
43
|
acc.push(
|
package/lib/utils/index.js
CHANGED
|
@@ -7,6 +7,7 @@ const reportDiagnostics = require('./report-diagnostics');
|
|
|
7
7
|
const resolveConfigOptions = require('./resolve-config-options');
|
|
8
8
|
const formatHost = require('./format-host');
|
|
9
9
|
const resolveOutDir = require('./resolve-outdir');
|
|
10
|
+
const resolveOutDirSync = require('./resolve-outdir-sync');
|
|
10
11
|
|
|
11
12
|
module.exports = {
|
|
12
13
|
isUsingTypeScript,
|
|
@@ -16,4 +17,5 @@ module.exports = {
|
|
|
16
17
|
resolveConfigOptions,
|
|
17
18
|
formatHost,
|
|
18
19
|
resolveOutDir,
|
|
20
|
+
resolveOutDirSync,
|
|
19
21
|
};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
'use strict';
|
|
2
4
|
|
|
3
5
|
const fse = require('fs-extra');
|
|
@@ -13,5 +15,5 @@ const getConfigPath = require('./get-config-path');
|
|
|
13
15
|
module.exports = (dir, filename = undefined) => {
|
|
14
16
|
const filePath = getConfigPath(dir, { filename });
|
|
15
17
|
|
|
16
|
-
return fse.pathExistsSync(filePath);
|
|
18
|
+
return filePath ? fse.pathExistsSync(filePath) : false;
|
|
17
19
|
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const resolveConfigOptions = require('./resolve-config-options');
|
|
5
|
+
const isUsingTypescriptSync = require('./is-using-typescript-sync');
|
|
6
|
+
|
|
7
|
+
const DEFAULT_TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
8
|
+
/**
|
|
9
|
+
* Gets the outDir value from config file (tsconfig)
|
|
10
|
+
* @param {string} dir
|
|
11
|
+
* @param {string | undefined} configFilename
|
|
12
|
+
* @returns {string | undefined}
|
|
13
|
+
*/
|
|
14
|
+
module.exports = (dir, configFilename = DEFAULT_TS_CONFIG_FILENAME) => {
|
|
15
|
+
return isUsingTypescriptSync(dir)
|
|
16
|
+
? resolveConfigOptions(path.join(dir, configFilename)).options.outDir
|
|
17
|
+
: undefined;
|
|
18
|
+
};
|
package/package.json
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/typescript-utils",
|
|
3
|
-
"version": "0.0.0-experimental.
|
|
3
|
+
"version": "0.0.0-experimental.ac6c76c235167c88527fdfe0ec095479acc4ed86",
|
|
4
4
|
"description": "Typescript support for Strapi",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"strapi",
|
|
7
7
|
"generators"
|
|
8
8
|
],
|
|
9
|
+
"homepage": "https://strapi.io",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/strapi/strapi/issues"
|
|
12
|
+
},
|
|
9
13
|
"repository": {
|
|
10
14
|
"type": "git",
|
|
11
|
-
"url": "
|
|
15
|
+
"url": "https://github.com/strapi/strapi.git",
|
|
12
16
|
"directory": "packages/utils/typescript"
|
|
13
17
|
},
|
|
14
18
|
"license": "SEE LICENSE IN LICENSE",
|
|
@@ -36,18 +40,17 @@
|
|
|
36
40
|
},
|
|
37
41
|
"dependencies": {
|
|
38
42
|
"chalk": "4.1.2",
|
|
39
|
-
"cli-table3": "0.6.
|
|
40
|
-
"fs-extra": "11.
|
|
41
|
-
"lodash": "4.
|
|
42
|
-
"prettier": "3.
|
|
43
|
-
"typescript": "5.
|
|
43
|
+
"cli-table3": "0.6.5",
|
|
44
|
+
"fs-extra": "11.3.4",
|
|
45
|
+
"lodash": "4.18.1",
|
|
46
|
+
"prettier": "3.3.3",
|
|
47
|
+
"typescript": "5.4.5"
|
|
44
48
|
},
|
|
45
49
|
"devDependencies": {
|
|
46
50
|
"@types/fs-extra": "11.0.4"
|
|
47
51
|
},
|
|
48
52
|
"engines": {
|
|
49
|
-
"node": ">=
|
|
53
|
+
"node": ">=20.0.0 <=26.x.x",
|
|
50
54
|
"npm": ">=6.0.0"
|
|
51
|
-
}
|
|
52
|
-
"gitHead": "fb442e5e12dd3f611303691bf85a249520ba348b"
|
|
55
|
+
}
|
|
53
56
|
}
|
package/.eslintignore
DELETED
package/.eslintrc.js
DELETED