diff options
author | Nick Coutsos <[email protected]> | 2021-12-14 18:00:24 -0500 |
---|---|---|
committer | Nick Coutsos <[email protected]> | 2021-12-14 18:00:24 -0500 |
commit | c9d70d5734e3496f992f3a4d9aa3c046526b1592 (patch) | |
tree | f3daebdd43a0259da5ddf0a3885dcaf706155b21 /api/services | |
parent | 4aa3a4f566b1cceddc763c287ba22ebc365030d2 (diff) | |
download | keymap-editor-c9d70d5734e3496f992f3a4d9aa3c046526b1592.tar.gz keymap-editor-c9d70d5734e3496f992f3a4d9aa3c046526b1592.zip |
Generate an empty keymap if keymap.json can't be found
Diffstat (limited to 'api/services')
-rw-r--r-- | api/services/github/files.js | 21 | ||||
-rw-r--r-- | api/services/zmk/local-source.js | 14 |
2 files changed, 33 insertions, 2 deletions
diff --git a/api/services/github/files.js b/api/services/github/files.js index 65c4c83..9c2b6f9 100644 --- a/api/services/github/files.js +++ b/api/services/github/files.js @@ -16,11 +16,30 @@ class MissingRepoFile extends Error { async function fetchKeyboardFiles (installationId, repository, branch) { const { data: { token: installationToken } } = await auth.createInstallationToken(installationId) const { data: info } = await fetchFile(installationToken, repository, 'config/info.json', { raw: true, branch }) - const { data: keymap } = await fetchFile(installationToken, repository, 'config/keymap.json', { raw: true, branch }) + const keymap = await fetchKeymap(installationToken, repository, branch) const originalCodeKeymap = await findCodeKeymap(installationToken, repository, branch) return { info, keymap, originalCodeKeymap } } +async function fetchKeymap (installationToken, repository, branch) { + try { + const { data : keymap } = await fetchFile(installationToken, repository, 'config/keymap.json', { raw: true, branch }) + return keymap + } catch (err) { + if (err instanceof MissingRepoFile) { + return { + keyboard: 'unknown', + keymap: 'unknown', + layout: 'unknown', + layer_names: ['default'], + layers: [[]] + } + } else { + throw err + } + } +} + async function fetchFile (installationToken, repository, path, options = {}) { const { raw = false, branch = null } = options const url = `/repos/${repository}/contents/${path}` diff --git a/api/services/zmk/local-source.js b/api/services/zmk/local-source.js index 11b9859..a0db079 100644 --- a/api/services/zmk/local-source.js +++ b/api/services/zmk/local-source.js @@ -6,6 +6,14 @@ const { parseKeymap } = require('./keymap') const ZMK_PATH = path.join(__dirname, '..', '..', '..', 'zmk-config') const KEYBOARD = 'dactyl' +const EMPTY_KEYMAP = { + keyboard: 'unknown', + keymap: 'unknown', + layout: 'unknown', + layer_names: ['default'], + layers: [[]] +} + function loadBehaviors() { return JSON.parse(fs.readFileSync(path.join(__dirname, 'data', 'zmk-behaviors.json'))) } @@ -21,7 +29,11 @@ function loadLayout (layout = 'LAYOUT') { function loadKeymap () { const keymapPath = path.join(ZMK_PATH, 'config', 'keymap.json') - return parseKeymap(JSON.parse(fs.readFileSync(keymapPath))) + const keymapContent = fs.existsSync(keymapPath) + ? JSON.parse(fs.readFileSync(keymapPath)) + : EMPTY_KEYMAP + + return parseKeymap(keymapContent) } function findKeymapFile () { |