Skip to content

Commit

Permalink
ADD edit shortcuts support
Browse files Browse the repository at this point in the history
  • Loading branch information
RecuencoJones committed Apr 18, 2024
1 parent 417e612 commit 9e11ab0
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Cheesecuts

Apparently I had to write \~400 lines of code to display a blank page with a summary of shortcuts. Yes.
Apparently I had to write \~500 lines of code to display a blank page with a summary of shortcuts. Yes.

## Usage

Expand Down
145 changes: 136 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"fix-path": "3.0.0",
"i18next": "22.0.6",
"js-yaml": "4.1.0",
"open-editor": "3.0.0",
"winston": "3.8.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/background/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ const projectRoot = resolve(__dirname, '..', '..');
const shortcutFiles = [ 'shortcuts.yaml', 'shortcuts.yml' ];
const logFile = 'log.txt';

module.exports = { isDevelopment, userAppHome, projectRoot, shortcutsFiles: shortcutFiles, logFile };
module.exports = { isDevelopment, userAppHome, projectRoot, shortcutFiles, logFile };
10 changes: 8 additions & 2 deletions src/background/handlers/shortcuts.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
const { logger } = require('../logger');
const { getShortcuts } = require('../services/shortcuts');
const { getShortcuts, editShortcuts } = require('../services/shortcuts');

/**
* @param {import('electron').IpcMain} ipc bus
*/
function useShortcutsHandler(ipc) {
ipc.handle('getShortcuts', async () => {
logger.info('[ipc] Get shortcuts');
logger.info('[ipc.getShortcuts] Get shortcuts');
const groups = await getShortcuts();

return groups;
});

ipc.handle('editShortcuts', async () => {
logger.info('[ipc.editShortcuts] Edit shortcuts');

await editShortcuts();
});
}

module.exports = { useShortcutsHandler };
3 changes: 2 additions & 1 deletion src/background/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ contextBridge.exposeInMainWorld('os', process.platform);

contextBridge.exposeInMainWorld('api', {
...useLiteralInvokify(
'getShortcuts'
'getShortcuts',
'editShortcuts'
)
});
29 changes: 24 additions & 5 deletions src/background/services/shortcuts.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { resolve } = require('path');
const { readFile } = require('fs/promises');
const { readFile, stat, writeFile } = require('fs/promises');
const { load, dump } = require('js-yaml');
const edit = require('open-editor');
const { shell } = require('electron');
const { logger } = require('../logger');
const { userAppHome, projectRoot, shortcutsFiles } = require('../constants');
const { load } = require('js-yaml');
const { userAppHome, projectRoot, shortcutFiles } = require('../constants');

function getDefaultConfig() {
return {
Expand All @@ -13,7 +15,7 @@ function getDefaultConfig() {
async function loadShortcutsConfig() {
let config;

const possibleFiles = [ userAppHome, projectRoot ].map((dir) => shortcutsFiles.map((file) => resolve(dir, file))).flat();
const possibleFiles = [ userAppHome, projectRoot ].map((dir) => shortcutFiles.map((file) => resolve(dir, file))).flat();

for (const filename of possibleFiles) {
const file = resolve(userAppHome, filename);
Expand Down Expand Up @@ -70,4 +72,21 @@ async function getShortcuts() {
return groupShortcuts(config);
}

module.exports = { getShortcuts };
async function editShortcuts() {
const file = resolve(userAppHome, shortcutFiles[0]);

// no hate for vi, but for some reason it is set as $EDITOR by some dependency
// and it won't open for editing. Meh.
if (process.env.EDITOR && process.env.EDITOR !== 'vi') {
edit([ file ]);
} else {
logger.warn('[ipc.editShortcuts] EDITOR is not defined, opening folder instead');
shell.openPath(userAppHome);

await stat(file).catch(() => {
writeFile(file, dump(getDefaultConfig()), 'utf8');
});
}
}

module.exports = { getShortcuts, editShortcuts };
Loading

0 comments on commit 9e11ab0

Please sign in to comment.