@strapi/typescript-utils 0.0.0-next.e3b4cdeebf6e9b0cd5575ff80b8a86715d44ce98 → 0.0.0-next.e6eaa3d0563c85f80fd88b258df70a55c057096e

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.
@@ -1,7 +1,122 @@
1
1
  'use strict';
2
2
 
3
- const generateSchemasDefinitions = require('./schemas');
3
+ const path = require('path');
4
+ const chalk = require('chalk');
4
5
 
5
- module.exports = {
6
- generateSchemasDefinitions,
6
+ const { TYPES_ROOT_DIR, GENERATED_OUT_DIR } = require('./constants');
7
+ const { saveDefinitionToFileSystem, createLogger, timer } = require('./utils');
8
+ const generateContentTypesDefinitions = require('./content-types');
9
+ const generateComponentsDefinitions = require('./components');
10
+
11
+ const GENERATORS = {
12
+ contentTypes: generateContentTypesDefinitions,
13
+ components: generateComponentsDefinitions,
7
14
  };
15
+
16
+ /**
17
+ * @typedef GenerateConfig
18
+ *
19
+ * @property {object} strapi
20
+ * @property {boolean} pwd
21
+ * @property {object} [artifacts]
22
+ * @property {boolean} [artifacts.contentTypes]
23
+ * @property {boolean} [artifacts.components]
24
+ * @property {boolean} [artifacts.services]
25
+ * @property {boolean} [artifacts.controllers]
26
+ * @property {boolean} [artifacts.policies]
27
+ * @property {boolean} [artifacts.middlewares]
28
+ * @property {object} [logger]
29
+ * @property {boolean} [logger.silent]
30
+ * @property {boolean} [logger.debug]
31
+ * @property {boolean} [logger.verbose]
32
+ */
33
+
34
+ /**
35
+ * Generate types definitions based on the given configuration
36
+ *
37
+ * @param {GenerateConfig} [config]
38
+ */
39
+ const generate = async (config = {}) => {
40
+ const { pwd, rootDir = TYPES_ROOT_DIR, strapi, artifacts = {}, logger: loggerConfig } = config;
41
+ const reports = {};
42
+ const logger = createLogger(loggerConfig);
43
+ const psTimer = timer().start();
44
+
45
+ const registryPwd = path.join(pwd, rootDir, GENERATED_OUT_DIR);
46
+ const generatorConfig = { strapi, pwd: registryPwd, logger };
47
+
48
+ const returnWithMessage = () => {
49
+ const nbWarnings = chalk.yellow(`${logger.warnings} warning(s)`);
50
+ const nbErrors = chalk.red(`${logger.errors} error(s)`);
51
+
52
+ const status = logger.errors > 0 ? chalk.red('errored') : chalk.green('completed successfully');
53
+
54
+ psTimer.end();
55
+
56
+ logger.info(`The task ${status} with ${nbWarnings} and ${nbErrors} in ${psTimer.duration}s.`);
57
+
58
+ return reports;
59
+ };
60
+
61
+ const enabledArtifacts = Object.keys(artifacts).filter((p) => artifacts[p] === true);
62
+
63
+ logger.info('Starting the type generation process');
64
+ logger.debug(`Enabled artifacts: ${enabledArtifacts.join(', ')}`);
65
+
66
+ for (const artifact of enabledArtifacts) {
67
+ const boldArtifact = chalk.bold(artifact); // used for log messages
68
+
69
+ logger.info(`Generating types for ${boldArtifact}`);
70
+
71
+ if (artifact in GENERATORS) {
72
+ const generator = GENERATORS[artifact];
73
+
74
+ try {
75
+ const artifactGenTimer = timer().start();
76
+
77
+ reports[artifact] = await generator(generatorConfig);
78
+
79
+ artifactGenTimer.end();
80
+
81
+ logger.debug(`Generated ${boldArtifact} in ${artifactGenTimer.duration}s`);
82
+ } catch (e) {
83
+ logger.error(
84
+ `Failed to generate types for ${boldArtifact}: ${e.message ?? e.toString()}. Exiting`
85
+ );
86
+ return returnWithMessage();
87
+ }
88
+ } else {
89
+ logger.warn(`The types generator for ${boldArtifact} is not implemented, skipping`);
90
+ }
91
+ }
92
+
93
+ for (const artifact of Object.keys(reports)) {
94
+ const boldArtifact = chalk.bold(artifact); // used for log messages
95
+
96
+ const artifactFsTimer = timer().start();
97
+
98
+ const report = reports[artifact];
99
+ const filename = `${artifact}.d.ts`;
100
+
101
+ try {
102
+ const outPath = await saveDefinitionToFileSystem(registryPwd, filename, report.output);
103
+ const relativeOutPath = path.relative(__dirname, outPath);
104
+
105
+ artifactFsTimer.end();
106
+
107
+ logger.info(`Saved ${boldArtifact} types in ${chalk.bold(relativeOutPath)}`);
108
+ logger.debug(`Saved ${boldArtifact} in ${artifactFsTimer.duration}s`);
109
+ } catch (e) {
110
+ logger.error(
111
+ `An error occurred while saving ${boldArtifact} types to the filesystem: ${
112
+ e.message ?? e.toString()
113
+ }. Exiting`
114
+ );
115
+ return returnWithMessage();
116
+ }
117
+ }
118
+
119
+ return returnWithMessage();
120
+ };
121
+
122
+ module.exports = { generate };
@@ -0,0 +1,211 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const assert = require('assert');
5
+ const ts = require('typescript');
6
+ const prettier = require('prettier');
7
+ const fse = require('fs-extra');
8
+ const chalk = require('chalk');
9
+
10
+ const { factory } = ts;
11
+
12
+ /**
13
+ * Aggregate the given TypeScript nodes into a single string
14
+ *
15
+ * @param {ts.Node[]} definitions
16
+ * @return {string}
17
+ */
18
+ const emitDefinitions = (definitions) => {
19
+ const nodeArray = factory.createNodeArray(definitions);
20
+
21
+ const sourceFile = ts.createSourceFile(
22
+ 'placeholder.ts',
23
+ '',
24
+ ts.ScriptTarget.ESNext,
25
+ true,
26
+ ts.ScriptKind.TS
27
+ );
28
+
29
+ const printer = ts.createPrinter({ omitTrailingSemicolon: true });
30
+
31
+ return printer.printList(ts.ListFormat.MultiLine, nodeArray, sourceFile);
32
+ };
33
+
34
+ /**
35
+ * Save the given string representation of TS nodes in a file
36
+ * If the given directory doesn't exist, it'll be created automatically
37
+ *
38
+ * @param {string} dir
39
+ * @param {string} file
40
+ * @param {string} content
41
+ *
42
+ * @return {Promise<string>} The path of the created file
43
+ */
44
+ const saveDefinitionToFileSystem = async (dir, file, content) => {
45
+ const filepath = path.join(dir, file);
46
+
47
+ fse.ensureDirSync(dir);
48
+ await fse.writeFile(filepath, content);
49
+
50
+ return filepath;
51
+ };
52
+
53
+ /**
54
+ * Format the given definitions.
55
+ * Uses the existing config if one is defined in the project.
56
+ *
57
+ * @param {string} content
58
+ * @returns {Promise<string>}
59
+ */
60
+ const format = async (content) => {
61
+ const configFile = await prettier.resolveConfigFile();
62
+ const config = configFile
63
+ ? await prettier.resolveConfig(configFile)
64
+ : // Default config
65
+ {
66
+ singleQuote: true,
67
+ useTabs: false,
68
+ tabWidth: 2,
69
+ };
70
+
71
+ Object.assign(config, { parser: 'typescript' });
72
+
73
+ return prettier.format(content, config);
74
+ };
75
+
76
+ /**
77
+ * Generate the extension block for a shared component from strapi/strapi
78
+ *
79
+ * @param {string} registry The registry to extend
80
+ * @param {Array<{ uid: string; definition: ts.TypeNode }>} definitions
81
+ * @returns {ts.ModuleDeclaration}
82
+ */
83
+ const generateSharedExtensionDefinition = (registry, definitions) => {
84
+ const properties = definitions.map(({ uid, definition }) =>
85
+ factory.createPropertySignature(
86
+ undefined,
87
+ factory.createStringLiteral(uid, true),
88
+ undefined,
89
+ factory.createTypeReferenceNode(factory.createIdentifier(definition.name.escapedText))
90
+ )
91
+ );
92
+
93
+ return factory.createModuleDeclaration(
94
+ [factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
95
+ factory.createStringLiteral('@strapi/types', true),
96
+ factory.createModuleBlock([
97
+ factory.createModuleDeclaration(
98
+ [factory.createModifier(ts.SyntaxKind.ExportKeyword)],
99
+ factory.createIdentifier('Shared'),
100
+ factory.createModuleBlock(
101
+ properties.length > 0
102
+ ? [
103
+ factory.createInterfaceDeclaration(
104
+ [factory.createModifier(ts.SyntaxKind.ExportKeyword)],
105
+ factory.createIdentifier(registry),
106
+ undefined,
107
+ undefined,
108
+ properties
109
+ ),
110
+ ]
111
+ : []
112
+ )
113
+ ),
114
+ ]),
115
+ ts.NodeFlags.ExportContext
116
+ );
117
+ };
118
+
119
+ const createLogger = (options = {}) => {
120
+ const { silent = false, debug = false } = options;
121
+
122
+ const state = { errors: 0, warning: 0 };
123
+
124
+ return {
125
+ get warnings() {
126
+ return state.warning;
127
+ },
128
+
129
+ get errors() {
130
+ return state.errors;
131
+ },
132
+
133
+ debug(...args) {
134
+ if (silent || !debug) {
135
+ return;
136
+ }
137
+
138
+ console.log(chalk.cyan(`[DEBUG]\t[${new Date().toISOString()}] (Typegen)`), ...args);
139
+ },
140
+
141
+ info(...args) {
142
+ if (silent) {
143
+ return;
144
+ }
145
+
146
+ console.info(chalk.blue(`[INFO]\t[${new Date().toISOString()}] (Typegen)`), ...args);
147
+ },
148
+
149
+ warn(...args) {
150
+ state.warning += 1;
151
+
152
+ if (silent) {
153
+ return;
154
+ }
155
+
156
+ console.warn(chalk.yellow(`[WARN]\t[${new Date().toISOString()}] (Typegen)`), ...args);
157
+ },
158
+
159
+ error(...args) {
160
+ state.errors += 1;
161
+
162
+ if (silent) {
163
+ return;
164
+ }
165
+
166
+ console.error(chalk.red(`[ERROR]\t[${new Date().toISOString()}] (Typegen)`), ...args);
167
+ },
168
+ };
169
+ };
170
+
171
+ const timer = () => {
172
+ const state = {
173
+ start: null,
174
+ end: null,
175
+ };
176
+
177
+ return {
178
+ start() {
179
+ assert(state.start === null, 'The timer has already been started');
180
+ assert(state.end === null, 'The timer has already been ended');
181
+
182
+ state.start = Date.now();
183
+
184
+ return this;
185
+ },
186
+
187
+ end() {
188
+ assert(state.start !== null, 'The timer needs to be started before ending it');
189
+ assert(state.end === null, 'The timer has already been ended');
190
+
191
+ state.end = Date.now();
192
+
193
+ return this;
194
+ },
195
+
196
+ get duration() {
197
+ assert(state.start !== null, 'The timer has not been started');
198
+
199
+ return ((state.end ?? Date.now) - state.start) / 1000;
200
+ },
201
+ };
202
+ };
203
+
204
+ module.exports = {
205
+ emitDefinitions,
206
+ saveDefinitionToFileSystem,
207
+ format,
208
+ generateSharedExtensionDefinition,
209
+ createLogger,
210
+ timer,
211
+ };
package/package.json CHANGED
@@ -1,11 +1,16 @@
1
1
  {
2
2
  "name": "@strapi/typescript-utils",
3
- "version": "0.0.0-next.e3b4cdeebf6e9b0cd5575ff80b8a86715d44ce98",
3
+ "version": "0.0.0-next.e6eaa3d0563c85f80fd88b258df70a55c057096e",
4
4
  "description": "Typescript support for Strapi",
5
5
  "keywords": [
6
6
  "strapi",
7
7
  "generators"
8
8
  ],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git://github.com/strapi/strapi.git",
12
+ "directory": "packages/utils/typescript"
13
+ },
9
14
  "license": "SEE LICENSE IN LICENSE",
10
15
  "author": {
11
16
  "name": "Strapi Solutions SAS",
@@ -32,14 +37,14 @@
32
37
  "dependencies": {
33
38
  "chalk": "4.1.2",
34
39
  "cli-table3": "0.6.2",
35
- "fs-extra": "10.0.1",
40
+ "fs-extra": "10.0.0",
36
41
  "lodash": "4.17.21",
37
42
  "prettier": "2.8.4",
38
- "typescript": "5.0.4"
43
+ "typescript": "5.2.2"
39
44
  },
40
45
  "engines": {
41
- "node": ">=14.19.1 <=18.x.x",
46
+ "node": ">=18.0.0 <=20.x.x",
42
47
  "npm": ">=6.0.0"
43
48
  },
44
- "gitHead": "e3b4cdeebf6e9b0cd5575ff80b8a86715d44ce98"
49
+ "gitHead": "e6eaa3d0563c85f80fd88b258df70a55c057096e"
45
50
  }
@@ -1,20 +1,19 @@
1
1
  {
2
- "$schema": "https://json.schemastore.org/tsconfig",
3
-
4
- "compilerOptions": {
5
- "module": "ES2020",
6
- "moduleResolution": "node",
7
- "lib": ["ES2020", "DOM"],
8
- "target": "ES5",
9
-
10
- "jsx": "react",
11
- "sourceMap": true,
12
- "incremental": true,
13
-
14
- "allowJs": true,
15
- "allowSyntheticDefaultImports": true,
16
- "resolveJsonModule": true,
17
- "noEmit": true,
18
- "skipLibCheck": true
19
- }
20
- }
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "compilerOptions": {
4
+ "target": "ESNext",
5
+ "module": "ESNext",
6
+ "moduleResolution": "Bundler",
7
+ "useDefineForClassFields": true,
8
+ "lib": ["DOM", "DOM.Iterable", "ESNext"],
9
+ "allowJs": false,
10
+ "skipLibCheck": true,
11
+ "esModuleInterop": true,
12
+ "allowSyntheticDefaultImports": true,
13
+ "strict": true,
14
+ "forceConsistentCasingInFileNames": true,
15
+ "resolveJsonModule": true,
16
+ "noEmit": true,
17
+ "jsx": "react-jsx"
18
+ }
19
+ }
@@ -1,19 +1,20 @@
1
1
  {
2
- "$schema": "https://json.schemastore.org/tsconfig",
3
-
4
- "compilerOptions": {
5
- "module": "CommonJS",
6
- "moduleResolution": "Node",
7
- "lib": ["ES2020"],
8
- "target": "ES2019",
2
+ "$schema": "https://json.schemastore.org/tsconfig",
9
3
 
10
- "strict": false,
11
- "skipLibCheck": true,
12
- "forceConsistentCasingInFileNames": true,
4
+ "compilerOptions": {
5
+ "module": "CommonJS",
6
+ "moduleResolution": "Node",
7
+ "lib": ["ES2020"],
8
+ "target": "ES2019",
13
9
 
14
- "incremental": true,
15
- "esModuleInterop": true,
16
- "resolveJsonModule": true,
17
- "noEmitOnError": true
18
- }
19
- }
10
+ "strict": false,
11
+ "skipLibCheck": true,
12
+ "forceConsistentCasingInFileNames": true,
13
+
14
+ "incremental": true,
15
+ "esModuleInterop": true,
16
+ "resolveJsonModule": true,
17
+ "noEmitOnError": true,
18
+ "noImplicitThis": true
19
+ }
20
+ }
@@ -1,108 +0,0 @@
1
- 'use strict';
2
-
3
- jest.mock('../../../generators/schemas/utils', () => ({
4
- getSchemaInterfaceName: jest.fn(),
5
- }));
6
-
7
- const ts = require('typescript');
8
- const { get } = require('lodash/fp');
9
-
10
- const { generateGlobalDefinition } = require('../../../generators/schemas/global');
11
- const { getSchemaInterfaceName } = require('../../../generators/schemas/utils');
12
-
13
- const getSchemasInterfaceNode = get('body.statements[0].body.statements[0]');
14
-
15
- describe('Global', () => {
16
- afterAll(() => {
17
- jest.resetAllMocks();
18
- });
19
-
20
- const assertGlobalNodeStructure = (node) => {
21
- // "declare global"
22
- expect(node.kind).toBe(ts.SyntaxKind.ModuleDeclaration);
23
- expect(node.modifiers).toHaveLength(1);
24
- expect(node.modifiers[0].kind).toBe(ts.SyntaxKind.DeclareKeyword);
25
- expect(node.name.originalKeywordKind).toBe(ts.SyntaxKind.GlobalKeyword);
26
- expect(node.name.escapedText).toBe('global');
27
-
28
- // "namespace Strapi"
29
- const [strapiNamespace] = node.body.statements;
30
-
31
- expect(strapiNamespace.kind).toBe(ts.SyntaxKind.ModuleDeclaration);
32
- expect(strapiNamespace.name.kind).toBe(ts.SyntaxKind.Identifier);
33
- expect(strapiNamespace.name.escapedText).toBe('Strapi');
34
-
35
- // "interface Schemas"
36
- const [schemasInterface] = strapiNamespace.body.statements;
37
-
38
- expect(schemasInterface.kind).toBe(ts.SyntaxKind.InterfaceDeclaration);
39
- expect(schemasInterface.name.escapedText).toBe('Schemas');
40
- };
41
-
42
- describe('Generate Global Definition', () => {
43
- beforeEach(() => {
44
- jest.resetAllMocks();
45
- });
46
-
47
- test('With empty definition', () => {
48
- const definitions = [];
49
-
50
- const globalNode = generateGlobalDefinition(definitions);
51
-
52
- assertGlobalNodeStructure(globalNode);
53
-
54
- expect(getSchemaInterfaceName).not.toHaveBeenCalled();
55
-
56
- const schemasNode = getSchemasInterfaceNode(globalNode);
57
-
58
- expect(schemasNode.members).toHaveLength(0);
59
- });
60
-
61
- test('With no definition', () => {
62
- const globalNode = generateGlobalDefinition();
63
-
64
- assertGlobalNodeStructure(globalNode);
65
-
66
- expect(getSchemaInterfaceName).not.toHaveBeenCalled();
67
-
68
- const schemasNode = getSchemasInterfaceNode(globalNode);
69
-
70
- expect(schemasNode.members).toHaveLength(0);
71
- });
72
-
73
- test('With multiple definitions', () => {
74
- const definitions = [
75
- { schema: { uid: 'api::foo.foo' } },
76
- { schema: { uid: 'api::bar.bar' } },
77
- { schema: { uid: 'api::foobar.foobar' } },
78
- { schema: { uid: 'default.barfoo' } },
79
- ];
80
-
81
- getSchemaInterfaceName.mockReturnValue('Placeholder');
82
-
83
- const globalNode = generateGlobalDefinition(definitions);
84
-
85
- assertGlobalNodeStructure(globalNode);
86
-
87
- const schemasNode = getSchemasInterfaceNode(globalNode);
88
-
89
- expect(schemasNode.members).toHaveLength(definitions.length);
90
-
91
- definitions.forEach(({ schema }, index) => {
92
- const { uid } = schema;
93
- const node = schemasNode.members[index];
94
-
95
- expect(node.kind).toBe(ts.SyntaxKind.PropertySignature);
96
-
97
- expect(getSchemaInterfaceName).toHaveBeenCalledWith(uid);
98
-
99
- expect(node.name.kind).toBe(ts.SyntaxKind.StringLiteral);
100
- expect(node.name.text).toBe(uid);
101
- expect(node.name.singleQuote).toBeTruthy();
102
-
103
- expect(node.type.kind).toBe(ts.SyntaxKind.TypeReference);
104
- expect(node.type.typeName.escapedText).toBe('Placeholder');
105
- });
106
- });
107
- });
108
- });
@@ -1,37 +0,0 @@
1
- 'use strict';
2
-
3
- const ts = require('typescript');
4
-
5
- const reportDiagnostics = require('../utils/report-diagnostics');
6
- const formatHost = require('../utils/format-host');
7
- const resolveConfigOptions = require('../utils/resolve-config-options');
8
-
9
- /**
10
- * Prints a diagnostic every time the watch status changes.
11
- * This is mainly for messages like "Starting compilation" or "Compilation completed".
12
- */
13
- const reportWatchStatusChanged = (diagnostic) => {
14
- console.info(ts.formatDiagnostic(diagnostic, formatHost));
15
- };
16
-
17
- module.exports = {
18
- run(configPath) {
19
- const createProgram = ts.createSemanticDiagnosticsBuilderProgram;
20
-
21
- const { fileNames, options, projectReferences, watchOptions } =
22
- resolveConfigOptions(configPath);
23
-
24
- const host = ts.createWatchCompilerHost(
25
- fileNames,
26
- options,
27
- ts.sys,
28
- createProgram,
29
- reportDiagnostics,
30
- reportWatchStatusChanged,
31
- projectReferences,
32
- watchOptions
33
- );
34
-
35
- ts.createWatchProgram(host);
36
- },
37
- };
@@ -1,67 +0,0 @@
1
- 'use strict';
2
-
3
- /* eslint-disable no-bitwise */
4
-
5
- const ts = require('typescript');
6
- const { factory } = require('typescript');
7
-
8
- const { getSchemaInterfaceName } = require('./utils');
9
-
10
- /**
11
- *
12
- * @param {object} schemaDefinition
13
- * @param {ts.InterfaceDeclaration} schemaDefinition.definition
14
- * @param {object} schemaDefinition.schema
15
- */
16
- const schemaDefinitionToPropertySignature = ({ schema }) => {
17
- const { uid } = schema;
18
-
19
- const interfaceTypeName = getSchemaInterfaceName(uid);
20
-
21
- return factory.createPropertySignature(
22
- undefined,
23
- factory.createStringLiteral(uid, true),
24
- undefined,
25
- factory.createTypeReferenceNode(factory.createIdentifier(interfaceTypeName))
26
- );
27
- };
28
-
29
- /**
30
- * Generate the global module augmentation block
31
- *
32
- * @param {Array<{ schema: object; definition: ts.TypeNode }>} schemasDefinitions
33
- * @returns {ts.ModuleDeclaration}
34
- */
35
- const generateGlobalDefinition = (schemasDefinitions = []) => {
36
- const properties = schemasDefinitions.map(schemaDefinitionToPropertySignature);
37
-
38
- return factory.createModuleDeclaration(
39
- [factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
40
- factory.createIdentifier('global'),
41
- factory.createModuleBlock([
42
- factory.createModuleDeclaration(
43
- undefined,
44
- factory.createIdentifier('Strapi'),
45
- factory.createModuleBlock([
46
- factory.createInterfaceDeclaration(
47
- undefined,
48
- factory.createIdentifier('Schemas'),
49
- undefined,
50
- undefined,
51
- properties
52
- ),
53
- ]),
54
- ts.NodeFlags.Namespace |
55
- ts.NodeFlags.ExportContext |
56
- ts.NodeFlags.Ambient |
57
- ts.NodeFlags.ContextFlags
58
- ),
59
- ]),
60
- ts.NodeFlags.ExportContext |
61
- ts.NodeFlags.GlobalAugmentation |
62
- ts.NodeFlags.Ambient |
63
- ts.NodeFlags.ContextFlags
64
- );
65
- };
66
-
67
- module.exports = { generateGlobalDefinition };