Skip to content

Commit

Permalink
Add upgrade script for theme imports
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesricky committed Feb 24, 2025
1 parent a0c85c0 commit 21bff9b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/util/package-json.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ export class PackageJson {
delete this.json.devDependencies?.[name];
}

getDependencyVersion(name: string): string | undefined {
return this.json.dependencies?.[name] || this.json.devDependencies?.[name];
}

hasDependency(name: string): boolean {
return !!(this.json.dependencies?.[name] || this.json.devDependencies?.[name]);
}

save() {
writeFileSync(this.path, JSON.stringify(this.json, null, 4));
}
Expand Down
75 changes: 75 additions & 0 deletions src/v8/update-theme-imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { existsSync, readFileSync, unlinkSync, writeFileSync } from "fs";
import { glob } from "glob";
import { Project } from "ts-morph";

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

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

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

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

const themeImport = sourceFile.getImportDeclaration((declaration) => declaration.getModuleSpecifierValue() === "@comet/admin-theme");

if (!themeImport) continue;

const namedImports = themeImport.getNamedImports();
const importStructures = namedImports.map((namedImport) => ({
name: namedImport.getName(),
alias: namedImport.getAliasNode()?.getText(),
}));

themeImport.remove();

const adminImport = sourceFile.getImportDeclaration((declaration) => declaration.getModuleSpecifierValue() === "@comet/admin");

if (adminImport) {
adminImport.addNamedImports(
importStructures.map((importStructure) => ({
name: importStructure.name,
alias: importStructure.alias,
})),
);
} else {
sourceFile.addImportDeclaration({
namedImports: importStructures.map((importStructure) => ({
name: importStructure.name,
alias: importStructure.alias,
})),
moduleSpecifier: "@comet/admin",
});
}

await sourceFile.save();
}

if (existsSync("admin/package.json")) {
const packageJson = new PackageJson("admin/package.json");
const themeVersion = packageJson.getDependencyVersion("@comet/admin-theme");
packageJson.removeDependency("@comet/admin-theme");

if (!packageJson.hasDependency("@comet/admin")) {
packageJson.addDependency("@comet/admin", themeVersion || "^8.0.0");
}

packageJson.save();
}

const vendorsPath = "admin/src/vendors.d.ts";
if (existsSync(vendorsPath)) {
const content = readFileSync(vendorsPath, "utf-8");
const updatedContent = content.replace('/// <reference types="@comet/admin-theme" />\n', "");

if (updatedContent.trim() === "") {
unlinkSync(vendorsPath);
} else {
writeFileSync(vendorsPath, updatedContent);
}
}
}

0 comments on commit 21bff9b

Please sign in to comment.