mirror of
https://github.com/stefanpejcic/openpanel
synced 2025-06-26 18:28:26 +00:00
cleaner
This commit is contained in:
@@ -1,111 +0,0 @@
|
||||
import {
|
||||
API,
|
||||
FileInfo,
|
||||
JSCodeshift,
|
||||
JSXElement,
|
||||
ASTPath,
|
||||
Collection,
|
||||
} from "jscodeshift";
|
||||
import execa from "execa";
|
||||
import { prettierFormat } from "../utils/swizzle/prettierFormat";
|
||||
|
||||
export const parser = "tsx";
|
||||
|
||||
// runs .bin/jscodeshift with the default export transformer on the current directory
|
||||
export const addDevtoolsComponent = async () => {
|
||||
const jscodeshiftExecutable = require.resolve(".bin/jscodeshift");
|
||||
const { stderr } = execa.sync(jscodeshiftExecutable, [
|
||||
"./",
|
||||
"--extensions=ts,tsx,js,jsx",
|
||||
"--parser=tsx",
|
||||
`--transform=${__dirname}/../src/transformers/add-devtools-component.ts`,
|
||||
`--ignore-pattern=**/.cache/**`,
|
||||
`--ignore-pattern=**/node_modules/**`,
|
||||
`--ignore-pattern=**/build/**`,
|
||||
`--ignore-pattern=**/.next/**`,
|
||||
]);
|
||||
|
||||
if (stderr) {
|
||||
console.log(stderr);
|
||||
}
|
||||
};
|
||||
|
||||
export default async function transformer(file: FileInfo, api: API) {
|
||||
const j = api.jscodeshift;
|
||||
const source = j(file.source);
|
||||
|
||||
const refineElement = source.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name: "Refine",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const hasRefineElement = refineElement.length !== 0;
|
||||
|
||||
if (!hasRefineElement) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (hasDevtoolsImport(j, source) && hasDevtoolsProvider(j, source)) {
|
||||
return;
|
||||
}
|
||||
|
||||
addDevtoolsImport(j, source);
|
||||
refineElement.forEach((path) => {
|
||||
wrapWithDevtoolsProvider(j, path);
|
||||
});
|
||||
|
||||
return await prettierFormat(source.toSource());
|
||||
}
|
||||
|
||||
export const hasDevtoolsImport = (j: JSCodeshift, source: Collection) => {
|
||||
return source.find(j.ImportDeclaration, {
|
||||
source: {
|
||||
value: "@refinedev/devtools",
|
||||
},
|
||||
}).length;
|
||||
};
|
||||
|
||||
export const hasDevtoolsProvider = (j: JSCodeshift, source: Collection) => {
|
||||
return source.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name: "DevtoolsProvider",
|
||||
},
|
||||
},
|
||||
}).length;
|
||||
};
|
||||
|
||||
export const addDevtoolsImport = (j: JSCodeshift, source: Collection) => {
|
||||
const devtoolsImport = j.importDeclaration(
|
||||
[
|
||||
j.importSpecifier(j.identifier("DevtoolsProvider")),
|
||||
j.importSpecifier(j.identifier("DevtoolsPanel")),
|
||||
],
|
||||
j.literal("@refinedev/devtools"),
|
||||
);
|
||||
|
||||
source.get().node.program.body.unshift(devtoolsImport);
|
||||
};
|
||||
|
||||
const wrapWithDevtoolsProvider = (
|
||||
j: JSCodeshift,
|
||||
refineEelement: ASTPath<JSXElement>,
|
||||
) => {
|
||||
const panel = j.jsxElement(
|
||||
j.jsxOpeningElement(j.jsxIdentifier("DevtoolsPanel")),
|
||||
);
|
||||
panel.openingElement.selfClosing = true;
|
||||
|
||||
const provider = j.jsxElement(
|
||||
j.jsxOpeningElement(j.jsxIdentifier("DevtoolsProvider")),
|
||||
j.jsxClosingElement(j.jsxIdentifier("DevtoolsProvider")),
|
||||
// Pass in the refineEelement component as children
|
||||
[refineEelement.value, panel],
|
||||
);
|
||||
|
||||
j(refineEelement).replaceWith(provider);
|
||||
return { panel, provider };
|
||||
};
|
||||
@@ -1,121 +0,0 @@
|
||||
import {
|
||||
API,
|
||||
FileInfo,
|
||||
JSXAttribute,
|
||||
JSXExpressionContainer,
|
||||
ArrayExpression,
|
||||
} from "jscodeshift";
|
||||
|
||||
export const parser = "tsx";
|
||||
|
||||
export default function transformer(file: FileInfo, api: API, options: any) {
|
||||
const j = api.jscodeshift;
|
||||
const source = j(file.source);
|
||||
|
||||
const rootElement = source.find(j.JSXElement, {
|
||||
openingElement: {
|
||||
name: {
|
||||
name: "Refine",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (rootElement.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// prepare actions
|
||||
const actions = options.__actions.split(",");
|
||||
|
||||
const getPath = (resourceName: string, action: string) => {
|
||||
if (action === "list") {
|
||||
return `/${resourceName}`;
|
||||
}
|
||||
|
||||
if (action === "create") {
|
||||
return `/${resourceName}/create`;
|
||||
}
|
||||
|
||||
if (action === "edit") {
|
||||
return `/${resourceName}/edit/:id`;
|
||||
}
|
||||
|
||||
if (action === "show") {
|
||||
return `/${resourceName}/show/:id`;
|
||||
}
|
||||
|
||||
return `/${resourceName}`;
|
||||
};
|
||||
|
||||
const resourceProperty = [
|
||||
j.property(
|
||||
"init",
|
||||
j.identifier("name"),
|
||||
j.stringLiteral(options.__resourceName),
|
||||
),
|
||||
];
|
||||
|
||||
actions.map((item: string) => {
|
||||
resourceProperty.push(
|
||||
j.property(
|
||||
"init",
|
||||
j.identifier(item),
|
||||
j.stringLiteral(getPath(options.__resourceName, item)),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
rootElement.replaceWith((path) => {
|
||||
const attributes = path.node.openingElement.attributes;
|
||||
if (!attributes) {
|
||||
return path.node;
|
||||
}
|
||||
|
||||
const resourcePropIndex = attributes.findIndex(
|
||||
(attr) =>
|
||||
attr.type === "JSXAttribute" && attr.name.name === "resources",
|
||||
);
|
||||
|
||||
const resourceObjectExpression = j.objectExpression(resourceProperty);
|
||||
|
||||
// if no resources prop, add it
|
||||
if (resourcePropIndex === -1) {
|
||||
attributes.push(
|
||||
j.jsxAttribute(
|
||||
j.jsxIdentifier("resources"),
|
||||
j.jsxExpressionContainer(
|
||||
j.arrayExpression([resourceObjectExpression]),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return path.node;
|
||||
}
|
||||
|
||||
const resourceValue = (attributes[resourcePropIndex] as JSXAttribute)
|
||||
.value as JSXExpressionContainer;
|
||||
|
||||
// resources={RESOURCE_CONSTANT} => resources={[...RESOURCE_CONSTANT, {name: "post", list: List}]}
|
||||
if (resourceValue.expression.type === "Identifier") {
|
||||
attributes[resourcePropIndex] = j.jsxAttribute(
|
||||
j.jsxIdentifier("resources"),
|
||||
j.jsxExpressionContainer(
|
||||
j.arrayExpression([
|
||||
j.spreadElement(resourceValue.expression),
|
||||
resourceObjectExpression,
|
||||
]),
|
||||
),
|
||||
);
|
||||
|
||||
return path.node;
|
||||
}
|
||||
|
||||
// resources={[...resources]} => resources={[...resources, {name: "post", list: List}]}
|
||||
const resourceArray = resourceValue.expression as ArrayExpression;
|
||||
resourceArray.elements.push(resourceObjectExpression);
|
||||
|
||||
return path.node;
|
||||
});
|
||||
|
||||
return source.toSource();
|
||||
}
|
||||
Reference in New Issue
Block a user