feat(update): More logs for update script #2968 (#3010)

Before:

![340fffdbf558fa7a1bc1036972e0679](https://github.com/user-attachments/assets/e20e1c2f-1e1c-4477-82ee-231163d53085)

After:
Case1:

![1adc44294f2001dc43cd411254dfc2d](https://github.com/user-attachments/assets/e7bebd32-1083-4bc1-b00b-346c9ba66d43)
Case2:

![8cd3445877d5311ff8d9b9f66adb30e](https://github.com/user-attachments/assets/596ad777-6f6d-4b62-ae0e-32b82ebefb20)

In Test Env:

![df5b20de6d1437e471c6dcfc1e55178](https://github.com/user-attachments/assets/1eb53cdc-4398-4e61-874d-4c72e037e57f)

Close #2968

---------

Co-authored-by: 李嘉图·M·路 <146103794+Ricardo-M-Zheng@users.noreply.github.com>
Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
zhizheng
2025-06-11 17:40:52 +02:00
committed by GitHub
co-authored by 李嘉图·M·路 Marvin Hagemeister
parent 1213aad90d
commit b2807b7720
+99 -2
View File
@@ -1,6 +1,7 @@
import * as path from "@std/path";
import * as JSONC from "@std/jsonc";
import * as tsmorph from "ts-morph";
import * as colors from "@std/fmt/colors";
export const SyntaxKind = tsmorph.ts.SyntaxKind;
@@ -16,6 +17,17 @@ export interface DenoJson {
imports?: Record<string, string>;
}
// add a progress bar function
function createProgressBar(current: number, total: number): string {
const percentage = Math.round((current / total) * 100);
const progressLength = 40;
const filled = Math.round((percentage / 100) * progressLength);
const empty = progressLength - filled;
const bar = "█".repeat(filled) + "░".repeat(empty);
return `[${bar}] ${percentage}% (${current}/${total})`;
}
async function format(filePath: string) {
const command = new Deno.Command(Deno.execPath(), {
args: ["fmt", filePath],
@@ -79,6 +91,13 @@ const compat = new Set([
]);
export async function updateProject(dir: string) {
// add initial log
// deno-lint-ignore no-console
console.log(colors.blue("🚀 Starting Fresh 1 to Fresh 2 migration..."));
// deno-lint-ignore no-console
console.log(colors.yellow("📝 Updating configuration files..."));
// Update config
await updateDenoJson(dir, (config) => {
if (config.imports !== null && typeof config.imports !== "object") {
@@ -127,23 +146,97 @@ export async function updateProject(dir: string) {
}
});
// add completion log
// deno-lint-ignore no-console
console.log(colors.green("✅ Configuration updated successfully"));
// deno-lint-ignore no-console
console.log(colors.cyan("🔍 Scanning for source files..."));
// Update routes folder
const project = new tsmorph.Project();
const sfs = project.addSourceFilesAtPaths(
path.join(dir, "**", "*.{js,jsx,ts,tsx}"),
);
// deno-lint-ignore no-console
console.log(colors.cyan(`📁 Found ${sfs.length} files to process`));
if (sfs.length === 0) {
// deno-lint-ignore no-console
console.log(colors.green("🎉 Migration completed successfully!"));
return;
}
// deno-lint-ignore no-console
console.log(colors.yellow("🔄 Processing files..."));
// Track progress with a single counter
let completedFiles = 0;
const modifiedFilesList: string[] = [];
// process files sequentially to show proper pro
await Promise.all(sfs.map(async (sourceFile) => {
try {
return await updateFile(sourceFile);
const wasModified = await updateFile(sourceFile);
if (wasModified) {
modifiedFilesList.push(sourceFile.getFilePath());
}
return wasModified;
} catch (err) {
// deno-lint-ignore no-console
console.error(`Could not process ${sourceFile.getFilePath()}`);
throw err;
} finally {
completedFiles++;
// update progress bar
const progress = createProgressBar(completedFiles, sfs.length);
Deno.stdout.writeSync(
new TextEncoder().encode(`\r${colors.blue(progress)} Complete!`),
);
}
}));
// add completion log
// deno-lint-ignore no-console
console.log("");
// add migration summary
// deno-lint-ignore no-console
console.log("\n" + colors.bold("📊 Migration Summary:"));
// deno-lint-ignore no-console
console.log(` Total files processed: ${sfs.length}`);
// deno-lint-ignore no-console
console.log(` Successfully modified: ${modifiedFilesList.length}`);
// deno-lint-ignore no-console
console.log(
` Unmodified (no changes needed): ${
sfs.length - modifiedFilesList.length
}`,
);
// Display modified files list
if (modifiedFilesList.length > 0) {
// deno-lint-ignore no-console
console.log("\n" + colors.bold("📝 Modified Files:"));
modifiedFilesList.forEach((filePath) => {
// show relative path
const relativePath = path.relative(dir, filePath);
// deno-lint-ignore no-console
console.log(colors.green(` ✓ ${relativePath}`));
});
}
// deno-lint-ignore no-console
console.log("\n" + colors.green("🎉 Migration completed successfully!"));
}
async function updateFile(sourceFile: tsmorph.SourceFile): Promise<void> {
async function updateFile(sourceFile: tsmorph.SourceFile): Promise<boolean> {
// keep original text for comparison
const originalText = sourceFile.getFullText();
const newImports: ImportState = {
core: new Set(),
runtime: new Set(),
@@ -325,6 +418,10 @@ async function updateFile(sourceFile: tsmorph.SourceFile): Promise<void> {
await sourceFile.save();
await format(sourceFile.getFilePath());
// Check if the file was actually modified
const finalText = sourceFile.getFullText();
return originalText !== finalText;
}
function removeEmptyImport(d: tsmorph.ImportDeclaration) {