Учебник по Electron.js

Разное

Получение информации об операционной системе

Получить информацию об операционной системе пользователя позволяют следующие свойства из модуля process:

console.log(process.platform); // win32
console.log(process.arch);     // x64
console.log(process.pid);      // 9008
console.log(process.env);

Примерный результат:

{
  ALLUSERSPROFILE: 'C:\\ProgramData',
  ANDROID_SDK_HOME: 'C:\\Android\\Program\\AndroidSDK',
  APPDATA: 'C:\\Users\\Unicross\\AppData\\Roaming',
  CommonProgramFiles: 'C:\\Program Files\\Common Files',
  'CommonProgramFiles(x86)': 'C:\\Program Files (x86)\\
  Common Files',
  CommonProgramW6432: 'C:\\Program Files\\Common Files',
  COMPUTERNAME: 'ASUS',
  ComSpec: 'C:\\Windows\\system32\\cmd.exe',
  configsetroot: 'C:\\Windows\\ConfigSetRoot',
  dp0: 'C:\\book\\e1\\node_modules\\.bin\\',
  FP_NO_HOST_CHECK: 'NO',
  HOME: 'C:\\Users\\Unicross',
  HOMEDRIVE: 'C:',
  HOMEPATH: '\\Users\\Unicross',
  INIT_CWD: 'C:\\book\\e1',
  JAVA_HOME: 'C:\\OpenJDK\\jdk-11',
  LOCALAPPDATA: 'C:\\Users\\Unicross\\AppData\\Local',
  LOGONSERVER: '\\\\ASUS',
  NODE: 'C:\\nodejs\\node12\\node.exe',
  NODE_EXE: 'C:\\nodejs\\node12\\\\node.exe',
  NPM_CLI_JS: 'C:\\nodejs\\node12\\\\node_modules\\npm\\bin\\
  npm-cli.js',
  npm_config_access: '',
  npm_config_allow_same_version: '',
  npm_config_also: '',
  npm_config_always_auth: '',
  npm_config_argv: '{"remain":[],"cooked":["run","start"],
  "original":["run","start"]}',
  npm_config_audit: 'true',
  npm_config_audit_level: 'low',
  npm_config_auth_type: 'legacy',
  npm_config_before: '',
  npm_config_bin_links: 'true',
  npm_config_browser: '',
  npm_config_ca: '',
  npm_config_cache: 'C:\\Users\\Unicross\\AppData\\Roaming\\
  npm-cache',
  npm_config_cache_lock_retries: '10',
  npm_config_cache_lock_stale: '60000',
  npm_config_cache_lock_wait: '10000',
  npm_config_cache_max: 'Infinity',
  npm_config_cache_min: '10',
  npm_config_cafile: '',
  npm_config_cert: '',
  npm_config_cidr: '',
  npm_config_color: 'true',
  npm_config_commit_hooks: 'true',
  npm_config_depth: 'Infinity',
  npm_config_description: 'true',
  npm_config_dev: '',
  npm_config_dry_run: '',
  npm_config_editor: 'notepad.exe',
  npm_config_engine_strict: '',
  npm_config_fetch_retries: '2',
  npm_config_fetch_retry_factor: '10',
  npm_config_fetch_retry_maxtimeout: '60000',
  npm_config_fetch_retry_mintimeout: '10000',
  npm_config_force: '',
  npm_config_format_package_lock: 'true',
  npm_config_fund: 'true',
  npm_config_git: 'git',
  npm_config_git_tag_version: 'true',
  npm_config_global: '',
  npm_config_globalconfig: 'C:\\Users\\Unicross\\AppData\\
  Roaming\\npm\\etc\\npmrc',
  npm_config_globalignorefile: 'C:\\Users\\Unicross\\AppData\\
  Roaming\\npm\\etc\\npmignore',
  npm_config_global_style: '',
  npm_config_group: '',
  npm_config_ham_it_up: '',
  npm_config_heading: 'npm',
  npm_config_https_proxy: '',
  npm_config_if_present: '',
  npm_config_ignore_prepublish: '',
  npm_config_ignore_scripts: '',
  npm_config_init_author_email: '',
  npm_config_init_author_name: '',
  npm_config_init_author_url: '',
  npm_config_init_license: 'ISC',
  npm_config_init_module: 'C:\\Users\\Unicross\\.npm-init.js',
  npm_config_init_version: '1.0.0',
  npm_config_json: '',
  npm_config_key: '',
  npm_config_legacy_bundling: '',
  npm_config_link: '',
  npm_config_local_address: '',
  npm_config_loglevel: 'notice',
  npm_config_logs_max: '10',
  npm_config_long: '',
  npm_config_maxsockets: '50',
  npm_config_message: '%s',
  npm_config_metrics_registry: 'https://registry.npmjs.org/',
  npm_config_node_gyp: 'C:\\nodejs\\node12\\node_modules\\
  npm\\node_modules\\node-gyp\\bin\\node-gyp.js',
  npm_config_node_options: '',
  npm_config_node_version: '12.18.3',
  npm_config_noproxy: '',
  npm_config_offline: '',
  npm_config_onload_script: '',
  npm_config_only: '',
  npm_config_optional: 'true',
  npm_config_otp: '',
  npm_config_package_lock: 'true',
  npm_config_package_lock_only: '',
  npm_config_parseable: '',
  npm_config_prefer_offline: '',
  npm_config_prefer_online: '',
  npm_config_prefix: 'C:\\Users\\Unicross\\AppData\\
  Roaming\\npm',
  npm_config_preid: '',
  npm_config_production: '',
  npm_config_progress: 'true',
  npm_config_proxy: '',
  npm_config_read_only: '',
  npm_config_rebuild_bundle: 'true',
  npm_config_registry: 'https://registry.npmjs.org/',
  npm_config_rollback: 'true',
  npm_config_save: 'true',
  npm_config_save_bundle: '',
  npm_config_save_dev: '',
  npm_config_save_exact: '',
  npm_config_save_optional: '',
  npm_config_save_prefix: '^',
  npm_config_save_prod: '',
  npm_config_scope: '',
  npm_config_scripts_prepend_node_path: 'warn-only',
  npm_config_script_shell: '',
  npm_config_searchexclude: '',
  npm_config_searchlimit: '20',
  npm_config_searchopts: '',
  npm_config_searchstaleness: '900',
  npm_config_send_metrics: '',
  npm_config_shell: 'C:\\Windows\\system32\\cmd.exe',
  npm_config_shrinkwrap: 'true',
  npm_config_sign_git_commit: '',
  npm_config_sign_git_tag: '',
  npm_config_sso_poll_frequency: '500',
  npm_config_sso_type: 'oauth',
  npm_config_strict_ssl: 'true',
  npm_config_tag: 'latest',
  npm_config_tag_version_prefix: 'v',
  npm_config_timing: '',
  npm_config_tmp: 'C:\\Users\\Unicross\\AppData\\Local\\Temp',
  npm_config_umask: '0000',
  npm_config_unicode: '',
  npm_config_unsafe_perm: 'true',
  npm_config_update_notifier: 'true',
  npm_config_usage: '',
  npm_config_user: '',
  npm_config_userconfig: 'C:\\Users\\Unicross\\.npmrc',
  npm_config_user_agent: 'npm/6.14.6 node/v12.18.3 win32 x64',
  npm_config_version: '',
  npm_config_versions: '',
  npm_config_viewer: 'browser',
  npm_execpath: 'C:\\nodejs\\node12\\node_modules\\npm\\bin\\
  npm-cli.js',
  npm_lifecycle_event: 'start',
  npm_lifecycle_script: 'electron .',
  npm_node_execpath: 'C:\\nodejs\\node12\\node.exe',
  npm_package_author: '',
  npm_package_description: '',
  npm_package_devDependencies_electron: '^12.0.5',
  npm_package_license: 'MIT',
  npm_package_main: 'main.js',
  npm_package_name: 'e1',
  npm_package_scripts_start: 'electron .',
  npm_package_version: '1.0.0',
  NPM_PREFIX_NPM_CLI_JS: 'C:\\Users\\Unicross\\AppData\\
  Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
  NUMBER_OF_PROCESSORS: '4',
  ORIGINAL_XDG_CURRENT_DESKTOP: 'undefined',
  OS: 'Windows_NT',
  Path: 'C:\\Windows\\system32;C:\\Windows;
  C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\
  WindowsPowerShell\\v1.0\\...',
  PATHEXT: '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JSE;.WSF;.WSH;.MSC',
  PROCESSOR_ARCHITECTURE: 'AMD64',
  PROCESSOR_IDENTIFIER: 'Intel64 Family 6 Model 58 Stepping 9,
  GenuineIntel',
  PROCESSOR_LEVEL: '6',
  PROCESSOR_REVISION: '3a09',
  ProgramData: 'C:\\ProgramData',
  ProgramFiles: 'C:\\Program Files',
  'ProgramFiles(x86)': 'C:\\Program Files (x86)',
  ProgramW6432: 'C:\\Program Files',
  PROMPT: '$P$G',
  PSModulePath: 'C:\\Windows\\system32\\WindowsPowerShell\\
  v1.0\\Modules\\',
  PUBLIC: 'C:\\Users\\Public',
  SESSIONNAME: 'Console',
  SystemDrive: 'C:',
  SystemRoot: 'C:\\Windows',
  TEMP: 'C:\\Users\\Unicross\\AppData\\Local\\Temp',
  TMP: 'C:\\Users\\Unicross\\AppData\\Local\\Temp',
  USERDOMAIN: 'ASUS',
  USERDOMAIN_ROAMINGPROFILE: 'ASUS',
  USERNAME: 'Unicross',
  USERPROFILE: 'C:\\Users\\Unicross',
  VS110COMNTOOLS: 'C:\\Program Files (x86)\\Microsoft Visual
  Studio 11.0\\Common7\\Tools\\',
  VS120COMNTOOLS: 'C:\\Program Files (x86)\\Microsoft Visual
  Studio 12.0\\Common7\\Tools\\',
  windir: 'C:\\Windows',
  _prog: 'node'
}

