aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNick Coutsos <[email protected]>2021-11-12 11:24:20 -0500
committerNick Coutsos <[email protected]>2021-11-12 11:24:20 -0500
commit1d9a8a4c675297976fca76738c454ac1ce84f051 (patch)
tree9cbf242537f88e9bad3d645b937ac08045884661
parentf8f593131d2be8309cd6b4b94f695ba8350eef74 (diff)
downloadkeymap-editor-1d9a8a4c675297976fca76738c454ac1ce84f051.tar.gz
keymap-editor-1d9a8a4c675297976fca76738c454ac1ce84f051.zip
Validate config/<name>.keymap existence on initial load
-rw-r--r--api/routes/github.js15
-rw-r--r--api/services/github/files.js22
2 files changed, 25 insertions, 12 deletions
diff --git a/api/routes/github.js b/api/routes/github.js
index 241b40f..640a8ca 100644
--- a/api/routes/github.js
+++ b/api/routes/github.js
@@ -15,7 +15,7 @@ const {
InvalidRepoError,
} = require('../services/github')
const { createInstallationToken } = require('../services/github/auth')
-const { MissingRepoFile } = require('../services/github/files')
+const { MissingRepoFile, findCodeKeymap } = require('../services/github/files')
const { parseKeymap, validateKeymapJson, KeymapValidationError } = require('../services/zmk/keymap')
const { validateInfoJson, InfoValidationError } = require('../services/zmk/layout')
@@ -103,11 +103,14 @@ const getKeyboardFiles = async (req, res, next) => {
const { branch } = req.query
try {
- const keyboardFiles = await fetchKeyboardFiles(installationId, repository, branch)
- validateInfoJson(keyboardFiles.info)
- validateKeymapJson(keyboardFiles.keymap)
- keyboardFiles.keymap = parseKeymap(keyboardFiles.keymap)
- res.json(keyboardFiles)
+ const { info, keymap } = await fetchKeyboardFiles(installationId, repository, branch)
+ validateInfoJson(info)
+ validateKeymapJson(keymap)
+
+ res.json({
+ info,
+ keymap: parseKeymap(keymap)
+ })
} catch (err) {
if (err instanceof MissingRepoFile) {
return res.status(400).json({
diff --git a/api/services/github/files.js b/api/services/github/files.js
index bf0cc89..65c4c83 100644
--- a/api/services/github/files.js
+++ b/api/services/github/files.js
@@ -17,8 +17,8 @@ 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 })
-
- return { info, keymap }
+ const originalCodeKeymap = await findCodeKeymap(installationToken, repository, branch)
+ return { info, keymap, originalCodeKeymap }
}
async function fetchFile (installationToken, repository, path, options = {}) {
@@ -40,16 +40,25 @@ async function fetchFile (installationToken, repository, path, options = {}) {
}
}
-async function commitChanges (installationId, repository, branch, layout, keymap) {
- const { data: { token: installationToken } } = await auth.createInstallationToken(installationId)
- const generatedKeymap = zmk.generateKeymap(layout, keymap)
-
+async function findCodeKeymap (installationToken, repository, branch) {
// Assume that the relevant files are under `config/` and not a complicated
// directory structure, and that there are fewer than 1000 files in this path
// (a limitation of GitHub's repo contents API).
const { data: directory } = await fetchFile(installationToken, repository, 'config', { branch })
const originalCodeKeymap = directory.find(file => file.name.toLowerCase().endsWith('.keymap'))
+ if (!originalCodeKeymap) {
+ throw new MissingRepoFile('config/*.keymap')
+ }
+
+ return originalCodeKeymap
+}
+
+async function commitChanges (installationId, repository, branch, layout, keymap) {
+ const { data: { token: installationToken } } = await auth.createInstallationToken(installationId)
+ const generatedKeymap = zmk.generateKeymap(layout, keymap)
+
+ const originalCodeKeymap = await findCodeKeymap(installationToken, repository, branch)
const { data: {sha, commit} } = await api.request({ url: `/repos/${repository}/commits/${branch}`, token: installationToken })
const { data: { sha: newTreeSha } } = await api.request({
@@ -99,5 +108,6 @@ async function commitChanges (installationId, repository, branch, layout, keymap
module.exports = {
MissingRepoFile,
fetchKeyboardFiles,
+ findCodeKeymap,
commitChanges
}