@l10nmonster/cli 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/l10n.cjs +88 -0
  2. package/package.json +5 -7
package/l10n.cjs ADDED
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env node
2
+ /* eslint-disable no-underscore-dangle */
3
+
4
+ const path = require('path');
5
+ const { existsSync } = require('fs');
6
+ const { Command, Argument, InvalidArgumentError } = require('commander');
7
+ const { builtInCmds, runL10nMonster } = require('./out/l10nCommands.cjs');
8
+
9
+ /* eslint-disable no-invalid-this */
10
+
11
+ // eslint-disable-next-line no-unused-vars
12
+ function intOptionParser(value, _dummyPrevious) {
13
+ const parsedValue = parseInt(value, 10);
14
+ if (isNaN(parsedValue)) {
15
+ throw new InvalidArgumentError('Not an integer');
16
+ }
17
+ return parsedValue;
18
+ }
19
+
20
+ async function runMonsterCLI(monsterConfigPath) {
21
+ const monsterCLI = new Command();
22
+ monsterCLI
23
+ .name('l10n')
24
+ .version('0.1.0', '--version', 'output the current version number')
25
+ .description('Continuous localization for the rest of us.')
26
+ .option('-v, --verbose [level]', '0=error, 1=warning, 2=info, 3=verbose', intOptionParser)
27
+ .option('-p, --prj <prj1,...>', 'limit source to specified projects')
28
+ .option('--arg <string>', 'optional config constructor argument')
29
+ .option('--regression', 'keep variables constant during regression testing');
30
+ try {
31
+ const actionHandler = async function actionHandler() {
32
+ const options = this.opts();
33
+ // Need to hack into the guts of commander as it doesn't seem to expose argument names
34
+ const args = Object.fromEntries(this._args.map((arg, idx) => [
35
+ arg._name,
36
+ arg.variadic ? this.args.slice(idx) : this.args[idx]
37
+ ]));
38
+ await runL10nMonster(monsterConfigPath, monsterCLI.opts(), async l10n => {
39
+ await l10n[this.name()]({ ...options, ...args });
40
+ });
41
+ };
42
+ const Config = require(monsterConfigPath);
43
+ [ ...builtInCmds, ...(Config.extensionCmds ?? []) ]
44
+ .forEach(Cmd => {
45
+ const help = Cmd.help;
46
+ const cmd = monsterCLI.command(Cmd.name)
47
+ .description(help.description)
48
+ .action(actionHandler);
49
+ help.options && help.options.forEach(opt => cmd.option(...opt));
50
+ help.requiredOptions && help.requiredOptions.forEach(opt => cmd.requiredOption(...opt));
51
+ help.arguments && help.arguments.forEach(([ arg, desc, choices]) => {
52
+ if (choices) {
53
+ cmd.addArgument(new Argument(arg, desc).choices(choices));
54
+ } else {
55
+ cmd.argument(arg, desc);
56
+ }
57
+ });
58
+ });
59
+ await monsterCLI.parseAsync();
60
+ } catch(e) {
61
+ console.error(`Unable to run: ${e.stack || e}`);
62
+ process.exit(1);
63
+ }
64
+ }
65
+
66
+ function findConfig() {
67
+ let baseDir = path.resolve('.'),
68
+ previousDir = null;
69
+ while (baseDir !== previousDir) {
70
+ const configPath = path.join(baseDir, 'l10nmonster.cjs');
71
+ if (existsSync(configPath)) {
72
+ return configPath;
73
+ }
74
+ previousDir = baseDir;
75
+ baseDir = path.resolve(baseDir, '..');
76
+ }
77
+ return '';
78
+ }
79
+
80
+ (async () => {
81
+ const cfg = findConfig();
82
+ if (cfg) {
83
+ await runMonsterCLI(cfg);
84
+ } else {
85
+ console.error(`Unable to run: couldn't find an l10nmonster.cjs file!`);
86
+ process.exit(1);
87
+ }
88
+ })();
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@l10nmonster/cli",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Continuous localization for the rest of us",
5
5
  "bin": {
6
- "l10n": "out/l10n.cjs"
6
+ "l10n": "l10n.cjs"
7
7
  },
8
8
  "exports": {
9
9
  "import": "./l10nCommands.js",
@@ -13,8 +13,7 @@
13
13
  "type": "module",
14
14
  "scripts": {
15
15
  "esbuild": "esbuild l10nCommands.js --bundle --external:@l10nmonster/core --external:@l10nmonster/helpers --external:commander --external:winston --outfile=out/l10nCommands.cjs --format=cjs --platform=node --target=node18 --sourcemap",
16
- "esbuild-watch": "npm run esbuild -- --watch",
17
- "package": "pkg -t node18-macos-x64 --out-path bin out/l10n.cjs"
16
+ "esbuild-watch": "npm run esbuild -- --watch"
18
17
  },
19
18
  "repository": {
20
19
  "type": "git",
@@ -38,7 +37,7 @@
38
37
  },
39
38
  "dependencies": {
40
39
  "@l10nmonster/core": "^1",
41
- "commander": "^10",
40
+ "commander": "^12",
42
41
  "winston": "^3.7.2"
43
42
  },
44
43
  "peerDependencies": {
@@ -46,7 +45,6 @@
46
45
  },
47
46
  "devDependencies": {
48
47
  "esbuild": "latest",
49
- "eslint": "^8",
50
- "pkg": "^5.4.1"
48
+ "eslint": "latest"
51
49
  }
52
50
  }