Improved interface testing
This commit is contained in:
parent
7d841b2f5f
commit
1f3ecb5c58
10 changed files with 407 additions and 120 deletions
44
src/io.js
44
src/io.js
|
@ -1,4 +1,3 @@
|
|||
const json5 = require('json5')
|
||||
const yaml = require('js-yaml')
|
||||
const makerjs = require('makerjs')
|
||||
const jscad = require('@jscad/openjscad')
|
||||
|
@ -10,38 +9,43 @@ const kle = require('./kle')
|
|||
exports.interpret = (raw, logger) => {
|
||||
let config = raw
|
||||
let format = 'OBJ'
|
||||
if (a.type(raw)() != 'object') {
|
||||
if (a.type(raw)() == 'string') {
|
||||
try {
|
||||
config = yaml.safeLoad(raw)
|
||||
format = 'YAML'
|
||||
} catch (yamlex) {
|
||||
try {
|
||||
config = json5.parse(raw)
|
||||
format = 'JSON'
|
||||
} catch (jsonex) {
|
||||
try {
|
||||
config = new Function(raw)()
|
||||
format = 'JS Code'
|
||||
} catch (codeex) {
|
||||
logger('YAML exception:', yamlex)
|
||||
logger('JSON exception:', jsonex)
|
||||
logger('Code exception:', codeex)
|
||||
throw new Error('Input is not valid YAML, JSON, or JS Code!')
|
||||
}
|
||||
config = new Function(raw)()
|
||||
a.assert(
|
||||
a.type(config)() == 'object',
|
||||
'Input JS Code doesn\'t resolve into an object!'
|
||||
)
|
||||
format = 'JS'
|
||||
} catch (codeex) {
|
||||
logger('YAML exception:', yamlex)
|
||||
logger('Code exception:', codeex)
|
||||
throw new Error('Input is not valid YAML, JSON, or JS Code!')
|
||||
}
|
||||
}
|
||||
if (!config) {
|
||||
throw new Error('Input appears to be empty!')
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// assume it's KLE and try to convert it
|
||||
// if it's not, it'll throw anyway
|
||||
return kle.convert(config, logger)
|
||||
config = kle.convert(config, logger)
|
||||
format = 'KLE'
|
||||
} catch (kleex) {
|
||||
return [config, format]
|
||||
// nope... nevermind
|
||||
}
|
||||
|
||||
if (a.type(config)() != 'object') {
|
||||
throw new Error('Input doesn\'t resolve into an object!')
|
||||
}
|
||||
|
||||
if (!Object.keys(config).length) {
|
||||
throw new Error('Input appears to be empty!')
|
||||
}
|
||||
|
||||
return [config, format]
|
||||
}
|
||||
|
||||
exports.twodee = (model, debug) => {
|
||||
|
|
13
src/kle.js
13
src/kle.js
|
@ -1,18 +1,19 @@
|
|||
const u = require('./utils')
|
||||
const kle = require('kle-serial')
|
||||
const json5 = require('json5')
|
||||
const yaml = require('js-yaml')
|
||||
|
||||
exports.convert = (config, logger) => {
|
||||
const keyboard = kle.Serial.deserialize(config)
|
||||
const result = {points: {zones: {}}, pcbs: {main: {}}}
|
||||
|
||||
// if the keyboard notes are valid JSON, they get added to each key as metadata
|
||||
let meta = {}
|
||||
// if the keyboard notes are valid YAML/JSON, they get added to each key as metadata
|
||||
let meta
|
||||
try {
|
||||
meta = json5.parse(keyboard.meta.notes)
|
||||
meta = yaml.load(keyboard.meta.notes)
|
||||
} catch (ex) {
|
||||
// notes were not valid JSON, oh well...
|
||||
// notes were not valid YAML/JSON, oh well...
|
||||
}
|
||||
meta = meta || {}
|
||||
|
||||
let index = 1
|
||||
for (const key of keyboard.keys) {
|
||||
|
@ -68,5 +69,5 @@ exports.convert = (config, logger) => {
|
|||
result.points.zones[id] = converted
|
||||
}
|
||||
|
||||
return [result, 'KLE']
|
||||
return result
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue