@strapi/upgrade 0.0.0-experimental.f31889311d753b5f7d95198ae84d8fce1d156cd6 → 0.0.0-experimental.f3ec579d505563eb88e01308248766a76e49d9b3
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/README.md +1 -1
- package/dist/cli.js +13 -1455
- 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 -1432
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -1424
- package/dist/index.mjs.map +1 -1
- package/dist/modules/codemod-repository/constants.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 +2 -1
- package/dist/modules/format/formats.d.ts.map +1 -1
- package/dist/modules/project/constants.d.ts +6 -5
- package/dist/modules/project/constants.d.ts.map +1 -1
- package/dist/modules/project/project.d.ts +16 -2
- package/dist/modules/project/project.d.ts.map +1 -1
- package/dist/modules/project/types.d.ts +3 -0
- package/dist/modules/project/types.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 +4 -0
- package/dist/modules/upgrader/upgrader.d.ts.map +1 -1
- 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/utils.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 +13 -13
- package/resources/codemods/5.0.0/comment-out-lifecycle-files.code.ts +63 -0
- package/resources/codemods/5.0.0/dependency-upgrade-react-and-react-dom.json.ts +67 -0
- package/resources/codemods/5.0.0/deprecate-helper-plugin.code.ts +192 -0
- package/resources/codemods/5.0.0/sqlite3-to-better-sqlite3.json.ts +0 -1
- package/resources/codemods/5.1.0/dependency-better-sqlite3.json.ts +48 -0
- package/resources/utils/change-import.ts +118 -0
- package/resources/utils/replace-jsx.ts +49 -0
|
@@ -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,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
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { JSCodeshift, Collection } from 'jscodeshift';
|
|
2
|
+
|
|
3
|
+
export const replaceJSXElement = (
|
|
4
|
+
root: Collection,
|
|
5
|
+
j: JSCodeshift,
|
|
6
|
+
{
|
|
7
|
+
oldElementName,
|
|
8
|
+
newElementName,
|
|
9
|
+
oldDependency,
|
|
10
|
+
}: {
|
|
11
|
+
oldElementName: string;
|
|
12
|
+
newElementName: string;
|
|
13
|
+
oldDependency: string;
|
|
14
|
+
}
|
|
15
|
+
) => {
|
|
16
|
+
// Find the import declaration for the old dependency
|
|
17
|
+
const importDeclaration = root.find(j.ImportDeclaration, {
|
|
18
|
+
source: { value: oldDependency },
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
if (importDeclaration.size() === 0) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Get the local name of the imported element
|
|
26
|
+
const localName = importDeclaration
|
|
27
|
+
.find(j.ImportSpecifier, {
|
|
28
|
+
imported: { name: oldElementName },
|
|
29
|
+
})
|
|
30
|
+
.nodes()[0]?.local?.name;
|
|
31
|
+
|
|
32
|
+
if (!localName) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Replace JSX elements
|
|
37
|
+
root.findJSXElements(localName).forEach((path) => {
|
|
38
|
+
const openingElement = path.node.openingElement;
|
|
39
|
+
const closingElement = path.node.closingElement;
|
|
40
|
+
|
|
41
|
+
if (j.JSXIdentifier.check(openingElement.name)) {
|
|
42
|
+
openingElement.name.name = newElementName;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (closingElement && j.JSXIdentifier.check(closingElement.name)) {
|
|
46
|
+
closingElement.name.name = newElementName;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
};
|