-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactor configuration repository
- Loading branch information
1 parent
1f9e690
commit f14aae2
Showing
1 changed file
with
89 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,117 @@ | ||
import { | ||
parse, | ||
stringify, | ||
xml_document, | ||
parse, | ||
stringify, | ||
xml_document, | ||
} from "https://deno.land/x/[email protected]/mod.ts"; | ||
import { Tool, ToolRepository } from "./toolsrepository.ts"; | ||
import { TOOL_DIR } from "../env.ts"; | ||
import { join } from "$std/path/join.ts"; | ||
|
||
interface ConfigurationRepository { | ||
update: () => Promise<void>; | ||
update: () => Promise<void>; | ||
} | ||
|
||
interface ConfigurationManager { | ||
read: () => Promise<Configuration>; | ||
write: (_: Configuration) => Promise<void>; | ||
} | ||
|
||
export const fileConfigurationManager = ( | ||
filename: string, | ||
): ConfigurationManager => { | ||
const read = async (): Promise<Configuration> => { | ||
return await parse_xml(filename).catch( | ||
(error) => { | ||
throw error; | ||
}, | ||
) as Configuration; | ||
}; | ||
|
||
const write = async (configuration: Configuration) => { | ||
await Deno.writeTextFile( | ||
filename, | ||
stringify(configuration), | ||
); | ||
}; | ||
|
||
return { | ||
read, | ||
write, | ||
}; | ||
}; | ||
|
||
export const configurationRepository = ( | ||
toolRepository: ToolRepository, | ||
toolRepository: ToolRepository, | ||
configurationManager: ConfigurationManager, | ||
): ConfigurationRepository => { | ||
return { | ||
update: async () => { | ||
const xmldoc = await parse_xml("./tool_conf.xml").catch( | ||
(error) => { | ||
throw error; | ||
}, | ||
) as Configuration; | ||
function updateConfiguration( | ||
toolList: Tool[], | ||
configuration: Configuration, | ||
): Configuration { | ||
const customSection: SectionEntity = generateSection(toolList); | ||
|
||
if (!configuration.toolbox.section) { | ||
configuration.toolbox.section = []; | ||
} | ||
|
||
configuration.toolbox.section = configuration.toolbox.section!.filter(( | ||
item, | ||
) => item["@id"] != customSection["@id"]); | ||
|
||
configuration.toolbox.section!.push(customSection); | ||
return configuration; | ||
} | ||
|
||
const toolList = await toolRepository.list(); | ||
const updatedConfig = updateConfiguration( | ||
toolList, | ||
xmldoc, | ||
); | ||
await Deno.writeTextFile( | ||
"./tool_conf.xml", | ||
stringify(updatedConfig), | ||
); | ||
}, | ||
}; | ||
function generateSection( | ||
toolList: Tool[], | ||
id: string = "customsection", | ||
name: string = "Custom Section", | ||
): SectionEntity { | ||
return { | ||
"@id": id, | ||
"@name": name, | ||
tool: toolList.map((tool) => { | ||
return { | ||
"@file": join(TOOL_DIR, tool.name), | ||
}; | ||
}) as [ToolEntry], | ||
}; | ||
} | ||
return { | ||
update: async () => { | ||
const xmldoc = await configurationManager.read(); | ||
const toolList = await toolRepository.list(); | ||
const updatedConfig = updateConfiguration( | ||
toolList, | ||
xmldoc, | ||
); | ||
await configurationManager.write(updatedConfig); | ||
}, | ||
}; | ||
}; | ||
|
||
export interface Configuration extends xml_document { | ||
toolbox: Toolbox; | ||
toolbox: Toolbox; | ||
} | ||
|
||
export interface Toolbox { | ||
monitor: string; | ||
section?: (SectionEntity)[] | null; | ||
monitor: string; | ||
section?: (SectionEntity)[] | null; | ||
} | ||
|
||
async function parse_xml(filename: string): Promise<Configuration> { | ||
const text_file = await Deno.readTextFile(filename).catch((error) => { | ||
throw error; | ||
}); | ||
return parse(text_file) as Configuration; | ||
const text_file = await Deno.readTextFile(filename).catch((error) => { | ||
throw error; | ||
}); | ||
return parse(text_file) as Configuration; | ||
} | ||
|
||
export interface SectionEntity { | ||
"@id": string; | ||
"@name": string; | ||
tool?: ToolEntry[] | null; | ||
label?: Label | null; | ||
"@id": string; | ||
"@name": string; | ||
tool?: ToolEntry[] | null; | ||
} | ||
|
||
export interface ToolEntry { | ||
"@file": string; | ||
} | ||
|
||
export interface Label { | ||
id: string; | ||
text: string; | ||
} | ||
|
||
function updateConfiguration( | ||
toolList: Tool[], | ||
configuration: Configuration, | ||
): Configuration { | ||
const customSection: SectionEntity = generateSection(toolList); | ||
|
||
if (!configuration.toolbox.section) { | ||
configuration.toolbox.section = []; | ||
} | ||
|
||
configuration.toolbox.section = configuration.toolbox.section!.filter(( | ||
item, | ||
) => item["@id"] != customSection["@id"]); | ||
|
||
configuration.toolbox.section!.push(customSection); | ||
return configuration; | ||
} | ||
|
||
function generateSection( | ||
toolList: Tool[], | ||
id: string = "customsection", | ||
name: string = "Custom Section", | ||
): SectionEntity { | ||
return { | ||
"@id": id, | ||
"@name": name, | ||
tool: toolList.map((tool) => { | ||
return { | ||
"@file": join(TOOL_DIR, tool.name), | ||
}; | ||
}) as [ToolEntry], | ||
}; | ||
"@file": string; | ||
} |