mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-04-21 23:09:39 +00:00
refactor: Use async fs/promises for better performance
- Replace synchronous fs operations with async fs.promises - Use Promise.all for parallel file processing - Improves performance when processing multiple files
This commit is contained in:
@@ -2,10 +2,10 @@
|
||||
* Setup for i18n collection tests
|
||||
* Handles preprocessing of litegraph files that contain TypeScript 'declare' keywords
|
||||
*/
|
||||
import * as fs from 'fs'
|
||||
import { promises as fs } from 'fs'
|
||||
import * as path from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
import { globSync } from 'glob'
|
||||
import { glob } from 'glob'
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||||
const rootDir = path.resolve(__dirname, '..')
|
||||
@@ -16,30 +16,34 @@ const backupMap = new Map<string, string>()
|
||||
/**
|
||||
* Find all TypeScript files in litegraph that contain 'declare' keywords
|
||||
*/
|
||||
function findFilesWithDeclare(): string[] {
|
||||
async function findFilesWithDeclare(): Promise<string[]> {
|
||||
// Search for all .ts files in litegraph src directory
|
||||
const pattern = path.join(litegraphSrcDir, '**/*.ts')
|
||||
const files = globSync(pattern, {
|
||||
const files = await glob(pattern, {
|
||||
ignore: ['**/*.test.ts', '**/*.spec.ts', '**/node_modules/**']
|
||||
})
|
||||
|
||||
// Filter to only files that actually contain 'declare' keyword
|
||||
return files.filter(filePath => {
|
||||
try {
|
||||
const content = fs.readFileSync(filePath, 'utf-8')
|
||||
// Check for class property declarations with 'declare' keyword
|
||||
return /^\s*declare\s+/m.test(content)
|
||||
} catch (error) {
|
||||
console.warn(` ⚠ Could not read ${filePath}: ${error.message}`)
|
||||
return false
|
||||
}
|
||||
})
|
||||
const filesWithDeclare = await Promise.all(
|
||||
files.map(async (filePath) => {
|
||||
try {
|
||||
const content = await fs.readFile(filePath, 'utf-8')
|
||||
// Check for class property declarations with 'declare' keyword
|
||||
return /^\s*declare\s+/m.test(content) ? filePath : null
|
||||
} catch (error) {
|
||||
console.warn(` ⚠ Could not read ${filePath}: ${error.message}`)
|
||||
return null
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return filesWithDeclare.filter((file): file is string => file !== null)
|
||||
}
|
||||
|
||||
export async function preprocessLitegraph() {
|
||||
console.log('Preprocessing litegraph files for i18n collection...')
|
||||
|
||||
const filesToProcess = findFilesWithDeclare()
|
||||
const filesToProcess = await findFilesWithDeclare()
|
||||
|
||||
if (filesToProcess.length === 0) {
|
||||
console.log(' ℹ No files with declare keywords found')
|
||||
@@ -48,22 +52,24 @@ export async function preprocessLitegraph() {
|
||||
|
||||
console.log(` Found ${filesToProcess.length} files with declare keywords`)
|
||||
|
||||
for (const filePath of filesToProcess) {
|
||||
const originalContent = fs.readFileSync(filePath, 'utf-8')
|
||||
|
||||
// Store original content in memory
|
||||
backupMap.set(filePath, originalContent)
|
||||
|
||||
// Remove 'declare' keyword from class properties
|
||||
const modifiedContent = originalContent.replace(
|
||||
/^(\s*)declare\s+/gm,
|
||||
'$1// @ts-ignore - removed declare for Playwright\n$1'
|
||||
)
|
||||
|
||||
// Write modified content
|
||||
fs.writeFileSync(filePath, modifiedContent)
|
||||
console.log(` ✓ Processed ${path.relative(litegraphSrcDir, filePath)}`)
|
||||
}
|
||||
await Promise.all(
|
||||
filesToProcess.map(async (filePath) => {
|
||||
const originalContent = await fs.readFile(filePath, 'utf-8')
|
||||
|
||||
// Store original content in memory
|
||||
backupMap.set(filePath, originalContent)
|
||||
|
||||
// Remove 'declare' keyword from class properties
|
||||
const modifiedContent = originalContent.replace(
|
||||
/^(\s*)declare\s+/gm,
|
||||
'$1// @ts-ignore - removed declare for Playwright\n$1'
|
||||
)
|
||||
|
||||
// Write modified content
|
||||
await fs.writeFile(filePath, modifiedContent)
|
||||
console.log(` ✓ Processed ${path.relative(litegraphSrcDir, filePath)}`)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export async function restoreLitegraph() {
|
||||
@@ -73,10 +79,12 @@ export async function restoreLitegraph() {
|
||||
|
||||
console.log('Restoring original litegraph files...')
|
||||
|
||||
for (const [filePath, originalContent] of backupMap.entries()) {
|
||||
fs.writeFileSync(filePath, originalContent)
|
||||
console.log(` ✓ Restored ${path.relative(litegraphSrcDir, filePath)}`)
|
||||
}
|
||||
await Promise.all(
|
||||
Array.from(backupMap.entries()).map(async ([filePath, originalContent]) => {
|
||||
await fs.writeFile(filePath, originalContent)
|
||||
console.log(` ✓ Restored ${path.relative(litegraphSrcDir, filePath)}`)
|
||||
})
|
||||
)
|
||||
|
||||
backupMap.clear()
|
||||
}
|
||||
Reference in New Issue
Block a user