| [9af201e] | 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 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 |
|
|---|
| 8 | const REQUIRE_ERROR = 'require is not supported in browser';
|
|---|
| 9 | const REQUIRE_DIRECTORY_ERROR =
|
|---|
| 10 | 'loading a directory of commands is not supported in browser';
|
|---|
| 11 |
|
|---|
| 12 | export default {
|
|---|
| 13 | assert: {
|
|---|
| 14 | notStrictEqual: (a, b) => {
|
|---|
| 15 | // noop.
|
|---|
| 16 | },
|
|---|
| 17 | strictEqual: (a, b) => {
|
|---|
| 18 | // noop.
|
|---|
| 19 | },
|
|---|
| 20 | },
|
|---|
| 21 | cliui,
|
|---|
| 22 | findUp: () => undefined,
|
|---|
| 23 | getEnv: key => {
|
|---|
| 24 | // There is no environment in browser:
|
|---|
| 25 | return undefined;
|
|---|
| 26 | },
|
|---|
| 27 | inspect: console.log,
|
|---|
| 28 | getCallerFile: () => {
|
|---|
| 29 | throw new YError(REQUIRE_DIRECTORY_ERROR);
|
|---|
| 30 | },
|
|---|
| 31 | getProcessArgvBin,
|
|---|
| 32 | mainFilename: 'yargs',
|
|---|
| 33 | Parser,
|
|---|
| 34 | path: {
|
|---|
| 35 | basename: str => str,
|
|---|
| 36 | dirname: str => str,
|
|---|
| 37 | extname: str => str,
|
|---|
| 38 | relative: str => str,
|
|---|
| 39 | },
|
|---|
| 40 | process: {
|
|---|
| 41 | argv: () => [],
|
|---|
| 42 | cwd: () => '',
|
|---|
| 43 | execPath: () => '',
|
|---|
| 44 | // exit is noop browser:
|
|---|
| 45 | exit: () => {},
|
|---|
| 46 | nextTick: cb => {
|
|---|
| 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 |
|
|---|
| 85 | function sprintf(_str, ...args) {
|
|---|
| 86 | let str = '';
|
|---|
| 87 | const split = _str.split('%s');
|
|---|
| 88 | split.forEach((token, i) => {
|
|---|
| 89 | str += `${token}${split[i + 1] !== undefined && args[i] ? args[i] : ''}`;
|
|---|
| 90 | });
|
|---|
| 91 | return str;
|
|---|
| 92 | }
|
|---|