diff options
-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 () { |