forked from samuelcarreira/linux-release-info
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
171 lines (149 loc) · 4.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const fs = require('fs')
const os = require('os')
/**
* Get OS release info from the node os module and augment that with information
* from '/etc/os-release', '/usr/lib/os-release', or '/etc/alpine-release'. The
* information in that file is distribution-dependent. If not Linux return only
* the node os module info.
*
* @returns info {object} via Promise | callback | return value
*
* the file property in the info object will be filled in with one of:
* - undefined, if not Linux
* - the file path (above) used
* - an Error instance if no file could be read
*/
function linuxOsInfo (opts) {
const outputData = {
type: os.type(),
platform: os.platform(),
hostname: os.hostname(),
arch: os.arch(),
release: os.release(),
file: undefined,
}
let mode = 'promise'
opts = opts || {}
const list = Array.isArray(opts.list) ? opts.list : defaultList
if (typeof opts.mode === 'function') {
mode = 'callback'
} else if (opts.mode === 'sync') {
mode = 'sync'
}
if (os.type() !== 'Linux') {
if (mode === 'promise') {
return Promise.resolve(outputData)
} else if (mode === 'callback') {
return opts.mode(null, outputData)
} else {
return outputData
}
}
if (mode === 'sync') {
return synchronousRead()
} else {
// return a Promise that can be ignored if caller expects a callback
return new Promise(asynchronousRead)
}
// loop through the file list synchronously
function synchronousRead () {
for (let i = 0; i < list.length; i++) {
let data
try {
data = fs.readFileSync(list[i].path, 'utf8')
list[i].parser(data, outputData)
outputData.file = list[i].path
return outputData
} catch (e) {
// accumulate errors?
}
}
outputData.file = new Error('linux-os-info - no file found')
return outputData
}
// loop through the file list on completion of async reads
function asynchronousRead (resolve, reject) {
let i = 0
function tryRead () {
if (i >= list.length) {
const e = new Error('linux-os-info - no file found')
outputData.file = e
mode === 'promise' ? resolve(outputData) : opts.mode(null, outputData)
} else {
// try to read the file.
const file = list[i].path
fs.readFile(file, 'utf8', (err, data) => {
if (err) {
i += 1
tryRead()
} else {
list[i].parser(data, outputData)
outputData.file = file
mode === 'promise' ? resolve(outputData) : opts.mode(null, outputData)
}
})
}
}
tryRead()
}
}
//
// the default list of files to try to read and their parsers.
// in theory this can be replaced, especially for testing purposes.
// but it's not documented at this time unless one is reading this.
//
const defaultList = [
{path: '/etc/os-release', parser: etcOsRelease},
{path: '/usr/lib/os-release', parser: usrLibOsRelease},
{path: '/etc/alpine-release', parser: etcAlpineRelease}
]
//
// helper functions to parse file data
//
function etcOsRelease (data, outputData) {
addOsReleaseToOutputData(data, outputData)
}
function usrLibOsRelease (data, outputData) {
addOsReleaseToOutputData(data, outputData)
}
// the alpine-release file only contains the version string
// so fill in the basics based on that.
function etcAlpineRelease (data, outputData) {
outputData.name = 'Alpine'
outputData.id = 'alpine'
outputData.version = data
outputData.version_id = data
}
function addOsReleaseToOutputData (data, outputData) {
const lines = data.split('\n')
lines.forEach(line => {
const index = line.indexOf('=');
// only look at lines with at least a one character key
if (index >= 1) {
// lowercase key and remove quotes on value
const key = line.slice(0, index).toLowerCase();
const value = line.slice(index + 1).replace(/"/g, '');
Object.defineProperty(outputData, key, {
value: value,
writable: true,
enumerable: true,
configurable: true
})
}
});
}
module.exports = linuxOsInfo
//
// a tiny bit of testing
//
if (require.main === module) {
/* eslint-disable no-console */
console.log('testing synchronous')
console.log('synchronous:', linuxOsInfo({mode: 'sync'}))
console.log('testing promise')
linuxOsInfo()
.then(r => console.log('promise:', r))
.catch(e => console.log('promise error:', e))
console.log('testing callback')
linuxOsInfo({mode: function (err, data) {console.log('callback:', data)}})
}