mirror of
https://github.com/stefanpejcic/openpanel
synced 2025-06-26 18:28:26 +00:00
no packages and conf copied
This commit is contained in:
@@ -1,62 +0,0 @@
|
||||
import { API, Collection, FileInfo, JSCodeshift } from "jscodeshift";
|
||||
|
||||
export const parser = "tsx";
|
||||
|
||||
const updateStyles = (j: JSCodeshift, root: Collection<any>) => {
|
||||
const styleMinCssImport = root.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: "@pankod/refine-antd/dist/styles.min.css",
|
||||
},
|
||||
});
|
||||
|
||||
styleMinCssImport.replaceWith((path) => {
|
||||
path.node.source.value = "@pankod/refine-antd/dist/reset.css";
|
||||
|
||||
return path.node;
|
||||
});
|
||||
};
|
||||
|
||||
const updateActionButtonsPropstoHeaderButtons = (
|
||||
j: JSCodeshift,
|
||||
root: Collection<any>,
|
||||
) => {
|
||||
const components = ["Show", "Edit", "List", "Create"];
|
||||
const changedProps = {
|
||||
actionButtons: "headerButtons",
|
||||
pageHeaderProps: "headerProps",
|
||||
};
|
||||
|
||||
components.forEach((name) => {
|
||||
const element = root.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Object.keys(changedProps).forEach((prop) => {
|
||||
const jsxAttribute = element.find(j.JSXAttribute, {
|
||||
name: {
|
||||
name: prop,
|
||||
},
|
||||
});
|
||||
|
||||
if (jsxAttribute.length > 0) {
|
||||
jsxAttribute.forEach((path) => {
|
||||
path.node.name.name = changedProps[prop];
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export default function transformer(file: FileInfo, api: API): string {
|
||||
const j = api.jscodeshift;
|
||||
const source = j(file.source);
|
||||
|
||||
updateStyles(j, source);
|
||||
updateActionButtonsPropstoHeaderButtons(j, source);
|
||||
|
||||
return source.toSource();
|
||||
}
|
||||
@@ -1,306 +0,0 @@
|
||||
import { API, JSCodeshift, Collection, FileInfo } from "jscodeshift";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { install } from "../helpers";
|
||||
import { checkPackageLock } from "../helpers";
|
||||
|
||||
export const parser = "tsx";
|
||||
|
||||
function addRouterProvider(j: JSCodeshift, root: Collection<any>) {
|
||||
const routerProviderImports = root.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: "@pankod/refine-react-router",
|
||||
},
|
||||
});
|
||||
|
||||
if (routerProviderImports.length === 0) {
|
||||
// Import route provider
|
||||
root.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: "@pankod/refine",
|
||||
},
|
||||
}).insertAfter(
|
||||
j.importDeclaration(
|
||||
[j.importDefaultSpecifier(j.identifier("routerProvider"))],
|
||||
j.literal("@pankod/refine-react-router"),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
console.log(
|
||||
"WARNING: A router provider from @pankod/refine-react-router is already imported. This tool will not make any migration for router provider.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const refineRoots = root.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name: "Refine",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
refineRoots.replaceWith((path) => {
|
||||
const openingElement = path.node.openingElement;
|
||||
|
||||
let routesExpression: any;
|
||||
|
||||
const routesAttributeIndex = openingElement.attributes?.findIndex(
|
||||
(attribute) =>
|
||||
attribute.type === "JSXAttribute" &&
|
||||
attribute.name.type === "JSXIdentifier" &&
|
||||
attribute.name.name === "routes",
|
||||
);
|
||||
|
||||
const routerProviderAttributeIndex =
|
||||
openingElement.attributes?.findIndex(
|
||||
(attribute) =>
|
||||
attribute.type === "JSXAttribute" &&
|
||||
attribute.name.type === "JSXIdentifier" &&
|
||||
attribute.name.name === "routerProvider",
|
||||
);
|
||||
|
||||
if (routerProviderAttributeIndex > -1) {
|
||||
console.log(
|
||||
"WARNING: There is already a `routerProvider` attribute on Refine component. This tool will not make any migration for router provider.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (routesAttributeIndex > -1) {
|
||||
const routesAttribute =
|
||||
openingElement.attributes?.[routesAttributeIndex];
|
||||
|
||||
if (routesAttribute?.type === "JSXAttribute") {
|
||||
const attributeValue = routesAttribute.value;
|
||||
|
||||
if (attributeValue?.type === "JSXExpressionContainer") {
|
||||
routesExpression = attributeValue.expression;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (routesExpression) {
|
||||
openingElement.attributes?.push(
|
||||
j.jsxAttribute(
|
||||
j.jsxIdentifier("routerProvider"),
|
||||
j.jsxExpressionContainer(
|
||||
j.objectExpression([
|
||||
j.spreadElement(j.identifier("routerProvider")),
|
||||
j.property(
|
||||
"init",
|
||||
j.identifier("routes"),
|
||||
routesExpression,
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
if (routesAttributeIndex > -1) {
|
||||
openingElement.attributes?.splice(routesAttributeIndex, 1);
|
||||
}
|
||||
} else {
|
||||
openingElement.attributes?.push(
|
||||
j.jsxAttribute(
|
||||
j.jsxIdentifier("routerProvider"),
|
||||
j.jsxExpressionContainer(j.identifier("routerProvider")),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return path.node;
|
||||
});
|
||||
}
|
||||
|
||||
const moveResources = (j: JSCodeshift, root: Collection<any>) => {
|
||||
const newResources: { [key: string]: any }[] = [];
|
||||
|
||||
const resourceElements = root.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name: "Resource",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const rootElements = root.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name: "Refine",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const resourceImportSpecifiers = root.find(j.ImportSpecifier, {
|
||||
imported: {
|
||||
name: "Resource",
|
||||
},
|
||||
});
|
||||
|
||||
if (resourceElements.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get resources data
|
||||
resourceElements.forEach((resources) => {
|
||||
const newResource: { [key: string]: any } = {};
|
||||
|
||||
resources.node.openingElement.attributes?.forEach((resource) => {
|
||||
if (
|
||||
resource.type === "JSXAttribute" &&
|
||||
resource.name.type === "JSXIdentifier" &&
|
||||
resource.value
|
||||
) {
|
||||
newResource[resource.name.name] = resource.value;
|
||||
}
|
||||
});
|
||||
|
||||
newResources.push(newResource);
|
||||
});
|
||||
|
||||
// Construct a resources attribute with the resources data
|
||||
const newAttributes = j.jsxAttribute(
|
||||
j.jsxIdentifier("resources"),
|
||||
j.jsxExpressionContainer(
|
||||
j.arrayExpression(
|
||||
newResources.map((resource) => {
|
||||
const newValue = j.objectExpression(
|
||||
Object.entries(resource).map(([key, value]) => {
|
||||
const valueToPut = value.expression
|
||||
? value.expression
|
||||
: value;
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier(key),
|
||||
valueToPut as any,
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
return newValue;
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Add resources attribute
|
||||
rootElements.replaceWith((path) => {
|
||||
const attributes = path.node.openingElement.attributes;
|
||||
|
||||
const resourcesAttributeIndex = attributes.findIndex(
|
||||
(attribute) =>
|
||||
attribute.type === "JSXAttribute" &&
|
||||
attribute.name.name === "resources",
|
||||
);
|
||||
|
||||
if (resourcesAttributeIndex !== -1) {
|
||||
console.log(
|
||||
"WARNING: There is already a 'resources' attribute on Refine component. This tool will not touch it.",
|
||||
);
|
||||
|
||||
return path.node;
|
||||
}
|
||||
|
||||
attributes?.push(newAttributes);
|
||||
|
||||
return path.node;
|
||||
});
|
||||
|
||||
resourceElements.remove();
|
||||
resourceImportSpecifiers.remove();
|
||||
|
||||
// Clear the body of Refine component
|
||||
rootElements.replaceWith((path) => {
|
||||
const openingElement = path.node.openingElement;
|
||||
|
||||
path.node.closingElement = null;
|
||||
openingElement.selfClosing = true;
|
||||
|
||||
return path.node;
|
||||
});
|
||||
};
|
||||
|
||||
const packagesToUpdate = [
|
||||
"@pankod/refine-airtable",
|
||||
"@pankod/refine-graphql",
|
||||
"@pankod/refine-hasura",
|
||||
"@pankod/refine-nestjsx-crud",
|
||||
"@pankod/refine-nextjs-router",
|
||||
"@pankod/refine-react-router",
|
||||
"@pankod/refine-simple-rest",
|
||||
"@pankod/refine-strapi",
|
||||
"@pankod/refine-strapi-graphql",
|
||||
"@pankod/refine-supabase",
|
||||
];
|
||||
|
||||
export async function postTransform(files: any, flags: any) {
|
||||
const rootDir = path.join(process.cwd(), files[0]);
|
||||
const packageJsonPath = path.join(rootDir, "package.json");
|
||||
const useYarn = checkPackageLock(rootDir) === "yarn.lock";
|
||||
let packageJsonData;
|
||||
|
||||
try {
|
||||
packageJsonData = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`Error: failed to load package.json from ${packageJsonPath}, ensure provided directory is root.`,
|
||||
);
|
||||
}
|
||||
|
||||
const dependenciesToInstall: Array<{
|
||||
name: string;
|
||||
version: string;
|
||||
}> = [
|
||||
{
|
||||
name: "@pankod/refine",
|
||||
version: "2.x.x",
|
||||
},
|
||||
{
|
||||
name: "@pankod/refine-react-router",
|
||||
version: "2.x.x",
|
||||
},
|
||||
];
|
||||
|
||||
for (const key of Object.keys(packageJsonData.dependencies)) {
|
||||
if (packagesToUpdate.includes(key)) {
|
||||
dependenciesToInstall.push({
|
||||
name: key,
|
||||
version: "2.x.x",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!flags.dry) {
|
||||
await install(
|
||||
rootDir,
|
||||
dependenciesToInstall.map((dep) => `${dep.name}@${dep.version}`),
|
||||
{
|
||||
useYarn,
|
||||
isOnline: true,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default function transformer(file: FileInfo, api: API): string {
|
||||
const j = api.jscodeshift;
|
||||
const source = j(file.source);
|
||||
|
||||
const rootElement = source.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name: "Refine",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (rootElement.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
addRouterProvider(j, source);
|
||||
moveResources(j, source);
|
||||
|
||||
return source.toSource();
|
||||
}
|
||||
@@ -1,691 +0,0 @@
|
||||
import {
|
||||
API,
|
||||
JSCodeshift,
|
||||
Collection,
|
||||
FileInfo,
|
||||
ImportSpecifier,
|
||||
JSXExpressionContainer,
|
||||
ObjectExpression,
|
||||
ObjectProperty,
|
||||
Identifier,
|
||||
} from "jscodeshift";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { install, remove } from "../helpers";
|
||||
import { checkPackageLock } from "../helpers";
|
||||
|
||||
export const parser = "tsx";
|
||||
|
||||
const availableCoreImports = [
|
||||
"Authenticated",
|
||||
"AuthenticatedProps",
|
||||
"CanAccess",
|
||||
"CanAccessProps",
|
||||
"Refine",
|
||||
"RefineProps",
|
||||
"LayoutWrapperProps",
|
||||
"LayoutWrapper",
|
||||
"LayoutProps",
|
||||
"DefaultLayout",
|
||||
"RouteChangeHandler",
|
||||
"UndoableQueue",
|
||||
"defaultAccessControlContext",
|
||||
"AccessControlContext",
|
||||
"AccessControlContextProvider",
|
||||
"CanParams",
|
||||
"CanReturnType",
|
||||
"IAccessControlContext",
|
||||
"TLogoutVariables",
|
||||
"TLogoutData",
|
||||
"IAuthContext",
|
||||
"Pagination",
|
||||
"Search",
|
||||
"CrudOperators",
|
||||
"CrudFilter",
|
||||
"CrudSort",
|
||||
"CrudFilters",
|
||||
"CrudSorting",
|
||||
"CustomResponse",
|
||||
"GetListResponse",
|
||||
"CreateResponse",
|
||||
"CreateManyResponse",
|
||||
"UpdateResponse",
|
||||
"UpdateManyResponse",
|
||||
"GetOneResponse",
|
||||
"GetManyResponse",
|
||||
"DeleteOneResponse",
|
||||
"DeleteManyResponse",
|
||||
"IDataContext",
|
||||
"IDataContextProvider",
|
||||
"defaultDataProvider",
|
||||
"DataProvider",
|
||||
"DataContext",
|
||||
"DataContextProvider",
|
||||
"ILiveContext",
|
||||
"ILiveContextProvider",
|
||||
"LiveContext",
|
||||
"LiveContextProvider",
|
||||
"defaultNotificationProvider",
|
||||
"NotificationContext",
|
||||
"NotificationContextProvider",
|
||||
"RefineContext",
|
||||
"RefineContextProvider",
|
||||
"ResourceContext",
|
||||
"ResourceContextProvider",
|
||||
"IResourceContext",
|
||||
"OptionsProps",
|
||||
"ResourceProps",
|
||||
"IResourceComponentsProps",
|
||||
"IResourceComponents",
|
||||
"IResourceItem",
|
||||
"RouterContext",
|
||||
"RouterContextProvider",
|
||||
"IRouterProvider",
|
||||
"IRouterContext",
|
||||
"PromptProps",
|
||||
"TranslationContext",
|
||||
"TranslationContextProvider",
|
||||
"Translate",
|
||||
"I18nProvider",
|
||||
"ITranslationContext",
|
||||
"UnsavedWarnContext",
|
||||
"UnsavedWarnContextProvider",
|
||||
"IUnsavedWarnContext",
|
||||
"importCSVMapper",
|
||||
"userFriendlyResourceName",
|
||||
"userFriendlySecond",
|
||||
"parseTableParams",
|
||||
"parseTableParamsFromQuery",
|
||||
"stringifyTableParams",
|
||||
"compareFilters",
|
||||
"unionFilters",
|
||||
"setInitialFilters",
|
||||
"file2Base64",
|
||||
"UseCanProps",
|
||||
"useCan",
|
||||
"useCanWithoutCache",
|
||||
"useAuthenticated",
|
||||
"useCheckError",
|
||||
"useGetIdentity",
|
||||
"useIsAuthenticated",
|
||||
"UseLoginReturnType",
|
||||
"useLogin",
|
||||
"useLogout",
|
||||
"usePermissions",
|
||||
"useIsExistAuthentication",
|
||||
"unionFilters",
|
||||
"useApiUrl",
|
||||
"UseCreateReturnType",
|
||||
"useCreate",
|
||||
"UseCreateManyReturnType",
|
||||
"useCreateMany",
|
||||
"UseCustomProps",
|
||||
"useCustom",
|
||||
"useDelete",
|
||||
"useDeleteMany",
|
||||
"UseListProps",
|
||||
"useList",
|
||||
"UseManyProps",
|
||||
"useMany",
|
||||
"UseOneProps",
|
||||
"useOne",
|
||||
"UseUpdateReturnType",
|
||||
"useUpdate",
|
||||
"useUpdateMany",
|
||||
"CSVDownloadProps",
|
||||
"LabelKeyObject",
|
||||
"useExport",
|
||||
"Authenticated",
|
||||
"CanAccess",
|
||||
"LayoutWrapper",
|
||||
"Refine",
|
||||
"RouteChangeHandler",
|
||||
"UndoableQueue",
|
||||
"file2Base64",
|
||||
"importCSVMapper",
|
||||
"parseTableParams",
|
||||
"parseTableParamsFromQuery",
|
||||
"setInitialFilters",
|
||||
"stringifyTableParams",
|
||||
"unionFilters",
|
||||
"useApiUrl",
|
||||
"useAuthenticated",
|
||||
"useCacheQueries",
|
||||
"useCan",
|
||||
"useCanWithoutCache",
|
||||
"useCancelNotification",
|
||||
"useCheckError",
|
||||
"useCreate",
|
||||
"useCreateMany",
|
||||
"useCustom",
|
||||
"useDelete",
|
||||
"useDeleteMany",
|
||||
"useExport",
|
||||
"useGetIdentity",
|
||||
"useGetLocale",
|
||||
"useGetManyQueries",
|
||||
"useGetOneQueries",
|
||||
"useHandleNotification",
|
||||
"useIsAuthenticated",
|
||||
"useIsExistAuthentication",
|
||||
"useList",
|
||||
"useListResourceQueries",
|
||||
"useLiveMode",
|
||||
"useLogin",
|
||||
"useLogout",
|
||||
"useMany",
|
||||
"useMutationMode",
|
||||
"useNavigation",
|
||||
"useNotification",
|
||||
"useOne",
|
||||
"usePermissions",
|
||||
"usePublish",
|
||||
"useRedirectionAfterSubmission",
|
||||
"useRefineContext",
|
||||
"useResource",
|
||||
"useResourceSubscription",
|
||||
"useResourceWithRoute",
|
||||
"useRouterContext",
|
||||
"useSetLocale",
|
||||
"useShow",
|
||||
"useSubscription",
|
||||
"useSyncWithLocation",
|
||||
"useTitle",
|
||||
"useTranslate",
|
||||
"useUpdate",
|
||||
"useUpdateMany",
|
||||
"useWarnAboutChange",
|
||||
"userFriendlyResourceName",
|
||||
"AuthenticatedProps",
|
||||
"CanAccessProps",
|
||||
"RefineProps",
|
||||
"LayoutWrapperProps",
|
||||
"LiveModeProps",
|
||||
"UseResourceSubscriptionProps",
|
||||
"PublishType",
|
||||
"UseSubscriptionProps",
|
||||
"LiveEvent",
|
||||
"HistoryType",
|
||||
"UseRedirectionAfterSubmissionType",
|
||||
"UseWarnAboutChangeType",
|
||||
"UseMutationModeType",
|
||||
"useRefineContext",
|
||||
"UseSyncWithLocationType",
|
||||
"TitleProps",
|
||||
"UseResourceType",
|
||||
"useResourceWithRoute",
|
||||
"useShowReturnType",
|
||||
"useShowProps",
|
||||
"UseGetLocaleType",
|
||||
"Fields",
|
||||
"NestedField",
|
||||
"QueryBuilderOptions",
|
||||
"MetaDataQuery",
|
||||
"VariableOptions",
|
||||
"HttpError",
|
||||
"BaseRecord",
|
||||
"Option",
|
||||
"MapDataFn",
|
||||
"MutationMode",
|
||||
"IUndoableQueue",
|
||||
"RedirectionTypes",
|
||||
"ResourceErrorRouterParams",
|
||||
"ResourceRouterParams",
|
||||
"SuccessErrorNotification",
|
||||
"OpenNotificationParams",
|
||||
"AuthProvider",
|
||||
];
|
||||
|
||||
function updateRefineImports(j: JSCodeshift, root: Collection<any>) {
|
||||
const refineCoreImports = root.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: "@pankod/refine-core",
|
||||
},
|
||||
});
|
||||
|
||||
if (refineCoreImports.length === 0) {
|
||||
const coreImports: ImportSpecifier[] = [];
|
||||
const antdImports: ImportSpecifier[] = [];
|
||||
|
||||
const refineImport = root.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: "@pankod/refine",
|
||||
},
|
||||
});
|
||||
|
||||
refineImport.replaceWith((path) => {
|
||||
for (const item of path.node.specifiers) {
|
||||
if (availableCoreImports.includes(item.local.name)) {
|
||||
coreImports.push(item as ImportSpecifier);
|
||||
} else {
|
||||
antdImports.push(item as ImportSpecifier);
|
||||
}
|
||||
}
|
||||
|
||||
path.node.specifiers = path.node.specifiers.filter(
|
||||
(p) => !antdImports.includes(p as ImportSpecifier),
|
||||
);
|
||||
|
||||
path.node.source.value = "@pankod/refine-core";
|
||||
|
||||
return path.node;
|
||||
});
|
||||
|
||||
const refineElement = root.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name: "Refine",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (refineElement.length > 0) {
|
||||
const notificationProviderJSXAttribute = root.find(j.JSXAttribute, {
|
||||
name: {
|
||||
name: "notificationProvider",
|
||||
},
|
||||
});
|
||||
|
||||
if (notificationProviderJSXAttribute.length === 0) {
|
||||
antdImports.push(
|
||||
j.importSpecifier(j.identifier("notificationProvider")),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (antdImports.length > 0) {
|
||||
root.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: "@pankod/refine-core",
|
||||
},
|
||||
}).insertAfter(
|
||||
j.importDeclaration(
|
||||
antdImports,
|
||||
j.literal("@pankod/refine-antd"),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (coreImports.length === 0) {
|
||||
refineImport.remove();
|
||||
}
|
||||
|
||||
const refineCSSImport = root.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: "@pankod/refine/dist/styles.min.css",
|
||||
},
|
||||
});
|
||||
|
||||
refineCSSImport.forEach((refineCSSImport) => {
|
||||
refineCSSImport.value.source.value =
|
||||
"@pankod/refine-antd/dist/styles.min.css";
|
||||
});
|
||||
} else {
|
||||
console.log(
|
||||
"WARNING: A refine core package from @pankod/refine-core is already imported. This tool will not make any migration for refine core.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const moveConfigProvider = (j: JSCodeshift, root: Collection<any>) => {
|
||||
const refineElement = root.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name: "Refine",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (refineElement.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const notificationProviderJSXAttribute = root.find(j.JSXAttribute, {
|
||||
name: {
|
||||
name: "notificationProvider",
|
||||
},
|
||||
});
|
||||
|
||||
if (notificationProviderJSXAttribute.length === 0) {
|
||||
refineElement.forEach((path) => {
|
||||
path.node.openingElement.attributes.push(
|
||||
j.jsxAttribute(
|
||||
j.jsxIdentifier("notificationProvider"),
|
||||
j.jsxExpressionContainer(
|
||||
j.identifier("notificationProvider"),
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
const configProviderJSXAttribute = root.find(j.JSXAttribute, {
|
||||
name: {
|
||||
name: "configProviderProps",
|
||||
},
|
||||
});
|
||||
|
||||
if (configProviderJSXAttribute.length > 0) {
|
||||
// Import ConfigProvider from @pankod/refine-antd
|
||||
const refineAntdImport = root.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: "@pankod/refine-antd",
|
||||
},
|
||||
});
|
||||
|
||||
if (refineAntdImport.length > 0) {
|
||||
refineAntdImport.forEach((path) => {
|
||||
path.node.specifiers.push(
|
||||
j.importSpecifier(j.identifier("ConfigProvider")),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
const configProviderValue = (
|
||||
(
|
||||
configProviderJSXAttribute.nodes()[0]
|
||||
.value as JSXExpressionContainer
|
||||
).expression as ObjectExpression
|
||||
).properties;
|
||||
|
||||
const newConfigProviderElement = j.jsxElement(
|
||||
j.jsxOpeningElement(
|
||||
j.jsxIdentifier("ConfigProvider"),
|
||||
configProviderValue.map((p: ObjectProperty) =>
|
||||
j.jsxAttribute(
|
||||
j.jsxIdentifier((p.key as Identifier).name),
|
||||
j.jsxExpressionContainer(p.value as any),
|
||||
),
|
||||
),
|
||||
),
|
||||
j.jsxClosingElement(j.jsxIdentifier("ConfigProvider")),
|
||||
refineElement.nodes(),
|
||||
);
|
||||
|
||||
refineElement.replaceWith(newConfigProviderElement);
|
||||
|
||||
configProviderJSXAttribute.remove();
|
||||
}
|
||||
};
|
||||
|
||||
const defaultLoginPage = (j: JSCodeshift, root: Collection<any>) => {
|
||||
const refineElement = root.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name: "Refine",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (refineElement.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const authProviderJSXAttribute = root.find(j.JSXAttribute, {
|
||||
name: {
|
||||
name: "authProvider",
|
||||
},
|
||||
});
|
||||
|
||||
const loginPageJSXAttribute = root.find(j.JSXAttribute, {
|
||||
name: {
|
||||
name: "LoginPage",
|
||||
},
|
||||
});
|
||||
|
||||
if (
|
||||
authProviderJSXAttribute.length > 0 &&
|
||||
loginPageJSXAttribute.length === 0
|
||||
) {
|
||||
refineElement.forEach((path) => {
|
||||
path.node.openingElement.attributes.push(
|
||||
j.jsxAttribute(
|
||||
j.jsxIdentifier("LoginPage"),
|
||||
j.jsxExpressionContainer(j.identifier("LoginPage")),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
const refineAntdImport = root.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: "@pankod/refine-antd",
|
||||
},
|
||||
});
|
||||
|
||||
if (refineAntdImport.length > 0) {
|
||||
refineAntdImport.forEach((path) => {
|
||||
path.node.specifiers.push(
|
||||
j.importSpecifier(j.identifier("LoginPage")),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const defaultLayout = (j: JSCodeshift, root: Collection<any>) => {
|
||||
const refineElement = root.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name: "Refine",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (refineElement.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const layoutJSXAttribute = root.find(j.JSXAttribute, {
|
||||
name: {
|
||||
name: "Layout",
|
||||
},
|
||||
});
|
||||
|
||||
if (layoutJSXAttribute.length === 0) {
|
||||
refineElement.forEach((path) => {
|
||||
path.node.openingElement.attributes.push(
|
||||
j.jsxAttribute(
|
||||
j.jsxIdentifier("Layout"),
|
||||
j.jsxExpressionContainer(j.identifier("Layout")),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
const refineAntdImport = root.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: "@pankod/refine-antd",
|
||||
},
|
||||
});
|
||||
|
||||
if (refineAntdImport.length > 0) {
|
||||
refineAntdImport.forEach((path) => {
|
||||
path.node.specifiers.push(
|
||||
j.importSpecifier(j.identifier("Layout")),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const defaultCatchAllPage = (j: JSCodeshift, root: Collection<any>) => {
|
||||
const refineElement = root.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name: "Refine",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (refineElement.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const catchAllJSXAttribute = root.find(j.JSXAttribute, {
|
||||
name: {
|
||||
name: "catchAll",
|
||||
},
|
||||
});
|
||||
|
||||
if (catchAllJSXAttribute.length > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
refineElement.forEach((path) => {
|
||||
path.node.openingElement.attributes.push(
|
||||
j.jsxAttribute(
|
||||
j.jsxIdentifier("catchAll"),
|
||||
j.jsxExpressionContainer(
|
||||
j.jsxElement(
|
||||
j.jsxOpeningElement(
|
||||
j.jsxIdentifier("ErrorComponent"),
|
||||
[],
|
||||
true,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
const refineAntdImport = root.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: "@pankod/refine-antd",
|
||||
},
|
||||
});
|
||||
|
||||
if (refineAntdImport.length > 0) {
|
||||
refineAntdImport.forEach((path) => {
|
||||
path.node.specifiers.push(
|
||||
j.importSpecifier(j.identifier("ErrorComponent")),
|
||||
);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const updateSetEditIdToSetId = (j: JSCodeshift, root: Collection<any>) => {
|
||||
const updatedFormHooks = [
|
||||
"useEditableTable",
|
||||
"useModalForm",
|
||||
"useDrawerForm",
|
||||
];
|
||||
|
||||
for (const formHook of updatedFormHooks) {
|
||||
const useEditableTableHook = root.find(j.CallExpression, {
|
||||
callee: {
|
||||
name: formHook,
|
||||
},
|
||||
});
|
||||
|
||||
useEditableTableHook.forEach((path) => {
|
||||
const setEditIdProperty = path.parentPath.node.id.properties.find(
|
||||
(p) => p.value.name === "setEditId",
|
||||
);
|
||||
|
||||
if (setEditIdProperty) {
|
||||
setEditIdProperty.value.name = "setId: setEditId";
|
||||
}
|
||||
|
||||
const editIdProperty = path.parentPath.node.id.properties.find(
|
||||
(p) => p.value.name === "editId",
|
||||
);
|
||||
|
||||
if (editIdProperty) {
|
||||
editIdProperty.value.name = "id: editId";
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const packagesToUpdate = [
|
||||
"@pankod/refine-airtable",
|
||||
"@pankod/refine-graphql",
|
||||
"@pankod/refine-hasura",
|
||||
"@pankod/refine-nestjsx-crud",
|
||||
"@pankod/refine-nextjs-router",
|
||||
"@pankod/refine-react-router",
|
||||
"@pankod/refine-simple-rest",
|
||||
"@pankod/refine-strapi",
|
||||
"@pankod/refine-strapi-graphql",
|
||||
"@pankod/refine-supabase",
|
||||
"@pankod/refine-appwrite",
|
||||
"@pankod/refine-ably",
|
||||
"@pankod/@pankod/refine-strapi-v4",
|
||||
];
|
||||
|
||||
export async function postTransform(files: any, flags: any) {
|
||||
const rootDir = path.join(process.cwd(), files[0]);
|
||||
const packageJsonPath = path.join(rootDir, "package.json");
|
||||
const useYarn = checkPackageLock(rootDir) === "yarn.lock";
|
||||
let packageJsonData;
|
||||
|
||||
try {
|
||||
packageJsonData = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`Error: failed to load package.json from ${packageJsonPath}, ensure provided directory is root.`,
|
||||
);
|
||||
}
|
||||
|
||||
const dependenciesToInstall: Array<{
|
||||
name: string;
|
||||
version: string;
|
||||
}> = [
|
||||
{
|
||||
name: "@pankod/refine-core",
|
||||
version: "3.x.x",
|
||||
},
|
||||
{
|
||||
name: "@pankod/refine-antd",
|
||||
version: "3.x.x",
|
||||
},
|
||||
];
|
||||
|
||||
for (const key of Object.keys(packageJsonData.dependencies)) {
|
||||
if (packagesToUpdate.includes(key)) {
|
||||
dependenciesToInstall.push({
|
||||
name: key,
|
||||
version: "3.x.x",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!flags.dry) {
|
||||
await install(
|
||||
rootDir,
|
||||
dependenciesToInstall.map((dep) => `${dep.name}@${dep.version}`),
|
||||
{
|
||||
useYarn,
|
||||
isOnline: true,
|
||||
},
|
||||
);
|
||||
|
||||
await remove(rootDir, ["@pankod/refine"], {
|
||||
useYarn,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default function transformer(file: FileInfo, api: API): string {
|
||||
const j = api.jscodeshift;
|
||||
const source = j(file.source);
|
||||
|
||||
const refineImports = source.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: "@pankod/refine",
|
||||
},
|
||||
});
|
||||
|
||||
if (refineImports.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
updateRefineImports(j, source);
|
||||
moveConfigProvider(j, source);
|
||||
updateSetEditIdToSetId(j, source);
|
||||
defaultLoginPage(j, source);
|
||||
defaultLayout(j, source);
|
||||
defaultCatchAllPage(j, source);
|
||||
|
||||
return source.toSource();
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
import path from "path";
|
||||
import { API, FileInfo } from "jscodeshift";
|
||||
import PackageJson from "@npmcli/package-json";
|
||||
|
||||
import { addV3LegacyAuthProviderCompatibleTrueToAuthHooks } from "./v4/add-v3LegacyAuthProviderCompatible-true-to-auth-hooks";
|
||||
import { authProviderToLegacyAuthProvider } from "./v4/authProvider-to-legacyAuthProvider";
|
||||
import { metaDataToMeta } from "./v4/metadata-to-meta";
|
||||
import { moveDeprecatedAccessControlProps } from "./v4/move-deprecated-access-control";
|
||||
import { routerToLegacyRouter } from "./v4/router-to-legacy-router";
|
||||
import { resourceNameToResourceForButtons } from "./v4/resourceName-to-resource";
|
||||
import { separateImportsAntD } from "./v4/separate-imports-antd";
|
||||
import { separateImportsChakra } from "./v4/separate-imports-chakra";
|
||||
import { separateImportsMantine } from "./v4/separate-imports-mantine";
|
||||
import { separateImportsMUI } from "./v4/separate-imports-mui";
|
||||
import { separateImportsReactHookForm } from "./v4/separate-imports-react-hook-form";
|
||||
import { separateImportsReactQuery } from "./v4/separate-imports-react-query";
|
||||
import { separateImportsReactTable } from "./v4/separate-imports-react-table";
|
||||
import { useMenuToCore } from "./v4/use-menu-to-core";
|
||||
import { separateImportsReactRouterV6 } from "./v4/separate-imports-react-router-v6";
|
||||
import { fixV4Deprecations } from "./v4/fix-v4-deprecations";
|
||||
import { replacePankodImportsWithRefineDev } from "./v4/replace-pankod-imports-with-refinedev";
|
||||
import {
|
||||
CONFIG_FILE_NAME,
|
||||
CodemodConfig,
|
||||
install,
|
||||
checkPackageLock,
|
||||
} from "../helpers";
|
||||
|
||||
export async function postTransform(files: any, flags: any) {
|
||||
const config = new CodemodConfig(CONFIG_FILE_NAME);
|
||||
|
||||
if (flags.dry) {
|
||||
config.destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
const rootDir = path.join(process.cwd(), files[0]);
|
||||
|
||||
const pkgJson = await PackageJson.load(rootDir);
|
||||
|
||||
const hasPankodCLIInDependencies =
|
||||
!!pkgJson.content.dependencies?.["@pankod/refine-cli"];
|
||||
|
||||
const hasPankodCLIInDevDependencies =
|
||||
!!pkgJson.content.devDependencies?.["@pankod/refine-cli"];
|
||||
|
||||
const dependencies = {
|
||||
...pkgJson.content.dependencies,
|
||||
...config.getInstalls(),
|
||||
};
|
||||
|
||||
const devDependencies = {
|
||||
...pkgJson.content.devDependencies,
|
||||
};
|
||||
|
||||
for (const packageName of [
|
||||
...config.getUninstalls(),
|
||||
"@pankod/refine-cli",
|
||||
]) {
|
||||
delete dependencies[packageName];
|
||||
}
|
||||
|
||||
if (hasPankodCLIInDependencies || hasPankodCLIInDevDependencies) {
|
||||
dependencies["@refinedev/cli"] = "latest";
|
||||
}
|
||||
|
||||
if (hasPankodCLIInDevDependencies) {
|
||||
delete devDependencies["@pankod/refine-cli"];
|
||||
}
|
||||
|
||||
Object.keys(dependencies).forEach((dep) => {
|
||||
if (
|
||||
dep.startsWith("@pankod") &&
|
||||
![
|
||||
"@pankod/refine-react-location",
|
||||
"@pankod/refine-react-router",
|
||||
].includes(dep)
|
||||
) {
|
||||
delete dependencies[dep];
|
||||
|
||||
const isMUI = dep.includes("@pankod/refine-mui");
|
||||
const migratableMUIVersion = "^4.18.2";
|
||||
|
||||
dependencies[dep.replace("@pankod/refine-", "@refinedev/")] = isMUI
|
||||
? migratableMUIVersion
|
||||
: "latest";
|
||||
}
|
||||
});
|
||||
|
||||
pkgJson.update({
|
||||
dependencies,
|
||||
devDependencies,
|
||||
});
|
||||
|
||||
await pkgJson.save();
|
||||
|
||||
const useYarn = checkPackageLock(rootDir) === "yarn.lock";
|
||||
|
||||
await install(rootDir, null, { useYarn, isOnline: true });
|
||||
|
||||
config.destroy();
|
||||
}
|
||||
|
||||
export default function transformer(file: FileInfo, api: API): string {
|
||||
const j = api.jscodeshift;
|
||||
const source = j(file.source);
|
||||
|
||||
fixV4Deprecations(j, source);
|
||||
separateImportsReactRouterV6(j, source);
|
||||
addV3LegacyAuthProviderCompatibleTrueToAuthHooks(j, source);
|
||||
authProviderToLegacyAuthProvider(j, source);
|
||||
metaDataToMeta(j, source);
|
||||
moveDeprecatedAccessControlProps(j, source);
|
||||
resourceNameToResourceForButtons(j, source);
|
||||
routerToLegacyRouter(j, source);
|
||||
separateImportsAntD(j, source);
|
||||
separateImportsChakra(j, source);
|
||||
separateImportsMantine(j, source);
|
||||
separateImportsMUI(j, source);
|
||||
separateImportsReactHookForm(j, source);
|
||||
separateImportsReactQuery(j, source);
|
||||
separateImportsReactTable(j, source);
|
||||
useMenuToCore(j, source);
|
||||
replacePankodImportsWithRefineDev(j, source);
|
||||
|
||||
return source.toSource();
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
import {
|
||||
API,
|
||||
JSCodeshift,
|
||||
Collection,
|
||||
FileInfo,
|
||||
ObjectExpression,
|
||||
Identifier,
|
||||
Property,
|
||||
JSXAttribute,
|
||||
} from "jscodeshift";
|
||||
|
||||
export const parser = "tsx";
|
||||
|
||||
const removeColumnsOnUseDataGrid = (j: JSCodeshift, root: Collection<any>) => {
|
||||
const useDataGridHook = root.find(j.CallExpression, {
|
||||
callee: {
|
||||
name: "useDataGrid",
|
||||
},
|
||||
});
|
||||
|
||||
useDataGridHook.replaceWith((p) => {
|
||||
if (p.node.arguments.length === 0) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
const propertiesWithoutColumns = (
|
||||
p.node.arguments[0] as unknown as ObjectExpression
|
||||
).properties.filter(
|
||||
(p: Property) => (p.key as Identifier).name !== "columns",
|
||||
);
|
||||
|
||||
if (propertiesWithoutColumns.length === 0) {
|
||||
p.node.arguments = [];
|
||||
|
||||
return p.node;
|
||||
}
|
||||
|
||||
(p.node.arguments[0] as unknown as ObjectExpression).properties =
|
||||
propertiesWithoutColumns;
|
||||
|
||||
return p.node;
|
||||
});
|
||||
};
|
||||
|
||||
const addColumnsToUseDataGrid = (j: JSCodeshift, root: Collection<any>) => {
|
||||
const dataGridElement = root.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name: "DataGrid",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (dataGridElement.length === 0) {
|
||||
console.warn(
|
||||
"If you use `useDataGrid` hook, you need to use `DataGrid` element.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
dataGridElement.forEach((path) => {
|
||||
const hasColumnsAttribute = path.node.openingElement.attributes.find(
|
||||
(attribute) => (attribute as JSXAttribute).name?.name === "columns",
|
||||
);
|
||||
|
||||
if (hasColumnsAttribute) {
|
||||
return;
|
||||
}
|
||||
|
||||
path.node.openingElement.attributes.push(
|
||||
j.jsxAttribute(
|
||||
j.jsxIdentifier("columns"),
|
||||
j.jsxExpressionContainer(j.identifier("columns")),
|
||||
),
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
export default function transformer(file: FileInfo, api: API): string {
|
||||
const j = api.jscodeshift;
|
||||
const source = j(file.source);
|
||||
|
||||
const refineMuiUseDataGridImports = source.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: "@pankod/refine-mui",
|
||||
},
|
||||
specifiers: [
|
||||
{
|
||||
imported: {
|
||||
name: "useDataGrid",
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (refineMuiUseDataGridImports.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
removeColumnsOnUseDataGrid(j, source);
|
||||
addColumnsToUseDataGrid(j, source);
|
||||
|
||||
return source.toSource();
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
import { Collection, JSCodeshift } from "jscodeshift";
|
||||
|
||||
const authHookNames = [
|
||||
"useForgotPassword",
|
||||
"useGetIdentity",
|
||||
"useIsAuthenticated",
|
||||
"useAuthenticated",
|
||||
"useLogin",
|
||||
"useLogout",
|
||||
"useOnError",
|
||||
"useCheckError",
|
||||
"usePermission",
|
||||
"useRegister",
|
||||
"useUpdatePassword",
|
||||
"usePermissions",
|
||||
];
|
||||
|
||||
export const addV3LegacyAuthProviderCompatibleTrueToAuthHooks = (
|
||||
j: JSCodeshift,
|
||||
root: Collection<any>,
|
||||
) => {
|
||||
const authHooks = root.find(j.CallExpression, {
|
||||
callee: {
|
||||
name: (name: string) => authHookNames.includes(name),
|
||||
},
|
||||
});
|
||||
|
||||
authHooks.forEach((authHook) => {
|
||||
const args = authHook.value.arguments;
|
||||
|
||||
if (args.length === 0) {
|
||||
args.push(
|
||||
j.objectExpression([
|
||||
j.objectProperty(
|
||||
j.identifier("v3LegacyAuthProviderCompatible"),
|
||||
j.booleanLiteral(true),
|
||||
),
|
||||
]),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.length === 1) {
|
||||
const arg = args[0];
|
||||
if (arg.type === "ObjectExpression") {
|
||||
const legacyProp = arg.properties.find(
|
||||
(property) =>
|
||||
property["key"]?.name ===
|
||||
"v3LegacyAuthProviderCompatible",
|
||||
);
|
||||
|
||||
if (legacyProp) {
|
||||
return;
|
||||
}
|
||||
|
||||
arg.properties.push(
|
||||
j.objectProperty(
|
||||
j.identifier("v3LegacyAuthProviderCompatible"),
|
||||
j.booleanLiteral(true),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (arg.type === "Identifier") {
|
||||
// turn to spread syntax and add v3LegacyAuthProviderCompatible
|
||||
const newArg = j.objectExpression([
|
||||
j.spreadElement(arg),
|
||||
j.objectProperty(
|
||||
j.identifier("v3LegacyAuthProviderCompatible"),
|
||||
j.booleanLiteral(true),
|
||||
),
|
||||
]);
|
||||
|
||||
args[0] = newArg;
|
||||
}
|
||||
return;
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -1,43 +0,0 @@
|
||||
import { Collection, JSCodeshift } from "jscodeshift";
|
||||
|
||||
const REFINE_CORE_PATH = "@pankod/refine-core";
|
||||
|
||||
export const authProviderToLegacyAuthProvider = (
|
||||
j: JSCodeshift,
|
||||
root: Collection<any>,
|
||||
) => {
|
||||
const refineCorePath = root
|
||||
.find(j.ImportDeclaration)
|
||||
.filter((path) => path.node.source.value === REFINE_CORE_PATH)
|
||||
.find(j.ImportSpecifier);
|
||||
|
||||
const authProviderImport = refineCorePath.filter(
|
||||
(path) => path.node.imported.name === "AuthProvider",
|
||||
);
|
||||
|
||||
// change AuthProvider to LegacyAuthProvider as AuthProvider
|
||||
authProviderImport.forEach((authProvider) => {
|
||||
j(authProvider).replaceWith(
|
||||
j.importSpecifier(
|
||||
j.identifier("LegacyAuthProvider as AuthProvider"),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
// find <Refine>
|
||||
root.findJSXElements("Refine").forEach((path) => {
|
||||
const attributes = path.node.openingElement.attributes;
|
||||
|
||||
// find <Refine authProvider={}>
|
||||
const authProviderAttribute = attributes.find(
|
||||
(attribute) =>
|
||||
attribute.type === "JSXAttribute" &&
|
||||
attribute.name.name === "authProvider",
|
||||
);
|
||||
|
||||
if (!authProviderAttribute) return;
|
||||
|
||||
// change authProvider={} to legacyAuthProvider={}
|
||||
authProviderAttribute["name"]["name"] = "legacyAuthProvider";
|
||||
});
|
||||
};
|
||||
@@ -1,978 +0,0 @@
|
||||
import {
|
||||
JSCodeshift,
|
||||
Collection,
|
||||
ObjectExpression,
|
||||
Property,
|
||||
Identifier,
|
||||
ObjectProperty,
|
||||
BooleanLiteral,
|
||||
JSXAttribute,
|
||||
JSXExpressionContainer,
|
||||
ArrayExpression,
|
||||
} from "jscodeshift";
|
||||
|
||||
export const parser = "tsx";
|
||||
|
||||
const configToSpreadConfig = (j: JSCodeshift, source: Collection) => {
|
||||
const useListHooks = source.find(j.CallExpression, {
|
||||
callee: {
|
||||
name: "useList",
|
||||
},
|
||||
});
|
||||
|
||||
useListHooks.replaceWith((p) => {
|
||||
const configProperty = (
|
||||
p.node.arguments[0] as unknown as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === "config",
|
||||
);
|
||||
|
||||
if (!configProperty) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
const propertiesWithoutConfig = (
|
||||
p.node.arguments[0] as unknown as ObjectExpression
|
||||
).properties.filter(
|
||||
(p: Property) => (p.key as Identifier).name !== "config",
|
||||
);
|
||||
|
||||
const configProperties = (
|
||||
(configProperty as ObjectProperty).value as ObjectExpression
|
||||
).properties;
|
||||
|
||||
p.node.arguments = [
|
||||
j.objectExpression([
|
||||
...propertiesWithoutConfig,
|
||||
...configProperties,
|
||||
]),
|
||||
];
|
||||
|
||||
return p.node;
|
||||
});
|
||||
};
|
||||
|
||||
const sortToSorters = (j: JSCodeshift, source: Collection) => {
|
||||
const willCheckHooks = [
|
||||
"useCheckboxGroup",
|
||||
"useRadioGroup",
|
||||
"useSelect",
|
||||
"useAutocomplete",
|
||||
"useList",
|
||||
];
|
||||
|
||||
willCheckHooks.forEach((hookName) => {
|
||||
const useListHooks = source.find(j.CallExpression, {
|
||||
callee: {
|
||||
name: hookName,
|
||||
},
|
||||
});
|
||||
|
||||
useListHooks.replaceWith((p) => {
|
||||
if (p.node.arguments.length === 0) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
const sortProperty = (
|
||||
p.node.arguments[0] as unknown as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === "sort",
|
||||
);
|
||||
|
||||
if (!sortProperty) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
const propertiesWithoutSort = (
|
||||
p.node.arguments[0] as unknown as ObjectExpression
|
||||
).properties.filter(
|
||||
(p: Property) => (p.key as Identifier).name !== "sort",
|
||||
);
|
||||
|
||||
p.node.arguments = [
|
||||
j.objectExpression([
|
||||
...propertiesWithoutSort,
|
||||
j.objectProperty(
|
||||
j.identifier("sorters"),
|
||||
(sortProperty as any).value,
|
||||
),
|
||||
]),
|
||||
];
|
||||
|
||||
return p.node;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const sorterToSorters = (j: JSCodeshift, source: Collection) => {
|
||||
const willCheckHooks = ["useExport"];
|
||||
|
||||
willCheckHooks.forEach((hookName) => {
|
||||
const useListHooks = source.find(j.CallExpression, {
|
||||
callee: {
|
||||
name: hookName,
|
||||
},
|
||||
});
|
||||
|
||||
useListHooks.replaceWith((p) => {
|
||||
if (p.node.arguments.length === 0) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
const sortProperty = (
|
||||
p.node.arguments[0] as unknown as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === "sorter",
|
||||
);
|
||||
|
||||
if (!sortProperty) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
const propertiesWithoutSort = (
|
||||
p.node.arguments[0] as unknown as ObjectExpression
|
||||
).properties.filter(
|
||||
(p: Property) => (p.key as Identifier).name !== "sorter",
|
||||
);
|
||||
|
||||
p.node.arguments = [
|
||||
j.objectExpression([
|
||||
...propertiesWithoutSort,
|
||||
j.objectProperty(
|
||||
j.identifier("sorters"),
|
||||
(sortProperty as any).value,
|
||||
),
|
||||
]),
|
||||
];
|
||||
|
||||
return p.node;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const resourceNametoResource = (j: JSCodeshift, source: Collection) => {
|
||||
const willCheckHooks = ["useExport", "useImport"];
|
||||
|
||||
willCheckHooks.forEach((hookName) => {
|
||||
const useListHooks = source.find(j.CallExpression, {
|
||||
callee: {
|
||||
name: hookName,
|
||||
},
|
||||
});
|
||||
|
||||
useListHooks.replaceWith((p) => {
|
||||
if (p.node.arguments.length === 0) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
const resourceNameProperty = (
|
||||
p.node.arguments[0] as unknown as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === "resourceName",
|
||||
);
|
||||
|
||||
if (!resourceNameProperty) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
const propertiesWithoutResourceName = (
|
||||
p.node.arguments[0] as unknown as ObjectExpression
|
||||
).properties.filter(
|
||||
(p: Property) => (p.key as Identifier).name !== "resourceName",
|
||||
);
|
||||
|
||||
p.node.arguments = [
|
||||
j.objectExpression([
|
||||
...propertiesWithoutResourceName,
|
||||
j.objectProperty(
|
||||
j.identifier("resource"),
|
||||
(resourceNameProperty as any).value,
|
||||
),
|
||||
]),
|
||||
];
|
||||
|
||||
return p.node;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const deprecatedUseTablePaginationProps = [
|
||||
"initialCurrent",
|
||||
"initialPageSize",
|
||||
"hasPagination",
|
||||
];
|
||||
|
||||
const deprecatedUseTableFiltersProps = [
|
||||
"initialFilter",
|
||||
"permanentFilter",
|
||||
"defaultSetFilterBehavior",
|
||||
];
|
||||
|
||||
const deprecatedUseTableSortersProps = ["initialSorter", "permanentSorter"];
|
||||
|
||||
const fixDeprecatedReactTableProps = (j: JSCodeshift, source: Collection) => {
|
||||
const refineReactTableImports = source.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: "@pankod/refine-react-table",
|
||||
},
|
||||
specifiers: [
|
||||
{
|
||||
imported: {
|
||||
name: "useTable",
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (refineReactTableImports.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const useTableHooks = source.find(j.CallExpression, {
|
||||
callee: {
|
||||
name: "useTable",
|
||||
},
|
||||
});
|
||||
|
||||
useTableHooks.replaceWith((p) => {
|
||||
if (p.node.arguments.length === 0) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
const hasRefineCoreProps = (
|
||||
p.node.arguments[0] as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === "refineCoreProps",
|
||||
);
|
||||
|
||||
if (!hasRefineCoreProps) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
const otherProperties = (
|
||||
p.node.arguments[0] as ObjectExpression
|
||||
).properties.filter(
|
||||
(p: Property) => (p.key as Identifier).name !== "refineCoreProps",
|
||||
);
|
||||
|
||||
const paginationProperties = deprecatedUseTablePaginationProps.map(
|
||||
(prop) => {
|
||||
const property = (
|
||||
(hasRefineCoreProps as ObjectProperty)
|
||||
.value as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === prop,
|
||||
);
|
||||
|
||||
if (!property) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (prop === "hasPagination") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("mode"),
|
||||
j.literal(
|
||||
(
|
||||
(property as ObjectProperty)
|
||||
.value as BooleanLiteral
|
||||
).value
|
||||
? "server"
|
||||
: "off",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (prop === "initialCurrent") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("current"),
|
||||
(property as ObjectProperty).value,
|
||||
);
|
||||
}
|
||||
|
||||
if (prop === "initialPageSize") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("pageSize"),
|
||||
(property as ObjectProperty).value,
|
||||
);
|
||||
}
|
||||
|
||||
return;
|
||||
},
|
||||
);
|
||||
|
||||
const paginationProperty = j.property(
|
||||
"init",
|
||||
j.identifier("pagination"),
|
||||
j.objectExpression(paginationProperties.filter(Boolean)),
|
||||
);
|
||||
|
||||
const filtersProperties = deprecatedUseTableFiltersProps.map((prop) => {
|
||||
const property = (
|
||||
(hasRefineCoreProps as ObjectProperty).value as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === prop,
|
||||
);
|
||||
|
||||
if (!property) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (prop === "initialFilter") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("initial"),
|
||||
(property as ObjectProperty).value,
|
||||
);
|
||||
}
|
||||
|
||||
if (prop === "permanentFilter") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("permanent"),
|
||||
(property as ObjectProperty).value,
|
||||
);
|
||||
}
|
||||
|
||||
if (prop === "defaultSetFilterBehavior") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("defaultBehavior"),
|
||||
(property as ObjectProperty).value,
|
||||
);
|
||||
}
|
||||
|
||||
return;
|
||||
});
|
||||
|
||||
const filtersProperty = j.property(
|
||||
"init",
|
||||
j.identifier("filters"),
|
||||
j.objectExpression(filtersProperties.filter(Boolean)),
|
||||
);
|
||||
|
||||
const sortersProperties = deprecatedUseTableSortersProps.map((prop) => {
|
||||
const property = (
|
||||
(hasRefineCoreProps as ObjectProperty).value as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === prop,
|
||||
);
|
||||
|
||||
if (!property) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (prop === "initialSorter") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("initial"),
|
||||
(property as ObjectProperty).value,
|
||||
);
|
||||
}
|
||||
|
||||
if (prop === "permanentSorter") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("permanent"),
|
||||
(property as ObjectProperty).value,
|
||||
);
|
||||
}
|
||||
|
||||
return;
|
||||
});
|
||||
|
||||
const sortersProperty = j.property(
|
||||
"init",
|
||||
j.identifier("sorters"),
|
||||
j.objectExpression(sortersProperties.filter(Boolean)),
|
||||
);
|
||||
|
||||
const otherRefineCoreProps = (
|
||||
(hasRefineCoreProps as ObjectProperty).value as ObjectExpression
|
||||
).properties.filter(
|
||||
(p: Property) =>
|
||||
![
|
||||
...deprecatedUseTablePaginationProps,
|
||||
...deprecatedUseTableSortersProps,
|
||||
...deprecatedUseTableFiltersProps,
|
||||
].includes((p.key as Identifier).name),
|
||||
);
|
||||
|
||||
const refineCorePropsProperty = j.property(
|
||||
"init",
|
||||
j.identifier("refineCoreProps"),
|
||||
j.objectExpression(
|
||||
[
|
||||
...otherRefineCoreProps,
|
||||
(paginationProperty.value as ObjectExpression).properties
|
||||
.length > 0
|
||||
? paginationProperty
|
||||
: null,
|
||||
(filtersProperty.value as ObjectExpression).properties
|
||||
.length > 0
|
||||
? filtersProperty
|
||||
: null,
|
||||
(sortersProperty.value as ObjectExpression).properties
|
||||
.length > 0
|
||||
? sortersProperty
|
||||
: null,
|
||||
].filter(Boolean),
|
||||
),
|
||||
);
|
||||
|
||||
p.node.arguments = [
|
||||
j.objectExpression([...otherProperties, refineCorePropsProperty]),
|
||||
];
|
||||
|
||||
return p.node;
|
||||
});
|
||||
};
|
||||
|
||||
const fixDeprecatedUseTableProps = (j: JSCodeshift, source: Collection) => {
|
||||
const willCheckImports = ["useTable", "useDataGrid"];
|
||||
|
||||
willCheckImports.forEach((hook) => {
|
||||
const useTableHooks = source.find(j.CallExpression, {
|
||||
callee: {
|
||||
name: hook,
|
||||
},
|
||||
});
|
||||
|
||||
useTableHooks.replaceWith((p) => {
|
||||
if (p.node.arguments.length === 0) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
const otherProperties = (
|
||||
p.node.arguments[0] as ObjectExpression
|
||||
).properties.filter(
|
||||
(p: Property) =>
|
||||
![
|
||||
...deprecatedUseTablePaginationProps,
|
||||
...deprecatedUseTableSortersProps,
|
||||
...deprecatedUseTableFiltersProps,
|
||||
].includes((p.key as Identifier).name),
|
||||
);
|
||||
|
||||
const paginationProperties = deprecatedUseTablePaginationProps.map(
|
||||
(prop) => {
|
||||
const property = (
|
||||
p.node.arguments[0] as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === prop,
|
||||
);
|
||||
|
||||
if (!property) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (prop === "hasPagination") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("mode"),
|
||||
j.literal(
|
||||
(
|
||||
(property as ObjectProperty)
|
||||
.value as BooleanLiteral
|
||||
).value
|
||||
? "server"
|
||||
: "off",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (prop === "initialCurrent") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("current"),
|
||||
(property as ObjectProperty).value,
|
||||
);
|
||||
}
|
||||
|
||||
if (prop === "initialPageSize") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("pageSize"),
|
||||
(property as ObjectProperty).value,
|
||||
);
|
||||
}
|
||||
|
||||
return;
|
||||
},
|
||||
);
|
||||
|
||||
const paginationProperty = j.property(
|
||||
"init",
|
||||
j.identifier("pagination"),
|
||||
j.objectExpression(paginationProperties.filter(Boolean)),
|
||||
);
|
||||
|
||||
const filtersProperties = deprecatedUseTableFiltersProps.map(
|
||||
(prop) => {
|
||||
const property = (
|
||||
p.node.arguments[0] as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === prop,
|
||||
);
|
||||
|
||||
if (!property) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (prop === "initialFilter") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("initial"),
|
||||
(property as ObjectProperty).value,
|
||||
);
|
||||
}
|
||||
|
||||
if (prop === "permanentFilter") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("permanent"),
|
||||
(property as ObjectProperty).value,
|
||||
);
|
||||
}
|
||||
|
||||
if (prop === "defaultSetFilterBehavior") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("defaultBehavior"),
|
||||
(property as ObjectProperty).value,
|
||||
);
|
||||
}
|
||||
|
||||
return;
|
||||
},
|
||||
);
|
||||
|
||||
const filtersProperty = j.property(
|
||||
"init",
|
||||
j.identifier("filters"),
|
||||
j.objectExpression(filtersProperties.filter(Boolean)),
|
||||
);
|
||||
|
||||
const sortersProperties = deprecatedUseTableSortersProps.map(
|
||||
(prop) => {
|
||||
const property = (
|
||||
p.node.arguments[0] as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === prop,
|
||||
);
|
||||
|
||||
if (!property) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (prop === "initialSorter") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("initial"),
|
||||
(property as ObjectProperty).value,
|
||||
);
|
||||
}
|
||||
|
||||
if (prop === "permanentSorter") {
|
||||
return j.property(
|
||||
"init",
|
||||
j.identifier("permanent"),
|
||||
(property as ObjectProperty).value,
|
||||
);
|
||||
}
|
||||
|
||||
return;
|
||||
},
|
||||
);
|
||||
|
||||
const sortersProperty = j.property(
|
||||
"init",
|
||||
j.identifier("sorters"),
|
||||
j.objectExpression(sortersProperties.filter(Boolean)),
|
||||
);
|
||||
|
||||
p.node.arguments = [
|
||||
j.objectExpression(
|
||||
[
|
||||
...otherProperties,
|
||||
(paginationProperty.value as ObjectExpression)
|
||||
.properties.length > 0
|
||||
? paginationProperty
|
||||
: null,
|
||||
(filtersProperty.value as ObjectExpression).properties
|
||||
.length > 0
|
||||
? filtersProperty
|
||||
: null,
|
||||
(sortersProperty.value as ObjectExpression).properties
|
||||
.length > 0
|
||||
? sortersProperty
|
||||
: null,
|
||||
].filter(Boolean),
|
||||
),
|
||||
];
|
||||
|
||||
return p.node;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const fixUseListHasPaginationToPaginationMode = (
|
||||
j: JSCodeshift,
|
||||
source: Collection,
|
||||
) => {
|
||||
const useListHooks = source.find(j.CallExpression, {
|
||||
callee: {
|
||||
name: "useList",
|
||||
},
|
||||
});
|
||||
|
||||
useListHooks.replaceWith((p) => {
|
||||
if (p.node.arguments.length === 0) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
const hasPaginationProperty = (
|
||||
p.node.arguments[0] as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === "hasPagination",
|
||||
);
|
||||
|
||||
if (!hasPaginationProperty) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
const paginationProperty = (
|
||||
p.node.arguments[0] as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === "pagination",
|
||||
);
|
||||
|
||||
if (paginationProperty) {
|
||||
(paginationProperty as any).value.properties.push(
|
||||
j.property(
|
||||
"init",
|
||||
j.identifier("mode"),
|
||||
j.literal(
|
||||
(
|
||||
(hasPaginationProperty as ObjectProperty)
|
||||
.value as BooleanLiteral
|
||||
).value
|
||||
? "server"
|
||||
: "off",
|
||||
),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
(p.node.arguments[0] as ObjectExpression).properties.push(
|
||||
j.property(
|
||||
"init",
|
||||
j.identifier("pagination"),
|
||||
j.objectExpression([
|
||||
j.property(
|
||||
"init",
|
||||
j.identifier("mode"),
|
||||
j.literal(
|
||||
(
|
||||
(hasPaginationProperty as ObjectProperty)
|
||||
.value as BooleanLiteral
|
||||
).value
|
||||
? "server"
|
||||
: "off",
|
||||
),
|
||||
),
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
(p.node.arguments[0] as ObjectExpression).properties = (
|
||||
p.node.arguments[0] as ObjectExpression
|
||||
).properties.filter(
|
||||
(p: Property) => (p.key as Identifier).name !== "hasPagination",
|
||||
);
|
||||
|
||||
return p.node;
|
||||
});
|
||||
};
|
||||
|
||||
const fixUseSelectHasPaginationToPaginationMode = (
|
||||
j: JSCodeshift,
|
||||
source: Collection,
|
||||
) => {
|
||||
const useSelectHooks = source.find(j.CallExpression, {
|
||||
callee: {
|
||||
name: "useSelect",
|
||||
},
|
||||
});
|
||||
|
||||
useSelectHooks.replaceWith((p) => {
|
||||
const hasPaginationProperty = (
|
||||
p.node.arguments[0] as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === "hasPagination",
|
||||
);
|
||||
|
||||
const paginationProperty = (
|
||||
p.node.arguments[0] as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === "pagination",
|
||||
);
|
||||
|
||||
const hasMode = (
|
||||
paginationProperty as unknown as any
|
||||
)?.value?.properties?.find((p) => (p["name"] = "mode"));
|
||||
|
||||
if (hasPaginationProperty && !hasMode) {
|
||||
if (paginationProperty) {
|
||||
(paginationProperty as any).value.properties.push(
|
||||
j.property(
|
||||
"init",
|
||||
j.identifier("mode"),
|
||||
j.literal(
|
||||
(
|
||||
(hasPaginationProperty as ObjectProperty)
|
||||
.value as BooleanLiteral
|
||||
).value
|
||||
? "server"
|
||||
: "off",
|
||||
),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
(p.node.arguments[0] as ObjectExpression).properties.push(
|
||||
j.property(
|
||||
"init",
|
||||
j.identifier("pagination"),
|
||||
j.objectExpression([
|
||||
j.property(
|
||||
"init",
|
||||
j.identifier("mode"),
|
||||
j.literal(
|
||||
(
|
||||
(
|
||||
hasPaginationProperty as ObjectProperty
|
||||
).value as BooleanLiteral
|
||||
).value
|
||||
? "server"
|
||||
: "off",
|
||||
),
|
||||
),
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasPaginationProperty && !hasMode) {
|
||||
if (paginationProperty) {
|
||||
(paginationProperty as any).value.properties.push(
|
||||
j.property(
|
||||
"init",
|
||||
j.identifier("mode"),
|
||||
j.stringLiteral("server"),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
(p.node.arguments[0] as ObjectExpression).properties.push(
|
||||
j.property(
|
||||
"init",
|
||||
j.identifier("pagination"),
|
||||
j.objectExpression([
|
||||
j.property(
|
||||
"init",
|
||||
j.identifier("mode"),
|
||||
j.stringLiteral("server"),
|
||||
),
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
(p.node.arguments[0] as ObjectExpression).properties = (
|
||||
p.node.arguments[0] as ObjectExpression
|
||||
).properties.filter(
|
||||
(p: Property) => (p.key as Identifier).name !== "hasPagination",
|
||||
);
|
||||
|
||||
return p.node;
|
||||
});
|
||||
};
|
||||
|
||||
const useCustomConfigSortToSorters = (j: JSCodeshift, source: Collection) => {
|
||||
const useCustomHooks = source.find(j.CallExpression, {
|
||||
callee: {
|
||||
name: "useCustom",
|
||||
},
|
||||
});
|
||||
|
||||
useCustomHooks.replaceWith((p) => {
|
||||
if (p.node.arguments.length === 0) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
const configProperty = (
|
||||
p.node.arguments[0] as ObjectExpression
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === "config",
|
||||
);
|
||||
|
||||
if (!configProperty) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
const sortProperty = (
|
||||
(configProperty as ObjectProperty).value as any
|
||||
).properties.find(
|
||||
(p: Property) => (p.key as Identifier).name === "sort",
|
||||
);
|
||||
|
||||
if (!sortProperty) {
|
||||
return p.node;
|
||||
}
|
||||
|
||||
((configProperty as ObjectProperty).value as any).properties.push(
|
||||
j.property(
|
||||
"init",
|
||||
j.identifier("sorters"),
|
||||
(sortProperty as ObjectProperty).value,
|
||||
),
|
||||
);
|
||||
|
||||
((configProperty as ObjectProperty).value as any).properties = (
|
||||
(configProperty as ObjectProperty).value as any
|
||||
).properties.filter(
|
||||
(p: Property) => (p.key as Identifier).name !== "sort",
|
||||
);
|
||||
|
||||
return p.node;
|
||||
});
|
||||
};
|
||||
|
||||
const setSortertoSetSorters = (j: JSCodeshift, source: Collection) => {
|
||||
const willCheckHooks = ["useTable", "useDataGrid"];
|
||||
|
||||
willCheckHooks.forEach((hook) => {
|
||||
const updatedHooks = source.find(j.CallExpression, {
|
||||
callee: {
|
||||
name: hook,
|
||||
},
|
||||
});
|
||||
|
||||
if (updatedHooks.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
updatedHooks.forEach((path) => {
|
||||
const setSorterProperty = path.parentPath.node.id.properties.find(
|
||||
(p) => p.value.name === "setSorter",
|
||||
);
|
||||
|
||||
if (setSorterProperty) {
|
||||
setSorterProperty.value.name = "setSorters: setSorter";
|
||||
}
|
||||
|
||||
const sorterPropery = path.parentPath.node.id.properties.find(
|
||||
(p) => p.value.name === "sorter",
|
||||
);
|
||||
|
||||
if (sorterPropery) {
|
||||
sorterPropery.value.name = "sorters: sorter";
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const addCommentToUseSimpleList = (j: JSCodeshift, source: Collection) => {
|
||||
const useSimpleListHooks = source.find(j.CallExpression, {
|
||||
callee: {
|
||||
name: "useSimpleList",
|
||||
},
|
||||
});
|
||||
|
||||
useSimpleListHooks.forEach((path) => {
|
||||
const comment = j.commentLine(
|
||||
"`useSimpleList` does not accept all of Ant Design's `List` component props anymore. You can directly use `List` component instead.",
|
||||
false,
|
||||
true,
|
||||
);
|
||||
|
||||
path.parentPath.insertBefore(comment);
|
||||
});
|
||||
};
|
||||
|
||||
const resourceOptionstoMeta = (j: JSCodeshift, source: Collection) => {
|
||||
const refineElement = source.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name: "Refine",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (refineElement.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
refineElement.forEach((path) => {
|
||||
const resources = path.node.openingElement.attributes.find(
|
||||
(p) => (p as JSXAttribute).name?.name === "resources",
|
||||
);
|
||||
|
||||
if (!resources) {
|
||||
return;
|
||||
}
|
||||
|
||||
const options = (
|
||||
((resources as JSXAttribute).value as JSXExpressionContainer)
|
||||
.expression as ArrayExpression
|
||||
).elements.filter((p) => {
|
||||
const properties = (p as ObjectExpression).properties;
|
||||
|
||||
return (
|
||||
properties.find(
|
||||
(p) =>
|
||||
((p as ObjectProperty).key as Identifier).name ===
|
||||
"options",
|
||||
) !== undefined
|
||||
);
|
||||
});
|
||||
|
||||
if (options.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
options.forEach((p) => {
|
||||
const properties = (p as ObjectExpression).properties;
|
||||
|
||||
const optionsProperty = properties.find(
|
||||
(p) =>
|
||||
((p as ObjectProperty).key as Identifier).name ===
|
||||
"options",
|
||||
);
|
||||
|
||||
if (!optionsProperty) {
|
||||
return;
|
||||
}
|
||||
|
||||
(optionsProperty as ObjectProperty).key = j.identifier("meta");
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const fixV4Deprecations = async (j: JSCodeshift, source: Collection) => {
|
||||
configToSpreadConfig(j, source);
|
||||
sortToSorters(j, source);
|
||||
sorterToSorters(j, source);
|
||||
resourceNametoResource(j, source);
|
||||
fixDeprecatedReactTableProps(j, source);
|
||||
fixDeprecatedUseTableProps(j, source);
|
||||
fixUseListHasPaginationToPaginationMode(j, source);
|
||||
fixUseSelectHasPaginationToPaginationMode(j, source);
|
||||
useCustomConfigSortToSorters(j, source);
|
||||
setSortertoSetSorters(j, source);
|
||||
addCommentToUseSimpleList(j, source);
|
||||
resourceOptionstoMeta(j, source);
|
||||
};
|
||||
@@ -1,53 +0,0 @@
|
||||
import { JSCodeshift, Collection } from "jscodeshift";
|
||||
|
||||
export const metaDataToMeta = (j: JSCodeshift, source: Collection) => {
|
||||
// find all JSX elements
|
||||
source.find(j.JSXElement).forEach((path) => {
|
||||
const attributes = path.node.openingElement.attributes;
|
||||
if (!attributes) return;
|
||||
|
||||
// if they have a meta attribute, skip them.
|
||||
const hasMeta = attributes.some(
|
||||
(attribute) => attribute?.["name"]?.["name"] === "meta",
|
||||
);
|
||||
if (hasMeta) return;
|
||||
|
||||
// if they have a metaData change it to meta.
|
||||
path.node.openingElement.attributes.forEach((attribute) => {
|
||||
if (attribute?.["name"]?.["name"] === "metaData") {
|
||||
attribute["name"]["name"] = "meta";
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// find all call expressions
|
||||
source.find(j.CallExpression).forEach((path) => {
|
||||
// find all arguments
|
||||
path.node.arguments.forEach((argument) => {
|
||||
const properties = argument["properties"];
|
||||
if (!properties) return;
|
||||
|
||||
// if they have a meta argument, skip them.
|
||||
const hasMeta = properties.some(
|
||||
(property) => property?.["key"]?.["name"] === "meta",
|
||||
);
|
||||
if (hasMeta) return;
|
||||
|
||||
// if they have a metaData change it to meta.
|
||||
properties.forEach((property) => {
|
||||
if (
|
||||
property?.["shorthand"] &&
|
||||
property?.["key"]?.["name"] === "metaData"
|
||||
) {
|
||||
property["key"]["name"] = "meta";
|
||||
property["value"]["name"] === "metaData";
|
||||
property["shorthand"] = false;
|
||||
} else {
|
||||
if (property?.["key"]?.["name"] === "metaData") {
|
||||
property["key"]["name"] = "meta";
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -1,41 +0,0 @@
|
||||
import { JSCodeshift, Collection } from "jscodeshift";
|
||||
|
||||
const deprecatedPropName = "ignoreAccessControlProvider";
|
||||
const newName = "accessControl";
|
||||
const newProperty = "enabled";
|
||||
|
||||
export const moveDeprecatedAccessControlProps = (
|
||||
j: JSCodeshift,
|
||||
source: Collection,
|
||||
) => {
|
||||
source.find(j.JSXElement).forEach((path) => {
|
||||
const attributes = path.node.openingElement.attributes;
|
||||
if (!attributes) return;
|
||||
|
||||
const hasAccessControl = attributes.some(
|
||||
(attribute) => attribute?.["name"]?.["name"] === "accessControl",
|
||||
);
|
||||
|
||||
if (hasAccessControl) return;
|
||||
|
||||
const oldProp = path.node.openingElement.attributes.find(
|
||||
(attribute) => {
|
||||
return attribute?.["name"]?.["name"] === deprecatedPropName;
|
||||
},
|
||||
);
|
||||
|
||||
if (oldProp) {
|
||||
const oldValue = oldProp?.["value"]?.["expression"]?.["value"];
|
||||
const newValue = !oldValue;
|
||||
|
||||
oldProp["name"]["name"] = newName;
|
||||
|
||||
oldProp["value"]["expression"] = j.objectExpression([
|
||||
j.objectProperty(
|
||||
j.identifier(newProperty),
|
||||
j.booleanLiteral(newValue),
|
||||
),
|
||||
]);
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -1,110 +0,0 @@
|
||||
import { Collection, JSCodeshift } from "jscodeshift";
|
||||
import { CONFIG_FILE_NAME, CodemodConfig } from "../../helpers";
|
||||
|
||||
export const parser = "tsx";
|
||||
|
||||
const previousScope = "@pankod/refine-";
|
||||
const newScope = "@refinedev/";
|
||||
|
||||
const deprecatedPackages = [
|
||||
"@pankod/refine-react-location",
|
||||
"@pankod/refine-react-router",
|
||||
];
|
||||
|
||||
const getOldPackageName = (oldName: string) => {
|
||||
return `${previousScope}${
|
||||
oldName.replace(previousScope, "").split("/")[0]
|
||||
}`;
|
||||
};
|
||||
|
||||
const getNewPackageName = (oldName: string) => {
|
||||
return `${newScope}${oldName.replace(previousScope, "").split("/")[0]}`;
|
||||
};
|
||||
|
||||
const getNewImportValue = (oldValue: string) => {
|
||||
return oldValue.replace(previousScope, newScope);
|
||||
};
|
||||
|
||||
const renameImports = (j: JSCodeshift, source: Collection) => {
|
||||
const config = new CodemodConfig(CONFIG_FILE_NAME);
|
||||
|
||||
source
|
||||
.find(j.ImportDeclaration)
|
||||
.filter(
|
||||
(path) =>
|
||||
path.node.source.value?.toString()?.startsWith(previousScope) &&
|
||||
!deprecatedPackages.includes(
|
||||
path.node.source.value?.toString() ?? "",
|
||||
),
|
||||
)
|
||||
.forEach((path) => {
|
||||
// for example import line is: @pankod/refine-antd/dist/style.css
|
||||
const oldImportValue = path.node.source.value?.toString() ?? "";
|
||||
|
||||
// getOldPackageName will return @pankod/refine-antd
|
||||
const oldName = getOldPackageName(oldImportValue);
|
||||
|
||||
// getNewPackageName will return @refinedev/antd
|
||||
const newName = getNewPackageName(oldName);
|
||||
|
||||
// getNewImportValue will return @refinedev/antd/dist/style.css
|
||||
const newImportValue = getNewImportValue(oldImportValue);
|
||||
|
||||
config.addPackage(newName);
|
||||
config.removePackage(oldName);
|
||||
|
||||
j(path).replaceWith(
|
||||
j.importDeclaration(
|
||||
path.node.specifiers,
|
||||
j.literal(newImportValue),
|
||||
),
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const renameExports = (j: JSCodeshift, source: Collection) => {
|
||||
const config = new CodemodConfig(CONFIG_FILE_NAME);
|
||||
|
||||
source
|
||||
.find(j.ExportNamedDeclaration)
|
||||
.filter(
|
||||
(path) =>
|
||||
path.node.source &&
|
||||
path.node.source.value?.toString()?.startsWith(previousScope) &&
|
||||
!deprecatedPackages.includes(
|
||||
path.node.source.value?.toString() ?? "",
|
||||
),
|
||||
)
|
||||
.forEach((path) => {
|
||||
// for example import line is: @pankod/refine-antd/dist/style.css
|
||||
const oldImportValue = path.node.source.value?.toString() ?? "";
|
||||
|
||||
// getOldPackageName will return @pankod/refine-antd
|
||||
const oldName = getOldPackageName(oldImportValue);
|
||||
|
||||
// getNewPackageName will return @refinedev/antd
|
||||
const newName = getNewPackageName(oldName);
|
||||
|
||||
// getNewImportValue will return @refinedev/antd/dist/style.css
|
||||
const newImportValue = getNewImportValue(oldImportValue);
|
||||
|
||||
config.addPackage(newName);
|
||||
config.removePackage(oldName);
|
||||
|
||||
j(path).replaceWith(
|
||||
j.exportNamedDeclaration(
|
||||
path.node.declaration,
|
||||
path.node.specifiers,
|
||||
j.literal(newImportValue),
|
||||
),
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
export const replacePankodImportsWithRefineDev = async (
|
||||
j: JSCodeshift,
|
||||
source: Collection,
|
||||
) => {
|
||||
renameImports(j, source);
|
||||
renameExports(j, source);
|
||||
};
|
||||
@@ -1,48 +0,0 @@
|
||||
import { Collection, JSCodeshift } from "jscodeshift";
|
||||
|
||||
const COMPONENT_NAMES = [
|
||||
"ShowButton",
|
||||
"EditButton",
|
||||
"DeleteButton",
|
||||
"CloneButton",
|
||||
"ListButton",
|
||||
"RefreshButton",
|
||||
"CreateButton",
|
||||
];
|
||||
|
||||
const DEPRECATED_PROP_NAMES = ["resourceName", "resourceNameOrRouteName"];
|
||||
const NEW_PROP_NAME = "resource";
|
||||
|
||||
export const resourceNameToResourceForButtons = (
|
||||
j: JSCodeshift,
|
||||
source: Collection,
|
||||
) => {
|
||||
// find all JSX elements that are named in COMPONENT_NAMES
|
||||
const elements = source.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name: (name: string) => COMPONENT_NAMES.includes(name),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
elements.forEach((path) => {
|
||||
const attributes = path.node.openingElement.attributes;
|
||||
if (!attributes) return;
|
||||
|
||||
// if they have a NEW_PROP_NAME attribute, skip them.
|
||||
const hasNewAttribute = attributes.some(
|
||||
(attribute) => attribute?.["name"]?.["name"] === NEW_PROP_NAME,
|
||||
);
|
||||
|
||||
if (hasNewAttribute) return;
|
||||
|
||||
// if they have a metaData change it to meta.
|
||||
path.node.openingElement.attributes.forEach((attribute) => {
|
||||
if (DEPRECATED_PROP_NAMES.includes(attribute?.["name"]?.["name"])) {
|
||||
attribute["name"]["name"] = NEW_PROP_NAME;
|
||||
return;
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -1,86 +0,0 @@
|
||||
import { JSCodeshift, Collection, JSXAttribute } from "jscodeshift";
|
||||
|
||||
const legacyMap = {
|
||||
"@pankod/refine-react-router-v6": "@pankod/refine-react-router-v6/legacy",
|
||||
"@pankod/refine-nextjs-router": "@pankod/refine-nextjs-router/legacy",
|
||||
"@pankod/refine-nextjs-router/app":
|
||||
"@pankod/refine-nextjs-router/legacy-app",
|
||||
"@pankod/refine-nextjs-router/pages":
|
||||
"@pankod/refine-nextjs-router/legacy-pages",
|
||||
"@pankod/refine-remix-router": "@pankod/refine-remix-router/legacy",
|
||||
};
|
||||
|
||||
const oldRouterProp = "routerProvider";
|
||||
const newRouterProp = "legacyRouterProvider";
|
||||
|
||||
const renameImport = (
|
||||
j: JSCodeshift,
|
||||
source: Collection,
|
||||
from: string,
|
||||
to: string,
|
||||
) => {
|
||||
source
|
||||
.find(j.ImportDeclaration)
|
||||
.filter((path) => path.node.source.value === from)
|
||||
.forEach((path) => {
|
||||
j(path).replaceWith(
|
||||
j.importDeclaration(path.node.specifiers, j.literal(to)),
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const renameExport = (
|
||||
j: JSCodeshift,
|
||||
source: Collection,
|
||||
from: string,
|
||||
to: string,
|
||||
) => {
|
||||
source
|
||||
.find(j.ExportNamedDeclaration)
|
||||
.filter((path) => path.node.source && path.node.source.value === from)
|
||||
.forEach((path) => {
|
||||
j(path).replaceWith(
|
||||
j.exportNamedDeclaration(
|
||||
path.node.declaration,
|
||||
path.node.specifiers,
|
||||
j.literal(to),
|
||||
),
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const renameProp = (
|
||||
j: JSCodeshift,
|
||||
source: Collection,
|
||||
from: string,
|
||||
to: string,
|
||||
) => {
|
||||
source
|
||||
.find(j.JSXOpeningElement, { name: { name: "Refine" } })
|
||||
.forEach((path) => {
|
||||
const props = path.node.attributes;
|
||||
|
||||
const propIndex = props.findIndex(
|
||||
(prop) => (prop as JSXAttribute)?.name?.name === from,
|
||||
);
|
||||
|
||||
if (propIndex !== -1) {
|
||||
const prop = props[propIndex];
|
||||
|
||||
if ((prop as JSXAttribute)?.name) {
|
||||
if ("name" in prop) {
|
||||
prop.name.name = to;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const routerToLegacyRouter = (j: JSCodeshift, source: Collection) => {
|
||||
Object.entries(legacyMap).forEach(([from, to]) => {
|
||||
renameImport(j, source, from, to);
|
||||
renameExport(j, source, from, to);
|
||||
});
|
||||
|
||||
renameProp(j, source, oldRouterProp, newRouterProp);
|
||||
};
|
||||
@@ -1,121 +0,0 @@
|
||||
import { Collection, JSCodeshift } from "jscodeshift";
|
||||
import { CONFIG_FILE_NAME, CodemodConfig } from "../../helpers";
|
||||
import { separateImports } from "../../helpers";
|
||||
import {
|
||||
exported,
|
||||
rename,
|
||||
renameToDefault,
|
||||
other,
|
||||
} from "../../definitions/separated-imports/antd";
|
||||
|
||||
const REFINE_ANTD_PATH = "@pankod/refine-antd";
|
||||
const ANTD_PATH = "antd";
|
||||
const ANTD_VERSION = "^5.0.5";
|
||||
const ANTD_ICONS_PATH = "@ant-design/icons";
|
||||
const ANTD_ICONS_VERSION = "^5.0.1";
|
||||
|
||||
export const separateImportsAntD = (j: JSCodeshift, source: Collection) => {
|
||||
const config = new CodemodConfig(CONFIG_FILE_NAME);
|
||||
|
||||
separateImports({
|
||||
j,
|
||||
source,
|
||||
imports: exported,
|
||||
renameImports: rename,
|
||||
renameToDefault: renameToDefault,
|
||||
otherImports: other,
|
||||
currentLibName: REFINE_ANTD_PATH,
|
||||
nextLibName: ANTD_PATH,
|
||||
});
|
||||
|
||||
let addIcons = false;
|
||||
|
||||
const addAntd =
|
||||
source.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: ANTD_PATH,
|
||||
},
|
||||
}).length > 0;
|
||||
|
||||
// check Icons import
|
||||
const refineImport = source.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: REFINE_ANTD_PATH,
|
||||
},
|
||||
});
|
||||
|
||||
refineImport.replaceWith((p) => {
|
||||
for (const item of p.node.specifiers) {
|
||||
if (item.local.name === "Icons") {
|
||||
// flag for adding `@antd-design/icons` dependency
|
||||
addIcons = true;
|
||||
|
||||
// add new icon namespace import
|
||||
source
|
||||
.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: REFINE_ANTD_PATH,
|
||||
},
|
||||
})
|
||||
.forEach((path, i) => {
|
||||
if (i === 0) {
|
||||
path.insertAfter(
|
||||
j.importDeclaration(
|
||||
[
|
||||
j.importNamespaceSpecifier(
|
||||
j.identifier("Icons"),
|
||||
),
|
||||
],
|
||||
j.literal(ANTD_ICONS_PATH),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
p.node.specifiers = p.node.specifiers.filter(
|
||||
(p) => p.local.name !== "Icons",
|
||||
);
|
||||
|
||||
return p.node;
|
||||
});
|
||||
|
||||
if (addIcons) {
|
||||
config.addPackage(ANTD_ICONS_PATH, ANTD_ICONS_VERSION);
|
||||
|
||||
// add comment to antd-icons import
|
||||
source
|
||||
|
||||
.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: ANTD_ICONS_PATH,
|
||||
},
|
||||
})
|
||||
.forEach((path) => {
|
||||
path.node.comments = [
|
||||
{
|
||||
type: "CommentLine",
|
||||
value: ` It is recommended to use explicit import as seen below to reduce bundle size.`,
|
||||
},
|
||||
{
|
||||
type: "CommentLine",
|
||||
value: ` import { IconName } from "@ant-design/icons";`,
|
||||
},
|
||||
];
|
||||
});
|
||||
}
|
||||
if (addAntd) {
|
||||
config.addPackage(ANTD_PATH, ANTD_VERSION);
|
||||
}
|
||||
|
||||
// remove empty imports
|
||||
source
|
||||
.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: REFINE_ANTD_PATH,
|
||||
},
|
||||
})
|
||||
.filter((path) => path.node.specifiers.length === 0)
|
||||
.remove();
|
||||
};
|
||||
@@ -1,29 +0,0 @@
|
||||
import { Collection, JSCodeshift } from "jscodeshift";
|
||||
import {
|
||||
CONFIG_FILE_NAME,
|
||||
CodemodConfig,
|
||||
separateImports,
|
||||
} from "../../helpers";
|
||||
import { exported, rename } from "../../definitions/separated-imports/chakra";
|
||||
|
||||
export const separateImportsChakra = (j: JSCodeshift, source: Collection) => {
|
||||
const config = new CodemodConfig(CONFIG_FILE_NAME);
|
||||
|
||||
if (
|
||||
source.find(j.ImportDeclaration, {
|
||||
source: { value: "@pankod/refine-chakra-ui" },
|
||||
}).length > 0
|
||||
) {
|
||||
config.addPackage("@chakra-ui/react", "^2.5.1");
|
||||
}
|
||||
|
||||
separateImports({
|
||||
j,
|
||||
source,
|
||||
imports: exported,
|
||||
renameImports: rename,
|
||||
otherImports: {},
|
||||
currentLibName: "@pankod/refine-chakra-ui",
|
||||
nextLibName: "@chakra-ui/react",
|
||||
});
|
||||
};
|
||||
@@ -1,37 +0,0 @@
|
||||
import { Collection, JSCodeshift } from "jscodeshift";
|
||||
import {
|
||||
CONFIG_FILE_NAME,
|
||||
CodemodConfig,
|
||||
separateImports,
|
||||
} from "../../helpers";
|
||||
import {
|
||||
exported,
|
||||
rename,
|
||||
other,
|
||||
} from "../../definitions/separated-imports/mantine";
|
||||
|
||||
export const separateImportsMantine = (j: JSCodeshift, source: Collection) => {
|
||||
const config = new CodemodConfig(CONFIG_FILE_NAME);
|
||||
|
||||
if (
|
||||
source.find(j.ImportDeclaration, {
|
||||
source: { value: "@pankod/refine-mantine" },
|
||||
}).length > 0
|
||||
) {
|
||||
config.addPackage("@mantine/core", "^5.10.4");
|
||||
config.addPackage("@mantine/hooks", "^5.10.4");
|
||||
config.addPackage("@mantine/form", "^5.10.4");
|
||||
config.addPackage("@mantine/notifications", "^5.10.4");
|
||||
config.addPackage("@emotion/react", "^11.8.2");
|
||||
}
|
||||
|
||||
separateImports({
|
||||
j,
|
||||
source,
|
||||
imports: exported,
|
||||
renameImports: rename,
|
||||
otherImports: other,
|
||||
currentLibName: "@pankod/refine-mantine",
|
||||
nextLibName: "@mantine/core",
|
||||
});
|
||||
};
|
||||
@@ -1,35 +0,0 @@
|
||||
import { Collection, JSCodeshift } from "jscodeshift";
|
||||
import { exported } from "../../definitions/separated-imports/mui";
|
||||
import {
|
||||
CONFIG_FILE_NAME,
|
||||
CodemodConfig,
|
||||
separateImports,
|
||||
} from "../../helpers";
|
||||
|
||||
export const separateImportsMUI = (j: JSCodeshift, source: Collection) => {
|
||||
const config = new CodemodConfig(CONFIG_FILE_NAME);
|
||||
|
||||
if (
|
||||
source.find(j.ImportDeclaration, {
|
||||
source: { value: "@pankod/refine-mui" },
|
||||
}).length > 0
|
||||
) {
|
||||
config.addPackage("@emotion/react", "^11.8.2");
|
||||
config.addPackage("@emotion/styled", "^11.8.1");
|
||||
config.addPackage("@mui/lab", "^5.0.0-alpha.85");
|
||||
config.addPackage("@mui/material", "^5.8.6");
|
||||
config.addPackage("@mui/x-data-grid", "^5.12.1");
|
||||
}
|
||||
|
||||
separateImports({
|
||||
j,
|
||||
source,
|
||||
imports: ["MuiList"],
|
||||
renameImports: {
|
||||
MuiList: "List",
|
||||
},
|
||||
otherImports: exported,
|
||||
currentLibName: "@pankod/refine-mui",
|
||||
nextLibName: "@mui/material",
|
||||
});
|
||||
};
|
||||
@@ -1,39 +0,0 @@
|
||||
import { Collection, JSCodeshift } from "jscodeshift";
|
||||
import { exported } from "../../definitions/separated-imports/react-hook-form";
|
||||
import {
|
||||
CONFIG_FILE_NAME,
|
||||
CodemodConfig,
|
||||
separateImports,
|
||||
} from "../../helpers";
|
||||
|
||||
const REFINE_LIB_PATH = "@pankod/refine-react-hook-form";
|
||||
const REACT_HOOK_FORM_PATH = "react-hook-form";
|
||||
const REACT_HOOK_FORM_VERSION = "^7.30.0";
|
||||
|
||||
export const separateImportsReactHookForm = (
|
||||
j: JSCodeshift,
|
||||
source: Collection,
|
||||
) => {
|
||||
const config = new CodemodConfig(CONFIG_FILE_NAME);
|
||||
|
||||
separateImports({
|
||||
j,
|
||||
source,
|
||||
imports: exported,
|
||||
renameImports: {},
|
||||
otherImports: {},
|
||||
currentLibName: REFINE_LIB_PATH,
|
||||
nextLibName: REACT_HOOK_FORM_PATH,
|
||||
});
|
||||
|
||||
// if use `react-hook-form` add package.json
|
||||
const reactQuery = source.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: REACT_HOOK_FORM_PATH,
|
||||
},
|
||||
});
|
||||
|
||||
if (reactQuery.length) {
|
||||
config.addPackage(REACT_HOOK_FORM_PATH, REACT_HOOK_FORM_VERSION);
|
||||
}
|
||||
};
|
||||
@@ -1,39 +0,0 @@
|
||||
import { Collection, JSCodeshift } from "jscodeshift";
|
||||
import { exported } from "../../definitions/separated-imports/react-query";
|
||||
import {
|
||||
CONFIG_FILE_NAME,
|
||||
CodemodConfig,
|
||||
separateImports,
|
||||
} from "../../helpers";
|
||||
|
||||
const REFINE_LIB_PATH = "@pankod/refine-core";
|
||||
const REACT_QUERY_PATH = "@tanstack/react-query";
|
||||
const REACT_QUERY_VERSION = "^4.10.1";
|
||||
|
||||
export const separateImportsReactQuery = (
|
||||
j: JSCodeshift,
|
||||
source: Collection,
|
||||
) => {
|
||||
const config = new CodemodConfig(CONFIG_FILE_NAME);
|
||||
|
||||
separateImports({
|
||||
j,
|
||||
source,
|
||||
imports: exported,
|
||||
renameImports: {},
|
||||
otherImports: {},
|
||||
currentLibName: REFINE_LIB_PATH,
|
||||
nextLibName: REACT_QUERY_PATH,
|
||||
});
|
||||
|
||||
// if use `@tanstack/react-query` add package.json
|
||||
const reactQuery = source.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: REACT_QUERY_PATH,
|
||||
},
|
||||
});
|
||||
|
||||
if (reactQuery.length) {
|
||||
config.addPackage(REACT_QUERY_PATH, REACT_QUERY_VERSION);
|
||||
}
|
||||
};
|
||||
@@ -1,32 +0,0 @@
|
||||
import { Collection, JSCodeshift } from "jscodeshift";
|
||||
import { exported } from "../../definitions/separated-imports/react-router-v6";
|
||||
import {
|
||||
CONFIG_FILE_NAME,
|
||||
CodemodConfig,
|
||||
separateImports,
|
||||
} from "../../helpers";
|
||||
|
||||
export const separateImportsReactRouterV6 = (
|
||||
j: JSCodeshift,
|
||||
source: Collection,
|
||||
) => {
|
||||
const config = new CodemodConfig(CONFIG_FILE_NAME);
|
||||
|
||||
if (
|
||||
source.find(j.ImportDeclaration, {
|
||||
source: { value: "@pankod/refine-react-router-v6" },
|
||||
}).length > 0
|
||||
) {
|
||||
config.addPackage("react-router-dom", "^6.8.1");
|
||||
}
|
||||
|
||||
separateImports({
|
||||
j,
|
||||
source,
|
||||
imports: exported,
|
||||
renameImports: {},
|
||||
otherImports: {},
|
||||
currentLibName: "@pankod/refine-react-router-v6",
|
||||
nextLibName: "react-router-dom",
|
||||
});
|
||||
};
|
||||
@@ -1,32 +0,0 @@
|
||||
import { Collection, JSCodeshift } from "jscodeshift";
|
||||
import { exported } from "../../definitions/separated-imports/react-table";
|
||||
import {
|
||||
CONFIG_FILE_NAME,
|
||||
CodemodConfig,
|
||||
separateImports,
|
||||
} from "../../helpers";
|
||||
|
||||
export const separateImportsReactTable = (
|
||||
j: JSCodeshift,
|
||||
source: Collection,
|
||||
) => {
|
||||
const config = new CodemodConfig(CONFIG_FILE_NAME);
|
||||
|
||||
if (
|
||||
source.find(j.ImportDeclaration, {
|
||||
source: { value: "@pankod/refine-react-table" },
|
||||
}).length > 0
|
||||
) {
|
||||
config.addPackage("@tanstack/react-table", "^8.2.6");
|
||||
}
|
||||
|
||||
separateImports({
|
||||
j,
|
||||
source,
|
||||
imports: exported,
|
||||
renameImports: {},
|
||||
otherImports: {},
|
||||
currentLibName: "@pankod/refine-react-table",
|
||||
nextLibName: "@tanstack/react-table",
|
||||
});
|
||||
};
|
||||
@@ -1,94 +0,0 @@
|
||||
import { JSCodeshift, Collection } from "jscodeshift";
|
||||
|
||||
const REFINE_ANTD_PATH = "@pankod/refine-antd";
|
||||
const REFINE_MUI_PATH = "@pankod/refine-mui";
|
||||
const REFINE_CORE_PATH = "@pankod/refine-core";
|
||||
|
||||
const ANTD_IMPORTS_TO_MOVE_CORE = ["useMenu"];
|
||||
const MUI_IMPORTS_TO_MOVE_CORE = ["useMenu"];
|
||||
|
||||
const moveImports = (
|
||||
j: JSCodeshift,
|
||||
source: Collection,
|
||||
importNamesToMove: string[],
|
||||
fromModule: string,
|
||||
toModule: string,
|
||||
) => {
|
||||
importNamesToMove.forEach((importName) => {
|
||||
// get the import declaration to be moved
|
||||
const importsFromModule = source
|
||||
.find(j.ImportDeclaration)
|
||||
.filter((path) => path.node.source.value === fromModule)
|
||||
.find(j.ImportSpecifier);
|
||||
|
||||
// filter the imports to be moved
|
||||
const importsToBeMoved = importsFromModule.filter(
|
||||
(path) =>
|
||||
path.node.imported.name === importName ||
|
||||
path.node.local?.name === importName,
|
||||
);
|
||||
|
||||
if (!importsToBeMoved?.length) return;
|
||||
|
||||
importsToBeMoved.forEach((importToMove) => {
|
||||
// get the import declaration of the import to move
|
||||
const importsToModule = source
|
||||
.find(j.ImportDeclaration)
|
||||
.filter((path) => path.node.source.value === toModule);
|
||||
|
||||
// if there is no import declaration for the import to move, create import declaration and add it to the top of the file.
|
||||
if (!importsToModule?.length) {
|
||||
source
|
||||
.get()
|
||||
.node.program.body.unshift(
|
||||
j.importDeclaration(
|
||||
[importToMove.get().node],
|
||||
j.stringLiteral(toModule),
|
||||
),
|
||||
);
|
||||
|
||||
// remove the moved import
|
||||
j(importToMove).remove();
|
||||
return;
|
||||
}
|
||||
|
||||
// add new import from importsToBeMoved to the existing import declaration.
|
||||
// we select the first import declaration if there are multiple import declarations.
|
||||
const importTo = importsToModule.at(0).paths().at(0).get();
|
||||
j(importTo).replaceWith(
|
||||
j.importDeclaration(
|
||||
[...importTo.node.specifiers, importToMove.get().node],
|
||||
importTo.node.source,
|
||||
),
|
||||
);
|
||||
|
||||
// remove the moved import
|
||||
j(importToMove).remove();
|
||||
});
|
||||
});
|
||||
|
||||
// remove empty import declarations after moving imports e.g. import { } from "@pankod/refine-antd"
|
||||
source
|
||||
.find(j.ImportDeclaration)
|
||||
.filter((path) => path.node.source.value === fromModule)
|
||||
.filter((path) => !path.node.specifiers?.length)
|
||||
.forEach((path) => j(path).remove());
|
||||
};
|
||||
|
||||
export const useMenuToCore = (j: JSCodeshift, source: Collection) => {
|
||||
moveImports(
|
||||
j,
|
||||
source,
|
||||
ANTD_IMPORTS_TO_MOVE_CORE,
|
||||
REFINE_ANTD_PATH,
|
||||
REFINE_CORE_PATH,
|
||||
);
|
||||
|
||||
moveImports(
|
||||
j,
|
||||
source,
|
||||
MUI_IMPORTS_TO_MOVE_CORE,
|
||||
REFINE_MUI_PATH,
|
||||
REFINE_CORE_PATH,
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user