browser.mjs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* eslint-disable no-unused-vars */
  2. 'use strict';
  3. import cliui from 'https://unpkg.com/cliui@7.0.1/index.mjs'; // eslint-disable-line
  4. import Parser from 'https://unpkg.com/yargs-parser@19.0.0/browser.js'; // eslint-disable-line
  5. import {getProcessArgvBin} from '../../build/lib/utils/process-argv.js';
  6. import {YError} from '../../build/lib/yerror.js';
  7. const REQUIRE_ERROR = 'require is not supported in browser';
  8. const REQUIRE_DIRECTORY_ERROR =
  9. 'loading a directory of commands is not supported in browser';
  10. export default {
  11. assert: {
  12. notStrictEqual: (a, b) => {
  13. // noop.
  14. },
  15. strictEqual: (a, b) => {
  16. // noop.
  17. },
  18. },
  19. cliui,
  20. findUp: () => undefined,
  21. getEnv: key => {
  22. // There is no environment in browser:
  23. return undefined;
  24. },
  25. inspect: console.log,
  26. getCallerFile: () => {
  27. throw new YError(REQUIRE_DIRECTORY_ERROR);
  28. },
  29. getProcessArgvBin,
  30. mainFilename: 'yargs',
  31. Parser,
  32. path: {
  33. basename: str => str,
  34. dirname: str => str,
  35. extname: str => str,
  36. relative: str => str,
  37. },
  38. process: {
  39. argv: () => [],
  40. cwd: () => '',
  41. emitWarning: (warning, name) => {},
  42. execPath: () => '',
  43. // exit is noop browser:
  44. exit: () => {},
  45. nextTick: cb => {
  46. // eslint-disable-next-line no-undef
  47. window.setTimeout(cb, 1);
  48. },
  49. stdColumns: 80,
  50. },
  51. readFileSync: () => {
  52. return '';
  53. },
  54. require: () => {
  55. throw new YError(REQUIRE_ERROR);
  56. },
  57. requireDirectory: () => {
  58. throw new YError(REQUIRE_DIRECTORY_ERROR);
  59. },
  60. stringWidth: str => {
  61. return [...str].length;
  62. },
  63. // TODO: replace this with y18n once it's ported to ESM:
  64. y18n: {
  65. __: (...str) => {
  66. if (str.length === 0) return '';
  67. const args = str.slice(1);
  68. return sprintf(str[0], ...args);
  69. },
  70. __n: (str1, str2, count, ...args) => {
  71. if (count === 1) {
  72. return sprintf(str1, ...args);
  73. } else {
  74. return sprintf(str2, ...args);
  75. }
  76. },
  77. getLocale: () => {
  78. return 'en_US';
  79. },
  80. setLocale: () => {},
  81. updateLocale: () => {},
  82. },
  83. };
  84. function sprintf(_str, ...args) {
  85. let str = '';
  86. const split = _str.split('%s');
  87. split.forEach((token, i) => {
  88. str += `${token}${split[i + 1] !== undefined && args[i] ? args[i] : ''}`;
  89. });
  90. return str;
  91. }