|
import fetch from '@webreflection/fetch'; |
|
|
|
import { interpreter } from './interpreters.js'; |
|
import { absoluteURL, resolve } from './utils.js'; |
|
import { parse } from './toml.js'; |
|
|
|
|
|
|
|
export const getConfigURLAndType = (config, configURL = './config.txt') => { |
|
let type = typeof config; |
|
if (type === 'string' && /\.(json|toml|txt)$/.test(config)) |
|
type = RegExp.$1; |
|
else |
|
config = configURL; |
|
return [absoluteURL(config), type]; |
|
}; |
|
|
|
const parseString = config => { |
|
try { |
|
return JSON.parse(config); |
|
} |
|
|
|
catch (_) { |
|
return parse(config); |
|
} |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getRuntime = (id, config, configURL, options = {}) => { |
|
if (config) { |
|
|
|
|
|
const [absolute, type] = getConfigURLAndType(config, configURL); |
|
if (type === 'json') { |
|
options = fetch(absolute).json(); |
|
} else if (type === 'toml') { |
|
options = fetch(absolute).text().then(parse); |
|
} else if (type === 'string') { |
|
options = parseString(config); |
|
} else if (type === 'object' && config) { |
|
options = config; |
|
} else if (type === 'txt' && typeof options === 'string') { |
|
options = parseString(options); |
|
} |
|
config = absolute; |
|
|
|
} |
|
return resolve(options).then(options => interpreter[id](options, config)); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getRuntimeID = (type, version = '') => |
|
`${type}@${version}`.replace(/@$/, ''); |
|
|