-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support create profile * fix: regexp group index error * fix: cancel support discard profile changes
- Loading branch information
Showing
8 changed files
with
282 additions
and
105 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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export type RecursivePartial<T> = { | ||
[P in keyof T]?: T[P] extends object ? RecursivePartial<T[P]> : T[P]; | ||
}; | ||
|
||
export type RecursiveRequired<T> = { | ||
// eslint-disable-next-line ts/no-unsafe-function-type | ||
[K in keyof T]-?: T[K] extends object ? T[K] extends Function ? T[K] | ||
: RecursiveRequired<T[K]> | ||
: T[K]; | ||
}; | ||
|
||
export type RecursiveNonNullable<T> = { | ||
// eslint-disable-next-line ts/no-unsafe-function-type | ||
[K in keyof T]-?: T[K] extends object ? T[K] extends Function ? T[K] | ||
: RecursiveNonNullable<NonNullable<T[K]>> | ||
: NonNullable<T[K]>; | ||
}; | ||
|
||
type RecursiveOptionalKeys<T> = { | ||
[K in keyof T]-?: undefined extends T[K] ? K : T[K] extends object ? K | RecursiveOptionalKeys<T[K]> : never; | ||
}[keyof T]; | ||
|
||
export type RecursivePickOptional<T> = { | ||
[K in keyof T as K extends RecursiveOptionalKeys<T> ? K : never]?: T[K] extends object ? RecursivePickOptional<T[K]> : T[K]; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { readFile } from "node:fs/promises"; | ||
import { isNotNil } from "es-toolkit"; | ||
import { outputFile } from "fs-extra/esm"; | ||
import { logger } from "./log.js"; | ||
import { prefs as defaultPrefs } from "./zotero/preference.js"; | ||
|
||
export type Prefs = Record<string, string | number | boolean | undefined | null>; | ||
|
||
export class PrefsManager { | ||
private namespace: "pref" | "user_pref"; | ||
private prefs: Prefs; | ||
|
||
constructor(namespace: "pref" | "user_pref") { | ||
this.namespace = namespace; | ||
this.prefs = { ...defaultPrefs }; | ||
} | ||
|
||
private parsePrefjs(content: string) { | ||
// eslint-disable-next-line regexp/no-super-linear-backtracking | ||
const prefPattern = /^(pref|user_pref)\s*\(\s*["']([^"']+)["']\s*,\s*(.+)\s*\)\s*;$/gm; | ||
const matches = content.matchAll(prefPattern); | ||
for (const match of matches) { | ||
const key = match[2].trim(); | ||
const value = match[3].trim(); | ||
this.prefs[key] = value; | ||
}; | ||
} | ||
|
||
public async read(path: string) { | ||
const content = await readFile(path, "utf-8"); | ||
this.parsePrefjs(content); | ||
} | ||
|
||
private renderPrefjs() { | ||
return Object.entries(this.prefs).map(([key, value]) => { | ||
if (!isNotNil(value)) | ||
return ""; | ||
|
||
let cleanValue = ""; | ||
if (typeof value === "boolean") { | ||
cleanValue = `${value}`; | ||
} | ||
else if (typeof value === "string") { | ||
cleanValue = `${value.replace("\n", "\\n")}`; | ||
} | ||
else if (typeof value === "number") { | ||
cleanValue = value.toString(); | ||
} | ||
|
||
return `${this.namespace}("${key}", ${cleanValue});`; | ||
}).filter(c => !!c).join("\n"); | ||
} | ||
|
||
public async write(path: string) { | ||
const content = this.renderPrefjs(); | ||
// console.log(content); | ||
await outputFile(path, content, "utf-8"); | ||
logger.debug("The <profile>/prefs.js has been modified."); | ||
} | ||
|
||
setPref(key: string, value: any) { | ||
this.prefs[key] = value; | ||
}; | ||
|
||
setPrefs(prefs: Prefs) { | ||
Object.entries(prefs).forEach(([key, value]) => { | ||
this.setPref(key, value); | ||
}); | ||
} | ||
|
||
getPref(key: string) { | ||
return this.prefs[key] ?? undefined; | ||
} | ||
|
||
getPrefs() { | ||
return this.prefs; | ||
} | ||
} |
Oops, something went wrong.