@strapi/typescript-utils 0.0.0-next.ce84fada19d58a7dfbdd553035e6558f8befcba4 → 0.0.0-next.ce8ed0c8aa03361c3009f41af96f742be028ffd9

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.
@@ -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 [min, max] = definition.members;
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
- // Min
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
- expect(modifiers[0].typeArguments[0].members[1].kind).toBe(
844
- ts.SyntaxKind.PropertyDeclaration
845
- );
846
- expect(modifiers[0].typeArguments[0].members[1].name.escapedText).toBe('maxLength');
847
- expect(modifiers[0].typeArguments[0].members[1].type.kind).toBe(
848
- ts.SyntaxKind.NumericLiteral
849
- );
850
- expect(modifiers[0].typeArguments[0].members[1].type.text).toBe('12');
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
 
@@ -247,13 +247,13 @@ describe('Utils', () => {
247
247
  expect(objectNode.members).toHaveLength(2);
248
248
 
249
249
  expect(objectNode.members[0].kind).toBe(ts.SyntaxKind.PropertyDeclaration);
250
- expect(objectNode.members[0].name.escapedText).toBe('foo');
251
- expect(objectNode.members[0].type.kind).toBe(ts.SyntaxKind.StringLiteral);
252
- expect(objectNode.members[0].type.text).toBe('bar');
250
+ expect(objectNode.members[0].name.escapedText).toBe('bar');
251
+ expect(objectNode.members[0].type.kind).toBe(ts.SyntaxKind.TrueKeyword);
253
252
 
254
253
  expect(objectNode.members[1].kind).toBe(ts.SyntaxKind.PropertyDeclaration);
255
- expect(objectNode.members[1].name.escapedText).toBe('bar');
256
- expect(objectNode.members[1].type.kind).toBe(ts.SyntaxKind.TrueKeyword);
254
+ expect(objectNode.members[1].name.escapedText).toBe('foo');
255
+ expect(objectNode.members[1].type.kind).toBe(ts.SyntaxKind.StringLiteral);
256
+ expect(objectNode.members[1].type.text).toBe('bar');
257
257
  });
258
258
 
