Skip to content

Commit 4ae5f57

Browse files
author
Benjamin E. Coe
authored
fix(browser): fix shims so that yargs continues working in browser context (#2457)
1 parent bf31bd3 commit 4ae5f57

7 files changed

Lines changed: 29 additions & 14 deletions

File tree

‎docs/browser.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ the browser:
66

77
```html
88
<script type="module">
9-
import Yargs from 'https://unpkg.com/yargs@16.0.0-beta.1/browser.mjs';
9+
import Yargs from 'https://unpkg.com/yargs@18.0.0-browser.2/browser.mjs';
1010
const yargs = Yargs()
1111
.scriptName('>')
1212
.command('clear', 'clear the output window', () => {}, () => {
@@ -23,4 +23,4 @@ the browser:
2323
```
2424

2525
A full example can be found in [example/yargs.html](/example/yargs.html), or
26-
on [jsfiddle](https://jsfiddle.net/bencoe/m9fv2oet/3/).
26+
on [jsfiddle](https://jsfiddle.net/juL5zonp/).

‎lib/command.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import {
2525
DetailedArguments,
2626
} from './yargs-factory.js';
2727
import {maybeAsyncResult} from './utils/maybe-async-result.js';
28-
import {readdirSync} from 'node:fs';
29-
import {join, dirname} from 'node:path';
3028

3129
const DEFAULT_MARKER = /(^\*)|(^\$0)/;
3230
export type DefinitionOrCommandName = string | CommandHandlerDefinition;
@@ -63,16 +61,19 @@ export class CommandInstance {
6361
): void {
6462
opts = opts || {};
6563
this.requireCache.add(callerFile);
66-
const fullDirPath = join(dirname(callerFile), dir);
67-
const files = readdirSync(fullDirPath, {
64+
const fullDirPath = this.shim.path.join(
65+
this.shim.path.dirname(callerFile),
66+
dir
67+
);
68+
const files = this.shim.readdirSync(fullDirPath, {
6869
recursive: opts.recurse ? true : false,
6970
});
7071
// exclude 'json', 'coffee' from require-directory defaults
7172
if (!Array.isArray(opts.extensions)) opts.extensions = ['js'];
7273
// allow consumer to define their own visitor function
7374
const visit = typeof opts.visit === 'function' ? opts.visit : (o: any) => o;
7475
for (const fileb of files) {
75-
const file = fileb.toString('utf8');
76+
const file = fileb.toString();
7677

7778
// Support include / exclude logic from require-directory.
7879
if (opts.exclude) {
@@ -99,7 +100,7 @@ export class CommandInstance {
99100
if (file.endsWith(ext)) supportedExtension = true;
100101
}
101102
if (supportedExtension) {
102-
const joined = join(fullDirPath, file);
103+
const joined = this.shim.path.join(fullDirPath, file);
103104
const module = req(joined);
104105
const visited = visit(module, joined, file);
105106
if (visited) {

‎lib/platform-shims/browser.mjs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export default {
3737
dirname: str => str,
3838
extname: str => str,
3939
relative: str => str,
40+
join: (str, _str2) => str,
4041
},
4142
process: {
4243
argv: () => [],
@@ -54,6 +55,9 @@ export default {
5455
readFileSync: () => {
5556
return '';
5657
},
58+
readdirSync: () => {
59+
return [];
60+
},
5761
require: () => {
5862
throw new YError(REQUIRE_ERROR);
5963
},

‎lib/platform-shims/deno.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
dirname,
1010
extname,
1111
posix,
12+
join,
1213
} from 'https://deno.land/std/path/mod.ts';
1314

1415
import cliui from 'https://deno.land/x/[email protected]/deno.ts';
@@ -54,6 +55,7 @@ const path = {
5455
}
5556
},
5657
resolve: posix.resolve,
58+
join,
5759
};
5860

5961
// TODO: replace with Deno.consoleSize(Deno.stdout.rid)
@@ -94,6 +96,9 @@ export default {
9496
stdColumns: columns ?? null,
9597
},
9698
readFileSync: Deno.readTextFileSync,
99+
readdirSync: () => {
100+
return [];
101+
},
97102
require: () => {
98103
throw new YError(REQUIRE_ERROR);
99104
},

‎lib/platform-shims/esm.mjs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import { notStrictEqual, strictEqual } from 'assert'
44
import cliui from 'cliui'
55
import escalade from 'escalade/sync'
66
import { inspect } from 'util'
7-
import { readFileSync } from 'fs'
87
import { fileURLToPath } from 'url';
98
import Parser from 'yargs-parser'
10-
import { basename, dirname, extname, relative, resolve } from 'path'
9+
import { basename, dirname, extname, relative, resolve, join } from 'path'
1110
import { getProcessArgvBin } from '../../build/lib/utils/process-argv.js'
1211
import stringWidth from 'string-width';
1312
import y18n from 'y18n'
1413
import { createRequire } from 'node:module';
1514
import getCallerFile from 'get-caller-file';
15+
import { readFileSync, readdirSync } from 'node:fs'
1616

1717
const __dirname = fileURLToPath(import.meta.url);
1818
const mainFilename = __dirname.substring(0, __dirname.lastIndexOf('node_modules'));
@@ -37,7 +37,8 @@ export default {
3737
dirname,
3838
extname,
3939
relative,
40-
resolve
40+
resolve,
41+
join
4142
},
4243
process: {
4344
argv: () => process.argv,
@@ -52,6 +53,7 @@ export default {
5253
stdColumns: typeof process.stdout.columns !== 'undefined' ? process.stdout.columns : null
5354
},
5455
readFileSync,
56+
readdirSync,
5557
require,
5658
getCallerFile: () => {
5759
const callerFile = getCallerFile(3);

‎lib/typings/common-types.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export interface PlatformShim {
106106
dirname: (path: string) => string;
107107
relative: (p1: string, p2: string) => string;
108108
resolve: (p1: string, p2: string) => string;
109+
join: (p1: string, p2: string) => string;
109110
};
110111
process: {
111112
argv: () => string[];
@@ -117,6 +118,10 @@ export interface PlatformShim {
117118
stdColumns: number | null;
118119
};
119120
readFileSync: (path: string, encoding: string) => string;
121+
readdirSync: (
122+
path: string,
123+
opts: object
124+
) => Array<string | Buffer<ArrayBufferLike>>[];
120125
require: RequireType;
121126
y18n: Y18N;
122127
}

‎lib/utils/apply-extends.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {Dictionary, PlatformShim} from '../typings/common-types.js';
22
import {YError} from '../yerror.js';
3-
import {createRequire} from 'node:module';
4-
const require = createRequire(import.meta.url);
53

64
let previouslyVisitedConfigs: string[] = [];
75
let shim: PlatformShim;
@@ -36,7 +34,7 @@ export function applyExtends(
3634

3735
defaultConfig = isPath
3836
? JSON.parse(shim.readFileSync(pathToDefault, 'utf8'))
39-
: require(config.extends);
37+
: _shim.require(config.extends);
4038
delete config.extends;
4139
defaultConfig = applyExtends(
4240
defaultConfig,

0 commit comments

Comments
 (0)