linux-release-info
Advanced tools
Comparing version 1.0.1 to 2.0.0
@@ -1,7 +0,7 @@ | ||
# Changelog | ||
All notable changes to this project will be documented in this file. | ||
## [2.0.0] - 2018-11-02 | ||
* Read file synchronously option | ||
* Search on '/etc/os-release', '/usr/lib/os-release' or in a specified custom file | ||
* Code improvements (now requires Node >= 8) | ||
## [Unreleased] | ||
## [1.0.0] - 2018-02-10 | ||
* First public version |
197
index.js
@@ -1,57 +0,172 @@ | ||
"use strict" | ||
/*! | ||
* linux-release-info | ||
* Get Linux release info (distribution name, version, arch, release, etc.) | ||
* from '/etc/os-release' or '/usr/lib/os-release' files and from native os | ||
* module. On Windows and Darwin platforms it only returns common node os module | ||
* info (platform, hostname, release, and arch) | ||
* | ||
* Licensed under MIT | ||
* Copyright (c) 2018 [Samuel Carreira] | ||
*/ | ||
const fs = require("fs") | ||
const os = require("os") | ||
const fs = require('fs'); | ||
const os = require('os'); | ||
const {promisify} = require('util'); | ||
const readFileAsync = promisify(fs.readFile); | ||
/** | ||
* Get OS release info from '/etc/os-release' file and from native os module | ||
* on Windows or Darwin it only returns common os module info | ||
/** | ||
* Export a list of os-release files | ||
* | ||
* (note: async reading using native fs module) | ||
* | ||
* | ||
* @returns {Object} info from the current os | ||
* @param {string} customFile optional custom complete filepath | ||
* @returns {array} list of os-release files | ||
*/ | ||
module.exports = releaseInfo => { | ||
return new Promise((resolve, reject) => { | ||
function osreleaseFileList(customFile) { | ||
const DEFAULT_OS_RELEASE_FILES = ['/etc/os-release', '/usr/lib/os-release']; | ||
let outputData = { | ||
type: os.type(), | ||
platform: os.platform(), | ||
hostname: os.hostname(), | ||
arch: os.arch(), | ||
release: os.release(), | ||
if (customFile !== null && typeof customFile === 'string') { | ||
return Array(customFile); | ||
} else { | ||
return DEFAULT_OS_RELEASE_FILES; | ||
} | ||
} | ||
/** | ||
* Get OS release info from 'os-release' file and from native os module | ||
* on Windows or Darwin it only returns common os module info | ||
* (uses native fs module) | ||
* | ||
* @param {string} options.mode read mode, possible values: 'async' and 'sync' default 'async' mode | ||
* @param {string} options.custom_file custom complete file path with os info default null/none | ||
* if not provided the system will search on the '/etc/os-release' | ||
* and '/usr/lib/os-release' files | ||
* @param {boolean} options.debug if true, show console debug messages. default is false | ||
* @returns {object} info from the current os | ||
*/ | ||
module.exports = function releaseInfo(options = {mode: 'async', custom_file: null, debug: false}) { | ||
async function readAsyncOsreleaseFile(searchOsreleaseFileList, options) { | ||
let fileData = null; | ||
for (os_release_file of searchOsreleaseFileList) { | ||
try { | ||
if (options.debug) { | ||
console.log(`Trying to read '${os_release_file}'...`); | ||
} | ||
fileData = await readFileAsync(os_release_file, 'binary'); | ||
if (options.debug) { | ||
console.log('Read data:\n' + fileData); | ||
} | ||
break; | ||
} catch (error) { | ||
if (options.debug) { | ||
console.error(error); | ||
} | ||
} | ||
} | ||
if (fileData === null) { | ||
throw new Error('Cannot read os-release file!'); | ||
//return getOsInfo(); | ||
} | ||
return formatFileData(getOsInfo(), fileData); | ||
} | ||
function readSyncOsreleaseFile(searchOsreleaseFileList, options) { | ||
let fileData = null; | ||
for (os_release_file of searchOsreleaseFileList) { | ||
try { | ||
if (options.debug) { | ||
console.log(`Trying to read '${os_release_file}'...`); | ||
} | ||
fileData = fs.readFileSync(os_release_file, 'binary'); | ||
if (options.debug) { | ||
console.log('Read data:\n' + fileData); | ||
} | ||
break; | ||
} catch (error) { | ||
if (options.debug) { | ||
console.error(error); | ||
} | ||
} | ||
} | ||
if (fileData === null) { | ||
throw new Error('Cannot read os-release file!'); | ||
//return getOsInfo(); | ||
} | ||
return formatFileData(getOsInfo(), fileData); | ||
} | ||
if (os.type() !== 'Linux') { | ||
return resolve(outputData) | ||
if (os.type() !== 'Linux') { | ||
if (options.mode === 'sync') { | ||
return getOsInfo(); | ||
} else { | ||
return Promise.resolve(getOsInfo()); | ||
} | ||
} | ||
fs.readFile('/etc/os-release', 'binary', (err, data) => { | ||
if (err) { | ||
//console.error(`Error reading OS release: ${err}`) | ||
return reject(err) | ||
} | ||
const searchOsreleaseFileList = osreleaseFileList(options.custom_file); | ||
//console.log(data) // debug result | ||
if (options.mode === 'sync') { | ||
return readSyncOsreleaseFile(searchOsreleaseFileList, options); | ||
} else { | ||
return Promise.resolve(readAsyncOsreleaseFile(searchOsreleaseFileList, options)); | ||
} | ||
} | ||
const lines = data.split('\n') | ||
lines.forEach(element => { | ||
const linedata = element.split('=') | ||
if (linedata.length === 2) { | ||
linedata[1] = linedata[1].replace(/"/g, '') // remove quotes | ||
Object.defineProperty(outputData, linedata[0].toLowerCase(), { | ||
value: linedata[1], | ||
writable: true, | ||
enumerable: true, | ||
configurable: true | ||
}) | ||
} | ||
/** | ||
* Format file data: convert data to object keys/values | ||
* | ||
* @param {object} sourceData Source object to be appended | ||
* @param {string} srcParseData Input file data to be parsed | ||
* @returns {object} Formated object | ||
*/ | ||
function formatFileData(sourceData, srcParseData) { | ||
const lines = srcParseData.split('\n'); | ||
lines.forEach(element => { | ||
const linedata = element.split('='); | ||
if (linedata.length === 2) { | ||
linedata[1] = linedata[1].replace(/["'\r]/gi, ''); // remove quotes and return character | ||
Object.defineProperty(sourceData, linedata[0].toLowerCase(), { | ||
value: linedata[1], | ||
writable: true, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
} | ||
}); | ||
return resolve(outputData) | ||
}) | ||
}) | ||
return sourceData; | ||
} | ||
/** | ||
* Get OS Basic Info | ||
* (uses node 'os' native module) | ||
* | ||
* @returns {object} os basic info | ||
*/ | ||
function getOsInfo() { | ||
const osInfo = { | ||
type: os.type(), | ||
platform: os.platform(), | ||
hostname: os.hostname(), | ||
arch: os.arch(), | ||
release: os.release() | ||
}; | ||
return osInfo; | ||
} |
{ | ||
"name": "linux-release-info", | ||
"version": "1.0.1", | ||
"description": "Get Linux release info (distribution name, version, arch, release, etc.) from '/etc/os-release' file and from native os module. On Windows and Darwin it only returns common node os module info (platform, hostname, release and arch)", | ||
"version": "2.0.0", | ||
"description": "Get Linux release info (distribution name, version, arch, release, etc.) from 'os-release' file and from native os module. On Windows and Darwin it only returns common node os module info (platform, hostname, release and arch)", | ||
"main": "index.js", | ||
@@ -27,3 +27,8 @@ "scripts": { | ||
}, | ||
"homepage": "https://github.com/samuelcarreira/linux-release-info#readme" | ||
"homepage": "https://github.com/samuelcarreira/linux-release-info#readme", | ||
"devDependencies": {}, | ||
"engines": { | ||
"node": ">=8.0" | ||
}, | ||
"dependencies": {} | ||
} |
linux-release-info | ||
================= | ||
Get Linux release info (distribution name, version, arch, release, etc.) from '/etc/os-release' file and from native os module. On Windows and Darwin platforms it only returns common node os module info (platform, hostname, release, and arch) | ||
Get Linux release info (distribution name, version, arch, release, etc.) from '/etc/os-release' or '/usr/lib/os-release' files and from native os module. On Windows and Darwin platforms it only returns common node os module info (platform, hostname, release, and arch) | ||
###Highlights | ||
### Highlights | ||
* Lightweight without any dependencies (only native Node modules) | ||
* Async file reading | ||
* Asynchronous file reading | ||
* Synchronous file reading (NEW feature version >= 2.0.0) | ||
* Specify custom os-release file | ||
## Installation | ||
``` | ||
npm install --save linux-release-info | ||
``` | ||
## Usage | ||
**Basic usage (async)** | ||
``` | ||
const releaseInfo = require('linux-release-info') | ||
const releaseInfo = require('linux-release-info'); | ||
releaseInfo() | ||
.then(result => { | ||
console.log(`You are using ${result.pretty_name} on a ${result.arch} machine`) // Distro name (only on linux) and arch info | ||
console.log(result) // all data | ||
console.log(`You are using ${result.pretty_name} on a ${result.arch} machine`); // Distro name (only on linux) and arch info | ||
}) | ||
@@ -32,4 +33,27 @@ .catch(err => console.error(`Error reading OS release info: ${err}`)) | ||
``` | ||
**Synchronous read** | ||
``` | ||
const releaseInfo = require('linux-release-info'); | ||
#### Sample outputs | ||
try { | ||
const infoSyncData = releaseInfo({mode: 'sync'}); | ||
console.log(`You are using ${infoSyncData.pretty_name} on a ${infoSyncData.arch} machine`) | ||
} catch (err) { | ||
console.error(`Error reading OS release info: ${err}`); | ||
} | ||
``` | ||
**Custom os_release file** | ||
``` | ||
const infoSyncData = releaseInfo({mode: 'sync', custom_file: '/home/user/os_release_sample'), debug: true}); | ||
``` | ||
## Options | ||
- `options` `<Object>` | ||
- `mode` `<string>`: 'sync' or 'async' mode. Default is *async* mode | ||
- `custom_file` `<string>`: custom complete filepath with os info. If not provided the system will search on the '/etc/os-release' and '/usr/lib/os-release' files. Default is `null/none` | ||
- `debug` `<boolean>`: show console debug messages. Default is `false` | ||
### Sample outputs | ||
**Linux** | ||
@@ -72,2 +96,27 @@ ``` | ||
``` | ||
**Linux (Fedora)** | ||
``` | ||
{ type: 'Linux', | ||
platform: 'linux', | ||
hostname: 'localhost-live', | ||
arch: 'x64', | ||
release: '4.13.9-300.fc27.x86_64', | ||
name: 'Fedora', | ||
version: '27 (Workstation Edition)', | ||
id: 'fedora', | ||
version_id: '27', | ||
pretty_name: 'Fedora 27 (Workstation Edition)', | ||
ansi_color: '0;34', | ||
cpe_name: 'cpe:/o:fedoraproject:fedora:27', | ||
home_url: 'https://fedoraproject.org/', | ||
support_url: 'https://fedoraproject.org/wiki/Communicating_and_getting_help', | ||
bug_report_url: 'https://bugzilla.redhat.com/', | ||
redhat_bugzilla_product: 'Fedora', | ||
redhat_bugzilla_product_version: '27', | ||
redhat_support_product: 'Fedora', | ||
redhat_support_product_version: '27', | ||
privacy_policy_url: 'https://fedoraproject.org/wiki/Legal:PrivacyPolicy', | ||
variant: 'Workstation Edition', | ||
variant_id: 'workstation' } | ||
``` | ||
**Windows** | ||
@@ -81,6 +130,21 @@ ``` | ||
``` | ||
**macOS** | ||
``` | ||
{ type: 'Darwin', | ||
platform: 'darwin', | ||
hostname: 'Macbook-Air.home', | ||
arch: 'x64', | ||
release: '16.0.0' } | ||
``` | ||
####Extra tip | ||
### Requirements | ||
You need Node.js v.8.x or greater to use the version 2.x of this module | ||
#### About the sync mode | ||
It's not recommended to use blocking functions to access the filesystem. Use the synchronous mode only if you need to. | ||
#### Extra tip | ||
If you want info about Windows or Mac releases, you can try the following modules from sindresorhus: | ||
https://www.npmjs.com/package/win-release | ||
or | ||
https://www.npmjs.com/package/macos-release | ||
@@ -87,0 +151,0 @@ |
36
test.js
@@ -1,8 +0,30 @@ | ||
const releaseInfo = require('./index') | ||
/*! | ||
* linux-release-info test | ||
* | ||
* Licensed under MIT | ||
* Copyright (c) 2018 [Samuel Carreira] | ||
*/ | ||
console.time('benchmark'); // benchmark startup | ||
releaseInfo() | ||
.then(result => { | ||
console.log(result) // all data | ||
console.log(`You are using ${result.pretty_name} on a ${result.arch} machine`) // Distro name and arch info | ||
}) | ||
.catch(err => console.error(`Error reading OS release info: ${err}`)) | ||
const releaseInfo = require('./index'); | ||
const path = require('path'); | ||
console.log('Linux Release Info Test\n'); | ||
try { | ||
const infoSyncData = releaseInfo({ mode: 'sync' }); | ||
console.log(`Sync mode test:\n\tYou are using ${infoSyncData.pretty_name} on a ${infoSyncData.arch} machine`); // Distro name and arch info | ||
} catch (err) { | ||
console.error(`Error reading OS release info: ${err}`); | ||
} | ||
releaseInfo({ | ||
custom_file: path.resolve(__dirname, 'os_release_sample'), | ||
debug: true | ||
}).then(result => { | ||
console.log(`Custom file and async mode test:\n\tYou are using ${result.pretty_name} on a ${result.arch} machine`); | ||
console.timeEnd('benchmark'); | ||
}) | ||
.catch(err => console.error(`Error reading OS release info: ${err}`)); |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
13335
9
165
153