cli-argv-util 1.5.0 → 1.5.1

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.
@@ -1,4 +1,4 @@
1
- //! cli-argv-util v1.5.0 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
1
+ //! cli-argv-util v1.5.1 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
2
2
 
3
3
  export type StringFlagMap = {
4
4
  [flag: string]: string | undefined;
@@ -29,8 +29,12 @@ type JsonObject = {
29
29
  };
30
30
  declare const cliArgvUtil: {
31
31
  assert(ok: unknown, message: string | null): void;
32
- readPackageJson(): JsonObject | null;
33
- unescape(flags: StringFlagMap): StringFlagMap;
32
+ readPackageJson(): JsonObject;
33
+ escapers: {
34
+ regex: RegExp;
35
+ char: string;
36
+ }[];
37
+ unescape(text: string, pkg: JsonObject): string;
34
38
  parse(validFlags: string[]): Result;
35
39
  run(packageJson: {
36
40
  [key: string]: unknown;
@@ -1,4 +1,4 @@
1
- //! cli-argv-util v1.5.0 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
1
+ //! cli-argv-util v1.5.1 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
2
2
 
3
3
  import { execSync } from 'node:child_process';
4
4
  import chalk from 'chalk';
@@ -11,8 +11,9 @@ const cliArgvUtil = {
11
11
  throw new Error(`[replacer-util] ${message}`);
12
12
  },
13
13
  readPackageJson() {
14
- const pkgExists = fs.existsSync('package.json');
15
- const pkg = pkgExists ? JSON.parse(fs.readFileSync('package.json', 'utf-8')) : null;
14
+ const pkgFile = 'package.json';
15
+ const getPkg = () => JSON.parse(fs.readFileSync(pkgFile, 'utf-8'));
16
+ const pkg = fs.existsSync(pkgFile) ? getPkg() : {};
16
17
  const fixHiddenKeys = (pkgObj) => {
17
18
  const unhide = (key) => {
18
19
  const newKey = key.replace(/[@./]/g, '-');
@@ -21,60 +22,59 @@ const cliArgvUtil = {
21
22
  };
22
23
  Object.keys(pkgObj).forEach(unhide);
23
24
  };
24
- if (pkg?.dependencies)
25
+ if (pkg.dependencies)
25
26
  fixHiddenKeys(pkg.dependencies);
26
- if (pkg?.devDependencies)
27
+ if (pkg.devDependencies)
27
28
  fixHiddenKeys(pkg.devDependencies);
29
+ if (!pkg.cliConfig && pkg.replacerConfig)
30
+ pkg.cliConfig = pkg.replacerConfig;
28
31
  return pkg;
29
32
  },
30
- unescape(flags) {
31
- const escapers = [
32
- [/{{apos}}/g, "'"],
33
- [/{{bang}}/g, '!'],
34
- [/{{close-curly}}/g, '}'],
35
- [/{{equals}}/g, '='],
36
- [/{{gt}}/g, '>'],
37
- [/{{hash}}/g, '#'],
38
- [/{{lt}}/g, '<'],
39
- [/{{open-curly}}/g, '{'],
40
- [/{{pipe}}/g, '|'],
41
- [/{{quote}}/g, '"'],
42
- [/{{semi}}/g, ';'],
43
- [/{{space}}/g, ' '],
44
- ];
33
+ escapers: [
34
+ { regex: /{{apos}}/g, char: "'" },
35
+ { regex: /{{bang}}/g, char: '!' },
36
+ { regex: /{{close-curly}}/g, char: '}' },
37
+ { regex: /{{equals}}/g, char: '=' },
38
+ { regex: /{{gt}}/g, char: '>' },
39
+ { regex: /{{hash}}/g, char: '#' },
40
+ { regex: /{{lt}}/g, char: '<' },
41
+ { regex: /{{open-curly}}/g, char: '{' },
42
+ { regex: /{{pipe}}/g, char: '|' },
43
+ { regex: /{{quote}}/g, char: '"' },
44
+ { regex: /{{semi}}/g, char: ';' },
45
+ { regex: /{{space}}/g, char: ' ' },
46
+ ],
47
+ unescape(text, pkg) {
45
48
  const macroPattern = /^{{macro:(.*)}}$/;
46
- const flagEntries = Object.entries(flags);
47
- const usesMacros = flagEntries.some(entry => entry[1]?.match(macroPattern));
48
- const pkg = usesMacros ? cliArgvUtil.readPackageJson() : {};
49
- if (!pkg.cliConfig && pkg.replacerConfig)
50
- pkg.cliConfig = pkg.replacerConfig;
51
- const macros = pkg.cliConfig?.macros;
52
- const unescapeOne = (flagValue, escaper) => flagValue.replace(escaper[0], escaper[1]);
53
- const expandMacro = (flagValue) => {
54
- const macroName = flagValue.match(macroPattern)?.[1];
55
- const macroValue = macros?.[macroName];
56
- const missing = macroName && !macroValue;
57
- cliArgvUtil.assert(!missing, `Macro "${macroName}" used but not defined in package.json`);
58
- return macroName ? macroValue : flagValue;
59
- };
60
- const doReplacements = (flagValue) => !flagValue ? undefined : escapers.reduce(unescapeOne, expandMacro(flagValue));
61
- return Object.fromEntries(flagEntries.map(pair => [pair[0], doReplacements(pair[1])]));
49
+ const macros = (pkg.cliConfig?.macros ?? {});
50
+ const macroName = text.match(macroPattern)?.[1];
51
+ const macroValue = macros[macroName];
52
+ const expandedText = macroName ? macroValue : text;
53
+ const missing = macroName && !macroValue;
54
+ cliArgvUtil.assert(!missing, `Macro "${macroName}" used but not defined in package.json`);
55
+ const replace = (flagValue, escaper) => flagValue.replace(escaper.regex, escaper.char);
56
+ return cliArgvUtil.escapers.reduce(replace, expandedText);
62
57
  },
63
58
  parse(validFlags) {
59
+ const pkg = cliArgvUtil.readPackageJson();
64
60
  const toCamel = (token) => token.replace(/-./g, char => char[1].toUpperCase());
65
61
  const toEntry = (pair) => [toCamel(pair[0]), pair[1]];
66
62
  const toPair = (flag) => flag.replace(/^--/, '').split('=');
63
+ const unescape = (value) => !value ? undefined : cliArgvUtil.unescape(value, pkg);
67
64
  const args = cliArgvUtil.unquoteArgs(process.argv.slice(2));
68
65
  const pairs = args.filter(arg => /^--/.test(arg)).map(toPair);
69
- const flagMap = Object.fromEntries(pairs.map(toEntry));
70
- const onEntries = validFlags.map(flag => [toCamel(flag), toCamel(flag) in flagMap]);
66
+ const flagMapRaw = Object.fromEntries(pairs.map(toEntry));
67
+ const flagEntries = Object.entries(flagMapRaw);
68
+ const flagMap = Object.fromEntries(flagEntries.map(pair => [pair[0], unescape(pair[1])]));
69
+ const onEntries = validFlags.map(flag => [toCamel(flag), toCamel(flag) in flagMapRaw]);
71
70
  const flagOn = Object.fromEntries(onEntries);
72
71
  const invalidFlag = pairs.find(pair => !validFlags.includes(pair[0]))?.[0] ?? null;
73
72
  const helpMsg = '\nValid flags are --' + validFlags.join(' --');
74
- const params = args.filter(arg => !/^--/.test(arg));
73
+ const rawParams = args.filter(arg => !/^--/.test(arg));
74
+ const params = rawParams.map(param => cliArgvUtil.unescape(param, pkg));
75
75
  return {
76
- flagMap: cliArgvUtil.unescape(flagMap),
77
- flagMapRaw: flagMap,
76
+ flagMap: flagMap,
77
+ flagMapRaw: flagMapRaw,
78
78
  flagOn: flagOn,
79
79
  invalidFlag: invalidFlag,
80
80
  invalidFlagMsg: invalidFlag ? 'Invalid flag: --' + invalidFlag + helpMsg : null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cli-argv-util",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "Simple utility to parse command line parameters and flags (arguments vector)",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -36,6 +36,8 @@
36
36
  },
37
37
  "cliConfig": {
38
38
  "macros": {
39
+ "html-filename": "file.html",
40
+ "png-filename": "file.png",
39
41
  "lucky-number": "777"
40
42
  }
41
43
  },
@@ -61,17 +63,17 @@
61
63
  "slash": "~5.1"
62
64
  },
63
65
  "devDependencies": {
64
- "@eslint/js": "~9.39",
65
- "@types/node": "~25.0",
66
+ "@eslint/js": "~10.0",
67
+ "@types/node": "~25.5",
66
68
  "add-dist-header": "~1.6",
67
69
  "assert-deep-strict-equal": "~1.2",
68
70
  "copy-file-util": "~1.3",
69
- "eslint": "~9.39",
71
+ "eslint": "~10.1",
70
72
  "jshint": "~2.13",
71
73
  "mocha": "~11.7",
72
74
  "rimraf": "~6.1",
73
75
  "run-scripts-util": "~1.3",
74
76
  "typescript": "~5.9",
75
- "typescript-eslint": "~8.54"
77
+ "typescript-eslint": "~8.57"
76
78
  }
77
79
  }