@strapi/typescript-utils 0.0.0-next.f45143c5e2a8a9d85691d0abf79a3f42024a0c71 → 0.0.0-next.f4ec69568d980c6fee91ce2ee0f41c138347aa81
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/.eslintignore +1 -0
- package/LICENSE +18 -3
- package/index.d.ts +5 -0
- package/jest.config.js +1 -0
- package/lib/__tests__/generators/schemas/attributes.test.js +273 -123
- package/lib/__tests__/generators/schemas/imports.test.js +18 -16
- package/lib/__tests__/generators/schemas/utils.test.js +5 -59
- package/lib/compile.js +2 -6
- package/lib/compilers/basic.js +12 -4
- package/lib/compilers/index.js +0 -2
- package/lib/generators/common/imports.js +34 -0
- package/lib/generators/common/index.js +9 -0
- package/lib/generators/{schemas → common/models}/attributes.js +65 -41
- package/lib/generators/common/models/index.js +15 -0
- package/lib/generators/common/models/mappers.js +144 -0
- package/lib/generators/{schemas → common/models}/schema.js +15 -9
- package/lib/generators/{schemas → common/models}/utils.js +31 -18
- package/lib/generators/components/index.js +74 -0
- package/lib/generators/constants.js +6 -0
- package/lib/generators/content-types/index.js +74 -0
- package/lib/generators/index.js +118 -3
- package/lib/generators/utils.js +216 -0
- package/lib/index.js +0 -3
- package/package.json +16 -7
- package/tsconfigs/admin.json +18 -19
- package/tsconfigs/server.json +18 -16
- package/lib/__tests__/generators/schemas/global.test.js +0 -108
- package/lib/admin/create-tsconfig-file.js +0 -37
- package/lib/admin/index.js +0 -5
- package/lib/compilers/watch.js +0 -37
- package/lib/generators/schemas/global.js +0 -70
- package/lib/generators/schemas/imports.js +0 -33
- package/lib/generators/schemas/index.js +0 -185
- package/lib/generators/schemas/mappers.js +0 -131
|
@@ -17,13 +17,10 @@ const {
|
|
|
17
17
|
propEq,
|
|
18
18
|
} = require('lodash/fp');
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
* @returns {object}
|
|
25
|
-
*/
|
|
26
|
-
const getAllStrapiSchemas = (strapi) => ({ ...strapi.contentTypes, ...strapi.components });
|
|
20
|
+
const NAMESPACES = {
|
|
21
|
+
Struct: 'Struct',
|
|
22
|
+
Schema: 'Schema',
|
|
23
|
+
};
|
|
27
24
|
|
|
28
25
|
/**
|
|
29
26
|
* Extract a valid interface name from a schema uid
|
|
@@ -53,12 +50,16 @@ const getSchemaModelType = (schema) => {
|
|
|
53
50
|
* Get the parent type name to extend based on the schema's nature
|
|
54
51
|
*
|
|
55
52
|
* @param {object} schema
|
|
56
|
-
* @returns {string}
|
|
53
|
+
* @returns {string|null}
|
|
57
54
|
*/
|
|
58
55
|
const getSchemaExtendsTypeName = (schema) => {
|
|
59
56
|
const base = getSchemaModelType(schema);
|
|
60
57
|
|
|
61
|
-
|
|
58
|
+
if (base === null) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return `${NAMESPACES.Struct}.${upperFirst(base)}Schema`;
|
|
62
63
|
};
|
|
63
64
|
|
|
64
65
|
/**
|
|
@@ -110,7 +111,7 @@ const toTypeLiteral = (data) => {
|
|
|
110
111
|
throw new Error(`Cannot convert to object literal. Unknown type "${typeof data}"`);
|
|
111
112
|
}
|
|
112
113
|
|
|
113
|
-
const entries = Object.entries(data);
|
|
114
|
+
const entries = Object.entries(data).sort((a, b) => a[0].localeCompare(b[0]));
|
|
114
115
|
|
|
115
116
|
const props = entries.reduce((acc, [key, value]) => {
|
|
116
117
|
// Handle keys such as content-type-builder & co.
|
|
@@ -120,13 +121,7 @@ const toTypeLiteral = (data) => {
|
|
|
120
121
|
|
|
121
122
|
return [
|
|
122
123
|
...acc,
|
|
123
|
-
factory.createPropertyDeclaration(
|
|
124
|
-
undefined,
|
|
125
|
-
undefined,
|
|
126
|
-
identifier,
|
|
127
|
-
undefined,
|
|
128
|
-
toTypeLiteral(value)
|
|
129
|
-
),
|
|
124
|
+
factory.createPropertyDeclaration(undefined, identifier, undefined, toTypeLiteral(value)),
|
|
130
125
|
];
|
|
131
126
|
}, []);
|
|
132
127
|
|
|
@@ -149,8 +144,26 @@ const getDefinitionAttributesCount = (definition) => {
|
|
|
149
144
|
return attributesNode.type.members.length;
|
|
150
145
|
};
|
|
151
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Add the Schema.Attribute namespace before the typename
|
|
149
|
+
*
|
|
150
|
+
* @param {string} typeName
|
|
151
|
+
* @returns {string}
|
|
152
|
+
*/
|
|
153
|
+
const withAttributeNamespace = (typeName) => `${NAMESPACES.Schema}.Attribute.${typeName}`;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Add the schema namespace before the typename
|
|
157
|
+
*
|
|
158
|
+
* @param {string} typeName
|
|
159
|
+
* @returns {string}
|
|
160
|
+
*/
|
|
161
|
+
const withSchemaNamespace = (typeName) => `${NAMESPACES.schema}.${typeName}`;
|
|
162
|
+
|
|
152
163
|
module.exports = {
|
|
153
|
-
|
|
164
|
+
NAMESPACES,
|
|
165
|
+
withAttributeNamespace,
|
|
166
|
+
withSchemaNamespace,
|
|
154
167
|
getSchemaInterfaceName,
|
|
155
168
|
getSchemaExtendsTypeName,
|
|
156
169
|
getSchemaModelType,
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { factory } = require('typescript');
|
|
4
|
+
const { pipe, values, sortBy, map } = require('lodash/fp');
|
|
5
|
+
|
|
6
|
+
const { models } = require('../common');
|
|
7
|
+
const { emitDefinitions, format, generateSharedExtensionDefinition } = require('../utils');
|
|
8
|
+
|
|
9
|
+
const NO_COMPONENT_PLACEHOLDER_COMMENT = `/*
|
|
10
|
+
* The app doesn't have any components yet.
|
|
11
|
+
*/
|
|
12
|
+
`;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Generate type definitions for Strapi Components
|
|
16
|
+
*
|
|
17
|
+
* @param {object} [options]
|
|
18
|
+
* @param {object} options.strapi
|
|
19
|
+
* @param {object} options.logger
|
|
20
|
+
* @param {string} options.pwd
|
|
21
|
+
*/
|
|
22
|
+
const generateComponentsDefinitions = async (options = {}) => {
|
|
23
|
+
const { strapi } = options;
|
|
24
|
+
|
|
25
|
+
const { components } = strapi;
|
|
26
|
+
|
|
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
|
+
}
|
|
41
|
+
|
|
42
|
+
const formattedSchemasDefinitions = componentsDefinitions.reduce((acc, def) => {
|
|
43
|
+
acc.push(
|
|
44
|
+
// Definition
|
|
45
|
+
def.definition,
|
|
46
|
+
|
|
47
|
+
// Add a newline between each interface declaration
|
|
48
|
+
factory.createIdentifier('\n')
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
return acc;
|
|
52
|
+
}, []);
|
|
53
|
+
|
|
54
|
+
const allDefinitions = [
|
|
55
|
+
// Imports
|
|
56
|
+
...models.imports.generateImportDefinition(),
|
|
57
|
+
|
|
58
|
+
// Add a newline after the import statement
|
|
59
|
+
factory.createIdentifier('\n'),
|
|
60
|
+
|
|
61
|
+
// Schemas
|
|
62
|
+
...formattedSchemasDefinitions,
|
|
63
|
+
|
|
64
|
+
// Global
|
|
65
|
+
generateSharedExtensionDefinition('ComponentSchemas', componentsDefinitions),
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
const output = emitDefinitions(allDefinitions);
|
|
69
|
+
const formattedOutput = await format(output);
|
|
70
|
+
|
|
71
|
+
return { output: formattedOutput, stats: {} };
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
module.exports = generateComponentsDefinitions;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { factory } = require('typescript');
|
|
4
|
+
const { values, pipe, map, sortBy } = require('lodash/fp');
|
|
5
|
+
|
|
6
|
+
const { models } = require('../common');
|
|
7
|
+
const { emitDefinitions, format, generateSharedExtensionDefinition } = require('../utils');
|
|
8
|
+
|
|
9
|
+
const NO_CONTENT_TYPE_PLACEHOLDER_COMMENT = `/*
|
|
10
|
+
* The app doesn't have any content-types yet.
|
|
11
|
+
*/
|
|
12
|
+
`;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Generate type definitions for Strapi Content-Types
|
|
16
|
+
*
|
|
17
|
+
* @param {object} [options]
|
|
18
|
+
* @param {object} options.strapi
|
|
19
|
+
* @param {object} options.logger
|
|
20
|
+
* @param {string} options.pwd
|
|
21
|
+
*/
|
|
22
|
+
const generateContentTypesDefinitions = async (options = {}) => {
|
|
23
|
+
const { strapi } = options;
|
|
24
|
+
|
|
25
|
+
const { contentTypes } = strapi;
|
|
26
|
+
|
|
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
|
+
}
|
|
41
|
+
|
|
42
|
+
const formattedSchemasDefinitions = contentTypesDefinitions.reduce((acc, def) => {
|
|
43
|
+
acc.push(
|
|
44
|
+
// Definition
|
|
45
|
+
def.definition,
|
|
46
|
+
|
|
47
|
+
// Add a newline between each interface declaration
|
|
48
|
+
factory.createIdentifier('\n')
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
return acc;
|
|
52
|
+
}, []);
|
|
53
|
+
|
|
54
|
+
const allDefinitions = [
|
|
55
|
+
// Imports
|
|
56
|
+
...models.imports.generateImportDefinition(),
|
|
57
|
+
|
|
58
|
+
// Add a newline after the import statement
|
|
59
|
+
factory.createIdentifier('\n'),
|
|
60
|
+
|
|
61
|
+
// Schemas
|
|
62
|
+
...formattedSchemasDefinitions,
|
|
63
|
+
|
|
64
|
+
// Global
|
|
65
|
+
generateSharedExtensionDefinition('ContentTypeSchemas', contentTypesDefinitions),
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
const output = emitDefinitions(allDefinitions);
|
|
69
|
+
const formattedOutput = await format(output);
|
|
70
|
+
|
|
71
|
+
return { output: formattedOutput, stats: {} };
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
module.exports = generateContentTypesDefinitions;
|
package/lib/generators/index.js
CHANGED
|
@@ -1,7 +1,122 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const chalk = require('chalk');
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
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(process.cwd(), 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,216 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const assert = require('assert');
|
|
5
|
+
const ts = require('typescript');
|
|
6
|
+
const fse = require('fs-extra');
|
|
7
|
+
const chalk = require('chalk');
|
|
8
|
+
|
|
9
|
+
const { factory } = ts;
|
|
10
|
+
|
|
11
|
+
const MODULE_DECLARATION = '@strapi/strapi';
|
|
12
|
+
const PUBLIC_NAMESPACE = 'Public';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Aggregate the given TypeScript nodes into a single string
|
|
16
|
+
*
|
|
17
|
+
* @param {ts.Node[]} definitions
|
|
18
|
+
* @return {string}
|
|
19
|
+
*/
|
|
20
|
+
const emitDefinitions = (definitions) => {
|
|
21
|
+
const nodeArray = factory.createNodeArray(definitions);
|
|
22
|
+
|
|
23
|
+
const sourceFile = ts.createSourceFile(
|
|
24
|
+
'placeholder.ts',
|
|
25
|
+
'',
|
|
26
|
+
ts.ScriptTarget.ESNext,
|
|
27
|
+
true,
|
|
28
|
+
ts.ScriptKind.TS
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const printer = ts.createPrinter({ omitTrailingSemicolon: true });
|
|
32
|
+
|
|
33
|
+
return printer.printList(ts.ListFormat.MultiLine, nodeArray, sourceFile);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Save the given string representation of TS nodes in a file
|
|
38
|
+
* If the given directory doesn't exist, it'll be created automatically
|
|
39
|
+
*
|
|
40
|
+
* @param {string} dir
|
|
41
|
+
* @param {string} file
|
|
42
|
+
* @param {string} content
|
|
43
|
+
*
|
|
44
|
+
* @return {Promise<string>} The path of the created file
|
|
45
|
+
*/
|
|
46
|
+
const saveDefinitionToFileSystem = async (dir, file, content) => {
|
|
47
|
+
const filepath = path.join(dir, file);
|
|
48
|
+
|
|
49
|
+
fse.ensureDirSync(dir);
|
|
50
|
+
await fse.writeFile(filepath, content);
|
|
51
|
+
|
|
52
|
+
return filepath;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Format the given definitions.
|
|
57
|
+
* Uses the existing config if one is defined in the project.
|
|
58
|
+
*
|
|
59
|
+
* @param {string} content
|
|
60
|
+
* @returns {Promise<string>}
|
|
61
|
+
*/
|
|
62
|
+
const format = async (content) => {
|
|
63
|
+
// eslint-disable-next-line node/no-unsupported-features/es-syntax
|
|
64
|
+
const prettier = await import('prettier'); // ESM-only
|
|
65
|
+
|
|
66
|
+
const configFile = await prettier.resolveConfigFile();
|
|
67
|
+
const config = configFile
|
|
68
|
+
? await prettier.resolveConfig(configFile)
|
|
69
|
+
: // Default config
|
|
70
|
+
{
|
|
71
|
+
singleQuote: true,
|
|
72
|
+
useTabs: false,
|
|
73
|
+
tabWidth: 2,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
Object.assign(config, { parser: 'typescript' });
|
|
77
|
+
|
|
78
|
+
return prettier.format(content, config);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Generate the extension block for a shared component from strapi/strapi
|
|
83
|
+
*
|
|
84
|
+
* @param {string} registry The registry to extend
|
|
85
|
+
* @param {Array<{ uid: string; definition: ts.TypeNode }>} definitions
|
|
86
|
+
* @returns {ts.ModuleDeclaration}
|
|
87
|
+
*/
|
|
88
|
+
const generateSharedExtensionDefinition = (registry, definitions) => {
|
|
89
|
+
const properties = definitions.map(({ uid, definition }) =>
|
|
90
|
+
factory.createPropertySignature(
|
|
91
|
+
undefined,
|
|
92
|
+
factory.createStringLiteral(uid, true),
|
|
93
|
+
undefined,
|
|
94
|
+
factory.createTypeReferenceNode(factory.createIdentifier(definition.name.escapedText))
|
|
95
|
+
)
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
return factory.createModuleDeclaration(
|
|
99
|
+
[factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
|
|
100
|
+
factory.createStringLiteral(MODULE_DECLARATION, true),
|
|
101
|
+
factory.createModuleBlock([
|
|
102
|
+
factory.createModuleDeclaration(
|
|
103
|
+
[factory.createModifier(ts.SyntaxKind.ExportKeyword)],
|
|
104
|
+
factory.createIdentifier(PUBLIC_NAMESPACE),
|
|
105
|
+
factory.createModuleBlock(
|
|
106
|
+
properties.length > 0
|
|
107
|
+
? [
|
|
108
|
+
factory.createInterfaceDeclaration(
|
|
109
|
+
[factory.createModifier(ts.SyntaxKind.ExportKeyword)],
|
|
110
|
+
factory.createIdentifier(registry),
|
|
111
|
+
undefined,
|
|
112
|
+
undefined,
|
|
113
|
+
properties
|
|
114
|
+
),
|
|
115
|
+
]
|
|
116
|
+
: []
|
|
117
|
+
)
|
|
118
|
+
),
|
|
119
|
+
]),
|
|
120
|
+
ts.NodeFlags.ExportContext
|
|
121
|
+
);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const createLogger = (options = {}) => {
|
|
125
|
+
const { silent = false, debug = false } = options;
|
|
126
|
+
|
|
127
|
+
const state = { errors: 0, warning: 0 };
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
get warnings() {
|
|
131
|
+
return state.warning;
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
get errors() {
|
|
135
|
+
return state.errors;
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
debug(...args) {
|
|
139
|
+
if (silent || !debug) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
console.log(chalk.cyan(`[DEBUG]\t[${new Date().toISOString()}] (Typegen)`), ...args);
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
info(...args) {
|
|
147
|
+
if (silent) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
console.info(chalk.blue(`[INFO]\t[${new Date().toISOString()}] (Typegen)`), ...args);
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
warn(...args) {
|
|
155
|
+
state.warning += 1;
|
|
156
|
+
|
|
157
|
+
if (silent) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
console.warn(chalk.yellow(`[WARN]\t[${new Date().toISOString()}] (Typegen)`), ...args);
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
error(...args) {
|
|
165
|
+
state.errors += 1;
|
|
166
|
+
|
|
167
|
+
if (silent) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
console.error(chalk.red(`[ERROR]\t[${new Date().toISOString()}] (Typegen)`), ...args);
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const timer = () => {
|
|
177
|
+
const state = {
|
|
178
|
+
start: null,
|
|
179
|
+
end: null,
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
start() {
|
|
184
|
+
assert(state.start === null, 'The timer has already been started');
|
|
185
|
+
assert(state.end === null, 'The timer has already been ended');
|
|
186
|
+
|
|
187
|
+
state.start = Date.now();
|
|
188
|
+
|
|
189
|
+
return this;
|
|
190
|
+
},
|
|
191
|
+
|
|
192
|
+
end() {
|
|
193
|
+
assert(state.start !== null, 'The timer needs to be started before ending it');
|
|
194
|
+
assert(state.end === null, 'The timer has already been ended');
|
|
195
|
+
|
|
196
|
+
state.end = Date.now();
|
|
197
|
+
|
|
198
|
+
return this;
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
get duration() {
|
|
202
|
+
assert(state.start !== null, 'The timer has not been started');
|
|
203
|
+
|
|
204
|
+
return ((state.end ?? Date.now) - state.start) / 1000;
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
module.exports = {
|
|
210
|
+
emitDefinitions,
|
|
211
|
+
saveDefinitionToFileSystem,
|
|
212
|
+
format,
|
|
213
|
+
generateSharedExtensionDefinition,
|
|
214
|
+
createLogger,
|
|
215
|
+
timer,
|
|
216
|
+
};
|
package/lib/index.js
CHANGED
|
@@ -2,15 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
const compile = require('./compile');
|
|
4
4
|
const compilers = require('./compilers');
|
|
5
|
-
const admin = require('./admin');
|
|
6
5
|
const utils = require('./utils');
|
|
7
6
|
const generators = require('./generators');
|
|
8
7
|
|
|
9
8
|
module.exports = {
|
|
10
9
|
compile,
|
|
11
10
|
compilers,
|
|
12
|
-
admin,
|
|
13
11
|
generators,
|
|
14
|
-
|
|
15
12
|
...utils,
|
|
16
13
|
};
|
package/package.json
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/typescript-utils",
|
|
3
|
-
"version": "0.0.0-next.
|
|
3
|
+
"version": "0.0.0-next.f4ec69568d980c6fee91ce2ee0f41c138347aa81",
|
|
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",
|
|
@@ -20,6 +25,7 @@
|
|
|
20
25
|
}
|
|
21
26
|
],
|
|
22
27
|
"main": "./lib/index.js",
|
|
28
|
+
"types": "index.d.ts",
|
|
23
29
|
"directories": {
|
|
24
30
|
"lib": "./lib"
|
|
25
31
|
},
|
|
@@ -30,15 +36,18 @@
|
|
|
30
36
|
},
|
|
31
37
|
"dependencies": {
|
|
32
38
|
"chalk": "4.1.2",
|
|
33
|
-
"cli-table3": "0.6.
|
|
34
|
-
"fs-extra": "
|
|
39
|
+
"cli-table3": "0.6.5",
|
|
40
|
+
"fs-extra": "11.2.0",
|
|
35
41
|
"lodash": "4.17.21",
|
|
36
|
-
"prettier": "
|
|
37
|
-
"typescript": "
|
|
42
|
+
"prettier": "3.3.3",
|
|
43
|
+
"typescript": "5.3.2"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/fs-extra": "11.0.4"
|
|
38
47
|
},
|
|
39
48
|
"engines": {
|
|
40
|
-
"node": ">=
|
|
49
|
+
"node": ">=18.0.0 <=22.x.x",
|
|
41
50
|
"npm": ">=6.0.0"
|
|
42
51
|
},
|
|
43
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "f4ec69568d980c6fee91ce2ee0f41c138347aa81"
|
|
44
53
|
}
|
package/tsconfigs/admin.json
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
+
}
|