Repository: jiahaog/nativefier
File: app/src/components/menu.ts (TypeScript)

Code before:
      ],
    };
    (windowMenu.submenu as MenuItemConstructorOptions[]).push(
      {
        type: 'separator',
      },
      {
        label: 'Bring All to Front',
        role: 'front',
      },
    );
    menuTemplate = [electronMenu, editMenu, viewMenu, windowMenu, helpMenu];
  } else {
    menuTemplate = [editMenu, viewMenu, windowMenu, helpMenu];
  }

  try {
    const bookmarkConfigPath = path.join(__dirname, '..', 'bookmarks.json');
    if (fs.existsSync(bookmarkConfigPath)) {
      const bookmarksConfig = JSON.parse(
        fs.readFileSync(bookmarkConfigPath, 'utf-8'),
      );
      const bookmarksMenu: MenuItemConstructorOptions = {
        label: bookmarksConfig['menuLabel'],
        submenu: bookmarksConfig['bookmarks'].map((el) => {
          if (el['type'] === 'link') {
            let accelerator = null;
            if ('shortcut' in el) {
              accelerator = el['shortcut'];
            }
            return {
              label: el['title'],
              click: () => {
                gotoUrl(el['url']);
              },
              accelerator: accelerator,
            };
          } else if (el['type'] === 'separator') {
            return {
              type: 'separator',
            };
          }
        }),
      };
      menuTemplate.splice(menuTemplate.length - 2, 0, bookmarksMenu);
    }
  } catch (err) {
    console.warn(
      'Failed to load & parse bookmarks configuration JSON file.',
      err,
    );

Review comment (suggestion):
We have line space available, the abbreviation is unnecessary and may look weird to someone not used to it; please rename `el` to `bookmark`

Code after:
          label: 'Hide Others',
          accelerator: 'Cmd+Shift+H',
          role: 'hideOthers',
        },
        {
          label: 'Show All',
          role: 'unhide',
        },
        {
          type: 'separator',
        },
        {
          label: 'Quit',
          accelerator: 'Cmd+Q',
          click: appQuit,
        },
      ],
    };
    (windowMenu.submenu as MenuItemConstructorOptions[]).push(
      {
        type: 'separator',
      },
      {
        label: 'Bring All to Front',
        role: 'front',
      },
    );
    menuTemplate = [electronMenu, editMenu, viewMenu, windowMenu, helpMenu];
  } else {
    menuTemplate = [editMenu, viewMenu, windowMenu, helpMenu];
  }

  try {
    const bookmarkConfigPath = path.join(__dirname, '..', 'bookmarks.json');
    if (fs.existsSync(bookmarkConfigPath)) {
      const bookmarksMenuConfig: BookmarksMenuConfig = JSON.parse(
        fs.readFileSync(bookmarkConfigPath, 'utf-8'),
      );
      const bookmarksMenu: MenuItemConstructorOptions = {
        label: bookmarksMenuConfig.menuLabel,
        submenu: bookmarksMenuConfig.bookmarks.map((bookmark) => {
          if (bookmark.type === 'link') {
            if (!('title' in bookmark && 'url' in bookmark)) {
              throw Error(
                'All links in the bookmarks menu must have a title and url.',
              );
            }
            try {
              new URL(bookmark.url);
            } catch (_) {
              throw Error('Bookmark URL "' + bookmark.url + '"is invalid.');