comfy-ts 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +363 -0
- package/dist/cli.js +764 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.cjs +4171 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +9910 -0
- package/dist/index.d.ts +9894 -58
- package/dist/index.js +4092 -2
- package/dist/index.js.map +1 -1
- package/dist/log-CdfgILde.js +10 -0
- package/dist/log-CdfgILde.js.map +1 -0
- package/dist/run-tui-SDSJo1Qf.js +3896 -0
- package/dist/run-tui-SDSJo1Qf.js.map +1 -0
- package/package.json +103 -75
- package/src/cli/comfy-ts-cli.ts +36 -0
- package/src/cli/gen.ts +47 -0
- package/src/cli/outline.ts +82 -0
- package/src/cli/tui/components/DraftsOverlay.tsx +44 -0
- package/src/cli/tui/components/EditField.tsx +27 -0
- package/src/cli/tui/components/Header.tsx +56 -0
- package/src/cli/tui/components/HostPanel.tsx +37 -0
- package/src/cli/tui/components/LogsPanel.tsx +23 -0
- package/src/cli/tui/components/LorasOverlay.tsx +49 -0
- package/src/cli/tui/components/OverlayList.tsx +25 -0
- package/src/cli/tui/components/PickerOverlays.tsx +66 -0
- package/src/cli/tui/components/PreviewPanel.tsx +78 -0
- package/src/cli/tui/components/PromptOverlay.tsx +16 -0
- package/src/cli/tui/components/StatusBar.tsx +171 -0
- package/src/cli/tui/components/TextOverlay.tsx +72 -0
- package/src/cli/tui/components/TreePanel.tsx +51 -0
- package/src/cli/tui/components/TuiApp.tsx +246 -0
- package/src/cli/tui/components/VarsPanel.tsx +86 -0
- package/src/cli/tui/findDefinedWorkflow.ts +16 -0
- package/src/cli/tui/keys.ts +20 -0
- package/src/cli/tui/protocolImagePainter.ts +95 -0
- package/src/cli/tui/run-tui.tsx +76 -0
- package/src/cli/tui/state/DraftsSt.ts +273 -0
- package/src/cli/tui/state/EditorSt.ts +289 -0
- package/src/cli/tui/state/ExecSt.ts +173 -0
- package/src/cli/tui/state/HostSt.ts +230 -0
- package/src/cli/tui/state/LogsSt.ts +107 -0
- package/src/cli/tui/state/LorasSt.ts +185 -0
- package/src/cli/tui/state/PickerSt.ts +112 -0
- package/src/cli/tui/state/PreviewSt.ts +288 -0
- package/src/cli/tui/state/QueueSt.ts +59 -0
- package/src/cli/tui/state/SettingsSt.ts +100 -0
- package/src/cli/tui/state/TreeSt.ts +122 -0
- package/src/cli/tui/state/TuiSt.ts +175 -0
- package/src/cli/tui/state/WorkflowsSt.ts +93 -0
- package/src/cli/tui-env.ts +8 -0
- package/src/errors/InvalidPromptError.ts +12 -0
- package/src/graph/ComfyNode.ts +403 -0
- package/src/graph/ComfyNodeID.ts +28 -0
- package/src/graph/ComfyNodeOutput.ts +16 -0
- package/src/graph/NodeSlotSize.ts +4 -0
- package/src/graph/autoValue.ts +17 -0
- package/src/host/ComfyHost.ts +690 -0
- package/src/host/ComfyInstallType.ts +15 -0
- package/src/host/ComfyManager.ts +232 -0
- package/src/host/ComfyUploader.ts +110 -0
- package/src/host/ComfyWorkflowBuilder.ts +60 -0
- package/src/host/Requirements.ts +55 -0
- package/src/host/ResilientWebsocket.ts +147 -0
- package/src/host/loraManagerApi.ts +98 -0
- package/src/image-utils/_getPngMetadata.ts +53 -0
- package/src/image-utils/_getWebpMetadata.ts +38 -0
- package/src/image-utils/_parseExifData.ts +53 -0
- package/src/index.ts +89 -0
- package/src/litegraph/LiteGraphGroup.ts +22 -0
- package/src/litegraph/LiteGraphJSON.ts +46 -0
- package/src/litegraph/LiteGraphLink.ts +23 -0
- package/src/litegraph/LiteGraphLinkID.ts +6 -0
- package/src/litegraph/LiteGraphNode.ts +69 -0
- package/src/litegraph/LiteGraphNodeInput.ts +27 -0
- package/src/litegraph/LiteGraphNodeOutput.ts +23 -0
- package/src/litegraph/LiteGraphSlotIndex.ts +6 -0
- package/src/litegraph/convertFlowToLiteGraphJSON.ts +114 -0
- package/src/manager/ComfyRegistry.ts +124 -0
- package/src/manager/generated/KnownComfyCustomNodeName.ts +19280 -0
- package/src/manager/generated/KnownComfyPluginTitle.ts +5130 -0
- package/src/manager/generated/KnownComfyPluginURL.ts +2589 -0
- package/src/manager/generated/KnownModel_Base.ts +126 -0
- package/src/manager/generated/KnownModel_FileName.ts +2287 -0
- package/src/manager/generated/KnownModel_Name.ts +2287 -0
- package/src/manager/generated/KnownModel_SavePath.ts +263 -0
- package/src/manager/generated/KnownModel_Type.ts +97 -0
- package/src/manager/json/alter-list.json +224 -0
- package/src/manager/json/custom-node-list.extra.ts +13 -0
- package/src/manager/json/custom-node-list.json +21948 -0
- package/src/manager/json/extension-node-map.json +31282 -0
- package/src/manager/json/github-stats.json +12602 -0
- package/src/manager/json/model-list.extra.ts +26 -0
- package/src/manager/json/model-list.json +5052 -0
- package/src/manager/loaders/step1_downloadComfyManagerJSONs.ts +56 -0
- package/src/manager/loaders/step2_custom-node-list-loader.ts +149 -0
- package/src/manager/loaders/step3_extension-node-map-loader.ts +189 -0
- package/src/manager/loaders/step4_model-list-loader.ts +159 -0
- package/src/manager/script-update-manager.bun.ts +18 -0
- package/src/manager/types/ComfyManagerAPIFetchPolicy.ts +9 -0
- package/src/manager/types/ComfyManagerAPIModelList.ts +10 -0
- package/src/manager/types/ComfyManagerAPIPluginList.ts +11 -0
- package/src/manager/types/ComfyManagerFileModelInfo.ts +5 -0
- package/src/manager/types/ComfyManagerFilePluginContent.ts +13 -0
- package/src/manager/types/ComfyManagerFilePluginList.ts +5 -0
- package/src/manager/types/ComfyManagerModelInfo.ts +18 -0
- package/src/manager/types/ComfyManagerPluginContentMetadata.ts +16 -0
- package/src/manager/types/ComfyManagerPluginEnums.ts +8 -0
- package/src/manager/types/ComfyManagerPluginInfo.ts +50 -0
- package/src/runner/ComfyExecution.ts +301 -0
- package/src/runner/ComfyExecutionStatus.ts +2 -0
- package/src/runner/ComfyWorkflow.ts +395 -0
- package/src/runner/ComfyWorkflowLayout.ts +96 -0
- package/src/runner/ComfyWsApi.ts +170 -0
- package/src/runner/MediaImage.ts +262 -0
- package/src/sdk-generator/ComfyNodeSchema.ts +79 -0
- package/src/sdk-generator/ComfySchema.ts +369 -0
- package/src/sdk-generator/ComfyUIObjectInfoTypes.ts +95 -0
- package/src/sdk-generator/Primitives.ts +76 -0
- package/src/sdk-generator/_getUnionNameBasedOnFirstFoundEnumName.ts +39 -0
- package/src/sdk-generator/_pythonModuleToNamespace.ts +27 -0
- package/src/sdk-generator/_toQualifiedNodeKey.ts +5 -0
- package/src/sdk-generator/comfy-api-json.ts +16 -0
- package/src/sdk-generator/comfyui-sdk-codegen.ts +287 -0
- package/src/sdk-generator/comfyui-types.ts +56 -0
- package/src/sdk-generator/litegraphToApiRequestPayload.ts +157 -0
- package/src/ssh-host-manager/ssh-config-manager.ts +89 -0
- package/src/ssh-host-manager/ssh-config.windows.md +32 -0
- package/src/ssh-host-manager/ssh-manager.ts +114 -0
- package/src/state.ts +136 -0
- package/src/types/comfy-sdk.ts +106 -0
- package/src/types/index.ts +55 -0
- package/src/utils/CodeBuffer.ts +61 -0
- package/src/utils/ComfyColors.ts +38 -0
- package/src/utils/ansi.ts +33 -0
- package/src/utils/ansiImage.ts +36 -0
- package/src/utils/bang.ts +39 -0
- package/src/utils/deepCopyNaive.ts +1 -0
- package/src/utils/downloadFile.ts +77 -0
- package/src/utils/escapeJSKey.ts +32 -0
- package/src/utils/exhaust.ts +4 -0
- package/src/utils/extractErrorMessage.ts +11 -0
- package/src/utils/fuzzyMatch.ts +8 -0
- package/src/utils/githubRegexes.ts +2 -0
- package/src/utils/hashArrayBuffer.ts +6 -0
- package/src/utils/log.ts +9 -0
- package/src/utils/loraTag.ts +10 -0
- package/src/utils/mojibake.ts +60 -0
- package/src/utils/printArkResultInConsole.ts +16 -0
- package/src/utils/softValidate.ts +8 -0
- package/src/utils/stringifyReadable.ts +32 -0
- package/src/utils/toposort.ts +59 -0
- package/src/utils/tryExtracting.ts +41 -0
- package/src/utils/writeFile.ts +16 -0
- package/src/vars/ComfyVars.ts +531 -0
- package/src/vars/DefinedWorkflow.ts +112 -0
- package/src/vars/loraKeywords.ts +56 -0
- package/dist/index.d.mts +0 -74
- package/dist/index.mjs +0 -3
- package/dist/index.mjs.map +0 -1
- package/readme.md +0 -40
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import fs, { mkdirSync } from 'node:fs'
|
|
2
|
+
import https from 'node:https'
|
|
3
|
+
import { stdout } from 'node:process'
|
|
4
|
+
import { dirname } from 'pathe'
|
|
5
|
+
import type { AbsolutePath } from 'src/types/index.ts'
|
|
6
|
+
|
|
7
|
+
/** Usage example
|
|
8
|
+
* | ;(async () => {
|
|
9
|
+
* | try {
|
|
10
|
+
* | await downloadFile('https://example.com/file', 'folder/filename')
|
|
11
|
+
* | console.log('Download completed successfully')
|
|
12
|
+
* | } catch (error) {
|
|
13
|
+
* | console.error('Download failed:', error)
|
|
14
|
+
* | }
|
|
15
|
+
* | })()
|
|
16
|
+
* |
|
|
17
|
+
*/
|
|
18
|
+
export function downloadFile(
|
|
19
|
+
//
|
|
20
|
+
url: string,
|
|
21
|
+
outputPath: AbsolutePath | string,
|
|
22
|
+
logPrefix = ' - ',
|
|
23
|
+
): Promise<true> {
|
|
24
|
+
const baseDir = dirname(outputPath)
|
|
25
|
+
mkdirSync(baseDir, { recursive: true })
|
|
26
|
+
|
|
27
|
+
return new Promise<true>((resolve, reject) => {
|
|
28
|
+
const request = https.get(url, (response) => {
|
|
29
|
+
// initial response --------------------------------------------
|
|
30
|
+
// Check for redirect
|
|
31
|
+
if (response.statusCode === 302 || response.statusCode === 301) {
|
|
32
|
+
// Follow the redirect and call downloadFile recursively
|
|
33
|
+
const newUrl = response.headers.location!
|
|
34
|
+
console.log(`Redirected to ${newUrl}`)
|
|
35
|
+
resolve(downloadFile(newUrl, outputPath))
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (response.statusCode !== 200) {
|
|
40
|
+
reject(`Failed to download file: Status Code ${response.statusCode}`)
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// progress --------------------------------------------------
|
|
45
|
+
const rawLen = response.headers['content-length']
|
|
46
|
+
if (rawLen == null) {
|
|
47
|
+
console.warn(`download file: progress not available because no content-length header in response.`)
|
|
48
|
+
} else {
|
|
49
|
+
const totalSize = parseInt(rawLen, 10)
|
|
50
|
+
let downloaded = 0
|
|
51
|
+
let lastLoggedPercentage = 0
|
|
52
|
+
|
|
53
|
+
response.on('data', (chunk) => {
|
|
54
|
+
downloaded += chunk.length
|
|
55
|
+
const percentage = Math.round((downloaded / totalSize) * 100)
|
|
56
|
+
if (percentage >= lastLoggedPercentage + 1) {
|
|
57
|
+
// console.log(`${percentage}% downloaded`)
|
|
58
|
+
stdout.write(`\r${logPrefix}${percentage}% downloaded`)
|
|
59
|
+
lastLoggedPercentage = percentage
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// file stream -------------------------------------------
|
|
65
|
+
const fileStream = fs.createWriteStream(outputPath)
|
|
66
|
+
response.pipe(fileStream)
|
|
67
|
+
|
|
68
|
+
fileStream.on('finish', () => {
|
|
69
|
+
console.log(` (DONE)`)
|
|
70
|
+
fileStream.close()
|
|
71
|
+
resolve(true)
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
request.on('error', (err) => reject(err))
|
|
76
|
+
})
|
|
77
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const regex = /^[a-zA-Z_][a-zA-Z0-9_]*$/
|
|
2
|
+
|
|
3
|
+
const isValidJSKey = (s: string): boolean => {
|
|
4
|
+
return regex.test(s)
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const escapeJSKey = (s: string): string => {
|
|
8
|
+
if (typeof s !== 'string') {
|
|
9
|
+
return 'string'
|
|
10
|
+
// console.log(s)
|
|
11
|
+
// debugger
|
|
12
|
+
}
|
|
13
|
+
// ❌ probably wrong
|
|
14
|
+
if (!isValidJSKey(s)) {
|
|
15
|
+
// debugger
|
|
16
|
+
return `"${s}"`
|
|
17
|
+
}
|
|
18
|
+
return s
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const asJSAccessor = (s: string): string => {
|
|
22
|
+
if (typeof s !== 'string') {
|
|
23
|
+
return 'string'
|
|
24
|
+
// console.log(s)
|
|
25
|
+
// debugger
|
|
26
|
+
}
|
|
27
|
+
if (!isValidJSKey(s)) {
|
|
28
|
+
// debugger
|
|
29
|
+
return `["${s}"]`
|
|
30
|
+
}
|
|
31
|
+
return `.${s}`
|
|
32
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Extracts an error message from an exception stuff. */
|
|
2
|
+
export const extractErrorMessage = (error: unknown): string => {
|
|
3
|
+
if (error instanceof Error) return error.message
|
|
4
|
+
if (error === null) return 'null'
|
|
5
|
+
if (typeof error === 'string') return error
|
|
6
|
+
if (typeof error === 'object') {
|
|
7
|
+
if ('message' in error && typeof error.message === 'string') return error.message
|
|
8
|
+
return JSON.stringify(error)
|
|
9
|
+
}
|
|
10
|
+
return String(error)
|
|
11
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** case-insensitive subsequence match: 'wide' hits '16:9 (Widescreen)' */
|
|
2
|
+
export function fuzzyMatch(needle: string, hay: string): boolean {
|
|
3
|
+
const n = needle.toLowerCase()
|
|
4
|
+
const h = hay.toLowerCase()
|
|
5
|
+
let i = 0
|
|
6
|
+
for (const ch of h) if (ch === n[i]) i++
|
|
7
|
+
return i >= n.length
|
|
8
|
+
}
|
package/src/utils/log.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// loud console logging; errors must be observable to human AND LLM.
|
|
2
|
+
// COMFY_TS_QUIET=1 (set by drivers like the TUI) silences INFO chatter only —
|
|
3
|
+
// errors always print.
|
|
4
|
+
export const isQuiet = (): boolean => process.env.COMFY_TS_QUIET === '1'
|
|
5
|
+
export const logInfo = (msg: string): void => {
|
|
6
|
+
if (!isQuiet()) console.log(msg)
|
|
7
|
+
}
|
|
8
|
+
export const logError = (msg: string): void => console.error(`[comfy-ts] 🔴 ${msg}`)
|
|
9
|
+
export const logSuccess = (msg: string): void => console.log(`[comfy-ts] 🟢 ${msg}`)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* build a LoraManager-style lora tag: `<lora:name:strength>` or
|
|
3
|
+
* `<lora:name:strength:clipStrength>`. Pin the name to a host union for
|
|
4
|
+
* autocomplete + validation:
|
|
5
|
+
*
|
|
6
|
+
* loraTag<Comfy.Windows1.Union['E_LoraName']>('myLora', 0.8)
|
|
7
|
+
*/
|
|
8
|
+
export function loraTag<N extends string = string>(name: N, strength: number = 1, clipStrength?: number): string {
|
|
9
|
+
return clipStrength == null ? `<lora:${name}:${strength}>` : `<lora:${name}:${strength}:${clipStrength}>`
|
|
10
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// cp1252's 0x80-0x9F specials, unicode → original byte (the rest is latin1: code = byte)
|
|
2
|
+
const CP1252_REVERSE: Record<string, number> = {
|
|
3
|
+
'€': 0x80,
|
|
4
|
+
'‚': 0x82,
|
|
5
|
+
ƒ: 0x83,
|
|
6
|
+
'„': 0x84,
|
|
7
|
+
'…': 0x85,
|
|
8
|
+
'†': 0x86,
|
|
9
|
+
'‡': 0x87,
|
|
10
|
+
ˆ: 0x88,
|
|
11
|
+
'‰': 0x89,
|
|
12
|
+
Š: 0x8a,
|
|
13
|
+
'‹': 0x8b,
|
|
14
|
+
Œ: 0x8c,
|
|
15
|
+
Ž: 0x8e,
|
|
16
|
+
'‘': 0x91,
|
|
17
|
+
'’': 0x92,
|
|
18
|
+
'“': 0x93,
|
|
19
|
+
'”': 0x94,
|
|
20
|
+
'•': 0x95,
|
|
21
|
+
'–': 0x96,
|
|
22
|
+
'—': 0x97,
|
|
23
|
+
'˜': 0x98,
|
|
24
|
+
'™': 0x99,
|
|
25
|
+
š: 0x9a,
|
|
26
|
+
'›': 0x9b,
|
|
27
|
+
œ: 0x9c,
|
|
28
|
+
ž: 0x9e,
|
|
29
|
+
Ÿ: 0x9f,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* repair UTF-8 that was decoded as cp1252 (a Windows server re-reading its own
|
|
34
|
+
* output: `█` E2 96 88 arrives as `â–ˆ`). Reverses the cp1252 map back to
|
|
35
|
+
* bytes and re-decodes as UTF-8; any unmappable char or invalid byte sequence
|
|
36
|
+
* means the text was NOT mojibake — the original is returned untouched.
|
|
37
|
+
*/
|
|
38
|
+
export function repairMojibake(s: string): string {
|
|
39
|
+
let suspicious = false
|
|
40
|
+
for (let i = 0; i < s.length; i++) {
|
|
41
|
+
if (s.charCodeAt(i) > 0x7f) {
|
|
42
|
+
suspicious = true
|
|
43
|
+
break
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (!suspicious) return s
|
|
47
|
+
const bytes = new Uint8Array(s.length)
|
|
48
|
+
for (let i = 0; i < s.length; i++) {
|
|
49
|
+
const code = s.charCodeAt(i)
|
|
50
|
+
if (code <= 0xff) {
|
|
51
|
+
bytes[i] = code
|
|
52
|
+
continue
|
|
53
|
+
}
|
|
54
|
+
const mapped = CP1252_REVERSE[s[i] ?? '']
|
|
55
|
+
if (mapped == null) return s
|
|
56
|
+
bytes[i] = mapped
|
|
57
|
+
}
|
|
58
|
+
const decoded = Buffer.from(bytes).toString('utf8')
|
|
59
|
+
return decoded.includes('�') ? s : decoded
|
|
60
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ArkErrors } from 'arktype'
|
|
2
|
+
import { ansi as chalk } from 'src/utils/ansi.ts'
|
|
3
|
+
import { isQuiet } from 'src/utils/log.ts'
|
|
4
|
+
|
|
5
|
+
const MAX_PRINTED_ERRORS = 10
|
|
6
|
+
|
|
7
|
+
export function printArkResultInConsole(res: ArkErrors): void {
|
|
8
|
+
if (isQuiet()) return
|
|
9
|
+
console.log(chalk.red(' ❌ Ark validation failed:'))
|
|
10
|
+
for (const error of res.slice(0, MAX_PRINTED_ERRORS)) {
|
|
11
|
+
console.log(chalk.red(` - ${error.path.join('.')} : ${error.message}`))
|
|
12
|
+
}
|
|
13
|
+
if (res.length > MAX_PRINTED_ERRORS) {
|
|
14
|
+
console.log(chalk.red(` … ${res.length - MAX_PRINTED_ERRORS} more (${res.length} total)`))
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Type, type } from 'arktype'
|
|
2
|
+
import { printArkResultInConsole } from 'src/utils/printArkResultInConsole.ts'
|
|
3
|
+
|
|
4
|
+
export const softValidate = <T>(schema: Type<T>, stuff: unknown): T => {
|
|
5
|
+
const res = schema(stuff)
|
|
6
|
+
if (res instanceof type.errors) printArkResultInConsole(res)
|
|
7
|
+
return stuff as T
|
|
8
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const tab = ' '
|
|
2
|
+
|
|
3
|
+
export function readableStringify(obj: unknown, maxLevel = 3, level = 0): string {
|
|
4
|
+
if (level > maxLevel) return JSON.stringify(obj)
|
|
5
|
+
if (typeof obj !== 'object' || obj === null) return JSON.stringify(obj)
|
|
6
|
+
const indent = ' '.repeat(level + 1)
|
|
7
|
+
let result = '{\n'
|
|
8
|
+
const keys = Object.keys(obj)
|
|
9
|
+
for (let i = 0; i < keys.length; i++) {
|
|
10
|
+
// biome-ignore lint/style/noNonNullAssertion: don't care
|
|
11
|
+
const key = keys[i]!
|
|
12
|
+
// biome-ignore lint/suspicious/noExplicitAny: don't care
|
|
13
|
+
const value = (obj as any)[key]
|
|
14
|
+
if (value === undefined) continue
|
|
15
|
+
const valueType = typeof value
|
|
16
|
+
|
|
17
|
+
if (i > 0) result += ','
|
|
18
|
+
const thisIndent = i > 0 ? indent.slice(1) : indent
|
|
19
|
+
if (Array.isArray(value)) {
|
|
20
|
+
result += `${thisIndent}${JSON.stringify(key)}: ${JSON.stringify(value)}`
|
|
21
|
+
} else if (valueType === 'object' && value !== null) {
|
|
22
|
+
result += `${thisIndent}${JSON.stringify(key)}: ${readableStringify(value, maxLevel, level + 1)}`
|
|
23
|
+
} else {
|
|
24
|
+
const formattedValue = valueType === 'string' ? JSON.stringify(value) : value
|
|
25
|
+
result += `${thisIndent}${JSON.stringify(key)}: ${formattedValue}`
|
|
26
|
+
}
|
|
27
|
+
result += '\n'
|
|
28
|
+
}
|
|
29
|
+
result += `${tab.repeat(Math.max(0, level))}}`
|
|
30
|
+
|
|
31
|
+
return result
|
|
32
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
type ObjMap<T> = { [key: string]: T }
|
|
2
|
+
|
|
3
|
+
export type TNode = string
|
|
4
|
+
export type TEdge = [TNode, TNode]
|
|
5
|
+
|
|
6
|
+
export function toposort(nodes: TNode[], edges: TEdge[]): TNode[] {
|
|
7
|
+
return toposortAlgo(nodes, edges)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function toposortAlgo(nodes: TNode[], edges: TEdge[]) {
|
|
11
|
+
let cursor = nodes.length
|
|
12
|
+
const sorted = Array.from<TNode>({ length: cursor })
|
|
13
|
+
const visited: ObjMap<boolean> = {}
|
|
14
|
+
let k = cursor
|
|
15
|
+
|
|
16
|
+
while (k--) {
|
|
17
|
+
if (!visited[k]) visit(BONG(nodes[k]), k, [])
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return sorted
|
|
21
|
+
|
|
22
|
+
function visit(node: TNode, ix: number, predecessors: TNode[]): void {
|
|
23
|
+
let i = ix
|
|
24
|
+
if (predecessors.indexOf(node) >= 0) {
|
|
25
|
+
console.log(predecessors, node)
|
|
26
|
+
throw new Error('Cyclic dependency: ' + JSON.stringify(node))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (nodes.indexOf(node) === -1) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
'[toposort] Found unknown node. Make sure to provided all involved nodes. Unknown node: ' +
|
|
32
|
+
JSON.stringify(node) +
|
|
33
|
+
'; known nodes are ' +
|
|
34
|
+
JSON.stringify(nodes),
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (visited[i]) return
|
|
39
|
+
visited[i] = true
|
|
40
|
+
|
|
41
|
+
// outgoing edges
|
|
42
|
+
const outgoing = edges.filter((edge) => edge[0] === node)
|
|
43
|
+
i = outgoing.length
|
|
44
|
+
if (i) {
|
|
45
|
+
const preds = predecessors.concat(node)
|
|
46
|
+
do {
|
|
47
|
+
const child = BONG(outgoing[--i])[1]
|
|
48
|
+
visit(child, nodes.indexOf(child), preds)
|
|
49
|
+
} while (i)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
sorted[--cursor] = node
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function BONG<T>(x: T): NonNullable<T> {
|
|
57
|
+
if (x == null) throw new Error('BONG')
|
|
58
|
+
return x
|
|
59
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Maybe } from 'src/types/index.ts'
|
|
2
|
+
|
|
3
|
+
/** quick utility to verbosely attempt various data recovery tricks */
|
|
4
|
+
export enum RecoveryVerbosity {
|
|
5
|
+
Quiet = 0,
|
|
6
|
+
info = 1,
|
|
7
|
+
Verbose = 2,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function tryRecovering<T>({
|
|
11
|
+
verbose = RecoveryVerbosity.Quiet,
|
|
12
|
+
property,
|
|
13
|
+
hacks,
|
|
14
|
+
onFail,
|
|
15
|
+
onSuccess,
|
|
16
|
+
}: {
|
|
17
|
+
verbose?: RecoveryVerbosity
|
|
18
|
+
property: string
|
|
19
|
+
hacks: {
|
|
20
|
+
level?: RecoveryVerbosity
|
|
21
|
+
attempt: string
|
|
22
|
+
fn: () => Maybe<T>
|
|
23
|
+
}[]
|
|
24
|
+
onFail?: (propName: string) => void
|
|
25
|
+
onSuccess?: (propName: string, value: T) => void
|
|
26
|
+
}): T {
|
|
27
|
+
if (verbose >= RecoveryVerbosity.Verbose) console.log(`[🔶] trying to find a suitable value for ${property}...`)
|
|
28
|
+
|
|
29
|
+
for (const hack of hacks) {
|
|
30
|
+
if (verbose >= RecoveryVerbosity.Verbose) console.log(` > trying to use ${hack.attempt}...`)
|
|
31
|
+
const value = hack.fn()
|
|
32
|
+
if (value != null) {
|
|
33
|
+
if (verbose >= (hack.level ?? RecoveryVerbosity.info))
|
|
34
|
+
if (verbose) console.log(` 🔶 recovered ${property} using ${hack.attempt}.`)
|
|
35
|
+
onSuccess?.(property, value)
|
|
36
|
+
return value
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
onFail?.(property)
|
|
40
|
+
throw new Error(`[🔶] ${property} could not be recovered`)
|
|
41
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { writeFile } from 'node:fs'
|
|
2
|
+
import { ansi as chalk } from 'src/utils/ansi.ts'
|
|
3
|
+
import { logInfo } from 'src/utils/log.ts'
|
|
4
|
+
|
|
5
|
+
export const writeFileAsync = async (path: string, data: string, fmt: 'utf-8') => {
|
|
6
|
+
return new Promise<void>((resolve, reject) => {
|
|
7
|
+
logInfo(`writing file: ${chalk.blue(path)}`)
|
|
8
|
+
writeFile(path, data, fmt, (err) => {
|
|
9
|
+
if (err) {
|
|
10
|
+
reject(err)
|
|
11
|
+
} else {
|
|
12
|
+
resolve()
|
|
13
|
+
}
|
|
14
|
+
})
|
|
15
|
+
})
|
|
16
|
+
}
|