Skip to content

Commit

Permalink
Preparations to use javascript modules and other small fixes (#1050)
Browse files Browse the repository at this point in the history
* Preparing commit hook to accept mjs and cjs files, avoiding deleted files

* Renaming window-aux and app-config to .cjs

* Updating renderer electron imports

* Adding 'use strict' to all files

* Switching 'fs' import to use import

* Switching 'electron-store' import to use import

* Switching notification.js to use export

* Switching 'path' import to use import

* Switching 'electron' to use import

* Export section to have commas and be sorted, last line at end

* Adding all 3 js extensions to eslint target

* Small updates to cjs files to switch CRLF to LF, adding attributes
  • Loading branch information
araujoarthur0 authored Jan 8, 2024
1 parent f01717c commit c95578c
Show file tree
Hide file tree
Showing 54 changed files with 159 additions and 128 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*.bat text eol=crlf
*.cmd text eol=crlf
*.coffee text
*.cjs text eol=lf
*.css text
*.htm text diff=html
*.html text diff=html
Expand All @@ -21,6 +22,7 @@
*.less text
*.ls text
*.map text -diff
*.mjs text eol=lf
*.od text
*.onlydata text
*.php text diff=php
Expand Down
4 changes: 2 additions & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ get_changed_files()
while read -d $'\0' file
do
echo ${file}
done < <(git diff --staged -z --name-only -- "$@")
done < <(git diff --staged -z --name-only --diff-filter=d -- "$@")
}

readarray -t changed_css < <(get_changed_files 'css/*.css')
Expand All @@ -22,7 +22,7 @@ if [ ! -z "$changed_css" ]; then
pids="$pids $!"
fi

readarray -t changed_js < <(get_changed_files '*/*.js')
readarray -t changed_js < <(get_changed_files '*/*.cjs' '*/*.js' '*/*.mjs')
if [ ! -z "$changed_js" ]; then
npx eslint "${changed_js[@]}" &
pids="$pids $!"
Expand Down
6 changes: 3 additions & 3 deletions __tests__/__main__/import-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const {
validEntry
} = require('../../js/import-export');

const fs = require('fs');
const Store = require('electron-store');
const path = require('path');
import fs from 'fs';
import Store from 'electron-store';
import path from 'path';

