Skip to content

Commit

Permalink
Merge branch 'main' into mikro-orm-scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyomair committed Jan 8, 2025
2 parents c2289c3 + 92f0a75 commit f0558d5
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 32 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@comet/upgrade",
"version": "1.30.0",
"version": "1.33.0",
"description": "Upgrade scripts for Comet DXP",
"homepage": "https://github.com/vivid-planet/comet-upgrade#readme",
"bugs": {
Expand Down
49 changes: 49 additions & 0 deletions src/util/package-json.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { existsSync, readFileSync, writeFileSync } from "fs";

type Json = {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
};

export class PackageJson {
private path: string;
private json: Json;

constructor(path: string) {
if (!existsSync(path)) {
throw new Error("File does not exist");
}

this.path = path;
this.json = JSON.parse(readFileSync(path, "utf-8"));
}

addDependency(name: string, version: string, dev = false) {
if (dev) {
this.json.devDependencies ??= {};
this.json.devDependencies[name] = version;
} else {
this.json.dependencies ??= {};
this.json.dependencies[name] = version;
}
}

updateDependency(name: string, version: string) {
if (this.json.dependencies?.[name]) {
this.json.dependencies[name] = version;
}

if (this.json.devDependencies?.[name]) {
this.json.devDependencies[name] = version;
}
}

removeDependency(name: string) {
delete this.json.dependencies?.[name];
delete this.json.devDependencies?.[name];
}

save() {
writeFileSync(this.path, JSON.stringify(this.json, null, 4));
}
}
37 changes: 37 additions & 0 deletions src/v8/update-graphql-format-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Project, SyntaxKind } from "ts-morph";

/**
* From
*
* if (error instanceof ValidationError) {
* return new ValidationError("Invalid request.");
* }
*
* to
*
* if (error.extensions?.code === "GRAPHQL_VALIDATION_FAILED") {
* return new ValidationError("Invalid request.");
* }
*/
export default async function updateGraphQLFormatError() {
const project = new Project({ tsConfigFilePath: "./api/tsconfig.json" });

const sourceFile = project.getSourceFile("api/src/app.module.ts");

if (!sourceFile) {
throw new Error("app.module.ts not found");
}

// Change the import
sourceFile.getImportDeclaration((importDeclaration) => importDeclaration.getModuleSpecifierValue() === "apollo-server-express")?.remove();
sourceFile.addImportDeclaration({ namedImports: ["ValidationError"], moduleSpecifier: "@nestjs/apollo" });

// Update the if statement
sourceFile.getDescendantsOfKind(SyntaxKind.BinaryExpression).forEach((node) => {
if (node.getText() === "error instanceof ValidationError") {
node.replaceWithText(`error.extensions?.code === "GRAPHQL_VALIDATION_FAILED"`);
}
});

await sourceFile.save();
}
50 changes: 50 additions & 0 deletions src/v8/update-import-of-tooltip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { glob } from "glob";
import { Project } from "ts-morph";

export default async function updateImportOfTooltip() {
const project = new Project({ tsConfigFilePath: "./admin/tsconfig.json" });
const files: string[] = glob.sync(["admin/src/**/*.tsx"]);

for (const filePath of files) {
const sourceFile = project.getSourceFile(filePath);

if (!sourceFile) {
throw new Error(`Can't get source file for ${filePath}`);
}

const adminImport = sourceFile.getImportDeclaration((declaration) => declaration.getModuleSpecifierValue().includes("@comet/admin"));
const adminImports = adminImport?.getNamedImports().map((namedImport) => namedImport.getText());

if (adminImports) {
if (adminImports.includes("Tooltip")) {
continue;
}
}

const muiImport = sourceFile.getImportDeclaration((declaration) => declaration.getModuleSpecifierValue() === "@mui/material");

if (!muiImport) continue;

const namedImports = muiImport.getNamedImports();
const tooltipImport = namedImports.find((namedImport) => namedImport.getText() === "Tooltip");

if (tooltipImport) {
tooltipImport.remove();
}

if (muiImport.getNamedImports().length === 0) {
muiImport.remove();
}

if (adminImport) {
adminImport.addNamedImports(["Tooltip"]);
} else {
sourceFile.addImportDeclaration({
namedImports: ["Tooltip"],
moduleSpecifier: "@comet/admin",
});
}

await sourceFile.save();
}
}
21 changes: 21 additions & 0 deletions src/v8/update-mikro-orm-dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { existsSync } from "fs";

