@strapi/upgrade 5.0.0-beta.1 → 5.0.0-beta.11
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/dist/cli.js +1452 -5
- package/dist/cli.js.map +1 -1
- package/dist/index.js +228 -92
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +229 -93
- package/dist/index.mjs.map +1 -1
- package/dist/modules/codemod/codemod.d.ts +4 -2
- package/dist/modules/codemod/codemod.d.ts.map +1 -1
- package/dist/modules/codemod/types.d.ts +8 -1
- package/dist/modules/codemod/types.d.ts.map +1 -1
- package/dist/modules/codemod-repository/constants.d.ts.map +1 -1
- package/dist/modules/codemod-repository/repository.d.ts +5 -5
- package/dist/modules/codemod-repository/repository.d.ts.map +1 -1
- package/dist/modules/codemod-repository/types.d.ts +7 -3
- package/dist/modules/codemod-repository/types.d.ts.map +1 -1
- package/dist/modules/codemod-runner/codemod-runner.d.ts +6 -3
- package/dist/modules/codemod-runner/codemod-runner.d.ts.map +1 -1
- package/dist/modules/codemod-runner/index.d.ts +1 -0
- package/dist/modules/codemod-runner/index.d.ts.map +1 -1
- package/dist/modules/codemod-runner/types.d.ts +1 -0
- package/dist/modules/codemod-runner/types.d.ts.map +1 -1
- package/dist/modules/format/formats.d.ts +5 -0
- package/dist/modules/format/formats.d.ts.map +1 -1
- package/dist/modules/project/project.d.ts +1 -1
- package/dist/modules/project/project.d.ts.map +1 -1
- package/dist/modules/project/types.d.ts +1 -0
- package/dist/modules/project/types.d.ts.map +1 -1
- package/dist/modules/project/utils.d.ts +1 -1
- package/dist/modules/project/utils.d.ts.map +1 -1
- package/dist/modules/report/report.d.ts.map +1 -1
- package/dist/modules/upgrader/upgrader.d.ts.map +1 -1
- package/dist/modules/version/range.d.ts +2 -0
- package/dist/modules/version/range.d.ts.map +1 -1
- package/dist/tasks/codemods/index.d.ts +2 -1
- package/dist/tasks/codemods/index.d.ts.map +1 -1
- package/dist/tasks/codemods/list-codemods.d.ts +3 -0
- package/dist/tasks/codemods/list-codemods.d.ts.map +1 -0
- package/dist/tasks/codemods/run-codemods.d.ts +3 -0
- package/dist/tasks/codemods/run-codemods.d.ts.map +1 -0
- package/dist/tasks/codemods/types.d.ts +9 -3
- package/dist/tasks/codemods/types.d.ts.map +1 -1
- package/dist/tasks/codemods/utils.d.ts +6 -0
- package/dist/tasks/codemods/utils.d.ts.map +1 -0
- package/dist/tasks/index.d.ts +1 -1
- package/dist/tasks/index.d.ts.map +1 -1
- package/package.json +9 -8
- package/resources/codemods/5.0.0/entity-service-document-service.code.ts +437 -0
- package/resources/codemods/5.0.0/strapi-public-interface.code.ts +126 -0
- package/resources/codemods/5.0.0/utils-public-interface.code.ts +320 -0
- package/dist/_chunks/codemod-runner-aVWS9EOj.js +0 -798
- package/dist/_chunks/codemod-runner-aVWS9EOj.js.map +0 -1
- package/dist/_chunks/codemods-FxCTNGki.js +0 -105
- package/dist/_chunks/codemods-FxCTNGki.js.map +0 -1
- package/dist/_chunks/index-qYtax-pz.js +0 -103
- package/dist/_chunks/index-qYtax-pz.js.map +0 -1
- package/dist/_chunks/upgrade-_IPNaAR-.js +0 -361
- package/dist/_chunks/upgrade-_IPNaAR-.js.map +0 -1
- package/dist/tasks/codemods/codemods.d.ts +0 -3
- package/dist/tasks/codemods/codemods.d.ts.map +0 -1
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
import type { Transform, JSCodeshift, ASTPath, ObjectExpression } from 'jscodeshift';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
This codemod transforms entity service calls to match the new document service interface.
|
|
5
|
+
It supports all kind of argument parsing, including spread elements & deeply nested objects.
|
|
6
|
+
|
|
7
|
+
Here is a list of scenarios this was tested against
|
|
8
|
+
|
|
9
|
+
const uid = "api::xxx.xxx";
|
|
10
|
+
const entityId = 1;
|
|
11
|
+
|
|
12
|
+
Case: basic call
|
|
13
|
+
|
|
14
|
+
strapi.entityService.findOne(uid, entityId, {
|
|
15
|
+
fields: ["id", "name", "description"],
|
|
16
|
+
populate: ["author", "comments"],
|
|
17
|
+
publicationState: "preview",
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
Case: using a variable declared somewhere else
|
|
22
|
+
|
|
23
|
+
const objectParam_2 = {
|
|
24
|
+
fields: ["id", "name", "description"],
|
|
25
|
+
populate: ["author", "comments"],
|
|
26
|
+
publicationState: "preview",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
strapi.entityService.findOne(uid, entityId, objectParam_2);
|
|
30
|
+
|
|
31
|
+
Case: using a variable declared somewhere else with a spread element
|
|
32
|
+
|
|
33
|
+
const objectParam_3 = {
|
|
34
|
+
fields: ["id", "name", "description"],
|
|
35
|
+
populate: ["author", "comments"],
|
|
36
|
+
publicationState: "preview",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
strapi.entityService.findOne(uid, entityId, {
|
|
40
|
+
...objectParam_3,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
Case: using a variable declared somewhere else with a spread element and overwritten properties
|
|
45
|
+
|
|
46
|
+
const objectParam_4_1 = {
|
|
47
|
+
fields: ["id", "name", "description"],
|
|
48
|
+
populate: ["author", "comments"],
|
|
49
|
+
publicationState: "preview",
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const objectParam_4 = {
|
|
53
|
+
publicationState: "live",
|
|
54
|
+
...objectParam_4_1,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
strapi.entityService.findOne(uid, entityId, {
|
|
58
|
+
...objectParam_4,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
Case: using a variable declared somewhere else with a spread array element while that need its 1st element to be moved
|
|
62
|
+
|
|
63
|
+
const objectParam_5 = [
|
|
64
|
+
uid,
|
|
65
|
+
entityId,
|
|
66
|
+
{
|
|
67
|
+
fields: ["id", "name", "description"],
|
|
68
|
+
populate: ["author", "comments"],
|
|
69
|
+
publicationState: "preview",
|
|
70
|
+
},
|
|
71
|
+
];
|
|
72
|
+
|
|
73
|
+
strapi.entityService.findOne(...objectParam_5);
|
|
74
|
+
|
|
75
|
+
Case: using a variable declared somewhere else with a partial spread array
|
|
76
|
+
|
|
77
|
+
const objectParam_6 = [
|
|
78
|
+
entityId,
|
|
79
|
+
{
|
|
80
|
+
fields: ["id", "name", "description"],
|
|
81
|
+
populate: ["author", "comments"],
|
|
82
|
+
publicationState: "preview",
|
|
83
|
+
},
|
|
84
|
+
];
|
|
85
|
+
|
|
86
|
+
strapi.entityService.findOne(uid, ...objectParam_6);
|
|
87
|
+
|
|
88
|
+
Case: using a variable declared somewhere else with a partial & nested spread arrays
|
|
89
|
+
|
|
90
|
+
const objectParam_7_1 = [
|
|
91
|
+
{
|
|
92
|
+
fields: ["id", "name", "description"],
|
|
93
|
+
populate: ["author", "comments"],
|
|
94
|
+
publicationState: "preview",
|
|
95
|
+
},
|
|
96
|
+
];
|
|
97
|
+
|
|
98
|
+
const objectParam_7 = [entityId, ...objectParam_7_1];
|
|
99
|
+
|
|
100
|
+
strapi.entityService.findOne(uid, ...objectParam_7);
|
|
101
|
+
|
|
102
|
+
Case: using a variable declared somewhere else with a partial & nested spread arrays & objects
|
|
103
|
+
|
|
104
|
+
const objectParam_8_1 = {
|
|
105
|
+
publicationState: "preview",
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const objectParam_8 = [
|
|
109
|
+
entityId,
|
|
110
|
+
{
|
|
111
|
+
fields: ["id", "name", "description"],
|
|
112
|
+
populate: ["author", "comments"],
|
|
113
|
+
...objectParam_8_1,
|
|
114
|
+
},
|
|
115
|
+
];
|
|
116
|
+
|
|
117
|
+
strapi.entityService.findOne(uid, ...objectParam_8);
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
Case: some sort of mix of all the above
|
|
121
|
+
|
|
122
|
+
const objectParam_9_1 = {
|
|
123
|
+
publicationState: "preview",
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const objectParam_9 = {
|
|
127
|
+
fields: ["id", "name", "description"],
|
|
128
|
+
populate: ["author", "comments"],
|
|
129
|
+
...objectParam_9_1,
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
strapi.entityService.findOne(uid, ...[entityId, [objectParam_9]]);
|
|
133
|
+
|
|
134
|
+
Case: even more complex
|
|
135
|
+
|
|
136
|
+
const objectParam_10_1 = {
|
|
137
|
+
publicationState: "preview",
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const objectParam_10_2 = [uid, ...[12], ...[objectParam_10_1]];
|
|
141
|
+
const objectParam_10 = [...objectParam_10_2];
|
|
142
|
+
|
|
143
|
+
strapi.entityService.findOne(...[...objectParam_10]);
|
|
144
|
+
|
|
145
|
+
Case: find, create, update, delete with entityId as first argument
|
|
146
|
+
|
|
147
|
+
strapi.entityService.findMany(uid, {
|
|
148
|
+
fields: ["id", "name", "description"],
|
|
149
|
+
populate: ["author", "comments"],
|
|
150
|
+
publicationState: "preview",
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
strapi.entityService.create(uid, {
|
|
154
|
+
data: {
|
|
155
|
+
name: "John Doe",
|
|
156
|
+
age: 30,
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
strapi.entityService.update(uid, entityId, {
|
|
161
|
+
data: {
|
|
162
|
+
name: "John Doe",
|
|
163
|
+
age: 30,
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
strapi.entityService.delete(uid, entityId);
|
|
168
|
+
strapi.entityService.findOne(uid, entityId);
|
|
169
|
+
|
|
170
|
+
*/
|
|
171
|
+
|
|
172
|
+
const movedFunctions = ['findOne', 'findMany', 'count', 'create', 'update', 'delete'];
|
|
173
|
+
|
|
174
|
+
const functionsWithEntityId = ['findOne', 'update', 'delete'];
|
|
175
|
+
|
|
176
|
+
const transformDeclaration = (path: ASTPath<any>, name: any, j: JSCodeshift) => {
|
|
177
|
+
const declaration = findClosestDeclaration(path, name, j);
|
|
178
|
+
|
|
179
|
+
if (!declaration) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
transformElement(path, declaration.init, j);
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const transformElement = (path: ASTPath<any>, element: any, j: JSCodeshift) => {
|
|
187
|
+
switch (true) {
|
|
188
|
+
case j.ObjectExpression.check(element): {
|
|
189
|
+
transformObjectParam(path, element, j);
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
case j.Identifier.check(element): {
|
|
194
|
+
transformDeclaration(path, element.name, j);
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
case j.SpreadElement.check(element): {
|
|
199
|
+
transformElement(path, element.argument, j);
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
case j.ArrayExpression.check(element): {
|
|
204
|
+
element.elements.forEach((element) => {
|
|
205
|
+
transformElement(path, element, j);
|
|
206
|
+
});
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
default: {
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const transformObjectParam = (path: ASTPath<any>, expression: ObjectExpression, j: JSCodeshift) => {
|
|
216
|
+
expression.properties.forEach((prop) => {
|
|
217
|
+
switch (true) {
|
|
218
|
+
case j.ObjectProperty.check(prop): {
|
|
219
|
+
if (!j.Identifier.check(prop.key) && !j.Literal.check(prop.key)) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (j.Identifier.check(prop.key) && prop.key.name !== 'publicationState') {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (j.Literal.check(prop.key) && prop.key.value !== 'publicationState') {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (j.Identifier.check(prop.key) && prop.key.name === 'publicationState') {
|
|
232
|
+
if (!prop.computed && !prop.shorthand) {
|
|
233
|
+
prop.key.name = 'status';
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (prop.shorthand && !prop.computed) {
|
|
237
|
+
prop.shorthand = false;
|
|
238
|
+
prop.key = j.identifier('status');
|
|
239
|
+
prop.value = j.identifier('publicationState');
|
|
240
|
+
}
|
|
241
|
+
} else if (j.Literal.check(prop.key) && prop.key.value === 'publicationState') {
|
|
242
|
+
prop.key.value = 'status';
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
switch (true) {
|
|
246
|
+
case j.Literal.check(prop.value): {
|
|
247
|
+
prop.value = prop.value.value === 'live' ? j.literal('published') : j.literal('draft');
|
|
248
|
+
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
case j.Identifier.check(prop.value): {
|
|
252
|
+
const declaration = findClosestDeclaration(path, prop.value.name, j);
|
|
253
|
+
|
|
254
|
+
if (!declaration) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (j.Literal.check(declaration.init)) {
|
|
259
|
+
declaration.init =
|
|
260
|
+
declaration.init.value === 'live' ? j.literal('published') : j.literal('draft');
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
default: {
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
case j.SpreadElement.check(prop): {
|
|
273
|
+
transformElement(path, prop.argument, j);
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
default: {
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const findClosestDeclaration = (path: ASTPath<any>, name: string, j) => {
|
|
284
|
+
// find Identifier declaration
|
|
285
|
+
const scope = path.scope.lookup(name);
|
|
286
|
+
|
|
287
|
+
if (!scope) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return j(scope.path)
|
|
292
|
+
.find(j.VariableDeclarator, { id: { type: 'Identifier', name } })
|
|
293
|
+
.nodes()[0];
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
const transform: Transform = (file, api) => {
|
|
297
|
+
const j = api.jscodeshift;
|
|
298
|
+
|
|
299
|
+
const root = j(file.source);
|
|
300
|
+
|
|
301
|
+
root
|
|
302
|
+
.find(j.CallExpression, {
|
|
303
|
+
callee: {
|
|
304
|
+
type: 'MemberExpression',
|
|
305
|
+
object: {
|
|
306
|
+
type: 'MemberExpression',
|
|
307
|
+
object: {
|
|
308
|
+
type: 'Identifier',
|
|
309
|
+
name: 'strapi',
|
|
310
|
+
},
|
|
311
|
+
property: {
|
|
312
|
+
type: 'Identifier',
|
|
313
|
+
name: 'entityService',
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
property: {
|
|
317
|
+
type: 'Identifier',
|
|
318
|
+
name: (name) => movedFunctions.includes(name),
|
|
319
|
+
},
|
|
320
|
+
},
|
|
321
|
+
})
|
|
322
|
+
.replaceWith((path) => {
|
|
323
|
+
if (!j.MemberExpression.check(path.value.callee)) {
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const args = path.value.arguments;
|
|
328
|
+
|
|
329
|
+
if (args.length === 0) {
|
|
330
|
+
// we don't know how to transform this
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
type Args = typeof path.value.arguments;
|
|
335
|
+
|
|
336
|
+
function resolveArgs(args: Args): Args {
|
|
337
|
+
return args.flatMap((arg: Args[number]) => {
|
|
338
|
+
switch (true) {
|
|
339
|
+
case j.Identifier.check(arg):
|
|
340
|
+
case j.Literal.check(arg): {
|
|
341
|
+
return arg;
|
|
342
|
+
}
|
|
343
|
+
case j.SpreadElement.check(arg): {
|
|
344
|
+
switch (true) {
|
|
345
|
+
case j.Identifier.check(arg.argument): {
|
|
346
|
+
const identifier = arg.argument;
|
|
347
|
+
|
|
348
|
+
const declaration = findClosestDeclaration(path, identifier.name, j);
|
|
349
|
+
|
|
350
|
+
if (!declaration) {
|
|
351
|
+
return arg;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
switch (true) {
|
|
355
|
+
case j.ArrayExpression.check(declaration.init): {
|
|
356
|
+
return resolveArgs(declaration.init.elements);
|
|
357
|
+
}
|
|
358
|
+
default:
|
|
359
|
+
return arg;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
case j.ArrayExpression.check(arg.argument): {
|
|
363
|
+
return resolveArgs(arg.argument.elements as Args);
|
|
364
|
+
}
|
|
365
|
+
default: {
|
|
366
|
+
return arg;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
default: {
|
|
371
|
+
return arg;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const resolvedArgs = resolveArgs(args);
|
|
378
|
+
|
|
379
|
+
const [docUID, ...rest] = resolvedArgs;
|
|
380
|
+
|
|
381
|
+
// function with entityId as first argument
|
|
382
|
+
if (
|
|
383
|
+
j.Identifier.check(path.value.callee.property) &&
|
|
384
|
+
functionsWithEntityId.includes(path.value.callee.property.name)
|
|
385
|
+
) {
|
|
386
|
+
rest.splice(0, 1);
|
|
387
|
+
|
|
388
|
+
// in case no extra params are passed in the function e.g delete(uid, entityId)
|
|
389
|
+
if (rest.length === 0) {
|
|
390
|
+
rest.push(
|
|
391
|
+
j.objectExpression.from({
|
|
392
|
+
properties: [],
|
|
393
|
+
})
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const params = rest[0];
|
|
398
|
+
|
|
399
|
+
const placeholder = j.objectProperty(j.identifier('documentId'), j.literal('__TODO__'));
|
|
400
|
+
|
|
401
|
+
// add documentId to params with a placeholder
|
|
402
|
+
if (j.ObjectExpression.check(params)) {
|
|
403
|
+
params.properties.unshift(placeholder);
|
|
404
|
+
} else if (j.Identifier.check(params)) {
|
|
405
|
+
const declaration = findClosestDeclaration(path, params.name, j);
|
|
406
|
+
|
|
407
|
+
if (!declaration) {
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
if (j.ObjectExpression.check(declaration.init)) {
|
|
412
|
+
declaration.init.properties.unshift(placeholder);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
path.value.arguments.forEach((arg) => {
|
|
418
|
+
transformElement(path, arg, j);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
return j.callExpression(
|
|
422
|
+
j.memberExpression(
|
|
423
|
+
j.callExpression(j.memberExpression(j.identifier('strapi'), j.identifier('documents')), [
|
|
424
|
+
docUID,
|
|
425
|
+
]),
|
|
426
|
+
path.value.callee.property
|
|
427
|
+
),
|
|
428
|
+
rest
|
|
429
|
+
);
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
return root.toSource();
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
export const parser = 'tsx';
|
|
436
|
+
|
|
437
|
+
export default transform;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { Transform, JSCodeshift, Collection } from 'jscodeshift';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
This codemod transforms @strapi/strapi imports to use the new public interface.
|
|
5
|
+
|
|
6
|
+
ESM
|
|
7
|
+
Before:
|
|
8
|
+
|
|
9
|
+
import strapi from '@strapi/strapi';
|
|
10
|
+
strapi();
|
|
11
|
+
|
|
12
|
+
After:
|
|
13
|
+
|
|
14
|
+
import { createStrapi } from '@strapi/strapi'; // keeps the default import
|
|
15
|
+
createStrapi();
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
Common JS
|
|
20
|
+
Before:
|
|
21
|
+
|
|
22
|
+
const strapi = require('@strapi/strapi');
|
|
23
|
+
strapi();
|
|
24
|
+
|
|
25
|
+
After:
|
|
26
|
+
|
|
27
|
+
const strapi = require('@strapi/strapi');
|
|
28
|
+
strapi.createStrapi();
|
|
29
|
+
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
const transformStrapiImport = (root: Collection, j: JSCodeshift) => {
|
|
33
|
+
root.find(j.ImportDefaultSpecifier).forEach((path) => {
|
|
34
|
+
if (path.parent.value.source.value === '@strapi/strapi') {
|
|
35
|
+
const newSpecifiers = path.parent.value.specifiers.filter(
|
|
36
|
+
(specifier) => specifier.type !== 'ImportDefaultSpecifier'
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
j(path.parent).replaceWith(
|
|
40
|
+
j.importDeclaration(
|
|
41
|
+
[...newSpecifiers, j.importSpecifier(j.identifier('createStrapi'))],
|
|
42
|
+
j.literal('@strapi/strapi')
|
|
43
|
+
)
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
transformFunctionCalls(path.value.local.name, root, j);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const transformRequireImport = (root: Collection, j: JSCodeshift) => {
|
|
52
|
+
root
|
|
53
|
+
.find(j.VariableDeclarator, {
|
|
54
|
+
init: {
|
|
55
|
+
callee: {
|
|
56
|
+
name: 'require',
|
|
57
|
+
},
|
|
58
|
+
arguments: [{ value: '@strapi/strapi' }],
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
.forEach((path) => {
|
|
62
|
+
if (path.value.id.type === 'Identifier') {
|
|
63
|
+
const identifier = path.value.id.name;
|
|
64
|
+
|
|
65
|
+
root
|
|
66
|
+
.find(j.CallExpression, {
|
|
67
|
+
callee: {
|
|
68
|
+
type: 'Identifier',
|
|
69
|
+
name: identifier,
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
.forEach((callExpressionPath) => {
|
|
73
|
+
j(callExpressionPath).replaceWith(
|
|
74
|
+
j.callExpression(
|
|
75
|
+
j.memberExpression(j.identifier(identifier), j.identifier('createStrapi')),
|
|
76
|
+
callExpressionPath.value.arguments
|
|
77
|
+
)
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const transformFunctionCalls = (identifier: string, root: Collection, j: JSCodeshift) => {
|
|
85
|
+
root
|
|
86
|
+
.find(j.CallExpression, {
|
|
87
|
+
callee: {
|
|
88
|
+
type: 'Identifier',
|
|
89
|
+
name: identifier,
|
|
90
|
+
},
|
|
91
|
+
})
|
|
92
|
+
.forEach((path) => {
|
|
93
|
+
// we a type guard again to avoid ts issues
|
|
94
|
+
if (path.value.callee.type === 'Identifier') {
|
|
95
|
+
path.value.callee.name = 'createStrapi';
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Transformations
|
|
102
|
+
*
|
|
103
|
+
* With ESM imports
|
|
104
|
+
*
|
|
105
|
+
* import strapi from '@strapi/strapi'; => import strapi, { createStrapi } from '@strapi/strapi';
|
|
106
|
+
* strapi() => createStrapi()
|
|
107
|
+
*
|
|
108
|
+
* With CJS imports
|
|
109
|
+
*
|
|
110
|
+
* const strapi = require('@strapi/strapi'); => no transform
|
|
111
|
+
* strapi() => strapi.createStrapi()
|
|
112
|
+
*/
|
|
113
|
+
const transform: Transform = (file, api) => {
|
|
114
|
+
const j = api.jscodeshift;
|
|
115
|
+
|
|
116
|
+
const root = j(file.source);
|
|
117
|
+
|
|
118
|
+
transformStrapiImport(root, j);
|
|
119
|
+
transformRequireImport(root, j);
|
|
120
|
+
|
|
121
|
+
return root.toSource();
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export const parser = 'tsx';
|
|
125
|
+
|
|
126
|
+
export default transform;
|