@strapi/typescript-utils 4.2.0-beta.3 → 4.3.0-beta.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,155 @@
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
+ isArray,
15
+ isBoolean,
16
+ propEq,
17
+ } = require('lodash/fp');
18
+
19
+ /**
20
+ * Get all components and content-types in a Strapi application
21
+ *
22
+ * @param {Strapi} strapi
23
+ * @returns {object}
24
+ */
25
+ const getAllStrapiSchemas = strapi => ({ ...strapi.contentTypes, ...strapi.components });
26
+
27
+ /**
28
+ * Extract a valid interface name from a schema uid
29
+ *
30
+ * @param {string} uid
31
+ * @returns {string}
32
+ */
33
+ const getSchemaInterfaceName = pipe(replace(/(:.)/, ' '), camelCase, upperFirst);
34
+
35
+ /**
36
+ * Get the parent type name to extend based on the schema's nature
37
+ *
38
+ * @param {object} schema
39
+ * @returns {string}
40
+ */
41
+ const getSchemaExtendsTypeName = schema => {
42
+ const base = getSchemaModelType(schema);
43
+
44
+ return upperFirst(base) + 'Schema';
45
+ };
46
+
47
+ const getSchemaModelType = schema => {
48
+ const { modelType, kind } = schema;
49
+
50
+ // Components
51
+ if (modelType === 'component') {
52
+ return 'component';
53
+ }
54
+
55
+ // Content-Types
56
+ else if (modelType === 'contentType') {
57
+ return kind;
58
+ }
59
+
60
+ return null;
61
+ };
62
+
63
+ /**
64
+ * Get a type node based on a type and its params
65
+ *
66
+ * @param {string} typeName
67
+ * @param {ts.TypeNode[]} [params]
68
+ * @returns
69
+ */
70
+ const getTypeNode = (typeName, params = []) => {
71
+ return factory.createTypeReferenceNode(factory.createIdentifier(typeName), params);
72
+ };
73
+
74
+ /**
75
+ * Transform a regular JavaScript object or scalar value into a literal expression
76
+ * @param data
77
+ * @returns {ts.TypeNode}
78
+ */
79
+ const toTypeLiteral = data => {
80
+ if (isUndefined(data)) {
81
+ return factory.createLiteralTypeNode(ts.SyntaxKind.UndefinedKeyword);
82
+ }
83
+
84
+ if (isNull(data)) {
85
+ return factory.createLiteralTypeNode(ts.SyntaxKind.NullKeyword);
86
+ }
87
+
88
+ if (isString(data)) {
89
+ return factory.createStringLiteral(data, true);
90
+ }
91
+
92
+ if (isNumber(data)) {
93
+ return factory.createNumericLiteral(data);
94
+ }
95
+
96
+ if (isBoolean(data)) {
97
+ return data ? factory.createTrue() : factory.createFalse();
98
+ }
99
+
100
+ if (isArray(data)) {
101
+ return factory.createTupleTypeNode(data.map(item => toTypeLiteral(item)));
102
+ }
103
+
104
+ if (typeof data !== 'object') {
105
+ throw new Error(`Cannot convert to object literal. Unknown type "${typeof data}"`);
106
+ }
107
+
108
+ const entries = Object.entries(data);
109
+
110
+ const props = entries.reduce((acc, [key, value]) => {
111
+ // Handle keys such as content-type-builder & co.
112
+ const identifier = key.includes('-')
113
+ ? factory.createStringLiteral(key, true)
114
+ : factory.createIdentifier(key);
115
+
116
+ return [
117
+ ...acc,
118
+ factory.createPropertyDeclaration(
119
+ undefined,
120
+ undefined,
121
+ identifier,
122
+ undefined,
123
+ toTypeLiteral(value)
124
+ ),
125
+ ];
126
+ }, []);
127
+
128
+ return factory.createTypeLiteralNode(props);
129
+ };
130
+
131
+ /**
132
+ * Get the number of attributes generated for a given schema definition
133
+ *
134
+ * @param {ts.TypeNode} definition
135
+ * @returns {number | null}
136
+ */
137
+ const getDefinitionAttributesCount = definition => {
138
+ const attributesNode = definition.members.find(propEq('name.escapedText', 'attributes'));
139
+
140
+ if (!attributesNode) {
141
+ return null;
142
+ }
143
+
144
+ return attributesNode.type.members.length;
145
+ };
146
+
147
+ module.exports = {
148
+ getAllStrapiSchemas,
149
+ getSchemaInterfaceName,
150
+ getSchemaExtendsTypeName,
151
+ getSchemaModelType,
152
+ getDefinitionAttributesCount,
153
+ getTypeNode,
154
+ toTypeLiteral,
155
+ };
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.2.0-beta.3",
3
+ "version": "4.3.0-beta.2",
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": "c4addbad6ecbc8ef7633bbba3806f3b0a2ae5f49"
38
+ "gitHead": "42aba356ad1b0751584d3b375e83baa4a2c18f65"
36
39
  }
File without changes
File without changes