259
259
  test('Object', () => {
@@ -262,20 +262,20 @@ describe('Utils', () => {
262
262
  expect(node.kind).toBe(ts.SyntaxKind.TypeLiteral);
263
263
  expect(node.members).toHaveLength(2);
264
264
 
265
- const [firstMember, secondMember] = node.members;
265
+ const [barMember, fooMember] = node.members;
266
266
 
267
- expect(firstMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
268
- expect(firstMember.name.escapedText).toBe('foo');
269
- expect(firstMember.type.kind).toBe(ts.SyntaxKind.TupleType);
270
- expect(firstMember.type.elements).toHaveLength(3);
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);
267
+ expect(barMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
268
+ expect(barMember.name.escapedText).toBe('bar');
269
+ expect(barMember.type.kind).toBe(ts.SyntaxKind.LiteralType);
270
+ expect(barMember.type.literal).toBe(ts.SyntaxKind.NullKeyword);
274
271
 
275
- expect(secondMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
276
- expect(secondMember.name.escapedText).toBe('bar');
277
- expect(secondMember.type.kind).toBe(ts.SyntaxKind.LiteralType);
278
- expect(secondMember.type.literal).toBe(ts.SyntaxKind.NullKeyword);
272
+ expect(fooMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
273
+ expect(fooMember.name.escapedText).toBe('foo');
274
+ expect(fooMember.type.kind).toBe(ts.SyntaxKind.TupleType);
275
+ expect(fooMember.type.elements).toHaveLength(3);
276
+ expect(fooMember.type.elements[0].kind).toBe(ts.SyntaxKind.StringLiteral);
277
+ expect(fooMember.type.elements[1].kind).toBe(ts.SyntaxKind.TrueKeyword);
278
+ expect(fooMember.type.elements[2].kind).toBe(ts.SyntaxKind.FirstLiteralToken);
279
279
  });
280
280
 
281
281
  test('Object with complex keys', () => {
@@ -284,19 +284,19 @@ describe('Utils', () => {
284
284
  expect(node.kind).toBe(ts.SyntaxKind.TypeLiteral);
285
285
  expect(node.members).toHaveLength(2);
286
286
 
287
- const [firstMember, secondMember] = node.members;
287
+ const [fooBar, fooDashBar] = node.members;
288
288
 
289
- expect(firstMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
290
- expect(firstMember.name.kind).toBe(ts.SyntaxKind.StringLiteral);
291
- expect(firstMember.name.text).toBe('foo-bar');
292
- expect(firstMember.type.kind).toBe(ts.SyntaxKind.StringLiteral);
293
- expect(firstMember.type.text).toBe('foobar');
289
+ expect(fooBar.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
290
+ expect(fooBar.name.kind).toBe(ts.SyntaxKind.Identifier);
291
+ expect(fooBar.name.escapedText).toBe('foo');
292
+ expect(fooBar.type.kind).toBe(ts.SyntaxKind.StringLiteral);
293
+ expect(fooBar.type.text).toBe('bar');
294
294
 
295
- expect(secondMember.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
296
- expect(secondMember.name.kind).toBe(ts.SyntaxKind.Identifier);
297
- expect(secondMember.name.escapedText).toBe('foo');
298
- expect(secondMember.type.kind).toBe(ts.SyntaxKind.StringLiteral);
299
- expect(secondMember.type.text).toBe('bar');
295
+ expect(fooDashBar.kind).toBe(ts.SyntaxKind.PropertyDeclaration);
296
+ expect(fooDashBar.name.kind).toBe(ts.SyntaxKind.StringLiteral);
297
+ expect(fooDashBar.name.text).toBe('foo-bar');
298
+ expect(fooDashBar.type.kind).toBe(ts.SyntaxKind.StringLiteral);
299
+ expect(fooDashBar.type.text).toBe('foobar');
300
300
  });
301
301
 
302
302
  test('Invalid data type supplied (function)', () => {
@@ -18,9 +18,9 @@ module.exports = {
18
18
  },
19
19
 
20
20
  generateImportDefinition() {
21
- const formattedImports = imports.map((key) =>
22
- factory.createImportSpecifier(false, undefined, factory.createIdentifier(key))
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).map(([attributeName, attribute]) => {
26
- return attributeToPropertySignature(schema, attributeName, attribute);
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,
@@ -111,7 +111,7 @@ const toTypeLiteral = (data) => {
111
111
  throw new Error(`Cannot convert to object literal. Unknown type "${typeof data}"`);
112
112
  }
113
113
 
114
- const entries = Object.entries(data);
114
+ const entries = Object.entries(data).sort((a, b) => a[0].localeCompare(b[0]));
115
115
 
116
116
  const props = entries.reduce((acc, [key, value]) => {
117
117
  // 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 = Object.values(components).map((contentType) => ({
22
- uid: contentType.uid,
23
- definition: models.schema.generateSchemaDefinition(contentType),
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 = Object.values(contentTypes).map((contentType) => ({
22
- uid: contentType.uid,
23
- definition: models.schema.generateSchemaDefinition(contentType),
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(
@@ -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
  };
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/typescript-utils",
3
- "version": "0.0.0-next.ce84fada19d58a7dfbdd553035e6558f8befcba4",
3
+ "version": "0.0.0-next.ce8ed0c8aa03361c3009f41af96f742be028ffd9",
4
4
  "description": "Typescript support for Strapi",
5
5
  "keywords": [
6
6
  "strapi",
@@ -39,15 +39,14 @@
39
39
  "cli-table3": "0.6.5",
40
40
  "fs-extra": "11.2.0",
41
41
  "lodash": "4.17.21",
42
- "prettier": "3.2.5",
43
- "typescript": "5.3.2"
42
+ "prettier": "3.3.3",
43
+ "typescript": "5.4.4"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@types/fs-extra": "11.0.4"
47
47
  },
48
48
  "engines": {
49
- "node": ">=18.0.0 <=20.x.x",
49
+ "node": ">=18.0.0 <=22.x.x",
50
50
  "npm": ">=6.0.0"
51
- },
52
- "gitHead": "ce84fada19d58a7dfbdd553035e6558f8befcba4"
51
+ }
53
52
  }