{"size": 1928, "ext": "js", "max_stars_repo_path": "renderer/src/core/reference/writeCustomResources.js", "max_stars_repo_name": "Bobbykumar706584/autographa", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "renderer/src/core/reference/writeCustomResources.js", "max_issues_repo_name": "Bobbykumar706584/autographa", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "renderer/src/core/reference/writeCustomResources.js", "max_forks_repo_name": "Bobbykumar706584/autographa", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 45.9047619048, "max_line_length": 141, "alphanum_fraction": 0.5466804979}
import localforage from 'localforage';

const path = require('path');

export async function writeCustomResources({ resourceUrl }) {
    const fs = window.require('fs');
    const newpath = localStorage.getItem('userPath');
      let currentUser;
      await localforage.getItem('userProfile').then((value) => {
        currentUser = value.username;
      });
      const file = path.join(newpath, 'autographa', 'users', currentUser, 'ag-user-settings.json');
      return new Promise((resolve) => {
      if (fs.existsSync(file)) {
            fs.readFile(file, (err, data) => {
                const agSettingsJson = JSON.parse(data);
                switch (resourceUrl.key) {
                    case 'tn':
                      if (agSettingsJson?.resources.door43.translationNotes.includes(resourceUrl.url) === false) {
                          agSettingsJson?.resources.door43.translationNotes.push({ name: resourceUrl.resourceName, url: resourceUrl.url });
                      }
                      break;
                    case 'tq':
                    if (agSettingsJson?.resources.door43.translationQuestions.includes(resourceUrl.url) === false) {
                        agSettingsJson?.resources.door43.translationQuestions.push({ name: resourceUrl.resourceName, url: resourceUrl.url });
                    }
                      break;
                    case 'twlm':
                    if (agSettingsJson?.resources.door43.translationWords.includes(resourceUrl.url) === false) {
                        agSettingsJson?.resources.door43.translationWords.push({ name: resourceUrl.resourceName, url: resourceUrl.url });
                    }
                      break;
                    default:
                      return null;
                  }
                  fs.writeFileSync(file, JSON.stringify(agSettingsJson));
                  resolve(agSettingsJson);
            });
      }
  });
}
