@strapi/upgrade 0.0.0-experimental.fc1ac2acd58c8a5a858679956b6d102ac5ee4011 → 0.0.0-experimental.fca58d9b01ba89012d84a58a20556a6fd716cb92
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 +19 -4
- package/README.md +1 -1
- package/dist/cli.js +15 -6
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +14 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/index.js +9 -1199
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -1191
- 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 +6 -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 +3 -0
- 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/error/utils.d.ts +8 -0
- package/dist/modules/error/utils.d.ts.map +1 -1
- package/dist/modules/file-scanner/scanner.d.ts.map +1 -1
- package/dist/modules/format/formats.d.ts +6 -0
- package/dist/modules/format/formats.d.ts.map +1 -1
- package/dist/modules/project/constants.d.ts +6 -3
- package/dist/modules/project/constants.d.ts.map +1 -1
- package/dist/modules/project/index.d.ts +2 -0
- package/dist/modules/project/index.d.ts.map +1 -1
- package/dist/modules/project/project.d.ts +27 -5
- package/dist/modules/project/project.d.ts.map +1 -1
- package/dist/modules/project/types.d.ts +3 -10
- package/dist/modules/project/types.d.ts.map +1 -1
- package/dist/modules/project/utils.d.ts +6 -0
- package/dist/modules/project/utils.d.ts.map +1 -0
- package/dist/modules/report/report.d.ts.map +1 -1
- package/dist/modules/requirement/types.d.ts +2 -2
- package/dist/modules/requirement/types.d.ts.map +1 -1
- package/dist/modules/runner/json/transform.d.ts.map +1 -1
- package/dist/modules/upgrader/types.d.ts +6 -0
- package/dist/modules/upgrader/types.d.ts.map +1 -1
- package/dist/modules/upgrader/upgrader.d.ts +7 -3
- 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/modules/version/types.d.ts +2 -1
- package/dist/modules/version/types.d.ts.map +1 -1
- package/dist/package.json.js +6 -0
- package/dist/package.json.js.map +1 -0
- package/dist/package.json.mjs +4 -0
- package/dist/package.json.mjs.map +1 -0
- package/dist/src/cli/commands/codemods.js +120 -0
- package/dist/src/cli/commands/codemods.js.map +1 -0
- package/dist/src/cli/commands/codemods.mjs +116 -0
- package/dist/src/cli/commands/codemods.mjs.map +1 -0
- package/dist/src/cli/commands/upgrade.js +99 -0
- package/dist/src/cli/commands/upgrade.js.map +1 -0
- package/dist/src/cli/commands/upgrade.mjs +96 -0
- package/dist/src/cli/commands/upgrade.mjs.map +1 -0
- package/dist/src/cli/errors.js +18 -0
- package/dist/src/cli/errors.js.map +1 -0
- package/dist/src/cli/errors.mjs +16 -0
- package/dist/src/cli/errors.mjs.map +1 -0
- package/dist/src/cli/options.js +26 -0
- package/dist/src/cli/options.js.map +1 -0
- package/dist/src/cli/options.mjs +19 -0
- package/dist/src/cli/options.mjs.map +1 -0
- package/dist/src/modules/codemod/codemod.js +44 -0
- package/dist/src/modules/codemod/codemod.js.map +1 -0
- package/dist/src/modules/codemod/codemod.mjs +41 -0
- package/dist/src/modules/codemod/codemod.mjs.map +1 -0
- package/dist/src/modules/codemod/constants.js +17 -0
- package/dist/src/modules/codemod/constants.js.map +1 -0
- package/dist/src/modules/codemod/constants.mjs +11 -0
- package/dist/src/modules/codemod/constants.mjs.map +1 -0
- package/dist/src/modules/codemod/index.js +10 -0
- package/dist/src/modules/codemod/index.js.map +1 -0
- package/dist/src/modules/codemod/index.mjs +4 -0
- package/dist/src/modules/codemod/index.mjs.map +1 -0
- package/dist/src/modules/codemod-repository/constants.js +9 -0
- package/dist/src/modules/codemod-repository/constants.js.map +1 -0
- package/dist/src/modules/codemod-repository/constants.mjs +7 -0
- package/dist/src/modules/codemod-repository/constants.mjs.map +1 -0
- package/dist/src/modules/codemod-repository/index.js +10 -0
- package/dist/src/modules/codemod-repository/index.js.map +1 -0
- package/dist/src/modules/codemod-repository/index.mjs +4 -0
- package/dist/src/modules/codemod-repository/index.mjs.map +1 -0
- package/dist/src/modules/codemod-repository/repository.js +127 -0
- package/dist/src/modules/codemod-repository/repository.js.map +1 -0
- package/dist/src/modules/codemod-repository/repository.mjs +123 -0
- package/dist/src/modules/codemod-repository/repository.mjs.map +1 -0
- package/dist/src/modules/codemod-runner/codemod-runner.js +113 -0
- package/dist/src/modules/codemod-runner/codemod-runner.js.map +1 -0
- package/dist/src/modules/codemod-runner/codemod-runner.mjs +110 -0
- package/dist/src/modules/codemod-runner/codemod-runner.mjs.map +1 -0
- package/dist/src/modules/error/index.js +11 -0
- package/dist/src/modules/error/index.js.map +1 -0
- package/dist/src/modules/error/index.mjs +2 -0
- package/dist/src/modules/error/index.mjs.map +1 -0
- package/dist/src/modules/error/utils.js +33 -0
- package/dist/src/modules/error/utils.js.map +1 -0
- package/dist/src/modules/error/utils.mjs +28 -0
- package/dist/src/modules/error/utils.mjs.map +1 -0
- package/dist/src/modules/file-scanner/index.js +8 -0
- package/dist/src/modules/file-scanner/index.js.map +1 -0
- package/dist/src/modules/file-scanner/index.mjs +2 -0
- package/dist/src/modules/file-scanner/index.mjs.map +1 -0
- package/dist/src/modules/file-scanner/scanner.js +23 -0
- package/dist/src/modules/file-scanner/scanner.js.map +1 -0
- package/dist/src/modules/file-scanner/scanner.mjs +20 -0
- package/dist/src/modules/file-scanner/scanner.mjs.map +1 -0
- package/dist/src/modules/format/formats.js +107 -0
- package/dist/src/modules/format/formats.js.map +1 -0
- package/dist/src/modules/format/formats.mjs +94 -0
- package/dist/src/modules/format/formats.mjs.map +1 -0
- package/dist/src/modules/format/index.js +19 -0
- package/dist/src/modules/format/index.js.map +1 -0
- package/dist/src/modules/format/index.mjs +2 -0
- package/dist/src/modules/format/index.mjs.map +1 -0
- package/dist/src/modules/index.js +32 -0
- package/dist/src/modules/index.js.map +1 -0
- package/dist/src/modules/index.mjs +27 -0
- package/dist/src/modules/index.mjs.map +1 -0
- package/dist/src/modules/json/file.js +16 -0
- package/dist/src/modules/json/file.js.map +1 -0
- package/dist/src/modules/json/file.mjs +13 -0
- package/dist/src/modules/json/file.mjs.map +1 -0
- package/dist/src/modules/json/transform-api.js +38 -0
- package/dist/src/modules/json/transform-api.js.map +1 -0
- package/dist/src/modules/json/transform-api.mjs +35 -0
- package/dist/src/modules/json/transform-api.mjs.map +1 -0
- package/dist/src/modules/logger/index.js +8 -0
- package/dist/src/modules/logger/index.js.map +1 -0
- package/dist/src/modules/logger/index.mjs +2 -0
- package/dist/src/modules/logger/index.mjs.map +1 -0
- package/dist/src/modules/logger/logger.js +76 -0
- package/dist/src/modules/logger/logger.js.map +1 -0
- package/dist/src/modules/logger/logger.mjs +73 -0
- package/dist/src/modules/logger/logger.mjs.map +1 -0
- package/dist/src/modules/npm/constants.js +6 -0
- package/dist/src/modules/npm/constants.js.map +1 -0
- package/dist/src/modules/npm/constants.mjs +4 -0
- package/dist/src/modules/npm/constants.mjs.map +1 -0
- package/dist/src/modules/npm/package.js +55 -0
- package/dist/src/modules/npm/package.js.map +1 -0
- package/dist/src/modules/npm/package.mjs +52 -0
- package/dist/src/modules/npm/package.mjs.map +1 -0
- package/dist/src/modules/project/constants.js +45 -0
- package/dist/src/modules/project/constants.js.map +1 -0
- package/dist/src/modules/project/constants.mjs +35 -0
- package/dist/src/modules/project/constants.mjs.map +1 -0
- package/dist/src/modules/project/index.js +15 -0
- package/dist/src/modules/project/index.js.map +1 -0
- package/dist/src/modules/project/index.mjs +5 -0
- package/dist/src/modules/project/index.mjs.map +1 -0
- package/dist/src/modules/project/project.js +208 -0
- package/dist/src/modules/project/project.js.map +1 -0
- package/dist/src/modules/project/project.mjs +203 -0
- package/dist/src/modules/project/project.mjs.map +1 -0
- package/dist/src/modules/project/utils.js +26 -0
- package/dist/src/modules/project/utils.js.map +1 -0
- package/dist/src/modules/project/utils.mjs +21 -0
- package/dist/src/modules/project/utils.mjs.map +1 -0
- package/dist/src/modules/report/index.js +9 -0
- package/dist/src/modules/report/index.js.map +1 -0
- package/dist/src/modules/report/index.mjs +2 -0
- package/dist/src/modules/report/index.mjs.map +1 -0
- package/dist/src/modules/report/report.js +13 -0
- package/dist/src/modules/report/report.js.map +1 -0
- package/dist/src/modules/report/report.mjs +10 -0
- package/dist/src/modules/report/report.mjs.map +1 -0
- package/dist/src/modules/requirement/index.js +8 -0
- package/dist/src/modules/requirement/index.js.map +1 -0
- package/dist/src/modules/requirement/index.mjs +2 -0
- package/dist/src/modules/requirement/index.mjs.map +1 -0
- package/dist/src/modules/requirement/requirement.js +55 -0
- package/dist/src/modules/requirement/requirement.js.map +1 -0
- package/dist/src/modules/requirement/requirement.mjs +52 -0
- package/dist/src/modules/requirement/requirement.mjs.map +1 -0
- package/dist/src/modules/runner/code/code.js +21 -0
- package/dist/src/modules/runner/code/code.js.map +1 -0
- package/dist/src/modules/runner/code/code.mjs +18 -0
- package/dist/src/modules/runner/code/code.mjs.map +1 -0
- package/dist/src/modules/runner/code/index.js +8 -0
- package/dist/src/modules/runner/code/index.js.map +1 -0
- package/dist/src/modules/runner/code/index.mjs +2 -0
- package/dist/src/modules/runner/code/index.mjs.map +1 -0
- package/dist/src/modules/runner/index.js +10 -0
- package/dist/src/modules/runner/index.js.map +1 -0
- package/dist/src/modules/runner/index.mjs +5 -0
- package/dist/src/modules/runner/index.mjs.map +1 -0
- package/dist/src/modules/runner/json/index.js +8 -0
- package/dist/src/modules/runner/json/index.js.map +1 -0
- package/dist/src/modules/runner/json/index.mjs +2 -0
- package/dist/src/modules/runner/json/index.mjs.map +1 -0
- package/dist/src/modules/runner/json/json.js +21 -0
- package/dist/src/modules/runner/json/json.js.map +1 -0
- package/dist/src/modules/runner/json/json.mjs +18 -0
- package/dist/src/modules/runner/json/json.mjs.map +1 -0
- package/dist/src/modules/runner/json/transform.js +85 -0
- package/dist/src/modules/runner/json/transform.js.map +1 -0
- package/dist/src/modules/runner/json/transform.mjs +83 -0
- package/dist/src/modules/runner/json/transform.mjs.map +1 -0
- package/dist/src/modules/runner/runner.js +22 -0
- package/dist/src/modules/runner/runner.js.map +1 -0
- package/dist/src/modules/runner/runner.mjs +20 -0
- package/dist/src/modules/runner/runner.mjs.map +1 -0
- package/dist/src/modules/timer/constants.js +6 -0
- package/dist/src/modules/timer/constants.js.map +1 -0
- package/dist/src/modules/timer/constants.mjs +4 -0
- package/dist/src/modules/timer/constants.mjs.map +1 -0
- package/dist/src/modules/timer/index.js +10 -0
- package/dist/src/modules/timer/index.js.map +1 -0
- package/dist/src/modules/timer/index.mjs +4 -0
- package/dist/src/modules/timer/index.mjs.map +1 -0
- package/dist/src/modules/timer/timer.js +33 -0
- package/dist/src/modules/timer/timer.js.map +1 -0
- package/dist/src/modules/timer/timer.mjs +30 -0
- package/dist/src/modules/timer/timer.mjs.map +1 -0
- package/dist/src/modules/upgrader/constants.js +6 -0
- package/dist/src/modules/upgrader/constants.js.map +1 -0
- package/dist/src/modules/upgrader/constants.mjs +4 -0
- package/dist/src/modules/upgrader/constants.mjs.map +1 -0
- package/dist/src/modules/upgrader/index.js +10 -0
- package/dist/src/modules/upgrader/index.js.map +1 -0
- package/dist/src/modules/upgrader/index.mjs +4 -0
- package/dist/src/modules/upgrader/index.mjs.map +1 -0
- package/dist/src/modules/upgrader/upgrader.js +266 -0
- package/dist/src/modules/upgrader/upgrader.js.map +1 -0
- package/dist/src/modules/upgrader/upgrader.mjs +263 -0
- package/dist/src/modules/upgrader/upgrader.mjs.map +1 -0
- package/dist/src/modules/version/index.js +20 -0
- package/dist/src/modules/version/index.js.map +1 -0
- package/dist/src/modules/version/index.mjs +5 -0
- package/dist/src/modules/version/index.mjs.map +1 -0
- package/dist/src/modules/version/range.js +81 -0
- package/dist/src/modules/version/range.js.map +1 -0
- package/dist/src/modules/version/range.mjs +75 -0
- package/dist/src/modules/version/range.mjs.map +1 -0
- package/dist/src/modules/version/semver.js +26 -0
- package/dist/src/modules/version/semver.js.map +1 -0
- package/dist/src/modules/version/semver.mjs +20 -0
- package/dist/src/modules/version/semver.mjs.map +1 -0
- package/dist/src/modules/version/types.js +12 -0
- package/dist/src/modules/version/types.js.map +1 -0
- package/dist/src/modules/version/types.mjs +12 -0
- package/dist/src/modules/version/types.mjs.map +1 -0
- package/dist/src/tasks/codemods/list-codemods.js +40 -0
- package/dist/src/tasks/codemods/list-codemods.js.map +1 -0
- package/dist/src/tasks/codemods/list-codemods.mjs +38 -0
- package/dist/src/tasks/codemods/list-codemods.mjs.map +1 -0
- package/dist/src/tasks/codemods/run-codemods.js +36 -0
- package/dist/src/tasks/codemods/run-codemods.js.map +1 -0
- package/dist/src/tasks/codemods/run-codemods.mjs +34 -0
- package/dist/src/tasks/codemods/run-codemods.mjs.map +1 -0
- package/dist/src/tasks/codemods/utils.js +54 -0
- package/dist/src/tasks/codemods/utils.js.map +1 -0
- package/dist/src/tasks/codemods/utils.mjs +50 -0
- package/dist/src/tasks/codemods/utils.mjs.map +1 -0
- package/dist/src/tasks/index.js +15 -0
- package/dist/src/tasks/index.js.map +1 -0
- package/dist/src/tasks/index.mjs +7 -0
- package/dist/src/tasks/index.mjs.map +1 -0
- package/dist/src/tasks/upgrade/prompts/latest.js +48 -0
- package/dist/src/tasks/upgrade/prompts/latest.js.map +1 -0
- package/dist/src/tasks/upgrade/prompts/latest.mjs +46 -0
- package/dist/src/tasks/upgrade/prompts/latest.mjs.map +1 -0
- package/dist/src/tasks/upgrade/requirements/common.js +40 -0
- package/dist/src/tasks/upgrade/requirements/common.js.map +1 -0
- package/dist/src/tasks/upgrade/requirements/common.mjs +35 -0
- package/dist/src/tasks/upgrade/requirements/common.mjs.map +1 -0
- package/dist/src/tasks/upgrade/requirements/major.js +29 -0
- package/dist/src/tasks/upgrade/requirements/major.js.map +1 -0
- package/dist/src/tasks/upgrade/requirements/major.mjs +26 -0
- package/dist/src/tasks/upgrade/requirements/major.mjs.map +1 -0
- package/dist/src/tasks/upgrade/upgrade.js +69 -0
- package/dist/src/tasks/upgrade/upgrade.js.map +1 -0
- package/dist/src/tasks/upgrade/upgrade.mjs +67 -0
- package/dist/src/tasks/upgrade/upgrade.mjs.map +1 -0
- 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/dist/tasks/upgrade/prompts/index.d.ts +2 -0
- package/dist/tasks/upgrade/prompts/index.d.ts.map +1 -0
- package/dist/tasks/upgrade/prompts/latest.d.ts +9 -0
- package/dist/tasks/upgrade/prompts/latest.d.ts.map +1 -0
- package/dist/tasks/upgrade/requirements/major.d.ts.map +1 -1
- package/dist/tasks/upgrade/upgrade.d.ts.map +1 -1
- package/package.json +16 -15
- package/resources/codemods/5.0.0/comment-out-lifecycle-files.code.ts +63 -0
- package/resources/codemods/5.0.0/dependency-remove-strapi-plugin-i18n.json.ts +31 -0
- package/resources/codemods/5.0.0/dependency-upgrade-react-and-react-dom.json.ts +67 -0
- package/resources/codemods/5.0.0/dependency-upgrade-react-router-dom.json.ts +59 -0
- package/resources/codemods/5.0.0/dependency-upgrade-styled-components.json.ts +49 -0
- package/resources/codemods/5.0.0/deprecate-helper-plugin.code.ts +192 -0
- package/resources/codemods/5.0.0/entity-service-document-service.code.ts +437 -0
- package/resources/codemods/5.0.0/s3-keys-wrapped-in-credentials.code.ts +1 -1
- package/resources/codemods/5.0.0/sqlite3-to-better-sqlite3.json.ts +5 -3
- package/resources/codemods/5.0.0/strapi-public-interface.code.ts +126 -0
- package/resources/codemods/5.0.0/use-uid-for-config-namespace.code.ts +1 -1
- package/resources/codemods/5.0.0/utils-public-interface.code.ts +320 -0
- package/resources/codemods/5.1.0/dependency-better-sqlite3.json.ts +48 -0
- package/resources/examples/console.log-to-console.info.code.ts +1 -1
- package/resources/examples/disable-jsx-buttons.code.ts +42 -0
- package/resources/utils/change-import.ts +118 -0
- package/resources/utils/replace-jsx.ts +49 -0
- package/dist/_chunks/codemod-runner-oPpELfwO.js +0 -730
- package/dist/_chunks/codemod-runner-oPpELfwO.js.map +0 -1
- package/dist/_chunks/codemods-qc8_QMWD.js +0 -108
- package/dist/_chunks/codemods-qc8_QMWD.js.map +0 -1
- package/dist/_chunks/index-GHKaxdhJ.js +0 -103
- package/dist/_chunks/index-GHKaxdhJ.js.map +0 -1
- package/dist/_chunks/upgrade-4SxXG3Oe.js +0 -357
- package/dist/_chunks/upgrade-4SxXG3Oe.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,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;
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import { Transform, JSCodeshift, Collection } from 'jscodeshift';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
|
|
5
|
+
This codemod transforms @strapi/utils imports to change method calls to match the new public interface.
|
|
6
|
+
It will also warn about removed functions to avoid breaking user code.
|
|
7
|
+
|
|
8
|
+
ESM
|
|
9
|
+
|
|
10
|
+
Before:
|
|
11
|
+
|
|
12
|
+
import * as utils from '@strapi/utils';
|
|
13
|
+
|
|
14
|
+
utils.nameToSlug();
|
|
15
|
+
|
|
16
|
+
After:
|
|
17
|
+
|
|
18
|
+
import { strings } from '@strapi/utils';
|
|
19
|
+
|
|
20
|
+
strings.nameToSlug();
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
ESM
|
|
24
|
+
|
|
25
|
+
Before:
|
|
26
|
+
|
|
27
|
+
import { nameToSlug } from '@strapi/utils';
|
|
28
|
+
|
|
29
|
+
nameToSlug();
|
|
30
|
+
|
|
31
|
+
After:
|
|
32
|
+
|
|
33
|
+
import { strings } from '@strapi/utils';
|
|
34
|
+
|
|
35
|
+
strings.nameToSlug();
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
Common JS
|
|
40
|
+
|
|
41
|
+
Before:
|
|
42
|
+
|
|
43
|
+
const utils = require('@strapi/utils');
|
|
44
|
+
|
|
45
|
+
utils.nameToSlug();
|
|
46
|
+
|
|
47
|
+
After:
|
|
48
|
+
|
|
49
|
+
const { strings } = require('@strapi/utils');
|
|
50
|
+
|
|
51
|
+
strings.nameToSlug();
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
Common JS
|
|
55
|
+
|
|
56
|
+
Before:
|
|
57
|
+
|
|
58
|
+
const { nameToSlug } = require('@strapi/utils');
|
|
59
|
+
|
|
60
|
+
nameToSlug();
|
|
61
|
+
|
|
62
|
+
After:
|
|
63
|
+
|
|
64
|
+
const { strings } = require('@strapi/utils');
|
|
65
|
+
|
|
66
|
+
strings.nameToSlug();
|
|
67
|
+
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
const changes = {
|
|
71
|
+
strings: {
|
|
72
|
+
nameToSlug: 'nameToSlug',
|
|
73
|
+
nameToCollectionName: 'nameToCollectionName',
|
|
74
|
+
stringEquals: 'isEqual',
|
|
75
|
+
isCamelCase: 'isCamelCase',
|
|
76
|
+
isKebabCase: 'isKebabCase',
|
|
77
|
+
toKebabCase: 'toKebabCase',
|
|
78
|
+
toRegressedEnumValue: 'toRegressedEnumValue',
|
|
79
|
+
startsWithANumber: 'startsWithANumber',
|
|
80
|
+
joinBy: 'joinBy',
|
|
81
|
+
},
|
|
82
|
+
arrays: {
|
|
83
|
+
stringIncludes: 'includesString',
|
|
84
|
+
},
|
|
85
|
+
objects: {
|
|
86
|
+
keysDeep: 'keysDeep',
|
|
87
|
+
},
|
|
88
|
+
dates: {
|
|
89
|
+
generateTimestampCode: 'timestampCode',
|
|
90
|
+
},
|
|
91
|
+
async: {
|
|
92
|
+
pipeAsync: 'pipe',
|
|
93
|
+
mapAsync: 'map',
|
|
94
|
+
reduceAsync: 'reduce',
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const removed = [
|
|
99
|
+
'getCommonBeginning',
|
|
100
|
+
'templateConfiguration',
|
|
101
|
+
'removeUndefined',
|
|
102
|
+
'getConfigUrls',
|
|
103
|
+
'getAbsoluteAdminUrl',
|
|
104
|
+
'getAbsoluteServerUrl',
|
|
105
|
+
'forEachAsync',
|
|
106
|
+
];
|
|
107
|
+
|
|
108
|
+
const transformImports = (root: Collection, j: JSCodeshift) => {
|
|
109
|
+
root
|
|
110
|
+
.find(j.ImportDeclaration, {
|
|
111
|
+
source: { value: '@strapi/utils' },
|
|
112
|
+
})
|
|
113
|
+
.forEach((path) => {
|
|
114
|
+
if (!j.ImportDeclaration.check(path.value)) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
path.value.specifiers.forEach((specifier) => {
|
|
119
|
+
if (!j.ImportSpecifier.check(specifier)) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (removed.includes(specifier.imported.name)) {
|
|
124
|
+
console.warn(
|
|
125
|
+
`Function "${specifier.imported.name}" was removed. You will have to remove it from your code.`
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
for (const primitive of Object.keys(changes)) {
|
|
133
|
+
const functions = Object.keys(changes[primitive]);
|
|
134
|
+
|
|
135
|
+
const specifiersToRefactor = path.value.specifiers.filter((specifier) => {
|
|
136
|
+
return j.ImportSpecifier.check(specifier) && functions.includes(specifier.imported.name);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
if (specifiersToRefactor.length > 0) {
|
|
140
|
+
path.value.specifiers.unshift(j.importSpecifier(j.identifier(primitive)));
|
|
141
|
+
|
|
142
|
+
specifiersToRefactor.forEach((specifier) => {
|
|
143
|
+
const index = path.value.specifiers.indexOf(specifier);
|
|
144
|
+
path.value.specifiers.splice(index, 1);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (path.value.specifiers?.length === 0) {
|
|
150
|
+
j(path).remove();
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
root.find(j.ImportNamespaceSpecifier).forEach((specifierPath) => {
|
|
155
|
+
if (specifierPath.parent.value.source.value === '@strapi/utils') {
|
|
156
|
+
for (const primitive of Object.keys(changes)) {
|
|
157
|
+
const functions = Object.keys(changes[primitive]);
|
|
158
|
+
functions.forEach((funcName) => {
|
|
159
|
+
root
|
|
160
|
+
.find(j.CallExpression, {
|
|
161
|
+
callee: {
|
|
162
|
+
type: 'MemberExpression',
|
|
163
|
+
property: {
|
|
164
|
+
type: 'Identifier',
|
|
165
|
+
name: funcName,
|
|
166
|
+
},
|
|
167
|
+
object: {
|
|
168
|
+
type: 'Identifier',
|
|
169
|
+
name: specifierPath.value.local.name,
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
})
|
|
173
|
+
.replaceWith((path) => {
|
|
174
|
+
return j.callExpression(
|
|
175
|
+
j.memberExpression(
|
|
176
|
+
j.memberExpression(
|
|
177
|
+
j.identifier(specifierPath.value.local.name),
|
|
178
|
+
j.identifier(primitive)
|
|
179
|
+
),
|
|
180
|
+
j.identifier(changes[primitive][funcName])
|
|
181
|
+
),
|
|
182
|
+
path.value.arguments
|
|
183
|
+
);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
root
|
|
191
|
+
.find(j.VariableDeclarator, {
|
|
192
|
+
init: {
|
|
193
|
+
callee: {
|
|
194
|
+
name: 'require',
|
|
195
|
+
},
|
|
196
|
+
arguments: [{ value: '@strapi/utils' }],
|
|
197
|
+
},
|
|
198
|
+
})
|
|
199
|
+
.forEach((path) => {
|
|
200
|
+
// destructured require
|
|
201
|
+
|
|
202
|
+
if (j.ObjectPattern.check(path.value.id)) {
|
|
203
|
+
const properties = path.value.id.properties;
|
|
204
|
+
|
|
205
|
+
properties?.forEach((property) => {
|
|
206
|
+
if (!j.ObjectProperty.check(property) || !j.Identifier.check(property.value)) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (removed.includes(property.value.name)) {
|
|
211
|
+
console.warn(
|
|
212
|
+
`Function "${property.value.name}" was removed. You will have to remove it from your code.`
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
for (const primitive of Object.keys(changes)) {
|
|
220
|
+
const functions = Object.keys(changes[primitive]);
|
|
221
|
+
|
|
222
|
+
const propertiesToRefactor = properties?.filter((property) => {
|
|
223
|
+
return (
|
|
224
|
+
j.ObjectProperty.check(property) &&
|
|
225
|
+
j.Identifier.check(property.value) &&
|
|
226
|
+
functions.includes(property.value.name)
|
|
227
|
+
);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
if (propertiesToRefactor?.length > 0) {
|
|
231
|
+
const identifier = j.identifier(primitive);
|
|
232
|
+
|
|
233
|
+
properties?.unshift(
|
|
234
|
+
j.objectProperty.from({
|
|
235
|
+
key: identifier,
|
|
236
|
+
value: identifier,
|
|
237
|
+
shorthand: true,
|
|
238
|
+
})
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
propertiesToRefactor.forEach((property) => {
|
|
242
|
+
const index = properties?.indexOf(property);
|
|
243
|
+
properties?.splice(index, 1);
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (path.value.id.properties?.length === 0) {
|
|
249
|
+
j(path).remove();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// namespace require
|
|
254
|
+
if (path.value.id.type === 'Identifier') {
|
|
255
|
+
const identifier = path.value.id.name;
|
|
256
|
+
|
|
257
|
+
for (const primitive of Object.keys(changes)) {
|
|
258
|
+
const functions = Object.keys(changes[primitive]);
|
|
259
|
+
functions.forEach((funcName) => {
|
|
260
|
+
root
|
|
261
|
+
.find(j.CallExpression, {
|
|
262
|
+
callee: {
|
|
263
|
+
type: 'MemberExpression',
|
|
264
|
+
property: {
|
|
265
|
+
type: 'Identifier',
|
|
266
|
+
name: funcName,
|
|
267
|
+
},
|
|
268
|
+
object: {
|
|
269
|
+
type: 'Identifier',
|
|
270
|
+
name: identifier,
|
|
271
|
+
},
|
|
272
|
+
},
|
|
273
|
+
})
|
|
274
|
+
.replaceWith((path) => {
|
|
275
|
+
return j.callExpression(
|
|
276
|
+
j.memberExpression(
|
|
277
|
+
j.memberExpression(j.identifier(identifier), j.identifier(primitive)),
|
|
278
|
+
j.identifier(changes[primitive][funcName])
|
|
279
|
+
),
|
|
280
|
+
path.value.arguments
|
|
281
|
+
);
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
for (const primitive of Object.keys(changes)) {
|
|
289
|
+
const functions = Object.keys(changes[primitive]);
|
|
290
|
+
functions.forEach((funcName) => {
|
|
291
|
+
root
|
|
292
|
+
.find(j.CallExpression, {
|
|
293
|
+
callee: {
|
|
294
|
+
type: 'Identifier',
|
|
295
|
+
name: funcName,
|
|
296
|
+
},
|
|
297
|
+
})
|
|
298
|
+
.replaceWith((path) => {
|
|
299
|
+
if (j.Identifier.check(path.value.callee)) {
|
|
300
|
+
path.value.callee.name = changes[primitive][funcName];
|
|
301
|
+
return j.memberExpression(j.identifier(primitive), path.value);
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
const transform: Transform = (file, api) => {
|
|
309
|
+
const j = api.jscodeshift;
|
|
310
|
+
|
|
311
|
+
const root = j(file.source);
|
|
312
|
+
|
|
313
|
+
transformImports(root, j);
|
|
314
|
+
|
|
315
|
+
return root.toSource();
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
export const parser = 'tsx';
|
|
319
|
+
|
|
320
|
+
export default transform;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import semver from 'semver';
|
|
3
|
+
import type { modules } from '../../../dist';
|
|
4
|
+
|
|
5
|
+
const DEP_NAME = 'better-sqlite3';
|
|
6
|
+
const DEP_PATH = `dependencies.${DEP_NAME}`;
|
|
7
|
+
const DEP_VERSION = '11.3.0';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
*/
|
|
12
|
+
const transform: modules.runner.json.JSONTransform = (file, params) => {
|
|
13
|
+
return upgradeIfExists(file, params, DEP_PATH, DEP_VERSION);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default transform;
|
|
17
|
+
|
|
18
|
+
// TODO: move this to a utility once we solve the issue where codemods are not transpiled properly
|
|
19
|
+
const upgradeIfExists = (
|
|
20
|
+
file: modules.runner.json.JSONSourceFile,
|
|
21
|
+
params: modules.runner.json.JSONTransformParams,
|
|
22
|
+
packagePath: string,
|
|
23
|
+
targetVersion: string
|
|
24
|
+
) => {
|
|
25
|
+
const { cwd, json } = params;
|
|
26
|
+
|
|
27
|
+
// Return early if the file path is not the root package.json
|
|
28
|
+
if (file.path !== path.join(cwd, 'package.json')) {
|
|
29
|
+
return file.json;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const packageJson = json(file.json);
|
|
33
|
+
|
|
34
|
+
// Check if the package exists
|
|
35
|
+
if (packageJson.has(packagePath)) {
|
|
36
|
+
const currentVersion = packageJson.get(packagePath);
|
|
37
|
+
// ensure we only upgrade, not downgrade
|
|
38
|
+
if (
|
|
39
|
+
typeof currentVersion === 'string' &&
|
|
40
|
+
semver.valid(currentVersion) &&
|
|
41
|
+
semver.lt(currentVersion, targetVersion)
|
|
42
|
+
) {
|
|
43
|
+
packageJson.set(packagePath, targetVersion);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return packageJson.root();
|
|
48
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Transform } from 'jscodeshift';
|
|
2
|
+
import { relative } from 'node:path';
|
|
3
|
+
|
|
4
|
+
const transform: Transform = (file, api) => {
|
|
5
|
+
const j = api.jscodeshift;
|
|
6
|
+
const root = j.withParser('JSX')(file.source);
|
|
7
|
+
|
|
8
|
+
const isReactFile = file.path.endsWith('.jsx') || file.path.endsWith('.tsx');
|
|
9
|
+
|
|
10
|
+
if (!isReactFile) {
|
|
11
|
+
return root.toSource();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const fileName = relative(process.cwd(), file.path);
|
|
15
|
+
|
|
16
|
+
console.log(`Found React file: ${fileName}`);
|
|
17
|
+
|
|
18
|
+
const buttons = root.findJSXElements('Button');
|
|
19
|
+
|
|
20
|
+
console.log(`Found ${buttons.length} buttons in ${fileName}`);
|
|
21
|
+
|
|
22
|
+
buttons.forEach((button) => {
|
|
23
|
+
const { openingElement } = button.node;
|
|
24
|
+
const isDisabled = openingElement.attributes.some(
|
|
25
|
+
(attribute) => j.JSXAttribute.check(attribute) && attribute.name.name === 'disabled'
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
console.log(`Is disabled? ${isDisabled}`);
|
|
29
|
+
|
|
30
|
+
if (!isDisabled) {
|
|
31
|
+
openingElement.attributes.push(
|
|
32
|
+
j.jsxAttribute(j.jsxIdentifier('disabled'), j.jsxExpressionContainer(j.literal(true)))
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
console.log('Added the disabled attribute');
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
return root.toSource();
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export default transform;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { ImportDeclaration, JSCodeshift, Collection } from 'jscodeshift';
|
|
2
|
+
|
|
3
|
+
export const changeImportSpecifier = (
|
|
4
|
+
root: Collection,
|
|
5
|
+
j: JSCodeshift,
|
|
6
|
+
options: {
|
|
7
|
+
oldDependency: string;
|
|
8
|
+
newDependency: string;
|
|
9
|
+
oldMethodName: string;
|
|
10
|
+
newMethodName?: string;
|
|
11
|
+
}
|
|
12
|
+
): void => {
|
|
13
|
+
const { oldMethodName, newMethodName, oldDependency, newDependency } = options;
|
|
14
|
+
const methodNameToReplace = newMethodName ?? oldMethodName;
|
|
15
|
+
|
|
16
|
+
// Flag to check if the method was imported from the old dependency
|
|
17
|
+
let methodImportedFromOldDependency = false;
|
|
18
|
+
const methodAliases: string[] = [];
|
|
19
|
+
|
|
20
|
+
// Remove the method from the old dependency and check if it was imported
|
|
21
|
+
root
|
|
22
|
+
.find(j.ImportDeclaration)
|
|
23
|
+
.filter((path) => path.node.source.value === oldDependency)
|
|
24
|
+
.forEach((path) => {
|
|
25
|
+
const importDeclaration: ImportDeclaration = path.node;
|
|
26
|
+
|
|
27
|
+
// Check if the method is imported from the old dependency
|
|
28
|
+
const methodSpecifiers = importDeclaration.specifiers?.filter(
|
|
29
|
+
(specifier) =>
|
|
30
|
+
specifier.type === 'ImportSpecifier' && specifier.imported.name === oldMethodName
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
if (methodSpecifiers && methodSpecifiers.length > 0) {
|
|
34
|
+
methodImportedFromOldDependency = true;
|
|
35
|
+
|
|
36
|
+
// Collect all aliases for the method
|
|
37
|
+
methodSpecifiers.forEach((specifier) => {
|
|
38
|
+
if (specifier.local && specifier.local.name !== oldMethodName) {
|
|
39
|
+
methodAliases.push(specifier.local.name);
|
|
40
|
+
} else {
|
|
41
|
+
methodAliases.push(methodNameToReplace);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Remove the method specifiers from the old import
|
|
46
|
+
const updatedSpecifiers = importDeclaration.specifiers?.filter(
|
|
47
|
+
(specifier) =>
|
|
48
|
+
specifier.type !== 'ImportSpecifier' || specifier.imported.name !== oldMethodName
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
if (updatedSpecifiers && updatedSpecifiers.length > 0) {
|
|
52
|
+
// Replace the import with the updated specifiers if there are other imports left
|
|
53
|
+
j(path).replaceWith(j.importDeclaration(updatedSpecifiers, j.literal(oldDependency)));
|
|
54
|
+
} else {
|
|
55
|
+
// Remove the entire import statement if the specified method was the only import
|
|
56
|
+
j(path).remove();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Add new import dependency if the method was imported from the old dependency
|
|
62
|
+
if (methodImportedFromOldDependency) {
|
|
63
|
+
const dependencies = root
|
|
64
|
+
.find(j.ImportDeclaration)
|
|
65
|
+
.filter((path) => path.node.source.value === newDependency);
|
|
66
|
+
|
|
67
|
+
if (dependencies.length > 0) {
|
|
68
|
+
// we have to use a flag to prevent adding the method to multiple imports
|
|
69
|
+
let methodAdded = false;
|
|
70
|
+
dependencies.forEach((path) => {
|
|
71
|
+
const importDeclaration: ImportDeclaration = path.node;
|
|
72
|
+
if (!methodAdded) {
|
|
73
|
+
methodAliases.forEach((alias) => {
|
|
74
|
+
// Check if the methodNameToReplace or its alias is already imported
|
|
75
|
+
const specifiersArray = importDeclaration.specifiers || [];
|
|
76
|
+
const methodAlreadyExists = specifiersArray.some(
|
|
77
|
+
(specifier) =>
|
|
78
|
+
specifier.type === 'ImportSpecifier' &&
|
|
79
|
+
specifier.imported.name === methodNameToReplace && // Check if imported method matches
|
|
80
|
+
specifier.local?.name === alias // Check if local alias matches
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
if (!methodAlreadyExists) {
|
|
84
|
+
// If method does not exist, add it
|
|
85
|
+
const newSpecifier = j.importSpecifier(
|
|
86
|
+
j.identifier(methodNameToReplace),
|
|
87
|
+
j.identifier(alias)
|
|
88
|
+
);
|
|
89
|
+
path.get('specifiers').replace([...specifiersArray, newSpecifier]);
|
|
90
|
+
methodAdded = true;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
} else {
|
|
96
|
+
const newSpecifiers = methodAliases.map((alias) =>
|
|
97
|
+
j.importSpecifier(j.identifier(methodNameToReplace), j.identifier(alias))
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const newImportDeclaration = j.importDeclaration(newSpecifiers, j.literal(newDependency));
|
|
101
|
+
|
|
102
|
+
// Find the index of the first non-import declaration
|
|
103
|
+
const body = root.get().node.program.body;
|
|
104
|
+
const lastImportIndex = body.findIndex((node) => node.type !== 'ImportDeclaration');
|
|
105
|
+
|
|
106
|
+
if (lastImportIndex > -1) {
|
|
107
|
+
// Insert the new import declaration just before the first non-import node
|
|
108
|
+
body.splice(lastImportIndex, 0, newImportDeclaration);
|
|
109
|
+
} else {
|
|
110
|
+
// Check if 'use strict' exists at the beginning
|
|
111
|
+
const hasUseStrict =
|
|
112
|
+
body[0]?.type === 'ExpressionStatement' && body[0]?.expression?.value === 'use strict';
|
|
113
|
+
// Add the new import after 'use strict' if it exists, otherwise at the beginning
|
|
114
|
+
body.splice(hasUseStrict ? 1 : 0, 0, newImportDeclaration);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|