@henderea/arg-helper 1.2.2 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,19 +1,37 @@
1
1
  {
2
2
  "name": "@henderea/arg-helper",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "A utility wrapper around the arg command-line parser",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
7
7
  "repository": "git@github.com:henderea/arg-helper.git",
8
8
  "author": "Eric Henderson <henderea@gmail.com>",
9
9
  "license": "MIT",
10
+ "scripts": {
11
+ "lint": "eslint src",
12
+ "lint:fix": "eslint --fix src"
13
+ },
10
14
  "peerDependencies": {
11
15
  "arg": "5.x"
12
16
  },
13
17
  "dependencies": {
14
18
  "escalade": "^3.1.1"
15
19
  },
20
+ "devDependencies": {
21
+ "@typescript-eslint/eslint-plugin": "^5.58.0",
22
+ "@typescript-eslint/parser": "^5.58.0",
23
+ "eslint": "^8.38.0",
24
+ "eslint-config-henderea": "1.1.7",
25
+ "eslint-plugin-import": "^2.27.5",
26
+ "typescript": "^5.0.4"
27
+ },
16
28
  "files": [
17
29
  "src/**/*"
18
- ]
30
+ ],
31
+ "eslintConfig": {
32
+ "extends": "henderea",
33
+ "rules": {
34
+ "@typescript-eslint/ban-types": 0
35
+ }
36
+ }
19
37
  }
package/src/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- declare function argHelper(arg: any): {argParser: argHelper.argParser};
1
+ declare function argHelper(arg: any): { argParser: argHelper.argParser };
2
2
 