Можно также воспользоваться следующими методами из модуля os:

const os = require('os');
console.log( os.platform() );  // win32
console.log( os.type() );      // Windows_NT
console.log( os.version() );   // Windows 8 Single Language
console.log( os.arch() );      // x64
console.log( os.tmpdir() );
// C:\Users\Unicross\AppData\Local\Temp
console.log( os.homedir() );
// C:\Users\Unicross
console.log('freemem', os.freemem() );
console.log('totalmem', os.totalmem() );

Получить различные пути позволяют следующие методы объекта app:

console.log( app.getAppPath() ); // C:\book\e1
console.log( app.getPath('home') );
// C:\Users\Unicross
console.log( app.getPath('appData') );
// C:\Users\Unicross\AppData\Roaming
console.log( app.getPath('userData') );
// C:\Users\Unicross\AppData\Roaming\e1
console.log( app.getPath('temp') );
// C:\Users\Unicross\AppData\Local\Temp
console.log( app.getPath('desktop') );
// C:\Users\Unicross\Desktop
console.log( app.getPath('documents') );
// C:\Users\Unicross\Documents
console.log( app.getPath('downloads') );
// C:\Users\Unicross\Downloads
console.log( app.getPath('music') );
// C:\Users\Unicross\Music
console.log( app.getPath('pictures') );
// C:\Users\Unicross\Pictures
console.log( app.getPath('videos') );
// C:\Users\Unicross\Videos