Skip to content

Commit

Permalink
feat: import parser configurations from json files
Browse files Browse the repository at this point in the history
this is a CLI feature that will take a list of files which can each have
an individual configuration or a list of them, match them with an
existing configuration by title, and then save all of them.
  • Loading branch information
schradert committed Nov 4, 2024
1 parent 7999f1c commit 4d05174
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ yargs(hideBin(process.argv))
flagsCLI["names"] = !!argv.names;
},
)
.command(
"import",
clc.blue("Import parser configurations from JSON.\nUsage: import [file]"),
(yargs: typeof Argv) => {
return yargs
.positional("files", {
describe: clc.blue("JSON file paths with parser configurations."),
type: "string",
})
},
(argv: any) => {
commandCLI = "import";
argsCLI = argv._.slice(1);
},
)
.command(
"add",
clc.blue("Run all enabled parsers and save apps to steam.\nUsage: add"),
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class AppComponent {
});
ipcService.on("cli_message", (event, message) => {
this.settingsService.onLoad((appSettings) => {
if (["list", "enable", "disable"].includes(message.command)) {
if (["list", "enable", "disable", "import"].includes(message.command)) {
this.zone.run(() => {
this.router.navigate(["/parsers", -1], {
queryParams: {
Expand Down
25 changes: 25 additions & 0 deletions src/renderer/components/parsers.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
import { BehaviorSubject, Subscription, of, concat } from "rxjs";
import { map } from "rxjs/operators";
import { APP } from "../../variables";
import * as fs from "fs-extra";
import * as _ from "lodash";
import * as os from "os";
import {
Expand Down Expand Up @@ -876,6 +877,30 @@ export class ParsersComponent implements AfterViewInit, OnDestroy {
this.ipcService.send("all_done");
});
}
} else if (["import"].includes(parsedCLI.command)) {
for (let jsonFile of parsedCLI.args) {
fs.readFile(jsonFile, "utf8", (error: any, data: any) => {
if (error) {
this.ipcService.send("log", error);
} else {
try {
const parsed = JSON.parse(data) || [];
const configurations: UserConfiguration[] = Array.isArray(parsed) ? parsed : [parsed];
for (let saved of configurations) {
if (!this.parsersService.isConfigurationValid(saved)) {
this.ipcService.send("log", `User configuration is not valid: ${JSON.stringify(saved)}`);
} else {
const current = userConfigurations.filter(cfg => saved.configTitle == cfg.configTitle).at(0) || null;
this.parsersService.saveConfiguration({ saved, current });
}
}
} catch (e) {
this.ipcService.send("log", e);
}
}
});
}
this.ipcService.send("all_done");
}
},
);
Expand Down

0 comments on commit 4d05174

Please sign in to comment.