Skip to content

Commit

Permalink
fix: prefs.js parse, dont generate Zotero.Prefs types
Browse files Browse the repository at this point in the history
  • Loading branch information
northword committed Jan 6, 2025
1 parent 740dcaa commit b3ae37e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/scaffold/src/core/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export default class Build extends Base {
const matchs = [...content.matchAll(HTML_PREFERENCE_PATTERN)];
for (const match of matchs) {
const [matched, key] = match;
if (!prefsWithoutPrefix[key] && !prefsWithoutPrefix[key]) {
if (!(key in prefsWithoutPrefix) && !(key in prefsWithoutPrefix)) {
this.logger.warn(`preference key '${key}' in ${path.replace(`${dist}/`, "")} not init in prefs.js`);
continue;
}
Expand Down
64 changes: 39 additions & 25 deletions packages/scaffold/src/utils/prefs-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { readFile } from "node:fs/promises";
import { isNotNil } from "es-toolkit";
import { outputFile } from "fs-extra/esm";
import { logger } from "./log.js";

Expand All @@ -20,27 +19,14 @@ export class PrefsManager {
for (const match of matches) {
const key = match[2].trim();
const value = match[3].trim();
this.prefs[key] = value;

this.setPref(key, value);
};
}

private render() {
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; // `${value.replace("\n", "\\n")}`;
}
else if (typeof value === "number") {
cleanValue = value.toString();
}

return `${this.namespace}("${key}", ${cleanValue});`;
return `${this.namespace}("${key}", ${value});`;
}).filter(c => !!c).join("\n");
}

Expand All @@ -57,7 +43,33 @@ export class PrefsManager {
}

setPref(key: string, value: any) {
this.prefs[key] = value;
let cleanValue: any;
if (value === null || value === undefined) {
if (key in this.prefs)
delete this.prefs[key];
else
return;
}
else if (value === "true") {
cleanValue = true;
}
else if (value === "false") {
cleanValue = false;
}
else if (typeof value === "boolean") {
cleanValue = value;
}
else if (typeof value === "string") {
cleanValue = value; // `${value.replace("\n", "\\n")}`;
}
else if (typeof value === "number") {
cleanValue = value;
}
else {
cleanValue = value;
}

this.prefs[key] = cleanValue;
};

setPrefs(prefs: Prefs) {
Expand Down Expand Up @@ -115,13 +127,15 @@ type PluginPrefKey<K extends keyof _PluginPrefsMap> = \`${prefix}.\${K}\`;
type PluginPrefsMap = {
[K in keyof _PluginPrefsMap as PluginPrefKey<K>]: _PluginPrefsMap[K]
};
declare namespace _ZoteroTypes {
interface Prefs {
get: <K extends keyof PluginPrefsMap>(key: K, global?: boolean) => PluginPrefsMap[K];
set: <K extends keyof PluginPrefsMap>(key: K, value: PluginPrefsMap[K], global?: boolean) => any;
}
}
`;
return dtsContent;
}

const _backup = `
// declare namespace _ZoteroTypes {
// interface Prefs {
// get: <K extends keyof PluginPrefsMap>(key: K, global?: boolean) => PluginPrefsMap[K];
// set: <K extends keyof PluginPrefsMap>(key: K, value: PluginPrefsMap[K], global?: boolean) => any;
// }
// }
`;

0 comments on commit b3ae37e

Please sign in to comment.