mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-01-26 10:59:53 +00:00
Extract shared utilities into workspace package (#5843)
## Summary Extracts shared formatting and network utilities into dedicated workspace package. ## Changes - **What**: Created `@comfyorg/shared-frontend-utils` package containing formatUtil and networkUtil - **Breaking**: None - utilities remain accessible via path aliases in `tsconfig` Split `createAnnotatedPath` and `electronMirrorCheck` out and left in frontend, due to their tightly-coupled nature. See [discussion on this PR](https://github.com/Comfy-Org/ComfyUI_frontend/pull/5843#issuecomment-3344724727).
This commit is contained in:
22
packages/shared-frontend-utils/package.json
Normal file
22
packages/shared-frontend-utils/package.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "@comfyorg/shared-frontend-utils",
|
||||
"private": true,
|
||||
"version": "1.0.0",
|
||||
"description": "Shared frontend utils for ComfyUI Frontend",
|
||||
"scripts": {
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"keywords": [],
|
||||
"packageManager": "pnpm@10.17.1",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
"./formatUtil": "./src/formatUtil.ts",
|
||||
"./networkUtil": "./src/networkUtil.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^1.11.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.9.2"
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import type { ResultItem } from '@/schemas/apiSchema'
|
||||
import type { operations } from '@/types/comfyRegistryTypes'
|
||||
import type { operations } from '@comfyorg/registry-types'
|
||||
|
||||
export function formatCamelCase(str: string): string {
|
||||
// Check if the string is camel case
|
||||
@@ -194,27 +193,6 @@ export function isValidUrl(url: string): boolean {
|
||||
return false
|
||||
}
|
||||
}
|
||||
const hasAnnotation = (filepath: string): boolean =>
|
||||
/\[(input|output|temp)\]/i.test(filepath)
|
||||
|
||||
const createAnnotation = (filepath: string, rootFolder = 'input'): string =>
|
||||
!hasAnnotation(filepath) && rootFolder !== 'input' ? ` [${rootFolder}]` : ''
|
||||
|
||||
const createPath = (filename: string, subfolder = ''): string =>
|
||||
subfolder ? `${subfolder}/${filename}` : filename
|
||||
|
||||
/** Creates annotated filepath in format used by folder_paths.py */
|
||||
export function createAnnotatedPath(
|
||||
item: string | ResultItem,
|
||||
options: { rootFolder?: string; subfolder?: string } = {}
|
||||
): string {
|
||||
const { rootFolder = 'input', subfolder } = options
|
||||
if (typeof item === 'string')
|
||||
return `${createPath(item, subfolder)}${createAnnotation(item, rootFolder)}`
|
||||
return `${createPath(item.filename ?? '', item.subfolder)}${
|
||||
item.type ? createAnnotation(item.type, rootFolder) : ''
|
||||
}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a filepath into its filename and subfolder components.
|
||||
@@ -1,8 +1,5 @@
|
||||
import axios from 'axios'
|
||||
|
||||
import { electronAPI } from './envUtil'
|
||||
import { isValidUrl } from './formatUtil'
|
||||
|
||||
const VALID_STATUS_CODES = [200, 201, 301, 302, 307, 308]
|
||||
export const checkUrlReachable = async (url: string): Promise<boolean> => {
|
||||
try {
|
||||
@@ -14,17 +11,6 @@ export const checkUrlReachable = async (url: string): Promise<boolean> => {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a mirror is reachable from the electron App.
|
||||
* @param mirror - The mirror to check.
|
||||
* @returns True if the mirror is reachable, false otherwise.
|
||||
*/
|
||||
export const checkMirrorReachable = async (mirror: string) => {
|
||||
return (
|
||||
isValidUrl(mirror) && (await electronAPI().NetWork.canAccessUrl(mirror))
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user is likely in mainland China by:
|
||||
* 1. Checking navigator.language
|
||||
8
packages/shared-frontend-utils/tsconfig.json
Normal file
8
packages/shared-frontend-utils/tsconfig.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "src",
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": ["src/**/*"]
|
||||
}
|
||||
10
pnpm-lock.yaml
generated
10
pnpm-lock.yaml
generated
@@ -373,6 +373,16 @@ importers:
|
||||
|
||||
packages/registry-types: {}
|
||||
|
||||
packages/shared-frontend-utils:
|
||||
dependencies:
|
||||
axios:
|
||||
specifier: ^1.11.0
|
||||
version: 1.11.0
|
||||
devDependencies:
|
||||
typescript:
|
||||
specifier: ^5.9.2
|
||||
version: 5.9.2
|
||||
|
||||
packages/tailwind-utils:
|
||||
dependencies:
|
||||
clsx:
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import * as fs from 'fs'
|
||||
|
||||
import { comfyPageFixture as test } from '../browser_tests/fixtures/ComfyPage'
|
||||
import {
|
||||
formatCamelCase,
|
||||
normalizeI18nKey
|
||||
} from '../packages/shared-frontend-utils/src/formatUtil'
|
||||
import { CORE_MENU_COMMANDS } from '../src/constants/coreMenuCommands'
|
||||
import { DESKTOP_DIALOGS } from '../src/constants/desktopDialogs'
|
||||
import { SERVER_CONFIG_ITEMS } from '../src/constants/serverConfig'
|
||||
import type { FormItem, SettingParams } from '../src/platform/settings/types'
|
||||
import type { ComfyCommandImpl } from '../src/stores/commandStore'
|
||||
import { formatCamelCase, normalizeI18nKey } from '../src/utils/formatUtil'
|
||||
|
||||
const localePath = './src/locales/en/main.json'
|
||||
const commandsPath = './src/locales/en/commands.json'
|
||||
|
||||
@@ -3,8 +3,8 @@ import * as fs from 'fs'
|
||||
import type { ComfyNodeDef } from '@/schemas/nodeDefSchema'
|
||||
|
||||
import { comfyPageFixture as test } from '../browser_tests/fixtures/ComfyPage'
|
||||
import { normalizeI18nKey } from '../packages/shared-frontend-utils/src/formatUtil'
|
||||
import type { ComfyNodeDefImpl } from '../src/stores/nodeDefStore'
|
||||
import { normalizeI18nKey } from '../src/utils/formatUtil'
|
||||
|
||||
const localePath = './src/locales/en/main.json'
|
||||
const nodeDefsPath = './src/locales/en/nodeDefs.json'
|
||||
|
||||
@@ -60,8 +60,8 @@ import { computed, onMounted, ref, watch } from 'vue'
|
||||
import UrlInput from '@/components/common/UrlInput.vue'
|
||||
import type { UVMirror } from '@/constants/uvMirrors'
|
||||
import { st } from '@/i18n'
|
||||
import { checkMirrorReachable } from '@/utils/electronMirrorCheck'
|
||||
import { normalizeI18nKey } from '@/utils/formatUtil'
|
||||
import { checkMirrorReachable } from '@/utils/networkUtil'
|
||||
import { ValidationState } from '@/utils/validationUtil'
|
||||
|
||||
const FILE_URL_SCHEME = 'file://'
|
||||
|
||||
@@ -6,8 +6,8 @@ import { useToastStore } from '@/platform/updates/common/toastStore'
|
||||
import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
|
||||
import { app } from '@/scripts/app'
|
||||
import { useDialogService } from '@/services/dialogService'
|
||||
import { checkMirrorReachable } from '@/utils/electronMirrorCheck'
|
||||
import { electronAPI as getElectronAPI, isElectron } from '@/utils/envUtil'
|
||||
import { checkMirrorReachable } from '@/utils/networkUtil'
|
||||
|
||||
// Desktop documentation URLs
|
||||
const DESKTOP_DOCS = {
|
||||
|
||||
@@ -9,7 +9,7 @@ import type { InputSpec } from '@/schemas/nodeDefSchema'
|
||||
import type { ComfyWidgetConstructor } from '@/scripts/widgets'
|
||||
import { useNodeOutputStore } from '@/stores/imagePreviewStore'
|
||||
import { isImageUploadInput } from '@/types/nodeDefAugmentation'
|
||||
import { createAnnotatedPath } from '@/utils/formatUtil'
|
||||
import { createAnnotatedPath } from '@/utils/createAnnotatedPath'
|
||||
import { addToComboValues } from '@/utils/litegraphUtil'
|
||||
|
||||
const ACCEPTED_IMAGE_TYPES = 'image/png,image/jpeg,image/webp'
|
||||
|
||||
23
src/utils/createAnnotatedPath.ts
Normal file
23
src/utils/createAnnotatedPath.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import type { ResultItem } from '@/schemas/apiSchema'
|
||||
|
||||
const hasAnnotation = (filepath: string): boolean =>
|
||||
/\[(input|output|temp)\]/i.test(filepath)
|
||||
|
||||
const createAnnotation = (filepath: string, rootFolder = 'input'): string =>
|
||||
!hasAnnotation(filepath) && rootFolder !== 'input' ? ` [${rootFolder}]` : ''
|
||||
|
||||
const createPath = (filename: string, subfolder = ''): string =>
|
||||
subfolder ? `${subfolder}/${filename}` : filename
|
||||
|
||||
/** Creates annotated filepath in format used by folder_paths.py */
|
||||
export function createAnnotatedPath(
|
||||
item: string | ResultItem,
|
||||
options: { rootFolder?: string; subfolder?: string } = {}
|
||||
): string {
|
||||
const { rootFolder = 'input', subfolder } = options
|
||||
if (typeof item === 'string')
|
||||
return `${createPath(item, subfolder)}${createAnnotation(item, rootFolder)}`
|
||||
return `${createPath(item.filename ?? '', item.subfolder)}${
|
||||
item.type ? createAnnotation(item.type, rootFolder) : ''
|
||||
}`
|
||||
}
|
||||
13
src/utils/electronMirrorCheck.ts
Normal file
13
src/utils/electronMirrorCheck.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { electronAPI } from '@/utils/envUtil'
|
||||
import { isValidUrl } from '@/utils/formatUtil'
|
||||
|
||||
/**
|
||||
* Check if a mirror is reachable from the electron App.
|
||||
* @param mirror - The mirror to check.
|
||||
* @returns True if the mirror is reachable, false otherwise.
|
||||
*/
|
||||
export const checkMirrorReachable = async (mirror: string) => {
|
||||
return (
|
||||
isValidUrl(mirror) && (await electronAPI().NetWork.canAccessUrl(mirror))
|
||||
)
|
||||
}
|
||||
@@ -22,7 +22,13 @@
|
||||
"verbatimModuleSyntax": true,
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@/*": ["src/*"]
|
||||
"@/*": ["src/*"],
|
||||
"@/utils/formatUtil": [
|
||||
"packages/shared-frontend-utils/src/formatUtil.ts"
|
||||
],
|
||||
"@/utils/networkUtil": [
|
||||
"packages/shared-frontend-utils/src/networkUtil.ts"
|
||||
]
|
||||
},
|
||||
"typeRoots": ["src/types", "node_modules/@types"],
|
||||
"outDir": "./dist",
|
||||
|
||||
@@ -190,6 +190,9 @@ export default defineConfig({
|
||||
|
||||
resolve: {
|
||||
alias: {
|
||||
'@/utils/formatUtil': '/packages/shared-frontend-utils/src/formatUtil.ts',
|
||||
'@/utils/networkUtil':
|
||||
'/packages/shared-frontend-utils/src/networkUtil.ts',
|
||||
'@': '/src'
|
||||
}
|
||||
},
|
||||
|
||||
@@ -37,6 +37,9 @@ export default defineConfig({
|
||||
},
|
||||
resolve: {
|
||||
alias: {
|
||||
'@/utils/formatUtil': '/packages/shared-frontend-utils/src/formatUtil.ts',
|
||||
'@/utils/networkUtil':
|
||||
'/packages/shared-frontend-utils/src/networkUtil.ts',
|
||||
'@': '/src'
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user