setNovelWordCountSettings.js

(async () => {
  const path = ".obsidian/plugins/novel-word-count/data.json";
  // ⭐ Update your desired settings here
  const settings = new Map([
    ["settings.countType", "filesize"],
    ["settings.abbreviateDescriptions", true],
    ["settings.alignment", "right"],
    ["settings.folderCountType", "none"],
    ["settings.rootCountType", "none"],
  ]);

  try {
    const text = await app.vault.adapter.read(path);
    const json = JSON.parse(text);

    function updateNestedIfExists(obj, keyPath, value) {
      const keys = keyPath.split(".");
      let current = obj;

      for (let i = 0; i < keys.length - 1; i++) {
        const key = keys[i];
        if (!Object.prototype.hasOwnProperty.call(current, key)) {
          console.error(`⚠️ Missing key "${key}" in path "${keyPath}". Update skipped.`);
          return;
        }
        if (typeof current[key] !== "object" || current[key] === null) {
          console.error(`⚠️ Key "${key}" in path "${keyPath}" is not an object. Update skipped.`);
          return;
        }
        current = current[key];
      }

      const lastKey = keys[keys.length - 1];
      if (!Object.prototype.hasOwnProperty.call(current, lastKey) && keys.length > 1) {
        console.error(`⚠️ Missing subkey "${lastKey}" in path "${keyPath}". Update skipped.`);
        return;
      }

      current[lastKey] = value;
      console.log(`✔️ Updated "${keyPath}" ->`, value);
    }

    settings.forEach((value, key) => updateNestedIfExists(json, key, value));

    await app.vault.adapter.write(path, JSON.stringify(json, null, 2));
    console.log(`✅ Settings written to ${path}`);

    if (confirm("Changes applied. Reload Obsidian now so plugins pick up the changes?")) {
      console.log("🔄 Reloading...");
      location.reload();
    } else {
      console.log("🛑 Reload canceled. Review console output; reload manually when ready.");
    }
  } catch (error) {
    console.error(`❌ Failed to update ${path}:`, error);
  }
})();