@strapi/upgrade 0.0.0-experimental.e9122b401c96877b6707775c4f893660eab93ae3 → 0.0.0-experimental.e9144fc8a193f9875a2c0832d689c14001c79f00

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.
Files changed (39) hide show
  1. package/README.md +1 -1
  2. package/dist/chunks/logger-B44ixHKE.js +1649 -0
  3. package/dist/chunks/logger-B44ixHKE.js.map +1 -0
  4. package/dist/chunks/logger-DLKyDz9F.mjs +1599 -0
  5. package/dist/chunks/logger-DLKyDz9F.mjs.map +1 -0
  6. package/dist/cli.js +191 -1462
  7. package/dist/cli.js.map +1 -1
  8. package/dist/cli.mjs +211 -0
  9. package/dist/cli.mjs.map +1 -0
  10. package/dist/index.js +133 -1433
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.mjs +130 -1425
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/modules/error/utils.d.ts +8 -0
  15. package/dist/modules/error/utils.d.ts.map +1 -1
  16. package/dist/modules/file-scanner/scanner.d.ts.map +1 -1
  17. package/dist/modules/project/project.d.ts.map +1 -1
  18. package/dist/modules/upgrader/types.d.ts +6 -0
  19. package/dist/modules/upgrader/types.d.ts.map +1 -1
  20. package/dist/modules/upgrader/upgrader.d.ts +4 -0
  21. package/dist/modules/upgrader/upgrader.d.ts.map +1 -1
  22. package/dist/modules/version/range.d.ts.map +1 -1
  23. package/dist/modules/version/types.d.ts +2 -1
  24. package/dist/modules/version/types.d.ts.map +1 -1
  25. package/dist/tasks/codemods/utils.d.ts.map +1 -1
  26. package/dist/tasks/upgrade/prompts/index.d.ts +2 -0
  27. package/dist/tasks/upgrade/prompts/index.d.ts.map +1 -0
  28. package/dist/tasks/upgrade/prompts/latest.d.ts +9 -0
  29. package/dist/tasks/upgrade/prompts/latest.d.ts.map +1 -0
  30. package/dist/tasks/upgrade/requirements/major.d.ts.map +1 -1
  31. package/dist/tasks/upgrade/upgrade.d.ts.map +1 -1
  32. package/package.json +13 -13
  33. package/resources/codemods/5.0.0/deprecate-helper-plugin.code.ts +192 -0
  34. package/resources/codemods/5.0.0/sqlite3-to-better-sqlite3.json.ts +0 -1
  35. package/resources/codemods/5.1.0/dependency-better-sqlite3.json.ts +48 -0
  36. package/resources/utils/change-import.ts +23 -10
  37. package/resources/codemods/5.0.0/change-useAPIErrorHandler-import.code.ts +0 -21
  38. package/resources/codemods/5.0.0/nocontent-migrate-to-emptystatelayout.code.ts +0 -30
  39. package/resources/codemods/5.0.0/useRBAC-hook-import-change.code.ts +0 -21