3
3
  declare namespace argHelper {
4
4
  export interface Map<V> {
@@ -19,10 +19,14 @@ declare namespace argHelper {
19
19
  findVersion(callerPath: string, ...names: Array<string>): this;
20
20
  version(packageJsonFile: string, ...names: Array<string>): this;
21
21
  parse(argv?: Array<any> | null): Map<any>;
22
- readonly argv: Map<any>;
22
+ get argv(): Map<any>;
23
+ static terminalWidth(multiplier?: number): number;
23
24
  }
24
25
 
25
- export type argParser = () => ArgParser;
26
+ export interface argParser {
27
+ (): ArgParser;
28
+ terminalWidth(multiplier?: number): number;
29
+ }
26
30
  }
27
31
 
28
- export = argHelper;
32
+ export = argHelper;
package/src/index.js CHANGED
@@ -2,85 +2,95 @@ const fs = require('fs');
2
2
  const escalade = require('escalade/sync');
3
3
 
4
4
  class ArgParser {
5
- constructor(arg) {
6
- this._arg = arg;
7
- this._opts = {};
8
- this._names = {};
9
- this._helpText = null;
10
- this._packageJsonFile = null;
11
- }
5
+ constructor(arg) {
6
+ this._arg = arg;
7
+ this._opts = {};
8
+ this._names = {};
9
+ this._helpText = null;
10
+ this._packageJsonFile = null;
11
+ }
12
12
 
13
- flag(name, ...names) {
14
- let type = names.pop();
15
- let mainName = names.shift();
16
- this._opts[mainName] = type;
17
- this._names[mainName] = name;
18
- if(names.length > 0) {
19
- names.forEach(n => {
20
- this._opts[n] = mainName;
21
- });
22
- }
23
- return this;
24
- }
25
- flags(name, ...names) {
26
- let type = names.pop();
27
- return this.flag(name, ...names, [type]);
13
+ flag(name, ...names) {
14
+ let type = names.pop();
15
+ let mainName = names.shift();
16
+ this._opts[mainName] = type;
17
+ this._names[mainName] = name;
18
+ if(names.length > 0) {
19
+ names.forEach((n) => {
20
+ this._opts[n] = mainName;
21
+ });
28
22
  }
23
+ return this;
24
+ }
25
+ flags(name, ...names) {
26
+ let type = names.pop();
27
+ return this.flag(name, ...names, [type]);
28
+ }
29
29
 
30
- string(name, ...names) { return this.flag(name, ...names, String); }
31
- strings(name, ...names) { return this.flags(name, ...names, String); }
32
- bool(name, ...names) { return this.flag(name, ...names, Boolean); }
33
- bools(name, ...names) { return this.flags(name, ...names, Boolean); }
34
- number(name, ...names) { return this.flag(name, ...names, Number); }
35
- numbers(name, ...names) { return this.flags(name, ...names, Number); }
36
- count(name, ...names) { return this.flag(name, ...names, this._arg.COUNT); }
37
- help(helpText, ...names) {
38
- this._helpText = helpText;
39
- return this.bool('help', ...names);
30
+ string(name, ...names) { return this.flag(name, ...names, String); }
31
+ strings(name, ...names) { return this.flags(name, ...names, String); }
32
+ bool(name, ...names) { return this.flag(name, ...names, Boolean); }
33
+ bools(name, ...names) { return this.flags(name, ...names, Boolean); }
34
+ number(name, ...names) { return this.flag(name, ...names, Number); }
35
+ numbers(name, ...names) { return this.flags(name, ...names, Number); }
36
+ count(name, ...names) { return this.flag(name, ...names, this._arg.COUNT); }
37
+ help(helpText, ...names) {
38
+ this._helpText = helpText;
39
+ return this.bool('help', ...names);
40
+ }
41
+ findVersion(callerPath, ...names) {
42
+ const packageJsonFile = escalade(callerPath, (dir, fileNames) => fileNames.includes('package.json') && 'package.json');
43
+ if(packageJsonFile && fs.existsSync(packageJsonFile)) {
44
+ this.version(packageJsonFile, ...names);
40
45
  }
41
- findVersion(callerPath, ...names) {
42
- const packageJsonFile = escalade(callerPath, (dir, fileNames) => fileNames.includes('package.json') && 'package.json');
43
- if(packageJsonFile && fs.existsSync(packageJsonFile)) {
44
- this.version(packageJsonFile, ...names);
45
- }
46
- return this;
46
+ return this;
47
+ }
48
+ version(packageJsonFile, ...names) {
49
+ this._packageJsonFile = packageJsonFile;
50
+ return this.bool('version', ...names);
51
+ }
52
+
53
+ parse(argv = null) {
54
+ let config = { permissive: true };
55
+ if(argv !== null) { config.argv = argv; }
56
+ let options = this._arg(this._opts, config);
57
+ let rv = {};
58
+ Object.keys(options).forEach((k) => {
59
+ rv[k] = options[k];
60
+ if(Object.prototype.hasOwnProperty.call(this._names, k)) {
61
+ rv[this._names[k]] = options[k];
62
+ }
63
+ });
64
+ if(this._helpText && rv.help) {
65
+ console.log(this._helpText);
66
+ process.exit(0);
47
67
  }
48
- version(packageJsonFile, ...names) {
49
- this._packageJsonFile = packageJsonFile;
50
- return this.bool('version', ...names);
68
+ if(rv.version && this._packageJsonFile && fs.existsSync(this._packageJsonFile)) {
69
+ const packageJson = JSON.parse(fs.readFileSync(this._packageJsonFile));
70
+ const version = packageJson.version;
71
+ if(version) {
72
+ console.log(version);
73
+ process.exit(0);
74
+ }
51
75
  }
76
+ return rv;
77
+ }
52
78
 
53
- parse(argv = null) {
54
- let config = { permissive: true };
55
- if(argv !== null) { config.argv = argv; }
56
- let options = this._arg(this._opts, config);
57
- let rv = {};
58
- Object.keys(options).forEach(k => {
59
- rv[k] = options[k];
60
- if(this._names.hasOwnProperty(k)) {
61
- rv[this._names[k]] = options[k];
62
- }
63
- });
64
- if(this._helpText && rv.help) {
65
- console.log(this._helpText);
66
- process.exit(0);
67
- }
68
- if(rv.version && this._packageJsonFile && fs.existsSync(this._packageJsonFile)) {
69
- const packageJson = JSON.parse(fs.readFileSync(this._packageJsonFile));
70
- const version = packageJson.version;
71
- if(version) {
72
- console.log(version);
73
- process.exit(0);
74
- }
75
- }
76
- return rv;
77
- }
79
+ get argv() {
80
+ return this.parse();
81
+ }
78
82
 
79
- get argv() {
80
- return this.parse();
83
+ static terminalWidth(multiplier = 1) {
84
+ const cols = process.stdout.columns;
85
+ if(cols) {
86
+ return Math.round(cols * multiplier);
81
87
  }
88
+ return undefined;
89
+ }
82
90
  }
83
91
 
84
92
  module.exports = (arg) => {
85
- return { argParser: () => new ArgParser(arg) };
86
- }
93
+ const argParser = () => new ArgParser(arg);
94
+ argParser.terminalWidth = ArgParser.terminalWidth;
95
+ return { argParser };
96
+ };