system.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. module.metadata = {
  6. "stability": "unstable"
  7. };
  8. const { Cc, Ci, CC } = require('chrome');
  9. const options = require('@loader/options');
  10. const file = require('./io/file');
  11. const runtime = require("./system/runtime");
  12. const appStartup = Cc['@mozilla.org/toolkit/app-startup;1'].
  13. getService(Ci.nsIAppStartup);
  14. const appInfo = Cc["@mozilla.org/xre/app-info;1"].
  15. getService(Ci.nsIXULAppInfo);
  16. const directoryService = Cc['@mozilla.org/file/directory_service;1'].
  17. getService(Ci.nsIProperties);
  18. const PR_WRONLY = parseInt("0x02");
  19. const PR_CREATE_FILE = parseInt("0x08");
  20. const PR_APPEND = parseInt("0x10");
  21. const PR_TRUNCATE = parseInt("0x20");
  22. function openFile(path, mode) {
  23. let file = Cc["@mozilla.org/file/local;1"].
  24. createInstance(Ci.nsILocalFile);
  25. file.initWithPath(path);
  26. let stream = Cc["@mozilla.org/network/file-output-stream;1"].
  27. createInstance(Ci.nsIFileOutputStream);
  28. stream.init(file, mode, -1, 0);
  29. return stream
  30. }
  31. const { eAttemptQuit: E_ATTEMPT, eForceQuit: E_FORCE } = appStartup;
  32. /**
  33. * Parsed JSON object that was passed via `cfx --static-args "{ foo: 'bar' }"`
  34. */
  35. exports.staticArgs = options.staticArgs;
  36. /**
  37. * Environment variables. Environment variables are non-enumerable properties
  38. * of this object (key is name and value is value).
  39. */
  40. exports.env = require('./system/environment').env;
  41. /**
  42. * Ends the process with the specified `code`. If omitted, exit uses the
  43. * 'success' code 0. To exit with failure use `1`.
  44. * TODO: Improve platform to actually quit with an exit code.
  45. */
  46. exports.exit = function exit(code) {
  47. // This is used by 'cfx' to find out exit code.
  48. if ('resultFile' in options && options.resultFile) {
  49. let mode = PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE;
  50. let stream = openFile(options.resultFile, mode);
  51. let status = code ? 'FAIL' : 'OK';
  52. stream.write(status, status.length);
  53. stream.flush();
  54. stream.close();
  55. }
  56. appStartup.quit(code ? E_ATTEMPT : E_FORCE);
  57. };
  58. // Adapter for nodejs's stdout & stderr:
  59. // http://nodejs.org/api/process.html#process_process_stdout
  60. let stdout = Object.freeze({ write: dump, end: dump });
  61. exports.stdout = stdout;
  62. exports.stderr = stdout;
  63. /**
  64. * Returns a path of the system's or application's special directory / file
  65. * associated with a given `id`. For list of possible `id`s please see:
  66. * https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O#Getting_files_in_special_directories
  67. * http://mxr.mozilla.org/mozilla-central/source/xpcom/io/nsAppDirectoryServiceDefs.h
  68. * @example
  69. *
  70. * // get firefox profile path
  71. * let profilePath = require('system').pathFor('ProfD');
  72. * // get OS temp files directory (/tmp)
  73. * let temps = require('system').pathFor('TmpD');
  74. * // get OS desktop path for an active user (~/Desktop on linux
  75. * // or C:\Documents and Settings\username\Desktop on windows).
  76. * let desktopPath = require('system').pathFor('Desk');
  77. */
  78. exports.pathFor = function pathFor(id) {
  79. return directoryService.get(id, Ci.nsIFile).path;
  80. };
  81. /**
  82. * What platform you're running on (all lower case string).
  83. * For possible values see:
  84. * https://developer.mozilla.org/en/OS_TARGET
  85. */
  86. exports.platform = runtime.OS.toLowerCase();
  87. /**
  88. * What processor architecture you're running on:
  89. * `'arm', 'ia32', or 'x64'`.
  90. */
  91. exports.architecture = runtime.XPCOMABI.split('_')[0];
  92. /**
  93. * What compiler used for build:
  94. * `'msvc', 'n32', 'gcc2', 'gcc3', 'sunc', 'ibmc'...`
  95. */
  96. exports.compiler = runtime.XPCOMABI.split('_')[1];
  97. /**
  98. * The application's build ID/date, for example "2004051604".
  99. */
  100. exports.build = appInfo.appBuildID;
  101. /**
  102. * The XUL application's UUID.
  103. * This has traditionally been in the form
  104. * `{AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE}` but for some applications it may
  105. * be: "appname@vendor.tld".
  106. */
  107. exports.id = appInfo.ID;
  108. /**
  109. * The name of the application.
  110. */
  111. exports.name = appInfo.name;
  112. /**
  113. * The XUL application's version, for example "0.8.0+" or "3.7a1pre".
  114. */
  115. exports.version = appInfo.version;
  116. /**
  117. * XULRunner version.
  118. */
  119. exports.platformVersion = appInfo.platformVersion;
  120. /**
  121. * The name of the application vendor, for example "Mozilla".
  122. */
  123. exports.vendor = appInfo.vendor;