package/dist/index.js CHANGED
@@ -1,1461 +1,161 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const path$1 = require("node:path");
4
- const simpleGit = require("simple-git");
5
- const chalk = require("chalk");
6
- const semver = require("semver");
7
- const utils = require("@strapi/utils");
8
- const fp = require("lodash/fp");
9
- const fse = require("fs-extra");
10
- const assert = require("node:assert");
11
- const glob = require("glob");
12
- const Runner = require("jscodeshift/src/Runner");
13
- const node = require("esbuild-register/dist/node");
14
- const CliTable3 = require("cli-table3");
15
- const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
16
- const path__default = /* @__PURE__ */ _interopDefault(path$1);
17
- const simpleGit__default = /* @__PURE__ */ _interopDefault(simpleGit);
18
- const chalk__default = /* @__PURE__ */ _interopDefault(chalk);
19
- const semver__default = /* @__PURE__ */ _interopDefault(semver);
20
- const fse__default = /* @__PURE__ */ _interopDefault(fse);
21
- const assert__default = /* @__PURE__ */ _interopDefault(assert);
22
- const CliTable3__default = /* @__PURE__ */ _interopDefault(CliTable3);
23
- class Requirement {
24
- isRequired;
25
- name;
26
- testCallback;
27
- children;
28
- constructor(name, testCallback, isRequired) {
29
- this.name = name;
30
- this.testCallback = testCallback;
31
- this.isRequired = isRequired ?? true;
32
- this.children = [];
33
- }
34
- setChildren(children) {
35
- this.children = children;
36
- return this;
37
- }
38
- addChild(child) {
39
- this.children.push(child);
40
- return this;
41
- }
42
- asOptional() {
43
- const newInstance = requirementFactory(this.name, this.testCallback, false);
44
- newInstance.setChildren(this.children);
45
- return newInstance;
46
- }
47
- asRequired() {
48
- const newInstance = requirementFactory(this.name, this.testCallback, true);
49
- newInstance.setChildren(this.children);
50
- return newInstance;
51
- }
52
- async test(context) {
53
- try {
54
- await this.testCallback?.(context);
55
- return ok();
56
- } catch (e) {
57
- if (e instanceof Error) {
58
- return errored(e);
59
- }
60
- if (typeof e === "string") {
61
- return errored(new Error(e));
62
- }
63
- return errored(new Error("Unknown error"));
64
- }
65
- }
66
- }
67
- const ok = () => ({ pass: true, error: null });
68
- const errored = (error) => ({ pass: false, error });
69
- const requirementFactory = (name, testCallback, isRequired) => new Requirement(name, testCallback, isRequired);
70
- const index$g = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
71
- __proto__: null,
72
- requirementFactory
73
- }, Symbol.toStringTag, { value: "Module" }));
74
- const REQUIRE_AVAILABLE_NEXT_MAJOR = requirementFactory(
75
- "REQUIRE_AVAILABLE_NEXT_MAJOR",
76
- (context) => {
77
- const { project, target } = context;
78
- const currentMajor = project.strapiVersion.major;
79
- const targetedMajor = target.major;
80
- if (targetedMajor === currentMajor) {
81
- throw new Error(`You're already on the latest major version (v${currentMajor})`);
82
- }
83
- }
84
- );
85
- const REQUIRE_LATEST_FOR_CURRENT_MAJOR = requirementFactory(
86
- "REQUIRE_LATEST_FOR_CURRENT_MAJOR",
87
- (context) => {
88
- const { project, target, npmVersionsMatches } = context;
89
- if (npmVersionsMatches.length !== 1) {
90
- const invalidVersions = npmVersionsMatches.slice(0, -1);
91
- const invalidVersionsAsSemVer = invalidVersions.map((v) => v.version);
92
- const nbInvalidVersions = npmVersionsMatches.length;
93
- const currentMajor = project.strapiVersion.major;
94
- throw new Error(
95
- `Doing a major upgrade requires to be on the latest v${currentMajor} version, but found ${nbInvalidVersions} versions between the current one and ${target}: ${invalidVersionsAsSemVer}`
96
- );
97
- }
98
- }
99
- );
100
- const REQUIRE_GIT_CLEAN_REPOSITORY = requirementFactory(
101
- "REQUIRE_GIT_CLEAN_REPOSITORY",
102
- async (context) => {
103
- const git = simpleGit__default.default({ baseDir: context.project.cwd });
104
- const status = await git.status();
105
- if (!status.isClean()) {
106
- throw new Error(
107
- "Repository is not clean. Please commit or stash any changes before upgrading"
108
- );
109
- }
110
- }
111
- );
112
- const REQUIRE_GIT_REPOSITORY = requirementFactory(
113
- "REQUIRE_GIT_REPOSITORY",
114
- async (context) => {
115
- const git = simpleGit__default.default({ baseDir: context.project.cwd });
116
- const isRepo = await git.checkIsRepo();
117
- if (!isRepo) {
118
- throw new Error("Not a git repository (or any of the parent directories)");
119
- }
120
- }
121
- ).addChild(REQUIRE_GIT_CLEAN_REPOSITORY.asOptional());
122
- const REQUIRE_GIT_INSTALLED = requirementFactory(
123
- "REQUIRE_GIT_INSTALLED",
124
- async (context) => {
125
- const git = simpleGit__default.default({ baseDir: context.project.cwd });
126
- try {
127
- await git.version();
128
- } catch {
129
- throw new Error("Git is not installed");
130
- }
131
- }
132
- ).addChild(REQUIRE_GIT_REPOSITORY.asOptional());
133
- const REQUIRE_GIT = requirementFactory("REQUIRE_GIT", null).addChild(
134
- REQUIRE_GIT_INSTALLED.asOptional()
135
- );
136
- class Timer {
137
- interval;
138
- constructor() {
139
- this.reset();
140
- }
141
- get elapsedMs() {
142
- const { start, end } = this.interval;
143
- return end ? end - start : Date.now() - start;
144
- }
145
- get end() {
146
- return this.interval.end;
147
- }
148
- get start() {
149
- return this.interval.start;
150
- }
151
- stop() {
152
- this.interval.end = Date.now();
153
- return this.elapsedMs;
154
- }
155
- reset() {
156
- this.interval = { start: Date.now(), end: null };
157
- return this;
158
- }
159
- }
160
- const timerFactory = () => new Timer();
161
- const ONE_SECOND_MS = 1e3;
162
- const constants$4 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
163
- __proto__: null,
164
- ONE_SECOND_MS
165
- }, Symbol.toStringTag, { value: "Module" }));
166
- const index$f = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
167
- __proto__: null,
168
- constants: constants$4,
169
- timerFactory
170
- }, Symbol.toStringTag, { value: "Module" }));
171
- class JSONTransformAPI {
172
- json;
173
- constructor(json) {
174
- this.json = fp.cloneDeep(json);
175
- }
176
- get(path2, defaultValue) {
177
- if (!path2) {
178
- return this.root();
179
- }
180
- return fp.cloneDeep(fp.get(path2, this.json) ?? defaultValue);
181
- }
182
- has(path2) {
183
- return fp.has(path2, this.json);
184
- }
185
- merge(other) {
186
- this.json = fp.merge(other, this.json);
187
- return this;
188
- }
189
- root() {
190
- return fp.cloneDeep(this.json);
191
- }
192
- set(path2, value) {
193
- this.json = fp.set(path2, value, this.json);
194
- return this;
195
- }
196
- remove(path2) {
197
- this.json = fp.omit(path2, this.json);
198
- return this;
199
- }
200
- }
201
- const createJSONTransformAPI = (object) => new JSONTransformAPI(object);
202
- const readJSON = async (path2) => {
203
- const buffer = await fse__default.default.readFile(path2);
204
- return JSON.parse(buffer.toString());
205
- };
206
- const saveJSON = async (path2, json) => {
207
- const jsonAsString = `${JSON.stringify(json, null, 2)}
208
- `;
209
- await fse__default.default.writeFile(path2, jsonAsString);
210
- };
211
- var ReleaseType = /* @__PURE__ */ ((ReleaseType2) => {
212
- ReleaseType2["Major"] = "major";
213
- ReleaseType2["Minor"] = "minor";
214
- ReleaseType2["Patch"] = "patch";
215
- return ReleaseType2;
216
- })(ReleaseType || {});
217
- const types = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
218
- __proto__: null,
219
- ReleaseType
220
- }, Symbol.toStringTag, { value: "Module" }));
221
- const semVerFactory = (version2) => {
222
- return new semver__default.default.SemVer(version2);
223
- };
224
- const isLiteralSemVer = (str) => {
225
- const tokens = str.split(".");
226
- return tokens.length === 3 && tokens.every((token) => !Number.isNaN(+token) && Number.isInteger(+token));
227
- };
228
- const isValidSemVer = (str) => semver__default.default.valid(str) !== null;
229
- const isSemverInstance = (value) => {
230
- return value instanceof semver__default.default.SemVer;
231
- };
232
- const isSemVerReleaseType = (str) => {
233
- return Object.values(ReleaseType).includes(str);
234
- };
235
- const rangeFactory = (range) => {
236
- return new semver__default.default.Range(range);
237
- };
238
- const rangeFromReleaseType = (current, identifier) => {
239
- switch (identifier) {
240
- case ReleaseType.Major: {
241
- const nextMajor = semver__default.default.inc(current, "major");
242
- return rangeFactory(`>${current.raw} <=${nextMajor}`);
243
- }
244
- case ReleaseType.Patch: {
245
- const minor = semver__default.default.inc(current, "minor");
246
- return rangeFactory(`>${current.raw} <${minor}`);
247
- }
248
- case ReleaseType.Minor: {
249
- const major = semver__default.default.inc(current, "major");
250
- return rangeFactory(`>${current.raw} <${major}`);
251
- }
252
- default: {
253
- throw new Error("Not implemented");
254
- }
255
- }
256
- };
257
- const rangeFromVersions = (currentVersion, target) => {
258
- if (isSemverInstance(target)) {
259
- return rangeFactory(`>${currentVersion.raw} <=${target.raw}`);
260
- }
261
- if (isSemVerReleaseType(target)) {
262
- return rangeFromReleaseType(currentVersion, target);
263
- }
264
- throw new Error(`Invalid target set: ${target}`);
265
- };
266
- const isValidStringifiedRange = (str) => semver__default.default.validRange(str) !== null;
267
- const isRangeInstance = (range) => {
268
- return range instanceof semver__default.default.Range;
269
- };
270
- const index$e = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
271
- __proto__: null,
272
- Version: types,
273
- isLiteralSemVer,
274
- isRangeInstance,
275
- isSemVerReleaseType,
276
- isSemverInstance,
277
- isValidSemVer,
278
- isValidStringifiedRange,
279
- rangeFactory,
280
- rangeFromReleaseType,
281
- rangeFromVersions,
282
- semVerFactory
283
- }, Symbol.toStringTag, { value: "Module" }));
284
- class FileScanner {
285
- cwd;
286
- constructor(cwd) {
287
- this.cwd = cwd;
288
- }
289
- scan(patterns) {
290
- const filenames = glob.glob.sync(patterns, { cwd: this.cwd });
291
- return filenames.map((filename) => path__default.default.join(this.cwd, filename));
292
- }
293
- }
294
- const fileScannerFactory = (cwd) => new FileScanner(cwd);
295
- const index$d = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1
+ 'use strict';
2
+
3
+ var logger = require('./chunks/logger-B44ixHKE.js');
4
+ require('semver');
5
+ require('node:path');
6
+ require('node:assert');
7
+ require('chalk');
8
+ require('@strapi/utils');
9
+ require('fs-extra');
10
+ require('fast-glob');
11
+ require('jscodeshift/src/Runner');
12
+ require('lodash/fp');
13
+ require('esbuild-register/dist/node');
14
+ require('simple-git');
15
+ require('cli-table3');
16
+
17
+ var index$g = /*#__PURE__*/Object.freeze({
18
+ __proto__: null,
19
+ constants: logger.constants,
20
+ timerFactory: logger.timerFactory
21
+ });
22
+
23
+ var index$f = /*#__PURE__*/Object.freeze({
24
+ __proto__: null,
25
+ codemodList: logger.codemodList,
26
+ codemodUID: logger.codemodUID,
27
+ durationMs: logger.durationMs,
28
+ highlight: logger.highlight,
29
+ path: logger.path,
30
+ projectDetails: logger.projectDetails,
31
+ projectType: logger.projectType,
32
+ reports: logger.reports,
33
+ transform: logger.transform,
34
+ upgradeStep: logger.upgradeStep,
35
+ version: logger.version,
36
+ versionRange: logger.versionRange
37
+ });
38
+
39
+ var index$e = /*#__PURE__*/Object.freeze({
40
+ __proto__: null,
41
+ Version: logger.types,
42
+ isLiteralSemVer: logger.isLiteralSemVer,
43
+ isRangeInstance: logger.isRangeInstance,
44
+ isSemVerReleaseType: logger.isSemVerReleaseType,
45
+ isSemverInstance: logger.isSemverInstance,
46
+ isValidSemVer: logger.isValidSemVer,
47
+ isValidStringifiedRange: logger.isValidStringifiedRange,
48
+ rangeFactory: logger.rangeFactory,
49
+ rangeFromReleaseType: logger.rangeFromReleaseType,
50
+ rangeFromVersions: logger.rangeFromVersions,
51
+ semVerFactory: logger.semVerFactory
52
+ });
53
+
54
+ var index$d = /*#__PURE__*/Object.freeze({
296
55
  __proto__: null,
297
- fileScannerFactory
298
- }, Symbol.toStringTag, { value: "Module" }));
299
- class AbstractRunner {
300
- paths;
301
- configuration;
302
- constructor(paths, configuration) {
303
- this.paths = paths;
304
- this.configuration = configuration;
305
- }
306
- async run(codemod, configuration) {
307
- const isValidCodemod = this.valid(codemod);
308
- if (!isValidCodemod) {
309
- throw new Error(`Invalid codemod provided to the runner: ${codemod.filename}`);
310
- }
311
- const runConfiguration = { ...this.configuration, ...configuration };
312
- return this.runner(codemod.path, this.paths, runConfiguration);
313
- }
314
- }
315
- class CodeRunner extends AbstractRunner {
316
- runner = Runner.run;
317
- valid(codemod) {
318
- return codemod.kind === "code";
319
- }
320
- }
321
- const codeRunnerFactory = (paths, configuration) => {
322
- return new CodeRunner(paths, configuration);
323
- };
324
- const index$c = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
56
+ fileScannerFactory: logger.fileScannerFactory
57
+ });
58
+
59
+ var index$c = /*#__PURE__*/Object.freeze({
325
60
  __proto__: null,
326
- codeRunnerFactory
327
- }, Symbol.toStringTag, { value: "Module" }));
328
- const transformJSON = async (codemodPath, paths, config) => {
329
- const { dry } = config;
330
- const startTime = process.hrtime();
331
- const report = {
332
- ok: 0,
333
- nochange: 0,
334
- skip: 0,
335
- error: 0,
336
- timeElapsed: "",
337
- stats: {}
338
- };
339
- const esbuildOptions = {
340
- extensions: [".js", ".mjs", ".ts"],
341
- hookIgnoreNodeModules: false,
342
- hookMatcher: fp.isEqual(codemodPath)
343
- };
344
- const { unregister } = node.register(esbuildOptions);
345
- const module2 = require(codemodPath);
346
- unregister();
347
- const codemod = typeof module2.default === "function" ? module2.default : module2;
348
- assert__default.default(typeof codemod === "function", `Codemod must be a function. Found ${typeof codemod}`);
349
- for (const path2 of paths) {
350
- try {
351
- const json = await readJSON(path2);
352
- assert__default.default(typeof json === "object" && !Array.isArray(json) && json !== null);
353
- const file = { path: path2, json };
354
- const params = { cwd: config.cwd, json: createJSONTransformAPI };
355
- const out = await codemod(file, params);
356
- if (out === void 0) {
357
- report.error += 1;
358
- } else if (!fp.isEqual(json, out)) {
359
- if (!dry) {
360
- await saveJSON(path2, out);
361
- }
362
- report.ok += 1;
363
- } else {
364
- report.nochange += 1;
365
- }
366
- } catch {
367
- report.error += 1;
368
- }
369
- }
370
- const endTime = process.hrtime(startTime);
371
- report.timeElapsed = (endTime[0] + endTime[1] / 1e9).toFixed(3);
372
- return report;
373
- };
374
- class JSONRunner extends AbstractRunner {
375
- runner = transformJSON;
376
- valid(codemod) {
377
- return codemod.kind === "json";
378
- }
379
- }
380
- const jsonRunnerFactory = (paths, configuration) => {
381
- return new JSONRunner(paths, configuration);
382
- };
383
- const index$b = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
61
+ codeRunnerFactory: logger.codeRunnerFactory
62
+ });
63
+
64
+ var index$b = /*#__PURE__*/Object.freeze({
384
65
  __proto__: null,
385
- jsonRunnerFactory
386
- }, Symbol.toStringTag, { value: "Module" }));
387
- const PROJECT_PACKAGE_JSON = "package.json";
388
- const PROJECT_APP_ALLOWED_ROOT_PATHS = ["src", "config", "public"];
389
- const PROJECT_PLUGIN_ALLOWED_ROOT_PATHS = ["admin", "server"];
390
- const PROJECT_PLUGIN_ROOT_FILES = ["strapi-admin.js", "strapi-server.js"];
391
- const PROJECT_CODE_EXTENSIONS = [
392
- // Source files
393
- "js",
394
- "mjs",
395
- "ts",
396
- // React files
397
- "jsx",
398
- "tsx"
399
- ];
400
- const PROJECT_JSON_EXTENSIONS = ["json"];
401
- const PROJECT_ALLOWED_EXTENSIONS = [...PROJECT_CODE_EXTENSIONS, ...PROJECT_JSON_EXTENSIONS];
402
- const SCOPED_STRAPI_PACKAGE_PREFIX = "@strapi/";
403
- const STRAPI_DEPENDENCY_NAME = `${SCOPED_STRAPI_PACKAGE_PREFIX}strapi`;
404
- const constants$3 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
66
+ jsonRunnerFactory: logger.jsonRunnerFactory
67
+ });
68
+
69
+ var index$a = /*#__PURE__*/Object.freeze({
70
+ __proto__: null,
71
+ assertAppProject: logger.assertAppProject,
72
+ assertPluginProject: logger.assertPluginProject,
73
+ constants: logger.constants$1,
74
+ isApplicationProject: logger.isApplicationProject,
75
+ isPluginProject: logger.isPluginProject,
76
+ projectFactory: logger.projectFactory
77
+ });
78
+
79
+ var index$9 = /*#__PURE__*/Object.freeze({
405
80
  __proto__: null,
406
- PROJECT_ALLOWED_EXTENSIONS,
407
- PROJECT_APP_ALLOWED_ROOT_PATHS,
408
- PROJECT_CODE_EXTENSIONS,
409
- PROJECT_JSON_EXTENSIONS,
410
- PROJECT_PACKAGE_JSON,
411
- PROJECT_PLUGIN_ALLOWED_ROOT_PATHS,
412
- PROJECT_PLUGIN_ROOT_FILES,
413
- SCOPED_STRAPI_PACKAGE_PREFIX,
414
- STRAPI_DEPENDENCY_NAME
415
- }, Symbol.toStringTag, { value: "Module" }));
416
- class Project {
417
- cwd;
418
- // The following properties are assigned during the .refresh() call in the constructor.
419
- files;
420
- packageJSONPath;
421
- packageJSON;
422
- paths;
423
- constructor(cwd, config) {
424
- if (!fse__default.default.pathExistsSync(cwd)) {
425
- throw new Error(`ENOENT: no such file or directory, access '${cwd}'`);
426
- }
427
- this.cwd = cwd;
428
- this.paths = config.paths;
429
- this.refresh();
430
- }
431
- getFilesByExtensions(extensions) {
432
- return this.files.filter((filePath) => {
433
- const fileExtension = path__default.default.extname(filePath);
434
- return extensions.includes(fileExtension);
435
- });
436
- }
437
- refresh() {
438
- this.refreshPackageJSON();
439
- this.refreshProjectFiles();
440
- return this;
441
- }
442
- async runCodemods(codemods, options) {
443
- const runners = this.createProjectCodemodsRunners(options.dry);
444
- const reports2 = [];
445
- for (const codemod of codemods) {
446
- for (const runner of runners) {
447
- if (runner.valid(codemod)) {
448
- const report = await runner.run(codemod);
449
- reports2.push({ codemod, report });
450
- }
451
- }
452
- }
453
- return reports2;
454
- }
455
- createProjectCodemodsRunners(dry = false) {
456
- const jsonExtensions = PROJECT_JSON_EXTENSIONS.map((ext) => `.${ext}`);
457
- const codeExtensions = PROJECT_CODE_EXTENSIONS.map((ext) => `.${ext}`);
458
- const jsonFiles = this.getFilesByExtensions(jsonExtensions);
459
- const codeFiles = this.getFilesByExtensions(codeExtensions);
460
- const codeRunner = codeRunnerFactory(codeFiles, {
461
- dry,
462
- parser: "ts",
463
- runInBand: true,
464
- babel: true,
465
- extensions: PROJECT_CODE_EXTENSIONS.join(","),
466
- // Don't output any log coming from the runner
467
- print: false,
468
- silent: true,
469
- verbose: 0
470
- });
471
- const jsonRunner = jsonRunnerFactory(jsonFiles, { dry, cwd: this.cwd });
472
- return [codeRunner, jsonRunner];
473
- }
474
- refreshPackageJSON() {
475
- const packageJSONPath = path__default.default.join(this.cwd, PROJECT_PACKAGE_JSON);
476
- try {
477
- fse__default.default.accessSync(packageJSONPath);
478
- } catch {
479
- throw new Error(`Could not find a ${PROJECT_PACKAGE_JSON} file in ${this.cwd}`);
480
- }
481
- const packageJSONBuffer = fse__default.default.readFileSync(packageJSONPath);
482
- this.packageJSONPath = packageJSONPath;
483
- this.packageJSON = JSON.parse(packageJSONBuffer.toString());
484
- }
485
- refreshProjectFiles() {
486
- const scanner = fileScannerFactory(this.cwd);
487
- this.files = scanner.scan(this.paths);
488
- }
489
- }
490
- class AppProject extends Project {
491
- strapiVersion;
492
- type = "application";
493
- /**
494
- * Returns an array of allowed file paths for a Strapi application
495
- *
496
- * The resulting paths include app default files and the root package.json file.
497
- */
498
- static get paths() {
499
- const allowedRootPaths = formatGlobCollectionPattern(PROJECT_APP_ALLOWED_ROOT_PATHS);
500
- const allowedExtensions = formatGlobCollectionPattern(PROJECT_ALLOWED_EXTENSIONS);
501
- return [
502
- // App default files
503
- `./${allowedRootPaths}/**/*.${allowedExtensions}`,
504
- // Root package.json file
505
- PROJECT_PACKAGE_JSON
506
- ];
507
- }
508
- constructor(cwd) {
509
- super(cwd, { paths: AppProject.paths });
510
- this.refreshStrapiVersion();
511
- }
512
- refresh() {
513
- super.refresh();
514
- this.refreshStrapiVersion();
515
- return this;
516
- }
517
- refreshStrapiVersion() {
518
- this.strapiVersion = // First try to get the strapi version from the package.json dependencies
519
- this.findStrapiVersionFromProjectPackageJSON() ?? // If the version found is not a valid SemVer, get the Strapi version from the installed package
520
- this.findLocallyInstalledStrapiVersion();
521
- }
522
- findStrapiVersionFromProjectPackageJSON() {
523
- const projectName = this.packageJSON.name;
524
- const version2 = this.packageJSON.dependencies?.[STRAPI_DEPENDENCY_NAME];
525
- if (version2 === void 0) {
526
- throw new Error(
527
- `No version of ${STRAPI_DEPENDENCY_NAME} was found in ${projectName}. Are you in a valid Strapi project?`
528
- );
529
- }
530
- const isValidSemVer2 = isLiteralSemVer(version2) && semver__default.default.valid(version2) === version2;
531
- return isValidSemVer2 ? semVerFactory(version2) : void 0;
532
- }
533
- findLocallyInstalledStrapiVersion() {
534
- const packageSearchText = `${STRAPI_DEPENDENCY_NAME}/package.json`;
535
- let strapiPackageJSONPath;
536
- let strapiPackageJSON;
537
- try {
538
- strapiPackageJSONPath = require.resolve(packageSearchText, { paths: [this.cwd] });
539
- strapiPackageJSON = require(strapiPackageJSONPath);
540
- assert__default.default(typeof strapiPackageJSON === "object");
541
- } catch {
542
- throw new Error(
543
- `Cannot resolve module "${STRAPI_DEPENDENCY_NAME}" from paths [${this.cwd}]`
544
- );
545
- }
546
- const strapiVersion = strapiPackageJSON.version;
547
- if (!isValidSemVer(strapiVersion)) {
548
- throw new Error(
549
- `Invalid ${STRAPI_DEPENDENCY_NAME} version found in ${strapiPackageJSONPath} (${strapiVersion})`
550
- );
551
- }
552
- return semVerFactory(strapiVersion);
553
- }
554
- }
555
- const formatGlobCollectionPattern = (collection) => {
556
- assert__default.default(
557
- collection.length > 0,
558
- "Invalid pattern provided, the given collection needs at least 1 element"
559
- );
560
- return collection.length === 1 ? collection[0] : `{${collection}}`;
561
- };
562
- class PluginProject extends Project {
563
- type = "plugin";
564
- /**
565
- * Returns an array of allowed file paths for a Strapi plugin
566
- *
567
- * The resulting paths include plugin default files, the root package.json file, and plugin-specific files.
568
- */
569
- static get paths() {
570
- const allowedRootPaths = formatGlobCollectionPattern(
571
- PROJECT_PLUGIN_ALLOWED_ROOT_PATHS
572
- );
573
- const allowedExtensions = formatGlobCollectionPattern(PROJECT_ALLOWED_EXTENSIONS);
574
- return [
575
- // Plugin default files
576
- `./${allowedRootPaths}/**/*.${allowedExtensions}`,
577
- // Root package.json file
578
- PROJECT_PACKAGE_JSON,
579
- // Plugin root files
580
- ...PROJECT_PLUGIN_ROOT_FILES
581
- ];
582
- }
583
- constructor(cwd) {
584
- super(cwd, { paths: PluginProject.paths });
585
- }
586
- }
587
- const isPlugin = (cwd) => {
588
- const packageJSONPath = path__default.default.join(cwd, PROJECT_PACKAGE_JSON);
589
- try {
590
- fse__default.default.accessSync(packageJSONPath);
591
- } catch {
592
- throw new Error(`Could not find a ${PROJECT_PACKAGE_JSON} file in ${cwd}`);
593
- }
594
- const packageJSONBuffer = fse__default.default.readFileSync(packageJSONPath);
595
- const packageJSON = JSON.parse(packageJSONBuffer.toString());
596
- return packageJSON?.strapi?.kind === "plugin";
597
- };
598
- const projectFactory = (cwd) => {
599
- fse__default.default.accessSync(cwd);
600
- return isPlugin(cwd) ? new PluginProject(cwd) : new AppProject(cwd);
601
- };
602
- const isPluginProject = (project) => {
603
- return project instanceof PluginProject;
604
- };
605
- function assertPluginProject(project) {
606
- if (!isPluginProject(project)) {
607
- throw new Error("Project is not a plugin");
608
- }
609
- }
610
- const isApplicationProject = (project) => {
611
- return project instanceof AppProject;
612
- };
613
- function assertAppProject(project) {
614
- if (!isApplicationProject(project)) {
615
- throw new Error("Project is not an application");
616
- }
617
- }
618
- const index$a = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
81
+ AbortedError: logger.AbortedError,
82
+ NPMCandidateNotFoundError: logger.NPMCandidateNotFoundError,
83
+ UnexpectedError: logger.UnexpectedError,
84
+ unknownToError: logger.unknownToError
85
+ });
86
+
87
+ var index$8 = /*#__PURE__*/Object.freeze({
619
88
  __proto__: null,
620
- assertAppProject,
621
- assertPluginProject,
622
- constants: constants$3,
623
- isApplicationProject,
624
- isPluginProject,
625
- projectFactory
626
- }, Symbol.toStringTag, { value: "Module" }));
627
- class UnexpectedError extends Error {
628
- constructor() {
629
- super("Unexpected Error");
630
- }
631
- }
632
- const unknownToError = (e) => {
633
- if (e instanceof Error) {
634
- return e;
635
- }
636
- if (typeof e === "string") {
637
- return new Error(e);
638
- }
639
- return new UnexpectedError();
640
- };
641
- const index$9 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
89
+ codemodFactory: logger.codemodFactory,
90
+ constants: logger.constants$2
91
+ });
92
+
93
+ var index$7 = /*#__PURE__*/Object.freeze({
642
94
  __proto__: null,
643
- UnexpectedError,
644
- unknownToError
645
- }, Symbol.toStringTag, { value: "Module" }));
646
- const path = (path2) => chalk__default.default.blue(path2);
647
- const version = (version2) => {
648
- return chalk__default.default.italic.yellow(`v${version2}`);
649
- };
650
- const codemodUID = (uid) => {
651
- return chalk__default.default.bold.cyan(uid);
652
- };
653
- const projectDetails = (project) => {
654
- return `Project: TYPE=${projectType(project.type)}; CWD=${path(project.cwd)}; PATHS=${project.paths.map(path)}`;
655
- };
656
- const projectType = (type) => chalk__default.default.cyan(type);
657
- const versionRange = (range) => chalk__default.default.italic.yellow(range.raw);
658
- const transform = (transformFilePath) => chalk__default.default.cyan(transformFilePath);
659
- const highlight = (arg) => chalk__default.default.bold.underline(arg);
660
- const upgradeStep = (text, step) => {
661
- return chalk__default.default.bold(`(${step[0]}/${step[1]}) ${text}...`);
662
- };
663
- const reports = (reports2) => {
664
- const rows = reports2.map(({ codemod, report }, i) => {
665
- const fIndex = chalk__default.default.grey(i);
666
- const fVersion = chalk__default.default.magenta(codemod.version);
667
- const fKind = chalk__default.default.yellow(codemod.kind);
668
- const fFormattedTransformPath = chalk__default.default.cyan(codemod.format());
669
- const fTimeElapsed = i === 0 ? `${report.timeElapsed}s ${chalk__default.default.dim.italic("(cold start)")}` : `${report.timeElapsed}s`;
670
- const fAffected = report.ok > 0 ? chalk__default.default.green(report.ok) : chalk__default.default.grey(0);
671
- const fUnchanged = report.ok === 0 ? chalk__default.default.red(report.nochange) : chalk__default.default.grey(report.nochange);
672
- return [fIndex, fVersion, fKind, fFormattedTransformPath, fAffected, fUnchanged, fTimeElapsed];
673
- });
674
- const table = new CliTable3__default.default({
675
- style: { compact: true },
676
- head: [
677
- chalk__default.default.bold.grey("N°"),
678
- chalk__default.default.bold.magenta("Version"),
679
- chalk__default.default.bold.yellow("Kind"),
680
- chalk__default.default.bold.cyan("Name"),
681
- chalk__default.default.bold.green("Affected"),
682
- chalk__default.default.bold.red("Unchanged"),
683
- chalk__default.default.bold.blue("Duration")
684
- ]
685
- });
686
- table.push(...rows);
687
- return table.toString();
688
- };
689
- const codemodList = (codemods) => {
690
- const rows = codemods.map((codemod, index2) => {
691
- const fIndex = chalk__default.default.grey(index2);
692
- const fVersion = chalk__default.default.magenta(codemod.version);
693
- const fKind = chalk__default.default.yellow(codemod.kind);
694
- const fName = chalk__default.default.blue(codemod.format());
695
- const fUID = codemodUID(codemod.uid);
696
- return [fIndex, fVersion, fKind, fName, fUID];
697
- });
698
- const table = new CliTable3__default.default({
699
- style: { compact: true },
700
- head: [
701
- chalk__default.default.bold.grey("N°"),
702
- chalk__default.default.bold.magenta("Version"),
703
- chalk__default.default.bold.yellow("Kind"),
704
- chalk__default.default.bold.blue("Name"),
705
- chalk__default.default.bold.cyan("UID")
706
- ]
707
- });
708
- table.push(...rows);
709
- return table.toString();
710
- };
711
- const durationMs = (elapsedMs) => {
712
- const elapsedSeconds = (elapsedMs / ONE_SECOND_MS).toFixed(3);
713
- return `${elapsedSeconds}s`;
714
- };
715
- const index$8 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
95
+ codemodRepositoryFactory: logger.codemodRepositoryFactory,
96
+ constants: logger.constants$3
97
+ });
98
+
99
+ var index$6 = /*#__PURE__*/Object.freeze({
716
100
  __proto__: null,
717
- codemodList,
718
- codemodUID,
719
- durationMs,
720
- highlight,
721
- path,
722
- projectDetails,
723
- projectType,
724
- reports,
725
- transform,
726
- upgradeStep,
727
- version,
728
- versionRange
729
- }, Symbol.toStringTag, { value: "Module" }));
730
- const CODEMOD_CODE_SUFFIX = "code";
731
- const CODEMOD_JSON_SUFFIX = "json";
732
- const CODEMOD_ALLOWED_SUFFIXES = [CODEMOD_CODE_SUFFIX, CODEMOD_JSON_SUFFIX];
733
- const CODEMOD_EXTENSION = "ts";
734
- const CODEMOD_FILE_REGEXP = new RegExp(
735
- `^.+[.](${CODEMOD_ALLOWED_SUFFIXES.join("|")})[.]${CODEMOD_EXTENSION}$`
736
- );
737
- const constants$2 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
101
+ constants: logger.constants$4,
102
+ upgraderFactory: logger.upgraderFactory
103
+ });
104
+
105
+ var index$5 = /*#__PURE__*/Object.freeze({
738
106
  __proto__: null,
739
- CODEMOD_ALLOWED_SUFFIXES,
740
- CODEMOD_CODE_SUFFIX,
741
- CODEMOD_EXTENSION,
742
- CODEMOD_FILE_REGEXP,
743
- CODEMOD_JSON_SUFFIX
744
- }, Symbol.toStringTag, { value: "Module" }));
745
- class Codemod {
746
- uid;
747
- kind;
748
- version;
749
- baseDirectory;
750
- filename;
751
- path;
752
- constructor(options) {
753
- this.kind = options.kind;
754
- this.version = options.version;
755
- this.baseDirectory = options.baseDirectory;
756
- this.filename = options.filename;
757
- this.path = path__default.default.join(this.baseDirectory, this.version.raw, this.filename);
758
- this.uid = this.createUID();
759
- }
760
- createUID() {
761
- const name = this.format({ stripExtension: true, stripKind: true, stripHyphens: false });
762
- const kind = this.kind;
763
- const version2 = this.version.raw;
764
- return `${version2}-${name}-${kind}`;
765
- }
766
- format(options) {
767
- const { stripExtension = true, stripKind = true, stripHyphens = true } = options ?? {};
768
- let formatted = this.filename;
769
- if (stripExtension) {
770
- formatted = formatted.replace(new RegExp(`\\.${CODEMOD_EXTENSION}$`, "i"), "");
771
- }
772
- if (stripKind) {
773
- formatted = formatted.replace(`.${CODEMOD_CODE_SUFFIX}`, "").replace(`.${CODEMOD_JSON_SUFFIX}`, "");
774
- }
775
- if (stripHyphens) {
776
- formatted = formatted.replaceAll("-", " ");
777
- }
778
- return formatted;
779
- }
780
- }
781
- const codemodFactory = (options) => new Codemod(options);
782
- const index$7 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
107
+ requirementFactory: logger.requirementFactory
108
+ });
109
+
110
+ var index$4 = /*#__PURE__*/Object.freeze({
783
111
  __proto__: null,
784
- codemodFactory,
785
- constants: constants$2
786
- }, Symbol.toStringTag, { value: "Module" }));
787
- const INTERNAL_CODEMODS_DIRECTORY = path__default.default.join(
788
- __dirname,
789
- // upgrade/dist
790
- "..",
791
- // upgrade
792
- "resources",
793
- // upgrade/resources
794
- "codemods"
795
- // upgrade/resources/codemods
796
- );
797
- const constants$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
112
+ listCodemods: logger.listCodemods,
113
+ runCodemods: logger.runCodemods,
114
+ upgrade: logger.upgrade
115
+ });
116
+
117
+ var index$3 = /*#__PURE__*/Object.freeze({
798
118
  __proto__: null,
799
- INTERNAL_CODEMODS_DIRECTORY
800
- }, Symbol.toStringTag, { value: "Module" }));
801
- class CodemodRepository {
802
- groups;
803
- versions;
804
- cwd;
805
- constructor(cwd) {
806
- assert__default.default(fse__default.default.existsSync(cwd), `Invalid codemods directory provided "${cwd}"`);
807
- this.cwd = cwd;
808
- this.groups = {};
809
- this.versions = [];
810
- }
811
- refresh() {
812
- this.refreshAvailableVersions();
813
- this.refreshAvailableFiles();
814
- return this;
815
- }
816
- count(version2) {
817
- return this.findByVersion(version2).length;
818
- }
819
- versionExists(version2) {
820
- return version2.raw in this.groups;
821
- }
822
- has(uid) {
823
- const result = this.find({ uids: [uid] });
824
- if (result.length !== 1) {
825
- return false;
826
- }
827
- const { codemods } = result[0];
828
- return codemods.length === 1 && codemods[0].uid === uid;
829
- }
830
- find(q) {
831
- const entries = Object.entries(this.groups);
832
- return entries.filter(maybeFilterByRange).map(([version2, codemods]) => ({
833
- version: semVerFactory(version2),
834
- // Filter by UID if provided in the query
835
- codemods: codemods.filter(maybeFilterByUIDs)
836
- })).filter(({ codemods }) => codemods.length > 0);
837
- function maybeFilterByRange([version2]) {
838
- if (!isRangeInstance(q.range)) {
839
- return true;
840
- }
841
- return q.range.test(version2);
842
- }
843
- function maybeFilterByUIDs(codemod) {
844
- if (q.uids === void 0) {
845
- return true;
846
- }
847
- return q.uids.includes(codemod.uid);
848
- }
849
- }
850
- findByVersion(version2) {
851
- const literalVersion = version2.raw;
852
- const codemods = this.groups[literalVersion];
853
- return codemods ?? [];
854
- }
855
- findAll() {
856
- const entries = Object.entries(this.groups);
857
- return entries.map(([version2, codemods]) => ({
858
- version: semVerFactory(version2),
859
- codemods
860
- }));
861
- }
862
- refreshAvailableVersions() {
863
- this.versions = fse__default.default.readdirSync(this.cwd).filter((filename) => fse__default.default.statSync(path__default.default.join(this.cwd, filename)).isDirectory()).filter((filename) => semver__default.default.valid(filename) !== null).map((version2) => semVerFactory(version2)).sort(semver__default.default.compare);
864
- return this;
865
- }
866
- refreshAvailableFiles() {
867
- this.groups = {};
868
- for (const version2 of this.versions) {
869
- this.refreshAvailableFilesForVersion(version2);
870
- }
871
- }
872
- refreshAvailableFilesForVersion(version2) {
873
- const literalVersion = version2.raw;
874
- const versionDirectory = path__default.default.join(this.cwd, literalVersion);
875
- if (!fse__default.default.existsSync(versionDirectory)) {
876
- return;
877
- }
878
- this.groups[literalVersion] = fse__default.default.readdirSync(versionDirectory).filter((filename) => fse__default.default.statSync(path__default.default.join(versionDirectory, filename)).isFile()).filter((filename) => CODEMOD_FILE_REGEXP.test(filename)).map((filename) => {
879
- const kind = parseCodemodKindFromFilename(filename);
880
- const baseDirectory = this.cwd;
881
- return codemodFactory({ kind, baseDirectory, version: version2, filename });
119
+ loggerFactory: logger.loggerFactory
120
+ });
121
+
122
+ const codemodReportFactory = (codemod, report)=>({
123
+ codemod,
124
+ report
882
125
  });
883
- }
884
- }
885
- const parseCodemodKindFromFilename = (filename) => {
886
- const kind = filename.split(".").at(-2);
887
- assert__default.default(kind !== void 0);
888
- assert__default.default(CODEMOD_ALLOWED_SUFFIXES.includes(kind));
889
- return kind;
890
- };
891
- const codemodRepositoryFactory = (cwd = INTERNAL_CODEMODS_DIRECTORY) => {
892
- return new CodemodRepository(cwd);
893
- };
894
- const index$6 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
895
- __proto__: null,
896
- codemodRepositoryFactory,
897
- constants: constants$1
898
- }, Symbol.toStringTag, { value: "Module" }));
899
- class CodemodRunner {
900
- project;
901
- range;
902
- isDry;
903
- logger;
904
- selectCodemodsCallback;
905
- constructor(project, range) {
906
- this.project = project;
907
- this.range = range;
908
- this.isDry = false;
909
- this.logger = null;
910
- this.selectCodemodsCallback = null;
911
- }
912
- setRange(range) {
913
- this.range = range;
914
- return this;
915
- }
916
- setLogger(logger) {
917
- this.logger = logger;
918
- return this;
919
- }
920
- onSelectCodemods(callback) {
921
- this.selectCodemodsCallback = callback;
922
- return this;
923
- }
924
- dry(enabled = true) {
925
- this.isDry = enabled;
926
- return this;
927
- }
928
- createRepository(codemodsDirectory) {
929
- const repository = codemodRepositoryFactory(
930
- codemodsDirectory ?? INTERNAL_CODEMODS_DIRECTORY
931
- );
932
- repository.refresh();
933
- return repository;
934
- }
935
- async safeRunAndReport(codemods) {
936
- if (this.isDry) {
937
- this.logger?.warn?.(
938
- "Running the codemods in dry mode. No files will be modified during the process."
939
- );
940
- }
941
- try {
942
- const reports$1 = await this.project.runCodemods(codemods, { dry: this.isDry });
943
- this.logger?.raw?.(reports(reports$1));
944
- if (!this.isDry) {
945
- const nbAffectedTotal = reports$1.flatMap((report) => report.report.ok).reduce((acc, nb) => acc + nb, 0);
946
- this.logger?.debug?.(
947
- `Successfully ran ${highlight(codemods.length)} codemod(s), ${highlight(nbAffectedTotal)} change(s) have been detected`
948
- );
949
- }
950
- return successReport$1();
951
- } catch (e) {
952
- return erroredReport$1(unknownToError(e));
953
- }
954
- }
955
- async runByUID(uid, codemodsDirectory) {
956
- const repository = this.createRepository(codemodsDirectory);
957
- if (!repository.has(uid)) {
958
- throw new Error(`Unknown codemod UID provided: ${uid}`);
959
- }
960
- const codemods = repository.find({ uids: [uid] }).flatMap(({ codemods: codemods2 }) => codemods2);
961
- return this.safeRunAndReport(codemods);
962
- }
963
- async run(codemodsDirectory) {
964
- const repository = this.createRepository(codemodsDirectory);
965
- const codemodsInRange = repository.find({ range: this.range });
966
- const selectedCodemods = this.selectCodemodsCallback ? await this.selectCodemodsCallback(codemodsInRange) : codemodsInRange;
967
- if (selectedCodemods.length === 0) {
968
- this.logger?.debug?.(`Found no codemods to run for ${versionRange(this.range)}`);
969
- return successReport$1();
970
- }
971
- const codemods = selectedCodemods.flatMap(({ codemods: codemods2 }) => codemods2);
972
- const codemodsByVersion = fp.groupBy("version", codemods);
973
- const fRange = versionRange(this.range);
974
- this.logger?.debug?.(
975
- `Found ${highlight(codemods.length)} codemods for ${highlight(fp.size(codemodsByVersion))} version(s) using ${fRange}`
976
- );
977
- for (const [version$1, codemods2] of Object.entries(codemodsByVersion)) {
978
- this.logger?.debug?.(`- ${version(semVerFactory(version$1))} (${codemods2.length})`);
979
- }
980
- return this.safeRunAndReport(codemods);
981
- }
982
- }
983
- const codemodRunnerFactory = (project, range) => {
984
- return new CodemodRunner(project, range);
985
- };
986
- const successReport$1 = () => ({ success: true, error: null });
987
- const erroredReport$1 = (error) => ({ success: false, error });
988
- class Upgrader {
989
- project;
990
- npmPackage;
991
- target;
992
- codemodsTarget;
993
- isDry;
994
- logger;
995
- requirements;
996
- confirmationCallback;
997
- constructor(project, target, npmPackage) {
998
- this.project = project;
999
- this.npmPackage = npmPackage;
1000
- this.target = target;
1001
- this.syncCodemodsTarget();
1002
- this.isDry = false;
1003
- this.requirements = [];
1004
- this.logger = null;
1005
- this.confirmationCallback = null;
1006
- }
1007
- setRequirements(requirements) {
1008
- this.requirements = requirements;
1009
- return this;
1010
- }
1011
- setTarget(target) {
1012
- this.target = target;
1013
- return this;
1014
- }
1015
- syncCodemodsTarget() {
1016
- this.codemodsTarget = semVerFactory(
1017
- `${this.target.major}.${this.target.minor}.${this.target.patch}`
1018
- );
1019
- this.logger?.debug?.(
1020
- `The codemods target has been synced with the upgrade target. The codemod runner will now look for ${version(
1021
- this.codemodsTarget
1022
- )}`
1023
- );
1024
- return this;
1025
- }
1026
- overrideCodemodsTarget(target) {
1027
- this.codemodsTarget = target;
1028
- this.logger?.debug?.(
1029
- `Overriding the codemods target. The codemod runner will now look for ${version(target)}`
1030
- );
1031
- return this;
1032
- }
1033
- setLogger(logger) {
1034
- this.logger = logger;
1035
- return this;
1036
- }
1037
- onConfirm(callback) {
1038
- this.confirmationCallback = callback;
1039
- return this;
1040
- }
1041
- dry(enabled = true) {
1042
- this.isDry = enabled;
1043
- return this;
1044
- }
1045
- addRequirement(requirement) {
1046
- this.requirements.push(requirement);
1047
- const fRequired = requirement.isRequired ? "(required)" : "(optional)";
1048
- this.logger?.debug?.(
1049
- `Added a new requirement to the upgrade: ${highlight(requirement.name)} ${fRequired}`
1050
- );
1051
- return this;
1052
- }
1053
- async upgrade() {
1054
- this.logger?.info?.(
1055
- `Upgrading from ${version(this.project.strapiVersion)} to ${version(this.target)}`
1056
- );
1057
- if (this.isDry) {
1058
- this.logger?.warn?.(
1059
- "Running the upgrade in dry mode. No files will be modified during the process."
1060
- );
1061
- }
1062
- const range = rangeFromVersions(this.project.strapiVersion, this.target);
1063
- const codemodsRange = rangeFromVersions(this.project.strapiVersion, this.codemodsTarget);
1064
- const npmVersionsMatches = this.npmPackage?.findVersionsInRange(range) ?? [];
1065
- this.logger?.debug?.(
1066
- `Found ${highlight(npmVersionsMatches.length)} versions satisfying ${versionRange(range)}`
1067
- );
1068
- try {
1069
- this.logger?.info?.(upgradeStep("Checking requirement", [1, 4]));
1070
- await this.checkRequirements(this.requirements, {
1071
- npmVersionsMatches,
1072
- project: this.project,
1073
- target: this.target
1074
- });
1075
- this.logger?.info?.(upgradeStep("Applying the latest code modifications", [2, 4]));
1076
- await this.runCodemods(codemodsRange);
1077
- this.logger?.debug?.("Refreshing project information...");
1078
- this.project.refresh();
1079
- this.logger?.info?.(upgradeStep("Upgrading Strapi dependencies", [3, 4]));
1080
- await this.updateDependencies();
1081
- this.logger?.info?.(upgradeStep("Installing dependencies", [4, 4]));
1082
- await this.installDependencies();
1083
- } catch (e) {
1084
- return erroredReport(unknownToError(e));
1085
- }
1086
- return successReport();
1087
- }
1088
- async checkRequirements(requirements, context) {
1089
- for (const requirement of requirements) {
1090
- const { pass, error } = await requirement.test(context);
1091
- if (pass) {
1092
- await this.onSuccessfulRequirement(requirement, context);
1093
- } else {
1094
- await this.onFailedRequirement(requirement, error);
1095
- }
1096
- }
1097
- }
1098
- async onSuccessfulRequirement(requirement, context) {
1099
- const hasChildren = requirement.children.length > 0;
1100
- if (hasChildren) {
1101
- await this.checkRequirements(requirement.children, context);
1102
- }
1103
- }
1104
- async onFailedRequirement(requirement, originalError) {
1105
- const errorMessage = `Requirement failed: ${originalError.message} (${highlight(
1106
- requirement.name
1107
- )})`;
1108
- const warningMessage = originalError.message;
1109
- const confirmationMessage = `Ignore optional requirement "${highlight(requirement.name)}" ?`;
1110
- const error = new Error(errorMessage);
1111
- if (requirement.isRequired) {
1112
- throw error;
1113
- }
1114
- this.logger?.warn?.(warningMessage);
1115
- const response = await this.confirmationCallback?.(confirmationMessage);
1116
- if (!response) {
1117
- throw error;
1118
- }
1119
- }
1120
- async updateDependencies() {
1121
- const { packageJSON, packageJSONPath } = this.project;
1122
- const json = createJSONTransformAPI(packageJSON);
1123
- const dependencies = json.get("dependencies", {});
1124
- const strapiDependencies = this.getScopedStrapiDependencies(dependencies);
1125
- this.logger?.debug?.(
1126
- `Found ${highlight(strapiDependencies.length)} dependency(ies) to update`
1127
- );
1128
- strapiDependencies.forEach(
1129
- (dependency) => this.logger?.debug?.(`- ${dependency[0]} (${dependency[1]} -> ${this.target})`)
1130
- );
1131
- if (strapiDependencies.length === 0) {
1132
- return;
1133
- }
1134
- strapiDependencies.forEach(([name]) => json.set(`dependencies.${name}`, this.target.raw));
1135
- const updatedPackageJSON = json.root();
1136
- if (this.isDry) {
1137
- this.logger?.debug?.(`Skipping dependencies update (${chalk__default.default.italic("dry mode")})`);
1138
- return;
1139
- }
1140
- await saveJSON(packageJSONPath, updatedPackageJSON);
1141
- }
1142
- getScopedStrapiDependencies(dependencies) {
1143
- const { strapiVersion } = this.project;
1144
- const strapiDependencies = [];
1145
- for (const [name, version2] of Object.entries(dependencies)) {
1146
- const isScopedStrapiPackage = name.startsWith(SCOPED_STRAPI_PACKAGE_PREFIX);
1147
- const isOnCurrentStrapiVersion = isValidSemVer(version2) && version2 === strapiVersion.raw;
1148
- if (isScopedStrapiPackage && isOnCurrentStrapiVersion) {
1149
- strapiDependencies.push([name, semVerFactory(version2)]);
1150
- }
1151
- }
1152
- return strapiDependencies;
1153
- }
1154
- async installDependencies() {
1155
- const projectPath = this.project.cwd;
1156
- const packageManagerName = await utils.packageManager.getPreferred(projectPath);
1157
- this.logger?.debug?.(`Using ${highlight(packageManagerName)} as package manager`);
1158
- if (this.isDry) {
1159
- this.logger?.debug?.(`Skipping dependencies installation (${chalk__default.default.italic("dry mode")}`);
1160
- return;
1161
- }
1162
- await utils.packageManager.installDependencies(projectPath, packageManagerName, {
1163
- stdout: this.logger?.stdout,
1164
- stderr: this.logger?.stderr
126
+ const reportFactory = (report)=>({
127
+ ...report
1165
128
  });
1166
- }
1167
- async runCodemods(range) {
1168
- const codemodRunner = codemodRunnerFactory(this.project, range);
1169
- codemodRunner.dry(this.isDry);
1170
- if (this.logger) {
1171
- codemodRunner.setLogger(this.logger);
1172
- }
1173
- await codemodRunner.run();
1174
- }
1175
- }
1176
- const resolveNPMTarget = (project, target, npmPackage) => {
1177
- if (isSemverInstance(target)) {
1178
- return npmPackage.findVersion(target);
1179
- }
1180
- if (isSemVerReleaseType(target)) {
1181
- const range = rangeFromVersions(project.strapiVersion, target);
1182
- const npmVersionsMatches = npmPackage.findVersionsInRange(range);
1183
- return npmVersionsMatches.at(-1);
1184
- }
1185
- return void 0;
1186
- };
1187
- const upgraderFactory = (project, target, npmPackage) => {
1188
- const targetedNPMVersion = resolveNPMTarget(project, target, npmPackage);
1189
- if (!targetedNPMVersion) {
1190
- throw new Error(`Couldn't find a matching version in the NPM registry for "${target}"`);
1191
- }
1192
- const semverTarget = semVerFactory(targetedNPMVersion.version);
1193
- if (semver__default.default.eq(semverTarget, project.strapiVersion)) {
1194
- throw new Error(`The project is already on ${version(semverTarget)}`);
1195
- }
1196
- return new Upgrader(project, semverTarget, npmPackage);
1197
- };
1198
- const successReport = () => ({ success: true, error: null });
1199
- const erroredReport = (error) => ({ success: false, error });
1200
- const STRAPI_PACKAGE_NAME = "@strapi/strapi";
1201
- const constants = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1202
- __proto__: null,
1203
- STRAPI_PACKAGE_NAME
1204
- }, Symbol.toStringTag, { value: "Module" }));
1205
- const index$5 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1206
- __proto__: null,
1207
- constants,
1208
- upgraderFactory
1209
- }, Symbol.toStringTag, { value: "Module" }));
1210
- const NPM_REGISTRY_URL = "https://registry.npmjs.org";
1211
- class Package {
1212
- name;
1213
- packageURL;
1214
- npmPackage;
1215
- constructor(name) {
1216
- this.name = name;
1217
- this.packageURL = `${NPM_REGISTRY_URL}/${name}`;
1218
- this.npmPackage = null;
1219
- }
1220
- get isLoaded() {
1221
- return this.npmPackage !== null;
1222
- }
1223
- assertPackageIsLoaded(npmPackage) {
1224
- assert__default.default(this.isLoaded, "The package is not loaded yet");
1225
- }
1226
- getVersionsDict() {
1227
- this.assertPackageIsLoaded(this.npmPackage);
1228
- return this.npmPackage.versions;
1229
- }
1230
- getVersionsAsList() {
1231
- this.assertPackageIsLoaded(this.npmPackage);
1232
- return Object.values(this.npmPackage.versions);
1233
- }
1234
- findVersionsInRange(range) {
1235
- const versions = this.getVersionsAsList();
1236
- return versions.filter((v) => range.test(v.version)).filter((v) => isLiteralSemVer(v.version)).sort((v1, v2) => semver__default.default.compare(v1.version, v2.version));
1237
- }
1238
- findVersion(version2) {
1239
- const versions = this.getVersionsAsList();
1240
- return versions.find((npmVersion) => semver__default.default.eq(npmVersion.version, version2));
1241
- }
1242
- async refresh() {
1243
- const response = await fetch(this.packageURL);
1244
- assert__default.default(response.ok, `Request failed for ${this.packageURL}`);
1245
- this.npmPackage = await response.json();
1246
- return this;
1247
- }
1248
- versionExists(version2) {
1249
- return this.findVersion(version2) !== void 0;
1250
- }
1251
- }
1252
- const npmPackageFactory = (name) => new Package(name);
1253
- const upgrade = async (options) => {
1254
- const timer = timerFactory();
1255
- const { logger, codemodsTarget } = options;
1256
- const cwd = path__default.default.resolve(options.cwd ?? process.cwd());
1257
- const project = projectFactory(cwd);
1258
- logger.debug(projectDetails(project));
1259
- if (!isApplicationProject(project)) {
1260
- throw new Error(
1261
- `The "${options.target}" upgrade can only be run on a Strapi project; for plugins, please use "codemods".`
1262
- );
1263
- }
1264
- const npmPackage = npmPackageFactory(STRAPI_PACKAGE_NAME);
1265
- await npmPackage.refresh();
1266
- const upgrader = upgraderFactory(project, options.target, npmPackage).dry(options.dry ?? false).onConfirm(options.confirm ?? null).setLogger(logger);
1267
- if (codemodsTarget !== void 0) {
1268
- upgrader.overrideCodemodsTarget(codemodsTarget);
1269
- }
1270
- if (options.target === ReleaseType.Major) {
1271
- upgrader.addRequirement(REQUIRE_AVAILABLE_NEXT_MAJOR).addRequirement(REQUIRE_LATEST_FOR_CURRENT_MAJOR);
1272
- }
1273
- upgrader.addRequirement(REQUIRE_GIT.asOptional());
1274
- const upgradeReport = await upgrader.upgrade();
1275
- if (!upgradeReport.success) {
1276
- throw upgradeReport.error;
1277
- }
1278
- timer.stop();
1279
- logger.info(`Completed in ${durationMs(timer.elapsedMs)}`);
1280
- };
1281
- const resolvePath = (cwd) => path__default.default.resolve(cwd ?? process.cwd());
1282
- const getRangeFromTarget = (currentVersion, target) => {
1283
- if (isSemverInstance(target)) {
1284
- return rangeFactory(target);
1285
- }
1286
- const { major, minor, patch } = currentVersion;
1287
- switch (target) {
1288
- case ReleaseType.Major:
1289
- return rangeFactory(`${major}`);
1290
- case ReleaseType.Minor:
1291
- return rangeFactory(`${major}.${minor}`);
1292
- case ReleaseType.Patch:
1293
- return rangeFactory(`${major}.${minor}.${patch}`);
1294
- default:
1295
- throw new Error(`Invalid target set: ${target}`);
1296
- }
1297
- };
1298
- const findRangeFromTarget = (project, target) => {
1299
- if (isRangeInstance(target)) {
1300
- return target;
1301
- }
1302
- if (isApplicationProject(project)) {
1303
- return getRangeFromTarget(project.strapiVersion, target);
1304
- }
1305
- return rangeFactory("*");
1306
- };
1307
- const runCodemods = async (options) => {
1308
- const timer = timerFactory();
1309
- const { logger, uid } = options;
1310
- const cwd = resolvePath(options.cwd);
1311
- const project = projectFactory(cwd);
1312
- const range = findRangeFromTarget(project, options.target);
1313
- logger.debug(projectDetails(project));
1314
- logger.debug(`Range: set to ${versionRange(range)}`);
1315
- const codemodRunner = codemodRunnerFactory(project, range).dry(options.dry ?? false).onSelectCodemods(options.selectCodemods ?? null).setLogger(logger);
1316
- let report;
1317
- if (uid !== void 0) {
1318
- logger.debug(`Running a single codemod: ${codemodUID(uid)}`);
1319
- report = await codemodRunner.runByUID(uid);
1320
- } else {
1321
- report = await codemodRunner.run();
1322
- }
1323
- if (!report.success) {
1324
- throw report.error;
1325
- }
1326
- timer.stop();
1327
- logger.info(`Completed in ${timer.elapsedMs}`);
1328
- };
1329
- const listCodemods = async (options) => {
1330
- const { logger, target } = options;
1331
- const cwd = resolvePath(options.cwd);
1332
- const project = projectFactory(cwd);
1333
- const range = findRangeFromTarget(project, target);
1334
- logger.debug(projectDetails(project));
1335
- logger.debug(`Range: set to ${versionRange(range)}`);
1336
- const repo = codemodRepositoryFactory();
1337
- repo.refresh();
1338
- const groups = repo.find({ range });
1339
- const codemods = groups.flatMap((collection) => collection.codemods);
1340
- logger.debug(`Found ${highlight(codemods.length)} codemods`);
1341
- if (codemods.length === 0) {
1342
- logger.info(`Found no codemods matching ${versionRange(range)}`);
1343
- return;
1344
- }
1345
- const fCodemods = codemodList(codemods);
1346
- logger.raw(fCodemods);
1347
- };
1348
- const index$4 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1349
- __proto__: null,
1350
- listCodemods,
1351
- runCodemods,
1352
- upgrade
1353
- }, Symbol.toStringTag, { value: "Module" }));
1354
- class Logger {
1355
- isDebug;
1356
- isSilent;
1357
- nbErrorsCalls;
1358
- nbWarningsCalls;
1359
- constructor(options = {}) {
1360
- this.isDebug = options.debug ?? false;
1361
- this.isSilent = options.silent ?? false;
1362
- this.nbErrorsCalls = 0;
1363
- this.nbWarningsCalls = 0;
1364
- }
1365
- get isNotSilent() {
1366
- return !this.isSilent;
1367
- }
1368
- get errors() {
1369
- return this.nbErrorsCalls;
1370
- }
1371
- get warnings() {
1372
- return this.nbWarningsCalls;
1373
- }
1374
- get stdout() {
1375
- return this.isSilent ? void 0 : process.stdout;
1376
- }
1377
- get stderr() {
1378
- return this.isSilent ? void 0 : process.stderr;
1379
- }
1380
- setDebug(debug) {
1381
- this.isDebug = debug;
1382
- return this;
1383
- }
1384
- setSilent(silent) {
1385
- this.isSilent = silent;
1386
- return this;
1387
- }
1388
- debug(...args) {
1389
- const isDebugEnabled = this.isNotSilent && this.isDebug;
1390
- if (isDebugEnabled) {
1391
- console.log(chalk__default.default.cyan(`[DEBUG] [${nowAsISO()}]`), ...args);
1392
- }
1393
- return this;
1394
- }
1395
- error(...args) {
1396
- this.nbErrorsCalls += 1;
1397
- if (this.isNotSilent) {
1398
- console.error(chalk__default.default.red(`[ERROR] [${nowAsISO()}]`), ...args);
1399
- }
1400
- return this;
1401
- }
1402
- info(...args) {
1403
- if (this.isNotSilent) {
1404
- console.info(chalk__default.default.blue(`[INFO] [${(/* @__PURE__ */ new Date()).toISOString()}]`), ...args);
1405
- }
1406
- return this;
1407
- }
1408
- raw(...args) {
1409
- if (this.isNotSilent) {
1410
- console.log(...args);
1411
- }
1412
- return this;
1413
- }
1414
- warn(...args) {
1415
- this.nbWarningsCalls += 1;
1416
- if (this.isNotSilent) {
1417
- console.warn(chalk__default.default.yellow(`[WARN] [${(/* @__PURE__ */ new Date()).toISOString()}]`), ...args);
1418
- }
1419
- return this;
1420
- }
1421
- }
1422
- const nowAsISO = () => (/* @__PURE__ */ new Date()).toISOString();
1423
- const loggerFactory = (options = {}) => new Logger(options);
1424
- const index$3 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
129
+
130
+ var index$2 = /*#__PURE__*/Object.freeze({
1425
131
  __proto__: null,
1426
- loggerFactory
1427
- }, Symbol.toStringTag, { value: "Module" }));
1428
- const codemodReportFactory = (codemod, report) => ({
1429
- codemod,
1430
- report
132
+ codemodReportFactory: codemodReportFactory,
133
+ reportFactory: reportFactory
1431
134
  });
