@strapi/typescript-utils 0.0.0-next.f45143c5e2a8a9d85691d0abf79a3f42024a0c71 → 0.0.0-next.f5b09a8e61e059f02784478e27c310c6290be088
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/index.d.ts +5 -0
- package/jest.config.js +1 -0
- package/lib/__tests__/generators/schemas/attributes.test.js +52 -52
- package/lib/__tests__/generators/schemas/imports.test.js +18 -16
- package/lib/__tests__/generators/schemas/utils.test.js +5 -59
- package/lib/admin/create-tsconfig-file.js +2 -16
- package/lib/compile.js +2 -6
- package/lib/compilers/basic.js +12 -4
- package/lib/compilers/index.js +0 -2
- package/lib/generators/{schemas → common}/imports.js +8 -7
- package/lib/generators/common/index.js +9 -0
- package/lib/generators/{schemas → common/models}/attributes.js +33 -34
- package/lib/generators/common/models/index.js +15 -0
- package/lib/generators/{schemas → common/models}/mappers.js +27 -24
- package/lib/generators/{schemas → common/models}/schema.js +10 -6
- package/lib/generators/{schemas → common/models}/utils.js +29 -16
- package/lib/generators/components/index.js +58 -0
- package/lib/generators/constants.js +6 -0
- package/lib/generators/content-types/index.js +58 -0
- package/lib/generators/index.js +118 -3
- package/lib/generators/utils.js +211 -0
- package/package.json +11 -5
- package/tsconfigs/admin.json +18 -19
- package/tsconfigs/server.json +4 -3
- package/lib/__tests__/generators/schemas/global.test.js +0 -108
- package/lib/compilers/watch.js +0 -37
- package/lib/generators/schemas/global.js +0 -70
- package/lib/generators/schemas/index.js +0 -185
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const path = require('path');
|
|
4
|
-
|
|
5
|
-
const ts = require('typescript');
|
|
6
|
-
const { factory } = require('typescript');
|
|
7
|
-
|
|
8
|
-
const fp = require('lodash/fp');
|
|
9
|
-
const fse = require('fs-extra');
|
|
10
|
-
const prettier = require('prettier');
|
|
11
|
-
const chalk = require('chalk');
|
|
12
|
-
const CLITable = require('cli-table3');
|
|
13
|
-
|
|
14
|
-
const { generateImportDefinition } = require('./imports');
|
|
15
|
-
const { generateSchemaDefinition } = require('./schema');
|
|
16
|
-
const { generateGlobalDefinition } = require('./global');
|
|
17
|
-
const {
|
|
18
|
-
getAllStrapiSchemas,
|
|
19
|
-
getSchemaInterfaceName,
|
|
20
|
-
getSchemaModelType,
|
|
21
|
-
getDefinitionAttributesCount,
|
|
22
|
-
} = require('./utils');
|
|
23
|
-
|
|
24
|
-
const DEFAULT_OUT_FILENAME = 'schemas.d.ts';
|
|
25
|
-
|
|
26
|
-
const emitDefinitions = (definitions) => {
|
|
27
|
-
const nodeArray = factory.createNodeArray(definitions);
|
|
28
|
-
|
|
29
|
-
const sourceFile = ts.createSourceFile(
|
|
30
|
-
'placeholder.ts',
|
|
31
|
-
'',
|
|
32
|
-
ts.ScriptTarget.ESNext,
|
|
33
|
-
true,
|
|
34
|
-
ts.ScriptKind.TS
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
const printer = ts.createPrinter({ newLine: true, omitTrailingSemicolon: true });
|
|
38
|
-
|
|
39
|
-
return printer.printList(ts.ListFormat.MultiLine, nodeArray, sourceFile);
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const saveDefinitionToFileSystem = async (dir, file, content) => {
|
|
43
|
-
const filepath = path.join(dir, file);
|
|
44
|
-
|
|
45
|
-
await fse.writeFile(filepath, content);
|
|
46
|
-
|
|
47
|
-
return filepath;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Format the given definitions.
|
|
52
|
-
* Uses the existing config if one is defined in the project.
|
|
53
|
-
*
|
|
54
|
-
* @param {string} content
|
|
55
|
-
* @returns {Promise<string>}
|
|
56
|
-
*/
|
|
57
|
-
const format = async (content) => {
|
|
58
|
-
const configFile = await prettier.resolveConfigFile();
|
|
59
|
-
const config = configFile
|
|
60
|
-
? await prettier.resolveConfig(configFile)
|
|
61
|
-
: // Default config
|
|
62
|
-
{
|
|
63
|
-
singleQuote: true,
|
|
64
|
-
useTabs: false,
|
|
65
|
-
tabWidth: 2,
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
Object.assign(config, { parser: 'typescript' });
|
|
69
|
-
|
|
70
|
-
return prettier.format(content, config);
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const logDebugInformation = (definitions, options = {}) => {
|
|
74
|
-
const { filepath, verbose, silent } = options;
|
|
75
|
-
|
|
76
|
-
if (verbose) {
|
|
77
|
-
const table = new CLITable({
|
|
78
|
-
head: [
|
|
79
|
-
chalk.bold(chalk.green('Model Type')),
|
|
80
|
-
chalk.bold(chalk.blue('UID')),
|
|
81
|
-
chalk.bold(chalk.blue('Type')),
|
|
82
|
-
chalk.bold(chalk.gray('Attributes Count')),
|
|
83
|
-
],
|
|
84
|
-
colAligns: ['center', 'left', 'left', 'center'],
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
const sortedDefinitions = definitions.map((def) => ({
|
|
88
|
-
...def,
|
|
89
|
-
attributesCount: getDefinitionAttributesCount(def.definition),
|
|
90
|
-
}));
|
|
91
|
-
|
|
92
|
-
for (const { schema, attributesCount } of sortedDefinitions) {
|
|
93
|
-
const modelType = fp.upperFirst(getSchemaModelType(schema));
|
|
94
|
-
const interfaceType = getSchemaInterfaceName(schema.uid);
|
|
95
|
-
|
|
96
|
-
table.push([
|
|
97
|
-
chalk.greenBright(modelType),
|
|
98
|
-
chalk.blue(schema.uid),
|
|
99
|
-
chalk.blue(interfaceType),
|
|
100
|
-
chalk.grey(fp.isNil(attributesCount) ? 'N/A' : attributesCount),
|
|
101
|
-
]);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// Table
|
|
105
|
-
console.log(table.toString());
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (!silent) {
|
|
109
|
-
// Metrics
|
|
110
|
-
console.log(
|
|
111
|
-
chalk.greenBright(
|
|
112
|
-
`Generated ${definitions.length} type definition for your Strapi application's schemas.`
|
|
113
|
-
)
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
// Filepath
|
|
117
|
-
const relativePath = path.relative(process.cwd(), filepath);
|
|
118
|
-
|
|
119
|
-
console.log(
|
|
120
|
-
chalk.grey(`The definitions file has been generated here: ${chalk.bold(relativePath)}`)
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Generate type definitions for Strapi schemas
|
|
127
|
-
*
|
|
128
|
-
* @param {object} options
|
|
129
|
-
* @param {Strapi} options.strapi
|
|
130
|
-
* @param {{ distDir: string; appDir: string; }} options.dirs
|
|
131
|
-
* @param {string} [options.outDir]
|
|
132
|
-
* @param {string} [options.file]
|
|
133
|
-
* @param {boolean} [options.verbose]
|
|
134
|
-
*/
|
|
135
|
-
const generateSchemasDefinitions = async (options = {}) => {
|
|
136
|
-
const {
|
|
137
|
-
strapi,
|
|
138
|
-
outDir = process.cwd(),
|
|
139
|
-
file = DEFAULT_OUT_FILENAME,
|
|
140
|
-
verbose = false,
|
|
141
|
-
silent = false,
|
|
142
|
-
} = options;
|
|
143
|
-
|
|
144
|
-
const schemas = getAllStrapiSchemas(strapi);
|
|
145
|
-
|
|
146
|
-
const schemasDefinitions = Object.values(schemas).map((schema) => ({
|
|
147
|
-
schema,
|
|
148
|
-
definition: generateSchemaDefinition(schema),
|
|
149
|
-
}));
|
|
150
|
-
|
|
151
|
-
const formattedSchemasDefinitions = schemasDefinitions.reduce((acc, def) => {
|
|
152
|
-
acc.push(
|
|
153
|
-
// Definition
|
|
154
|
-
def.definition,
|
|
155
|
-
|
|
156
|
-
// Add a newline between each interface declaration
|
|
157
|
-
factory.createIdentifier('\n')
|
|
158
|
-
);
|
|
159
|
-
|
|
160
|
-
return acc;
|
|
161
|
-
}, []);
|
|
162
|
-
|
|
163
|
-
const allDefinitions = [
|
|
164
|
-
// Imports
|
|
165
|
-
generateImportDefinition(),
|
|
166
|
-
|
|
167
|
-
// Add a newline after the import statement
|
|
168
|
-
factory.createIdentifier('\n'),
|
|
169
|
-
|
|
170
|
-
// Schemas
|
|
171
|
-
...formattedSchemasDefinitions,
|
|
172
|
-
|
|
173
|
-
// Global
|
|
174
|
-
generateGlobalDefinition(schemasDefinitions),
|
|
175
|
-
];
|
|
176
|
-
|
|
177
|
-
const output = emitDefinitions(allDefinitions);
|
|
178
|
-
const formattedOutput = await format(output);
|
|
179
|
-
|
|
180
|
-
const definitionFilepath = await saveDefinitionToFileSystem(outDir, file, formattedOutput);
|
|
181
|
-
|
|
182
|
-
logDebugInformation(schemasDefinitions, { filepath: definitionFilepath, verbose, silent });
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
module.exports = generateSchemasDefinitions;
|