@strapi/typescript-utils 0.0.0-next.ff946d2c25a3e577b47132a357cac2932eb8e635 → 0.0.0-next.ffc36acb308febe288f1a31b62cbbb75b286585c
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/LICENSE +18 -3
- package/lib/__tests__/generators/schemas/attributes.test.js +285 -140
- package/lib/__tests__/generators/schemas/imports.test.js +18 -16
- package/lib/__tests__/generators/schemas/utils.test.js +33 -85
- 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 -8
- package/lib/generators/{schemas → common/models}/utils.js +30 -11
- 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/lib/utils/index.js +2 -0
- package/lib/utils/resolve-outdir-sync.js +18 -0
- package/package.json +15 -8
- 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 -67
- package/lib/generators/schemas/imports.js +0 -32
- package/lib/generators/schemas/index.js +0 -185
- package/lib/generators/schemas/mappers.js +0 -131
|
@@ -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/lib/utils/index.js
CHANGED
|
@@ -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,11 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/typescript-utils",
|
|
3
|
-
"version": "0.0.0-next.
|
|
3
|
+
"version": "0.0.0-next.ffc36acb308febe288f1a31b62cbbb75b286585c",
|
|
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",
|
|
@@ -31,15 +36,17 @@
|
|
|
31
36
|
},
|
|
32
37
|
"dependencies": {
|
|
33
38
|
"chalk": "4.1.2",
|
|
34
|
-
"cli-table3": "0.6.
|
|
35
|
-
"fs-extra": "
|
|
39
|
+
"cli-table3": "0.6.5",
|
|
40
|
+
"fs-extra": "11.2.0",
|
|
36
41
|
"lodash": "4.17.21",
|
|
37
|
-
"prettier": "
|
|
38
|
-
"typescript": "5.
|
|
42
|
+
"prettier": "3.3.3",
|
|
43
|
+
"typescript": "5.4.4"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/fs-extra": "11.0.4"
|
|
39
47
|
},
|
|
40
48
|
"engines": {
|
|
41
|
-
"node": ">=
|
|
49
|
+
"node": ">=18.0.0 <=22.x.x",
|
|
42
50
|
"npm": ">=6.0.0"
|
|
43
|
-
}
|
|
44
|
-
"gitHead": "ff946d2c25a3e577b47132a357cac2932eb8e635"
|
|
51
|
+
}
|
|
45
52
|
}
|
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
|
+
}
|
package/tsconfigs/server.json
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"module": "CommonJS",
|
|
6
|
+
"moduleResolution": "Node",
|
|
7
|
+
"lib": ["ES2020"],
|
|
8
|
+
"target": "ES2019",
|
|
13
9
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
10
|
+
"strict": false,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true,
|
|
13
|
+
|
|
14
|
+
"tsBuildInfoFile": "./.tsbuildinfo",
|
|
15
|
+
"incremental": true,
|
|
16
|
+
"esModuleInterop": true,
|
|
17
|
+
"resolveJsonModule": true,
|
|
18
|
+
"noEmitOnError": true,
|
|
19
|
+
"noImplicitThis": true
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -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 path = require('path');
|
|
4
|
-
const fs = require('fs-extra');
|
|
5
|
-
|
|
6
|
-
module.exports = async (dest) => {
|
|
7
|
-
const tsConfig = {
|
|
8
|
-
compilerOptions: {
|
|
9
|
-
lib: ['es2019', 'es2020.promise', 'es2020.bigint', 'es2020.string', 'DOM'],
|
|
10
|
-
noImplicitAny: false,
|
|
11
|
-
module: 'es2020',
|
|
12
|
-
target: 'es5',
|
|
13
|
-
jsx: 'react',
|
|
14
|
-
allowJs: true,
|
|
15
|
-
strict: true,
|
|
16
|
-
moduleResolution: 'node',
|
|
17
|
-
skipLibCheck: true,
|
|
18
|
-
esModuleInterop: true,
|
|
19
|
-
allowSyntheticDefaultImports: true,
|
|
20
|
-
resolveJsonModule: true,
|
|
21
|
-
noEmit: false,
|
|
22
|
-
incremental: true,
|
|
23
|
-
},
|
|
24
|
-
include: ['../../../src/admin/*', '../../../src/**/**/admin/src/*'],
|
|
25
|
-
exclude: ['node_modules', '**/*.test.js', '*.js'],
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const filePath = path.join(dest, 'admin', 'src', 'tsconfig.json');
|
|
29
|
-
|
|
30
|
-
try {
|
|
31
|
-
await fs.ensureFile(filePath);
|
|
32
|
-
|
|
33
|
-
await fs.writeJSON(filePath, tsConfig, { spaces: 2 });
|
|
34
|
-
} catch (err) {
|
|
35
|
-
console.log(err);
|
|
36
|
-
}
|
|
37
|
-
};
|
package/lib/admin/index.js
DELETED
package/lib/compilers/watch.js
DELETED
|
@@ -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 };
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { factory } = require('typescript');
|
|
4
|
-
|
|
5
|
-
const imports = [];
|
|
6
|
-
|
|
7
|
-
module.exports = {
|
|
8
|
-
getImports() {
|
|
9
|
-
return imports;
|
|
10
|
-
},
|
|
11
|
-
|
|
12
|
-
addImport(type) {
|
|
13
|
-
const hasType = imports.includes(type);
|
|
14
|
-
|
|
15
|
-
if (!hasType) {
|
|
16
|
-
imports.push(type);
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
|
|
20
|
-
generateImportDefinition() {
|
|
21
|
-
const formattedImports = imports.map((key) =>
|
|
22
|
-
factory.createImportSpecifier(false, undefined, factory.createIdentifier(key))
|
|
23
|
-
);
|
|
24
|
-
|
|
25
|
-
return factory.createImportDeclaration(
|
|
26
|
-
undefined,
|
|
27
|
-
factory.createImportClause(false, undefined, factory.createNamedImports(formattedImports)),
|
|
28
|
-
factory.createStringLiteral('@strapi/strapi'),
|
|
29
|
-
undefined
|
|
30
|
-
);
|
|
31
|
-
},
|
|
32
|
-
};
|