import { PackageJson } from "../util/package-json.util";

export const stage = "before-install";

export default async function updateNestDependencies() {
if (!existsSync("api/package.json")) {
return;
}

const packageJson = new PackageJson("api/package.json");

packageJson.updateDependency("@mikro-orm/cli", "^6.0.0");
packageJson.updateDependency("@mikro-orm/core", "^6.0.0");
packageJson.updateDependency("@mikro-orm/migrations", "^6.0.0");
packageJson.updateDependency("@mikro-orm/nestjs", "^6.0.2");
packageJson.updateDependency("@mikro-orm/postgresql", "^6.0.0");

packageJson.save();
}
49 changes: 20 additions & 29 deletions src/v8/update-nest-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,36 @@
import { existsSync } from "fs";
import { readFile, writeFile } from "fs/promises";

export const stage = "before-install";
import { PackageJson } from "../util/package-json.util";

function updateDependencyIfExists(dependencies: Record<string, string>, name: string, version: string) {
if (dependencies[name]) {
dependencies[name] = version;
}
}
export const stage = "before-install";

export default async function updateNestDependencies() {
if (!existsSync("api/package.json")) {
return;
}

const packageJson = JSON.parse(await readFile("api/package.json", "utf-8"));
const packageJson = new PackageJson("api/package.json");

if (packageJson.dependencies) {
packageJson.dependencies["@apollo/server"] = "^4.0.0";
delete packageJson.dependencies["apollo-server-core"];
delete packageJson.dependencies["apollo-server-express"];
packageJson.addDependency("@apollo/server", "^4.0.0");
packageJson.removeDependency("apollo-server-core");
packageJson.removeDependency("apollo-server-express");

updateDependencyIfExists(packageJson.dependencies, "@nestjs/apollo", "^12.0.0");
updateDependencyIfExists(packageJson.dependencies, "@nestjs/common", "^10.0.0");
updateDependencyIfExists(packageJson.dependencies, "@nestjs/core", "^10.0.0");
updateDependencyIfExists(packageJson.dependencies, "@nestjs/graphql", "^12.0.0");
// TODO remove when https://github.com/vivid-planet/comet/pull/2809 has been merged
updateDependencyIfExists(packageJson.dependencies, "@nestjs/passport", "^10.0.0");
updateDependencyIfExists(packageJson.dependencies, "@nestjs/platform-express", "^10.0.0");
packageJson.updateDependency("@nestjs/apollo", "^12.0.0");
packageJson.updateDependency("@nestjs/common", "^10.0.0");
packageJson.updateDependency("@nestjs/core", "^10.0.0");
packageJson.updateDependency("@nestjs/graphql", "^12.0.0");
// TODO remove when https://github.com/vivid-planet/comet/pull/2809 has been merged
packageJson.updateDependency("@nestjs/passport", "^10.0.0");
packageJson.updateDependency("@nestjs/platform-express", "^10.0.0");

updateDependencyIfExists(packageJson.dependencies, "graphql", "^16.6.0");
packageJson.updateDependency("graphql", "^16.6.0");

updateDependencyIfExists(packageJson.dependencies, "nestjs-console", "^9.0.0");
updateDependencyIfExists(packageJson.dependencies, "@golevelup/nestjs-discovery", "^4.0.0");
}
packageJson.updateDependency("nestjs-console", "^9.0.0");
packageJson.updateDependency("@golevelup/nestjs-discovery", "^4.0.0");

if (packageJson.devDependencies) {
updateDependencyIfExists(packageJson.devDependencies, "@nestjs/cli", "^10.0.0");
updateDependencyIfExists(packageJson.devDependencies, "@nestjs/schematics", "^10.0.0");
updateDependencyIfExists(packageJson.devDependencies, "@nestjs/testing", "^10.0.0");
}
packageJson.updateDependency("@nestjs/cli", "^10.0.0");
packageJson.updateDependency("@nestjs/schematics", "^10.0.0");
packageJson.updateDependency("@nestjs/testing", "^10.0.0");

await writeFile("api/package.json", JSON.stringify(packageJson, null, 4));
packageJson.save();
}

0 comments on commit f0558d5

Please sign in to comment.