1432
- const reportFactory = (report) => ({ ...report });
1433
- const index$2 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1434
- __proto__: null,
1435
- codemodReportFactory,
1436
- reportFactory
1437
- }, Symbol.toStringTag, { value: "Module" }));
1438
- const index$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
135
+
136
+ var index$1 = /*#__PURE__*/Object.freeze({
1439
137
  __proto__: null,
1440
138
  code: index$c,
1441
139
  json: index$b
1442
- }, Symbol.toStringTag, { value: "Module" }));
1443
- const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
140
+ });
141
+
142
+ var index = /*#__PURE__*/Object.freeze({
1444
143
  __proto__: null,
1445
- codemod: index$7,
1446
- codemodRepository: index$6,
144
+ codemod: index$8,
145
+ codemodRepository: index$7,
1447
146
  error: index$9,
1448
- f: index$8,
147
+ f: index$f,
1449
148
  fileScanner: index$d,
1450
149
  logger: index$3,
1451
150
  project: index$a,
1452
151
  report: index$2,
1453
- requirement: index$g,
152
+ requirement: index$5,
1454
153
  runner: index$1,
1455
- timer: index$f,
1456
- upgrader: index$5,
154
+ timer: index$g,
155
+ upgrader: index$6,
1457
156
  version: index$e
1458
- }, Symbol.toStringTag, { value: "Module" }));
157
+ });
158
+
1459
159
  exports.modules = index;
1460
160
  exports.tasks = index$4;
1461
161
  //# sourceMappingURL=index.js.map