mirror of
https://github.com/stefanpejcic/openpanel
synced 2025-06-26 18:28:26 +00:00
packages
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import { readJSON } from "fs-extra";
|
||||
import path from "path";
|
||||
|
||||
export const getProjectIdFromPackageJson = async (
|
||||
projectPath = process.cwd(),
|
||||
) => {
|
||||
try {
|
||||
const packageJson = await readJSON(path.join(projectPath, "package.json"), {
|
||||
encoding: "utf-8",
|
||||
});
|
||||
|
||||
const projectId = packageJson?.refine?.projectId as string;
|
||||
|
||||
if (projectId) {
|
||||
return projectId;
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
import execa from "execa";
|
||||
|
||||
export const setProjectIdToPackageJson = async (
|
||||
projectId: string,
|
||||
projectPath = process.cwd(),
|
||||
) => {
|
||||
try {
|
||||
execa.sync("npm", ["pkg", "set", `refine.projectId=${projectId}`], {
|
||||
cwd: projectPath,
|
||||
});
|
||||
return true;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
import execa from "execa";
|
||||
import path from "path";
|
||||
|
||||
export const setProjectIdToRefineComponent = async (
|
||||
projectId: string,
|
||||
projectPath = process.cwd(),
|
||||
) => {
|
||||
try {
|
||||
const jscodeshiftExecutable = require.resolve(".bin/jscodeshift");
|
||||
|
||||
const execution = execa.sync(
|
||||
jscodeshiftExecutable,
|
||||
[
|
||||
"./",
|
||||
"--extensions=ts,tsx,js,jsx",
|
||||
"--parser=tsx",
|
||||
`--transform=${path.resolve(
|
||||
path.join(__dirname, "..", "src", "project-id", "transform.ts"),
|
||||
)}`,
|
||||
"--ignore-pattern=**/.cache/**",
|
||||
"--ignore-pattern=**/node_modules/**",
|
||||
"--ignore-pattern=**/build/**",
|
||||
"--ignore-pattern=**/dist/**",
|
||||
"--ignore-pattern=**/.next/**",
|
||||
`--__projectId=${projectId}`,
|
||||
],
|
||||
{
|
||||
cwd: projectPath,
|
||||
timeout: 1000 * 10,
|
||||
},
|
||||
);
|
||||
|
||||
if (execution.stderr) {
|
||||
console.error(execution.stderr);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
return;
|
||||
};
|
||||
88
packages/devtools-server/src/project-id/transform.ts
Normal file
88
packages/devtools-server/src/project-id/transform.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import type { namedTypes } from "ast-types";
|
||||
import type {
|
||||
API,
|
||||
ASTPath,
|
||||
Collection,
|
||||
FileInfo,
|
||||
JSCodeshift,
|
||||
Options,
|
||||
} from "jscodeshift";
|
||||
|
||||
export const parser = "tsx";
|
||||
|
||||
const transformRefineOptions = (
|
||||
j: JSCodeshift,
|
||||
root: Collection<any>,
|
||||
projectId: string,
|
||||
) => {
|
||||
const refineElements: Array<ASTPath<namedTypes.JSXElement>> = [];
|
||||
|
||||
root.findJSXElements("Refine").forEach((path) => {
|
||||
refineElements.push(path);
|
||||
});
|
||||
|
||||
for (const path of refineElements) {
|
||||
const props = path.node.openingElement.attributes;
|
||||
|
||||
const optionsProp: any = props?.find(
|
||||
(attribute) =>
|
||||
attribute.type === "JSXAttribute" && attribute.name.name === "options",
|
||||
);
|
||||
if (!optionsProp) {
|
||||
path.node.openingElement.attributes?.push(
|
||||
j.jsxAttribute(
|
||||
j.jsxIdentifier("options"),
|
||||
j.jsxExpressionContainer(
|
||||
j.objectExpression([
|
||||
j.objectProperty(
|
||||
j.identifier("projectId"),
|
||||
j.stringLiteral(projectId),
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// for options={optionsProp}
|
||||
if (!optionsProp?.value.expression.properties) {
|
||||
break;
|
||||
}
|
||||
|
||||
// for options has already projectId
|
||||
const hasProjectId = optionsProp?.value.expression.properties.find(
|
||||
(p: any) => {
|
||||
if (p.type === "ObjectProperty") {
|
||||
return p.key.name === "projectId";
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
);
|
||||
|
||||
if (hasProjectId) break;
|
||||
|
||||
optionsProp?.value.expression.properties.push(
|
||||
j.objectProperty(j.identifier("projectId"), j.stringLiteral(projectId)),
|
||||
);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return root;
|
||||
};
|
||||
|
||||
export default function transformer(
|
||||
file: FileInfo,
|
||||
api: API,
|
||||
options: Options,
|
||||
) {
|
||||
const j = api.jscodeshift;
|
||||
const source = j(file.source);
|
||||
|
||||
transformRefineOptions(j, source, options.__projectId);
|
||||
|
||||
return source.toSource();
|
||||
}
|
||||
18
packages/devtools-server/src/project-id/update-project-id.ts
Normal file
18
packages/devtools-server/src/project-id/update-project-id.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { setProjectIdToPackageJson } from "./set-project-id-to-package-json";
|
||||
import { setProjectIdToRefineComponent } from "./set-project-id-to-refine-component";
|
||||
|
||||
export const updateProjectId = async (
|
||||
projectId: string,
|
||||
projectPath = process.cwd(),
|
||||
) => {
|
||||
try {
|
||||
await Promise.all([
|
||||
setProjectIdToPackageJson(projectId, projectPath),
|
||||
setProjectIdToRefineComponent(projectId, projectPath),
|
||||
]);
|
||||
|
||||
return true;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user