2024-01-08 17:45:54 +01:00
const execSync = require ( "node:child_process" ) . execSync ;
2024-08-27 15:52:59 -05:00
const path = require ( "node:path" ) ;
const rootPath = path . resolve ( ` ${ _ _dirname } /../ ` ) ;
const Log = require ( ` ${ rootPath } /js/logger.js ` ) ;
const os = require ( "node:os" ) ;
const fs = require ( "node:fs" ) ;
2024-01-04 22:38:53 +01:00
const si = require ( "systeminformation" ) ;
2017-02-28 01:41:21 -03:00
2024-08-27 15:52:59 -05:00
const modulePositions = [ ] ; // will get list from index.html
const regionRegEx = /"region ([^"]*)/i ;
const indexFileName = "index.html" ;
const discoveredPositionsJSFilename = "js/positions.js" ;
2021-01-05 18:37:01 +01:00
module . exports = {
2024-01-04 22:38:53 +01:00
async logSystemInformation ( ) {
try {
2024-01-07 17:28:17 +01:00
let installedNodeVersion = execSync ( "node -v" , { encoding : "utf-8" } ) . replace ( "v" , "" ) . replace ( /(?:\r\n|\r|\n)/g , "" ) ;
2024-01-14 09:15:30 +01:00
const staticData = await si . get ( {
system : "manufacturer, model, raspberry, virtual" ,
osInfo : "platform, distro, release, arch" ,
versions : "kernel, node, npm, pm2"
} ) ;
2024-01-04 22:38:53 +01:00
let systemDataString = "System information:" ;
2024-12-30 12:24:51 +01:00
systemDataString += ` \n ### SYSTEM: manufacturer: ${ staticData . system . manufacturer } ; model: ${ staticData . system . model } ; virtual: ${ staticData . system . virtual } ` ;
systemDataString += ` \n ### OS: platform: ${ staticData . osInfo . platform } ; distro: ${ staticData . osInfo . distro } ; release: ${ staticData . osInfo . release } ; arch: ${ staticData . osInfo . arch } ; kernel: ${ staticData . versions . kernel } ` ;
systemDataString += ` \n ### VERSIONS: electron: ${ process . versions . electron } ; used node: ${ staticData . versions . node } ; installed node: ${ installedNodeVersion } ; npm: ${ staticData . versions . npm } ; pm2: ${ staticData . versions . pm2 } ` ;
2024-01-07 17:28:17 +01:00
systemDataString += ` \n ### OTHER: timeZone: ${ Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone } ; ELECTRON_ENABLE_GPU: ${ process . env . ELECTRON _ENABLE _GPU } ` ;
2024-01-04 22:38:53 +01:00
Log . info ( systemDataString ) ;
2024-01-16 21:54:55 +01:00
// Return is currently only for jest
return systemDataString ;
2024-01-04 22:38:53 +01:00
} catch ( e ) {
Log . error ( e ) ;
}
2024-06-24 21:51:54 +02:00
} ,
// return all available module positions
getAvailableModulePositions ( ) {
2024-08-27 15:52:59 -05:00
return modulePositions ;
2024-06-24 21:51:54 +02:00
} ,
2024-09-18 07:37:09 +02:00
// return if position is on modulePositions Array (true/false)
2024-06-24 21:51:54 +02:00
moduleHasValidPosition ( position ) {
if ( this . getAvailableModulePositions ( ) . indexOf ( position ) === - 1 ) return false ;
return true ;
2024-08-27 15:52:59 -05:00
} ,
getModulePositions ( ) {
2024-09-28 08:52:09 -05:00
// if not already discovered
if ( modulePositions . length === 0 ) {
// get the lines of the index.html
2024-12-28 11:57:39 -06:00
const lines = fs . readFileSync ( indexFileName ) . toString ( ) . split ( "\n" ) ;
2024-09-28 08:52:09 -05:00
// loop thru the lines
lines . forEach ( ( line ) => {
// run the regex on each line
const results = regionRegEx . exec ( line ) ;
// if the regex returned something
if ( results && results . length > 0 ) {
// get the position parts and replace space with underscore
const positionName = results [ 1 ] . replace ( " " , "_" ) ;
// add it to the list
modulePositions . push ( positionName ) ;
}
} ) ;
2024-12-18 10:37:51 -06:00
try {
fs . writeFileSync ( discoveredPositionsJSFilename , ` const modulePositions= ${ JSON . stringify ( modulePositions ) } ` ) ;
}
catch ( error ) {
console . error ( "unable to write js/positions.js with the discovered module positions\nmake the MagicMirror/js folder writeable by the user starting MagicMirror" ) ;
}
2024-09-28 08:52:09 -05:00
}
2024-08-27 15:52:59 -05:00
// return the list to the caller
return modulePositions ;
2017-02-28 01:41:21 -03:00
}
} ;