UNPKG

pooliot-client

Version:
110 lines (86 loc) 2.35 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _fs = require('fs'); var _envfile = require('envfile'); var _envfile2 = _interopRequireDefault(_envfile); var _flowRuntime = require('flow-runtime'); var _flowRuntime2 = _interopRequireDefault(_flowRuntime); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const getHardwareInfo = () => { let cpuInfo; try { cpuInfo = (0, _fs.readFileSync)('/proc/cpuinfo', { encoding: 'utf8' }); } catch (e) { // if this fails, this is probably not a pi return null; } const model = cpuInfo.split('\n').map(line => line.replace(/\t/g, '')).filter(line => line.length > 0).map(line => line.split(':')).map(pair => pair.map(entry => entry.trim())).filter(pair => pair[0] === 'Hardware'); if (!model || model.length === 0) { return null; } return model[0][1]; }; const getOsInfo = () => { const snakeCaseToCamelCase = s => { let _sType = _flowRuntime2.default.string(); _flowRuntime2.default.param('s', _sType).assert(s); return s.toLowerCase().replace(/(_\w)/g, m => m[1].toUpperCase()); }; try { const parsed = _envfile2.default.parseFileSync('/etc/os-release'); const osInfo = {}; Object.keys(parsed).forEach(key => { osInfo[snakeCaseToCamelCase(key)] = parsed[key]; }); return osInfo; } catch (err) { console.warn(err); return { id: 'unknown' }; } }; const PI_MODEL_NO = ['BCM2708', 'BCM2709', 'BCM2710']; const isPi = model => PI_MODEL_NO.includes(model); const rpiVersion = () => { try { // eslint-disable-next-line return require('raspi-ver'); } catch (err) { return null; } }; const detect = () => { const model = getHardwareInfo(); const osInfo = getOsInfo(); if (isPi(model)) { return { id: 'rpi', name: 'Raspberry PI', user: 'pi', info: rpiVersion(), os: osInfo }; } if (model && model.startsWith('Rockchip')) { return { id: 'tkb', name: 'Tinker Board', user: 'linaro', os: osInfo }; } return { id: 'unknown', name: 'Unknown', os: osInfo }; }; let cache; exports.default = function detectBoard() { if (!cache) cache = detect(); return cache; }; //# sourceMappingURL=detectBoard.js.map