mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-24 07:46:41 +00:00
Convert os-release module to typescript
Change-type: patch Signed-off-by: Cameron Diver <cameron@resin.io>
This commit is contained in:
parent
c5acb2f66d
commit
c9904d8b5d
@ -1,34 +0,0 @@
|
||||
Promise = require 'bluebird'
|
||||
fs = require 'fs'
|
||||
_ = require 'lodash'
|
||||
|
||||
exports.getOSReleaseField = (path, field) ->
|
||||
try
|
||||
releaseData = fs.readFileSync(path)
|
||||
lines = releaseData.toString().split('\n')
|
||||
releaseItems = {}
|
||||
for line in lines
|
||||
[ key, val ] = line.split('=')
|
||||
releaseItems[_.trim(key)] = _.trim(val)
|
||||
if !releaseItems[field]?
|
||||
throw new Error("Field #{field} not available in #{path}")
|
||||
# Remove enclosing quotes: http://stackoverflow.com/a/19156197/2549019
|
||||
return releaseItems[field].replace(/^"(.+(?="$))"$/, '$1')
|
||||
catch err
|
||||
console.log('Could not get OS release field: ', err.message)
|
||||
return undefined
|
||||
|
||||
|
||||
exports.getOSVersionSync = (path) ->
|
||||
exports.getOSReleaseField(path, 'PRETTY_NAME')
|
||||
|
||||
exports.getOSVersion = (path) ->
|
||||
Promise.try ->
|
||||
exports.getOSVersionSync(path)
|
||||
|
||||
exports.getOSVariantSync = (path) ->
|
||||
exports.getOSReleaseField(path, 'VARIANT_ID')
|
||||
|
||||
exports.getOSVariant = (path) ->
|
||||
Promise.try ->
|
||||
exports.getOSVariantSync(path)
|
47
src/lib/os-release.ts
Normal file
47
src/lib/os-release.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import * as Bluebird from 'bluebird';
|
||||
import * as fs from 'fs';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
// FIXME: Don't use synchronous file reading and change call sites to support a promise
|
||||
function getOSReleaseField(path: string, field: string): string | undefined {
|
||||
try {
|
||||
const releaseData = fs.readFileSync(path, 'utf-8');
|
||||
const lines = releaseData.split('\n');
|
||||
const releaseItems: { [field: string]: string} = { };
|
||||
|
||||
for (const line of lines) {
|
||||
const [ key, value ] = line.split('=');
|
||||
releaseItems[_.trim(key)] = _.trim(value);
|
||||
}
|
||||
|
||||
if (releaseItems[field] == null) {
|
||||
throw new Error(`Field ${field} not available in ${path}`);
|
||||
}
|
||||
|
||||
// Remove enclosing quotes: http://stackoverflow.com/a/19156197/2549019
|
||||
return releaseItems[field].replace(/^"(.+(?="$))"$/, '$1');
|
||||
} catch(err) {
|
||||
console.log('Could not get OS release field: ', err.message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
export function getOSVersionSync(path: string): string | undefined {
|
||||
return getOSReleaseField(path, 'PRETTY_NAME');
|
||||
}
|
||||
|
||||
export function getOSVersion(path: string): Bluebird<string | undefined> {
|
||||
return Bluebird.try(() => {
|
||||
return getOSVersionSync(path);
|
||||
});
|
||||
}
|
||||
|
||||
export function getOSVariantSync(path: string): string | undefined {
|
||||
return getOSReleaseField(path, 'VARIANT_ID');
|
||||
}
|
||||
|
||||
export function getOSVariant(path: string): Bluebird<string | undefined> {
|
||||
return Bluebird.try(() => {
|
||||
return getOSVariantSync(path);
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user