UNPKG

3.24 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _fs = require('fs');
8
9var _envfile = require('envfile');
10
11var _envfile2 = _interopRequireDefault(_envfile);
12
13var _flowRuntime = require('flow-runtime');
14
15var _flowRuntime2 = _interopRequireDefault(_flowRuntime);
16
17function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
19const getModelInfo = () => {
20 try {
21 return (0, _fs.readFileSync)('/proc/device-tree/model', { encoding: 'utf-8' });
22 } catch (err) {
23 return '';
24 }
25};
26
27const getHardwareInfo = () => {
28 let cpuInfo;
29 try {
30 cpuInfo = (0, _fs.readFileSync)('/proc/cpuinfo', { encoding: 'utf-8' });
31 } catch (e) {
32 return {};
33 }
34
35 const data = new Map(cpuInfo.split('\n').map(line => line.replace(/\t/g, '')).filter(line => line.length).map(line => {
36 let _lineType = _flowRuntime2.default.string();
37
38 _flowRuntime2.default.param('line', _lineType).assert(line);
39
40 const splitted = line.split(':');
41 return [splitted[0].trim(), splitted.slice(1).join(':').trim()];
42 }));
43
44 return {
45 model: data.get('Hardware'),
46 serial: data.get('Serial'),
47 revision: data.get('Revision')
48 };
49};
50
51const getOsInfo = () => {
52 const snakeCaseToCamelCase = s => {
53 let _sType = _flowRuntime2.default.string();
54
55 _flowRuntime2.default.param('s', _sType).assert(s);
56
57 return s.toLowerCase().replace(/(_\w)/g, m => m[1].toUpperCase());
58 };
59
60 try {
61 const parsed = _envfile2.default.parseFileSync('/etc/os-release');
62 const osInfo = {};
63 Object.keys(parsed).forEach(key => {
64 osInfo[snakeCaseToCamelCase(key)] = parsed[key];
65 });
66
67 return osInfo;
68 } catch (err) {
69 console.warn(err);
70 return {
71 id: 'unknown'
72 };
73 }
74};
75
76const isPi = model => model.startsWith('Raspberry Pi');
77
78const rpiVersion = () => {
79 try {
80 // eslint-disable-next-line
81 return require('raspi-ver');
82 } catch (err) {
83 return null;
84 }
85};
86
87const detect = () => {
88 const hardwareInfo = getHardwareInfo();
89 const osInfo = getOsInfo();
90 const model = getModelInfo();
91
92 let user;
93
94 if (osInfo.id === 'osmc') {
95 user = 'osmc';
96 }
97
98 if (isPi(model)) {
99 return {
100 id: 'rpi',
101 name: 'Raspberry PI',
102 user: user || 'pi',
103 info: rpiVersion(),
104 os: osInfo,
105 hardware: hardwareInfo,
106 touchscreen: 'FT5406 memory based driver',
107 sound: {
108 type: 'alsamixer',
109 jack: [3, 1],
110 hdmi: [3, 2]
111 }
112 };
113 }
114
115 if (hardwareInfo.model && hardwareInfo.model.startsWith('Rockchip')) {
116 return {
117 id: 'tkb',
118 name: 'Tinker Board',
119 user: 'linaro',
120 os: osInfo,
121 hardware: hardwareInfo,
122 touchscreen: 'fts_ts',
123 sound: {
124 type: 'pulseaudio',
125 jack: [2],
126 hdmi: [3]
127 }
128 };
129 }
130
131 if (hardwareInfo.model === 'Vero4K') {
132 return {
133 id: 'vero4k',
134 name: 'Vero4K',
135 user,
136 os: osInfo,
137 hardware: hardwareInfo
138 };
139 }
140
141 return {
142 id: 'unknown',
143 name: 'Unknown',
144 os: osInfo,
145 hardware: hardwareInfo
146 };
147};
148
149let cache;
150
151exports.default = function detectBoard() {
152 if (!cache) cache = detect();
153 return cache;
154};
155//# sourceMappingURL=detectBoard.js.map
\No newline at end of file