describe('Import export', function()
{
Expand Down
4 changes: 3 additions & 1 deletion __tests__/__main__/main-window.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

const notification = require('../../js/notification.js');
const userPreferences = require('../../js/user-preferences.js');
const { savePreferences, defaultPreferences, resetPreferences } = userPreferences;
const { app, BrowserWindow, ipcMain } = require('electron');
import { app, BrowserWindow, ipcMain } from 'electron';

jest.mock('../../js/update-manager', () => ({
checkForUpdates: jest.fn(),
Expand Down
2 changes: 2 additions & 0 deletions __tests__/__main__/menus.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const { getContextMenuTemplate, getDockMenuTemplate, getEditMenuTemplate, getHelpMenuTemplate, getMainMenuTemplate, getViewMenuTemplate} = require('../../js/menus.js');

jest.mock('../../src/configs/i18next.config.js', () => ({
Expand Down
2 changes: 1 addition & 1 deletion __tests__/__main__/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const { createNotification, createLeaveNotification, updateDismiss, getDismiss } = require('../../js/notification.js');
const { getUserPreferences, savePreferences, resetPreferences } = require('../../js/user-preferences.js');
const { getDateStr } = require('../../js/date-aux.js');
const { app } = require('electron');
import { app } from 'electron';

function buildTimeString(now)
{
Expand Down
2 changes: 1 addition & 1 deletion __tests__/__main__/time-balance.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-undef */
'use strict';

const Store = require('electron-store');
import Store from 'electron-store';
import {
computeAllTimeBalanceUntil,
getFirstInputInDb,
Expand Down
10 changes: 6 additions & 4 deletions __tests__/__main__/update-manager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const ElectronStore = require('electron-store');
'use strict';

import Store from 'electron-store';
const { getDateStr } = require('../../js/date-aux');
const {shouldCheckForUpdates, checkForUpdates} = require('../../js/update-manager');

Expand Down Expand Up @@ -26,7 +28,7 @@ describe('js/update-manager.js', () =>
{
test('Should return true when was never checked', () =>
{
const store = new ElectronStore();
const store = new Store();
store.set('update-remind-me-after', false);
expect(shouldCheckForUpdates()).toBe(true);
});
Expand All @@ -35,15 +37,15 @@ describe('js/update-manager.js', () =>
{
const now = new Date();
now.setDate(now.getDate() - 1);
const store = new ElectronStore();
const store = new Store();
store.set('update-remind-me-after', getDateStr(now));
expect(shouldCheckForUpdates()).toBe(true);
});

test('Should return false when was checked today', () =>
{
const now = new Date();
const store = new ElectronStore();
const store = new Store();
store.set('update-remind-me-after', getDateStr(now));
expect(shouldCheckForUpdates()).toBe(false);
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/__main__/user-preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

const { booleanInputs, defaultPreferences, getDefaultWidthHeight, getPreferencesFilePath, getUserPreferences, savePreferences, showDay, switchCalendarView, notificationIsEnabled, getUserLanguage, getNotificationsInterval, repetitionIsEnabled, getUserPreferencesPromise, resetPreferences } = require('../../js/user-preferences');
const fs = require('fs');
import fs from 'fs';
const { themeOptions } = require('../../renderer/themes');
const { getLanguageMap, getLanguagesCodes, getLanguageName } = require('../../src/configs/app.config');

Expand Down
4 changes: 3 additions & 1 deletion __tests__/__main__/windows.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { BrowserWindow } = require('electron');
'use strict';

import { BrowserWindow } from 'electron';
const { getDateStr } = require('../../js/date-aux.js');
const windows = require('../../js/windows.js');
const {getWaiverWindow, tray, contextMenu, prefWindow, resetWindowsElements, openWaiverManagerWindow, getDialogCoordinates} = require('../../js/windows.js');
Expand Down
13 changes: 7 additions & 6 deletions __tests__/__renderer__/classes/BaseCalendar.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import ElectronStore from 'electron-store';
'use strict';

import Store from 'electron-store';
import { BaseCalendar } from '../../../renderer/classes/BaseCalendar.js';
import { generateKey } from '../../../js/date-db-formatter.js';
import { getUserPreferences, resetPreferences, savePreferences, switchCalendarView } from '../../../js/user-preferences.js';
const Store = require('electron-store');
const timeBalance = require('../../../js/time-balance');
import { calendarApi } from '../../../renderer/preload-scripts/calendar-api.js';

Expand Down Expand Up @@ -47,9 +48,9 @@ describe('BaseCalendar.js', () =>
const mocks = {};
beforeEach(() =>
{
const flexibleStore = new ElectronStore({name: 'flexible-store'});
const flexibleStore = new Store({name: 'flexible-store'});
flexibleStore.clear();
const waivedWorkdays = new ElectronStore({name: 'waived-workdays'});
const waivedWorkdays = new Store({name: 'waived-workdays'});
waivedWorkdays.clear();
ExtendedClass.prototype._initCalendar = () => {};
ExtendedClass.prototype._getTargetDayForAllTimeBalance = () => {};
Expand Down Expand Up @@ -120,10 +121,10 @@ describe('BaseCalendar.js', () =>
test('Should build with default internal store values', async(done) =>
{
ExtendedClass.prototype._initCalendar = () => { done(); };
const flexibleStore = new ElectronStore({name: 'flexible-store'});
const flexibleStore = new Store({name: 'flexible-store'});
flexibleStore.set('flexible', 'store');

const waivedWorkdays = new ElectronStore({name: 'waived-workdays'});
const waivedWorkdays = new Store({name: 'waived-workdays'});
waivedWorkdays.set('2022-01-01', {
reason: 'dismiss',
hours: '10:00'
Expand Down
2 changes: 2 additions & 0 deletions __tests__/__renderer__/classes/CalendarFactory.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

import { CalendarFactory } from '../../../renderer/classes/CalendarFactory.js';
import { FlexibleDayCalendar } from '../../../renderer/classes/FlexibleDayCalendar.js';
import { FlexibleMonthCalendar } from '../../../renderer/classes/FlexibleMonthCalendar.js';
Expand Down
3 changes: 2 additions & 1 deletion __tests__/__renderer__/classes/FlexibleDayCalendar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-undef */
'use strict';

const Store = require('electron-store');
import Store from 'electron-store';
import { computeAllTimeBalanceUntilAsync } from '../../../js/time-balance.js';
import { defaultPreferences } from '../../../js/user-preferences.js';
import { CalendarFactory } from '../../../renderer/classes/CalendarFactory.js';
Expand Down
2 changes: 1 addition & 1 deletion __tests__/__renderer__/classes/FlexibleMonthCalendar.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-undef */
'use strict';

const Store = require('electron-store');
import Store from 'electron-store';
import { computeAllTimeBalanceUntilAsync } from '../../../js/time-balance.js';
import { defaultPreferences } from '../../../js/user-preferences.js';
import { CalendarFactory } from '../../../renderer/classes/CalendarFactory.js';
Expand Down
2 changes: 2 additions & 0 deletions __tests__/__renderer__/notification-channel.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const notificationChannel = require('../../renderer/notification-channel.js');

describe('Notifications channel', () =>
Expand Down
4 changes: 2 additions & 2 deletions __tests__/__renderer__/preferences.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable no-undef */
'use strict';

const fs = require('fs');
const path = require('path');
import fs from 'fs';
import path from 'path';
const {
defaultPreferences,
getPreferencesFilePath,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/__renderer__/user-preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
isNotBoolean,
isNotificationInterval,
} = require('../../js/user-preferences');
const fs = require('fs');
import fs from 'fs';

describe('Should return false if the value is not boolean type', () =>
{
Expand Down
6 changes: 3 additions & 3 deletions __tests__/__renderer__/window-aux.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable no-undef */
'use strict';

const path = require('path');
import path from 'path';
const BrowserWindow = require('@electron/remote').BrowserWindow;
import * as windowAux from '../../js/window-aux.js';
import * as windowAux from '../../js/window-aux.cjs';

describe('window-aux.js Testing', function()
describe('window-aux.cjs Testing', function()
{
process.env.NODE_ENV = 'test';

Expand Down
8 changes: 4 additions & 4 deletions __tests__/__renderer__/workday-waiver.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable no-undef */
'use strict';

const Store = require('electron-store');
const fs = require('fs');
const path = require('path');
import Store from 'electron-store';
import fs from 'fs';
import path from 'path';
const Holidays = require('date-holidays');
/* eslint-disable-next-line no-global-assign */
window.$ = require('jquery');
Expand Down Expand Up @@ -150,7 +150,7 @@ async function testWaiverCount(expected)
expect($('#waiver-list-table tbody')[0].rows.length).toBe(expected);
}

jest.mock('../../js/window-aux.js');
jest.mock('../../js/window-aux.cjs');

describe('Test Workday Waiver Window', function()
{
Expand Down
6 changes: 3 additions & 3 deletions esm-main.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*eslint-disable no-useless-escape*/
'use strict';

const { app, ipcMain } = require('electron');
import { app, ipcMain } from 'electron';
const { createWindow, createMenu, getMainWindow, triggerStartupDialogs } = require('./js/main-window');
const { createNotification } = require('./js/notification');
const { openWaiverManagerWindow } = require('./js/windows.js');
const { setupI18n, getCurrentTranslation, setLanguageChangedCallback } = require('./src/configs/i18next.config.js');
const { handleSquirrelEvent } = require('./js/squirrel.js');
const { showAlert, showDialogSync } = require('./js/window-aux.js');
const { showAlert, showDialogSync } = require('./js/window-aux.cjs');

import { appConfig } from './js/app-config.js';
import { appConfig } from './js/app-config.cjs';
import { setupCalendarStore } from './main/calendar-aux.js';

if (appConfig.win32)
Expand Down
8 changes: 4 additions & 4 deletions js/app-config.js → js/app-config.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const { app } = require('electron');
const path = require('path');
const os = require('os');
const path = require('path');

const macOS = process.platform === 'darwin';
const win32 = process.platform === 'win32';
Expand All @@ -29,7 +29,7 @@ function getDetails()
return `Version: ${version}\nElectron: ${electronVersion}\nChrome: ${chromeVersion}\nNode.js: ${nodeVersion}\nOS: ${OSInfo}`;
}

export {
module.exports = {
appConfig,
getDetails
};
getDetails,
};
4 changes: 3 additions & 1 deletion js/date-aux.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ function getCurrentDateTimeStr()
}

export {
getDateStr, getMonthLength, getCurrentDateTimeStr
getCurrentDateTimeStr,
getDateStr,
getMonthLength,
};
4 changes: 2 additions & 2 deletions js/date-db-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ function generateKey(year, month, day)
}

export {
generateKey,
};
generateKey
};
2 changes: 1 addition & 1 deletion js/date-to-string-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ function getMonthName(languageData, monthIndex)

export {
getDayAbbr,
getMonthName
getMonthName,
};
6 changes: 4 additions & 2 deletions js/demo-generator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const { hourMinToHourFormatted, sumTime } = require('./time-math.js');
const { generateKey } = require('./date-db-formatter.js');
const Store = require('electron-store');
import Store from 'electron-store';

/**
* Returns a random integer between min (inclusive) and max (inclusive), rounding up to closest multiple of 5
Expand Down Expand Up @@ -66,4 +68,4 @@ function generateDemoInformation(dateFromStr, dateToStr, workingDays, usualTimes

module.exports = {
generateDemoInformation
};
};
8 changes: 4 additions & 4 deletions js/import-export.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*eslint-disable no-prototype-builtins*/
'use strict';

const Store = require('electron-store');
const fs = require('fs');
import Store from 'electron-store';
import fs from 'fs';

import { validateTime } from './time-math.js';
import { generateKey } from './date-db-formatter.js';
Expand Down Expand Up @@ -221,8 +221,8 @@ function migrateFixedDbToFlexible()
}

module.exports = {
importDatabaseFromFile,
exportDatabaseToFile,
importDatabaseFromFile,
migrateFixedDbToFlexible,
validEntry
validEntry,
};
Loading

0 comments on commit c95578c

Please sign in to comment.