index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. /*!
  3. // Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
  4. // Released under the MIT license
  5. // https://opensource.org/licenses/mit-license.php
  6. */
  7. /*!
  8. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9. Copyright (C) 2020 jeffy-g <hirotom1107@gmail.com>
  10. Released under the MIT license
  11. https://opensource.org/licenses/mit-license.php
  12. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  13. */
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. const lcid = require("lcid");
  16. const child_process_1 = require("child_process");
  17. const defaultLocale = "en_US";
  18. const getStdOut = async (command, args) => await new Promise((resolve) => {
  19. child_process_1.execFile(command, args, (err, stdout) => {
  20. resolve(err || stdout);
  21. });
  22. });
  23. const getStdOutSync = (command, args) => {
  24. try {
  25. return child_process_1.execFileSync(command, args);
  26. }
  27. catch (e) {
  28. return e;
  29. }
  30. };
  31. const normalise = (input) => input.replace(/_/, "-");
  32. function validate(result, processor) {
  33. if (typeof result === "string" || result.length) {
  34. return processor ? processor(result.toString()) : result.toString().trim();
  35. }
  36. return defaultLocale;
  37. }
  38. const getEnvLocale = (env = process.env) => env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE || "";
  39. function parseLocale(str) {
  40. const env = str.split("\n").reduce((env, definition) => {
  41. const [key, value] = definition.split("=");
  42. if (key && value) {
  43. env[key] = value.replace(/^"|"$/g, "");
  44. }
  45. return env;
  46. }, {});
  47. return getEnvLocale(env);
  48. }
  49. const purgeExtraToken = (str) => (str && str.replace(/[.:].*/, "")) || defaultLocale;
  50. const getSupportedLocale = (locale, locales) => locales.includes(locale) ? locale : defaultLocale;
  51. const [getAppleLocale, getAppleLocaleSync] = ((cmd0, args0, cmd1, args1) => {
  52. return [
  53. async () => {
  54. const results = await Promise.all([
  55. getStdOut(cmd0, args0).then(ret => validate(ret)),
  56. getStdOut(cmd1, args1).then(ret => validate(ret))
  57. ]);
  58. return getSupportedLocale(results[0], results[1]);
  59. },
  60. () => getSupportedLocale(validate(getStdOutSync(cmd0, args0)), validate(getStdOutSync(cmd1, args1)))
  61. ];
  62. })("defaults", ["read", "-globalDomain", "AppleLocale"], "locale", ["-a"]);
  63. const getUnixLocale = async () => purgeExtraToken(parseLocale(await getStdOut("locale").then(ret => validate(ret))));
  64. const getUnixLocaleSync = () => purgeExtraToken(parseLocale(validate(getStdOutSync("locale"))));
  65. const parseLCID = (result) => {
  66. const lcidCode = parseInt(result.replace("Locale", ""), 16);
  67. return lcid.from(lcidCode);
  68. };
  69. const [getWinLocale, getWinLocaleSync] = ((cmd0, args0) => {
  70. return [
  71. async () => validate(await getStdOut(cmd0, args0), parseLCID),
  72. () => validate(getStdOutSync(cmd0, args0), parseLCID)
  73. ];
  74. })("wmic", ["os", "get", "locale"]);
  75. let detector;
  76. {
  77. const gettersSlot = [
  78. {
  79. win32: getWinLocaleSync,
  80. darwin: getAppleLocaleSync,
  81. linux: getUnixLocaleSync,
  82. }, {
  83. win32: getWinLocale,
  84. darwin: getAppleLocale,
  85. linux: getUnixLocale,
  86. }
  87. ];
  88. const typeString = {}.toString;
  89. const isPromise = (o) => typeString.call(o) === "[object Promise]";
  90. let cacheLocal;
  91. const base = (async) => (options = {}) => {
  92. options = { spawn: true, cache: true, ...options };
  93. const cache = options.cache;
  94. if (cache && (cacheLocal === null || cacheLocal === void 0 ? void 0 : cacheLocal.length)) {
  95. return (async ? Promise.resolve(cacheLocal) : cacheLocal);
  96. }
  97. const functions = gettersSlot[+(!!async)];
  98. let locale;
  99. const withCache = (l, mustPromise) => {
  100. l = normalise(l);
  101. cacheLocal = cache ? l : void 0;
  102. return (mustPromise ? Promise.resolve(l) : l);
  103. };
  104. const envLocale = getEnvLocale();
  105. if (envLocale || !options.spawn) {
  106. locale = purgeExtraToken(envLocale);
  107. }
  108. else {
  109. let { platform } = process;
  110. if (platform !== "win32" && platform !== "darwin") {
  111. platform = "linux";
  112. }
  113. locale = functions[platform]();
  114. }
  115. if (isPromise(locale)) {
  116. return locale.then(result => withCache(result));
  117. }
  118. else {
  119. return withCache(locale, async === true || void 0);
  120. }
  121. };
  122. detector = (base(true));
  123. detector.sync = base();
  124. Object.defineProperties(detector, {
  125. purge: {
  126. value: () => cacheLocal = void 0,
  127. enumerable: false,
  128. },
  129. version: {
  130. value: "v1.0.8",
  131. enumerable: true,
  132. },
  133. });
  134. }
  135. exports.osLocale = Object.freeze(detector);