1 | /**
|
---|
2 | * @fileoverview Main entrypoint for libraries using yargs-parser in Node.js
|
---|
3 | * CJS and ESM environments.
|
---|
4 | *
|
---|
5 | * @license
|
---|
6 | * Copyright (c) 2016, Contributors
|
---|
7 | * SPDX-License-Identifier: ISC
|
---|
8 | */
|
---|
9 | import { format } from 'util';
|
---|
10 | import { readFileSync } from 'fs';
|
---|
11 | import { normalize, resolve } from 'path';
|
---|
12 | import { camelCase, decamelize, looksLikeNumber } from './string-utils.js';
|
---|
13 | import { YargsParser } from './yargs-parser.js';
|
---|
14 | // See https://github.com/yargs/yargs-parser#supported-nodejs-versions for our
|
---|
15 | // version support policy. The YARGS_MIN_NODE_VERSION is used for testing only.
|
---|
16 | const minNodeVersion = (process && process.env && process.env.YARGS_MIN_NODE_VERSION)
|
---|
17 | ? Number(process.env.YARGS_MIN_NODE_VERSION)
|
---|
18 | : 10;
|
---|
19 | if (process && process.version) {
|
---|
20 | const major = Number(process.version.match(/v([^.]+)/)[1]);
|
---|
21 | if (major < minNodeVersion) {
|
---|
22 | throw Error(`yargs parser supports a minimum Node.js version of ${minNodeVersion}. Read our version support policy: https://github.com/yargs/yargs-parser#supported-nodejs-versions`);
|
---|
23 | }
|
---|
24 | }
|
---|
25 | // Creates a yargs-parser instance using Node.js standard libraries:
|
---|
26 | const env = process ? process.env : {};
|
---|
27 | const parser = new YargsParser({
|
---|
28 | cwd: process.cwd,
|
---|
29 | env: () => {
|
---|
30 | return env;
|
---|
31 | },
|
---|
32 | format,
|
---|
33 | normalize,
|
---|
34 | resolve,
|
---|
35 | // TODO: figure out a way to combine ESM and CJS coverage, such that
|
---|
36 | // we can exercise all the lines below:
|
---|
37 | require: (path) => {
|
---|
38 | if (typeof require !== 'undefined') {
|
---|
39 | return require(path);
|
---|
40 | }
|
---|
41 | else if (path.match(/\.json$/)) {
|
---|
42 | return readFileSync(path, 'utf8');
|
---|
43 | }
|
---|
44 | else {
|
---|
45 | throw Error('only .json config files are supported in ESM');
|
---|
46 | }
|
---|
47 | }
|
---|
48 | });
|
---|
49 | const yargsParser = function Parser(args, opts) {
|
---|
50 | const result = parser.parse(args.slice(), opts);
|
---|
51 | return result.argv;
|
---|
52 | };
|
---|
53 | yargsParser.detailed = function (args, opts) {
|
---|
54 | return parser.parse(args.slice(), opts);
|
---|
55 | };
|
---|
56 | yargsParser.camelCase = camelCase;
|
---|
57 | yargsParser.decamelize = decamelize;
|
---|
58 | yargsParser.looksLikeNumber = looksLikeNumber;
|
---|
59 | export default yargsParser;
|
---|