Skip to content

Commit 7a162e4

Browse files
webfilteredchristian-byrne
authored andcommitted
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](#5843 (comment)).
1 parent c68de61 commit 7a162e4

File tree

15 files changed

+98
-43
lines changed

15 files changed

+98
-43
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "@comfyorg/shared-frontend-utils",
3+
"private": true,
4+
"version": "1.0.0",
5+
"description": "Shared frontend utils for ComfyUI Frontend",
6+
"scripts": {
7+
"typecheck": "tsc --noEmit"
8+
},
9+
"keywords": [],
10+
"packageManager": "[email protected]",
11+
"type": "module",
12+
"exports": {
13+
"./formatUtil": "./src/formatUtil.ts",
14+
"./networkUtil": "./src/networkUtil.ts"
15+
},
16+
"dependencies": {
17+
"axios": "^1.11.0"
18+
},
19+
"devDependencies": {
20+
"typescript": "^5.9.2"
21+
}
22+
}

src/utils/formatUtil.ts renamed to packages/shared-frontend-utils/src/formatUtil.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { ResultItem } from '@/schemas/apiSchema'
2-
import type { operations } from '@/types/comfyRegistryTypes'
1+
import type { operations } from '@comfyorg/registry-types'
32

43
export function formatCamelCase(str: string): string {
54
// Check if the string is camel case
@@ -194,27 +193,6 @@ export function isValidUrl(url: string): boolean {
194193
return false
195194
}
196195
}
197-
const hasAnnotation = (filepath: string): boolean =>
198-
/\[(input|output|temp)\]/i.test(filepath)
199-
200-
const createAnnotation = (filepath: string, rootFolder = 'input'): string =>
201-
!hasAnnotation(filepath) && rootFolder !== 'input' ? ` [${rootFolder}]` : ''
202-
203-
const createPath = (filename: string, subfolder = ''): string =>
204-
subfolder ? `${subfolder}/${filename}` : filename
205-
206-
/** Creates annotated filepath in format used by folder_paths.py */
207-
export function createAnnotatedPath(
208-
item: string | ResultItem,
209-
options: { rootFolder?: string; subfolder?: string } = {}
210-
): string {
211-
const { rootFolder = 'input', subfolder } = options
212-
if (typeof item === 'string')
213-
return `${createPath(item, subfolder)}${createAnnotation(item, rootFolder)}`
214-
return `${createPath(item.filename ?? '', item.subfolder)}${
215-
item.type ? createAnnotation(item.type, rootFolder) : ''
216-
}`
217-
}
218196

219197
/**
220198
* Parses a filepath into its filename and subfolder components.

src/utils/networkUtil.ts renamed to packages/shared-frontend-utils/src/networkUtil.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import axios from 'axios'
22

3-
import { electronAPI } from './envUtil'
4-
import { isValidUrl } from './formatUtil'
5-
63
const VALID_STATUS_CODES = [200, 201, 301, 302, 307, 308]
74
export const checkUrlReachable = async (url: string): Promise<boolean> => {
85
try {
@@ -14,17 +11,6 @@ export const checkUrlReachable = async (url: string): Promise<boolean> => {
1411
}
1512
}
1613

17-
/**
18-
* Check if a mirror is reachable from the electron App.
19-
* @param mirror - The mirror to check.
20-
* @returns True if the mirror is reachable, false otherwise.
21-
*/
22-
export const checkMirrorReachable = async (mirror: string) => {
23-
return (
24-
isValidUrl(mirror) && (await electronAPI().NetWork.canAccessUrl(mirror))
25-
)
26-
}
27-
2814
/**
2915
* Checks if the user is likely in mainland China by:
3016
* 1. Checking navigator.language
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "dist"
6+
},
7+
"include": ["src/**/*"]
8+
}

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/collect-i18n-general.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import * as fs from 'fs'
22

33
import { comfyPageFixture as test } from '../browser_tests/fixtures/ComfyPage'
4+
import {
5+
formatCamelCase,
6+
normalizeI18nKey
7+
} from '../packages/shared-frontend-utils/src/formatUtil'
48
import { CORE_MENU_COMMANDS } from '../src/constants/coreMenuCommands'
59
import { DESKTOP_DIALOGS } from '../src/constants/desktopDialogs'
610
import { SERVER_CONFIG_ITEMS } from '../src/constants/serverConfig'
711
import type { FormItem, SettingParams } from '../src/platform/settings/types'
812
import type { ComfyCommandImpl } from '../src/stores/commandStore'
9-
import { formatCamelCase, normalizeI18nKey } from '../src/utils/formatUtil'
1013

1114
const localePath = './src/locales/en/main.json'
1215
const commandsPath = './src/locales/en/commands.json'

scripts/collect-i18n-node-defs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import * as fs from 'fs'
33
import type { ComfyNodeDef } from '@/schemas/nodeDefSchema'
44

55
import { comfyPageFixture as test } from '../browser_tests/fixtures/ComfyPage'
6+
import { normalizeI18nKey } from '../packages/shared-frontend-utils/src/formatUtil'
67
import type { ComfyNodeDefImpl } from '../src/stores/nodeDefStore'
7-
import { normalizeI18nKey } from '../src/utils/formatUtil'
88

99
const localePath = './src/locales/en/main.json'
1010
const nodeDefsPath = './src/locales/en/nodeDefs.json'

src/components/install/mirror/MirrorItem.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ import { computed, onMounted, ref, watch } from 'vue'
6060
import UrlInput from '@/components/common/UrlInput.vue'
6161
import type { UVMirror } from '@/constants/uvMirrors'
6262
import { st } from '@/i18n'
63+
import { checkMirrorReachable } from '@/utils/electronMirrorCheck'
6364
import { normalizeI18nKey } from '@/utils/formatUtil'
64-
import { checkMirrorReachable } from '@/utils/networkUtil'
6565
import { ValidationState } from '@/utils/validationUtil'
6666
6767
const FILE_URL_SCHEME = 'file://'

src/extensions/core/electronAdapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { useToastStore } from '@/platform/updates/common/toastStore'
66
import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
77
import { app } from '@/scripts/app'
88
import { useDialogService } from '@/services/dialogService'
9+
import { checkMirrorReachable } from '@/utils/electronMirrorCheck'
910
import { electronAPI as getElectronAPI, isElectron } from '@/utils/envUtil'
10-
import { checkMirrorReachable } from '@/utils/networkUtil'
1111

1212
// Desktop documentation URLs
1313
const DESKTOP_DOCS = {

src/renderer/extensions/vueNodes/widgets/composables/useImageUploadWidget.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { InputSpec } from '@/schemas/nodeDefSchema'
99
import type { ComfyWidgetConstructor } from '@/scripts/widgets'
1010
import { useNodeOutputStore } from '@/stores/imagePreviewStore'
1111
import { isImageUploadInput } from '@/types/nodeDefAugmentation'
12-
import { createAnnotatedPath } from '@/utils/formatUtil'
12+
import { createAnnotatedPath } from '@/utils/createAnnotatedPath'
1313
import { addToComboValues } from '@/utils/litegraphUtil'
1414

1515
const ACCEPTED_IMAGE_TYPES = 'image/png,image/jpeg,image/webp'

0 commit comments

Comments
 (0)