|
1 | 1 | import { v1 } from "@docker/extension-api-client-types";
|
2 | 2 | import { parse, stringify } from "yaml";
|
3 | 3 | import { REGISTRY_YAML } from "./Constants";
|
4 |
| -import { readFileInPromptsVolume, writeToPromptsVolume } from "./FileUtils"; |
| 4 | +import { readFileInPromptsVolume, writeToPromptsVolume } from "./utils/Files"; |
5 | 5 | import { mergeDeep } from "./MergeDeep";
|
6 | 6 | import { ParsedParameters } from "./types/config";
|
7 | 7 |
|
8 | 8 | export const getRegistry = async (client: v1.DockerDesktopClient) => {
|
9 |
| - const parseRegistry = async () => { |
10 |
| - const registry = await readFileInPromptsVolume(client, REGISTRY_YAML) |
11 |
| - if (registry) { |
12 |
| - const value = parse(registry)['registry'] as { [key: string]: { ref: string, config: any } } |
13 |
| - if (!value) { |
14 |
| - client.desktopUI.toast.error('Failed to parse registry.yaml: ' + registry) |
15 |
| - } |
16 |
| - return value; |
17 |
| - } |
18 |
| - return {}; |
| 9 | + const parseRegistry = async () => { |
| 10 | + const registry = await readFileInPromptsVolume(client, REGISTRY_YAML); |
| 11 | + if (registry) { |
| 12 | + const value = parse(registry)["registry"] as { |
| 13 | + [key: string]: { ref: string; config: any }; |
| 14 | + }; |
| 15 | + if (!value) { |
| 16 | + client.desktopUI.toast.error( |
| 17 | + "Failed to parse registry.yaml: " + registry |
| 18 | + ); |
| 19 | + } |
| 20 | + return value; |
19 | 21 | }
|
20 |
| - const writeRegistryIfNotExists = async () => { |
21 |
| - const registry = await readFileInPromptsVolume(client, REGISTRY_YAML) |
22 |
| - if (!registry) { |
23 |
| - await writeToPromptsVolume(client, REGISTRY_YAML, 'registry: {}') |
24 |
| - } |
| 22 | + return {}; |
| 23 | + }; |
| 24 | + const writeRegistryIfNotExists = async () => { |
| 25 | + const registry = await readFileInPromptsVolume(client, REGISTRY_YAML); |
| 26 | + if (!registry) { |
| 27 | + await writeToPromptsVolume(client, REGISTRY_YAML, "registry: {}"); |
25 | 28 | }
|
26 |
| - try { |
27 |
| - await writeRegistryIfNotExists() |
28 |
| - return await parseRegistry() |
29 |
| - } |
30 |
| - catch (error) { |
31 |
| - client.desktopUI.toast.error('Failed to get prompt registry: ' + error) |
32 |
| - return {}; |
33 |
| - } |
34 |
| -} |
| 29 | + }; |
| 30 | + try { |
| 31 | + await writeRegistryIfNotExists(); |
| 32 | + return await parseRegistry(); |
| 33 | + } catch (error) { |
| 34 | + client.desktopUI.toast.error("Failed to get prompt registry: " + error); |
| 35 | + return {}; |
| 36 | + } |
| 37 | +}; |
35 | 38 |
|
36 | 39 | export const getStoredConfig = async (client: v1.DockerDesktopClient) => {
|
37 |
| - const parseConfig = async () => { |
38 |
| - const config = await readFileInPromptsVolume(client, 'config.yaml') |
39 |
| - if (config) { |
40 |
| - return parse(config) as Promise<{ [key: string]: { [key: string]: ParsedParameters } }>; |
41 |
| - } |
42 |
| - return {}; |
43 |
| - } |
44 |
| - const writeConfigIfNotExists = async () => { |
45 |
| - const config = await readFileInPromptsVolume(client, 'config.yaml') |
46 |
| - if (!config) { |
47 |
| - await writeToPromptsVolume(client, 'config.yaml', '{}') |
48 |
| - } |
49 |
| - } |
50 |
| - try { |
51 |
| - await writeConfigIfNotExists() |
52 |
| - return await parseConfig() |
| 40 | + const parseConfig = async () => { |
| 41 | + const config = await readFileInPromptsVolume(client, "config.yaml"); |
| 42 | + if (config) { |
| 43 | + return parse(config) as Promise<{ |
| 44 | + [key: string]: { [key: string]: ParsedParameters }; |
| 45 | + }>; |
53 | 46 | }
|
54 |
| - catch (error) { |
55 |
| - client.desktopUI.toast.error('Failed to get stored configs: ' + error) |
56 |
| - return {}; |
| 47 | + return {}; |
| 48 | + }; |
| 49 | + const writeConfigIfNotExists = async () => { |
| 50 | + const config = await readFileInPromptsVolume(client, "config.yaml"); |
| 51 | + if (!config) { |
| 52 | + await writeToPromptsVolume(client, "config.yaml", "{}"); |
57 | 53 | }
|
58 |
| -} |
| 54 | + }; |
| 55 | + try { |
| 56 | + await writeConfigIfNotExists(); |
| 57 | + return await parseConfig(); |
| 58 | + } catch (error) { |
| 59 | + client.desktopUI.toast.error("Failed to get stored configs: " + error); |
| 60 | + return {}; |
| 61 | + } |
| 62 | +}; |
59 | 63 |
|
60 | 64 | // if registry.yaml has a config, it must be the same as what you have stored
|
61 | 65 | // if that’s not true and the registry.yaml value is valid then you should sync with it
|
62 | 66 | // if it’s not true and the registry.yaml is invalid then the catalog item needs user assistance because the catalog has probably been updated with a breaking change
|
63 | 67 |
|
64 | 68 | // Replace conflicting registry values with config values
|
65 |
| -export const syncRegistryWithConfig = async (client: v1.DockerDesktopClient, registry: { [key: string]: { ref: string, config: any } }, config: { [key: string]: { [key: string]: ParsedParameters } }) => { |
66 |
| - if (Object.keys(config).length === 0) { |
67 |
| - return; |
68 |
| - } |
69 |
| - if (Object.keys(registry).length === 0) { |
70 |
| - return; |
71 |
| - } |
72 |
| - const oldRegString = JSON.stringify(registry) |
73 |
| - for (const [itemName, itemConfig] of Object.entries(config)) { |
74 |
| - if (registry[itemName]) { |
75 |
| - registry[itemName].config = { [itemName]: itemConfig } |
76 |
| - } |
77 |
| - } |
78 |
| - const newRegString = JSON.stringify(registry) |
79 |
| - if (oldRegString !== newRegString) { |
80 |
| - await writeToPromptsVolume(client, REGISTRY_YAML, stringify({ registry })) |
| 69 | +export const syncRegistryWithConfig = async ( |
| 70 | + client: v1.DockerDesktopClient, |
| 71 | + registry: { [key: string]: { ref: string; config: any } }, |
| 72 | + config: { [key: string]: { [key: string]: ParsedParameters } } |
| 73 | +) => { |
| 74 | + if (Object.keys(config).length === 0) { |
| 75 | + return; |
| 76 | + } |
| 77 | + if (Object.keys(registry).length === 0) { |
| 78 | + return; |
| 79 | + } |
| 80 | + const oldRegString = JSON.stringify(registry); |
| 81 | + for (const [itemName, itemConfig] of Object.entries(config)) { |
| 82 | + if (registry[itemName]) { |
| 83 | + registry[itemName].config = { [itemName]: itemConfig }; |
81 | 84 | }
|
82 |
| - return registry |
83 |
| -} |
| 85 | + } |
| 86 | + const newRegString = JSON.stringify(registry); |
| 87 | + if (oldRegString !== newRegString) { |
| 88 | + await writeToPromptsVolume(client, REGISTRY_YAML, stringify({ registry })); |
| 89 | + } |
| 90 | + return registry; |
| 91 | +}; |
0 commit comments