i18ntk 2.3.8 → 2.5.0

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/utils/cli-args.js DELETED
@@ -1,210 +0,0 @@
1
- /**
2
- * Simple CLI argument parser to replace commander
3
- * Supports commands, options, and flags
4
- */
5
-
6
- class Command {
7
- constructor(name, description = '') {
8
- this.name = name;
9
- this.description = description;
10
- this.options = [];
11
- this.flags = [];
12
- this.commands = [];
13
- this.actionHandler = null;
14
- this.requiredArgs = [];
15
- }
16
-
17
- description(desc) {
18
- this.description = desc;
19
- return this;
20
- }
21
-
22
- option(flags, description, defaultValue, required = false) {
23
- const option = {
24
- flags,
25
- description,
26
- required,
27
- defaultValue,
28
- long: flags.split(/\s*,\s*/).find(f => f.startsWith('--')).replace('--', ''),
29
- short: (flags.split(/\s*,\s*/).find(f => f.startsWith('-') && !f.startsWith('--')) || '').replace(/-/g, '')
30
- };
31
-
32
- this.options.push(option);
33
- return this;
34
- }
35
-
36
- requiredOption(flags, description, defaultValue) {
37
- return this.option(flags, description, defaultValue, true);
38
- }
39
-
40
- argument(name, description, validator) {
41
- this.requiredArgs.push({ name, description, validator });
42
- return this;
43
- }
44
-
45
- action(handler) {
46
- this.actionHandler = handler;
47
- return this;
48
- }
49
-
50
- command(name, description) {
51
- const cmd = new Command(name, description);
52
- this.commands.push(cmd);
53
- return cmd;
54
- }
55
-
56
- helpInformation() {
57
- let help = [];
58
-
59
- if (this.description) {
60
- help.push(` ${this.description}\n`);
61
- }
62
-
63
- help.push('Usage:');
64
- help.push(` ${process.argv[1]} ${this.name} [options]`);
65
-
66
- if (this.requiredArgs.length > 0) {
67
- help.push('\nArguments:');
68
- this.requiredArgs.forEach(arg => {
69
- help.push(` ${arg.name.padEnd(20)} ${arg.description}`);
70
- });
71
- }
72
-
73
- if (this.options.length > 0) {
74
- help.push('\nOptions:');
75
- this.options.forEach(opt => {
76
- const flags = [
77
- opt.short ? `-${opt.short}` : null,
78
- `--${opt.long}${opt.defaultValue !== undefined ? ` <${opt.long}>` : ''}`
79
- ].filter(Boolean).join(', ');
80
-
81
- let desc = opt.description || '';
82
- if (opt.defaultValue !== undefined) {
83
- desc += ` (default: ${opt.defaultValue})`;
84
- }
85
-
86
- help.push(` ${flags.padEnd(30)} ${desc}`);
87
- });
88
- }
89
-
90
- if (this.commands.length > 0) {
91
- help.push('\nCommands:');
92
- this.commands.forEach(cmd => {
93
- help.push(` ${cmd.name.padEnd(20)} ${cmd.description || ''}`);
94
- });
95
- }
96
-
97
- return help.join('\n');
98
- }
99
-
100
- parse(argv = process.argv.slice(2)) {
101
- const args = {};
102
- const commandArgs = [];
103
- let currentCommand = null;
104
-
105
- // Set default values
106
- this.options.forEach(opt => {
107
- if (opt.defaultValue !== undefined) {
108
- args[opt.long] = opt.defaultValue;
109
- }
110
- });
111
-
112
- // Parse arguments
113
- for (let i = 0; i < argv.length; i++) {
114
- const arg = argv[i];
115
-
116
- // Check for commands
117
- if (!arg.startsWith('-')) {
118
- const cmd = this.commands.find(c => c.name === arg);
119
- if (cmd) {
120
- currentCommand = cmd;
121
- continue;
122
- }
123
- commandArgs.push(arg);
124
- continue;
125
- }
126
-
127
- // Handle --option=value
128
- if (arg.includes('=')) {
129
- const [key, value] = arg.split('=');
130
- const option = this.options.find(opt =>
131
- `--${opt.long}` === key || (opt.short && `-${opt.short}` === key)
132
- );
133
-
134
- if (option) {
135
- args[option.long] = value === 'true' ? true : (value === 'false' ? false : value);
136
- }
137
- continue;
138
- }
139
-
140
- // Handle --option value or -o value
141
- const option = this.options.find(opt =>
142
- `--${opt.long}` === arg || (opt.short && `-${opt.short}` === arg)
143
- );
144
-
145
- if (option) {
146
- if (i + 1 < argv.length && !argv[i + 1].startsWith('-')) {
147
- const value = argv[++i];
148
- args[option.long] = value === 'true' ? true : (value === 'false' ? false : value);
149
- } else {
150
- args[option.long] = true;
151
- }
152
- continue;
153
- }
154
-
155
- // Handle --no-* flags
156
- if (arg.startsWith('--no-')) {
157
- const flagName = arg.substring(5);
158
- args[flagName] = false;
159
- continue;
160
- }
161
- }
162
-
163
- // Check required options
164
- const missingOptions = this.options
165
- .filter(opt => opt.required && args[opt.long] === undefined)
166
- .map(opt => `--${opt.long}`);
167
-
168
- if (missingOptions.length > 0) {
169
- throw new Error(`Missing required options: ${missingOptions.join(', ')}`);
170
- }
171
-
172
- // Check required arguments
173
- if (commandArgs.length < this.requiredArgs.length) {
174
- const missingArgs = this.requiredArgs
175
- .slice(commandArgs.length)
176
- .map(arg => `<${arg.name}>`);
177
-
178
- throw new Error(`Missing required arguments: ${missingArgs.join(' ')}`);
179
- }
180
-
181
- // Call action handler
182
- if (this.actionHandler) {
183
- const actionArgs = commandArgs.slice(0, this.requiredArgs.length);
184
- const remainingArgs = commandArgs.slice(this.requiredArgs.length);
185
-
186
- // Validate argument types if validators are provided
187
- for (let i = 0; i < this.requiredArgs.length; i++) {
188
- const arg = this.requiredArgs[i];
189
- if (arg.validator && !arg.validator(actionArgs[i])) {
190
- throw new Error(`Invalid value for argument ${arg.name}: ${actionArgs[i]}`);
191
- }
192
- }
193
-
194
- return this.actionHandler(...actionArgs, { ...args, _args: remainingArgs });
195
- }
196
-
197
- // If we have a command, parse with that
198
- if (currentCommand) {
199
- return currentCommand.parse(commandArgs);
200
- }
201
-
202
- return { ...args, _args: commandArgs };
203
- }
204
- }
205
-
206
- function program(name = '') {
207
- return new Command(name);
208
- }
209
-
210
- module.exports = { program, Command };
@@ -1,179 +0,0 @@
1
- /**
2
- * Minimal zero-dependency subset of commander used by i18ntk language CLIs.
3
- */
4
-
5
- function toCamelCase(input) {
6
- return String(input || '').replace(/-([a-z])/g, (_, char) => char.toUpperCase());
7
- }
8
-
9
- function parseOptionDefinition(flags, defaultValue) {
10
- const longMatch = flags.match(/--([a-zA-Z0-9-]+)/);
11
- const shortMatch = flags.match(/(^|\s)-([a-zA-Z])(\s|,|$)/);
12
- const hasValue = /<[^>]+>/.test(flags);
13
- const longName = longMatch ? longMatch[1] : null;
14
- const shortName = shortMatch ? shortMatch[2] : null;
15
- const name = toCamelCase(longName || shortName || '');
16
-
17
- return {
18
- flags,
19
- hasValue,
20
- defaultValue,
21
- longFlag: longName ? `--${longName}` : null,
22
- shortFlag: shortName ? `-${shortName}` : null,
23
- name
24
- };
25
- }
26
-
27
- function parseOptions(args, optionDefs) {
28
- const options = {};
29
-
30
- for (const def of optionDefs) {
31
- if (def.defaultValue !== undefined) {
32
- options[def.name] = def.defaultValue;
33
- } else if (!def.hasValue) {
34
- options[def.name] = false;
35
- }
36
- }
37
-
38
- for (let i = 0; i < args.length; i++) {
39
- const arg = args[i];
40
- if (!arg || !arg.startsWith('-')) {
41
- continue;
42
- }
43
-
44
- let matchedDef = null;
45
- let inlineValue;
46
-
47
- if (arg.startsWith('--')) {
48
- const [flag, value] = arg.split('=', 2);
49
- matchedDef = optionDefs.find(def => def.longFlag === flag);
50
- inlineValue = value;
51
- } else {
52
- matchedDef = optionDefs.find(def => def.shortFlag === arg);
53
- }
54
-
55
- if (!matchedDef) {
56
- continue;
57
- }
58
-
59
- if (!matchedDef.hasValue) {
60
- options[matchedDef.name] = true;
61
- continue;
62
- }
63
-
64
- let value = inlineValue;
65
- if (value === undefined) {
66
- const next = args[i + 1];
67
- if (next !== undefined && !String(next).startsWith('-')) {
68
- value = next;
69
- i++;
70
- }
71
- }
72
-
73
- options[matchedDef.name] = value !== undefined ? value : '';
74
- }
75
-
76
- return options;
77
- }
78
-
79
- class MiniCommand {
80
- constructor(name) {
81
- this._name = name;
82
- this._description = '';
83
- this._options = [];
84
- this._action = null;
85
- }
86
-
87
- description(text) {
88
- this._description = text;
89
- return this;
90
- }
91
-
92
- option(flags, _description, defaultValue) {
93
- this._options.push(parseOptionDefinition(flags, defaultValue));
94
- return this;
95
- }
96
-
97
- action(handler) {
98
- this._action = handler;
99
- return this;
100
- }
101
-
102
- execute(args) {
103
- const parsed = parseOptions(args, this._options);
104
- if (typeof this._action === 'function') {
105
- const result = this._action(parsed);
106
- if (result && typeof result.then === 'function') {
107
- result.catch(error => {
108
- console.error(error && error.message ? error.message : String(error));
109
- process.exit(1);
110
- });
111
- }
112
- }
113
- }
114
- }
115
-
116
- class MiniProgram {
117
- constructor() {
118
- this._name = '';
119
- this._description = '';
120
- this._version = '';
121
- this._options = [];
122
- this._commands = [];
123
- this._opts = {};
124
- }
125
-
126
- name(value) {
127
- this._name = value;
128
- return this;
129
- }
130
-
131
- description(value) {
132
- this._description = value;
133
- return this;
134
- }
135
-
136
- version(value) {
137
- this._version = value;
138
- return this;
139
- }
140
-
141
- option(flags, _description, defaultValue) {
142
- this._options.push(parseOptionDefinition(flags, defaultValue));
143
- return this;
144
- }
145
-
146
- command(name) {
147
- const command = new MiniCommand(name);
148
- this._commands.push(command);
149
- return command;
150
- }
151
-
152
- opts() {
153
- return { ...this._opts };
154
- }
155
-
156
- parse(argv = process.argv) {
157
- const args = argv.slice(2);
158
-
159
- if (this._commands.length > 0 && args.length > 0 && !args[0].startsWith('-')) {
160
- const command = this._commands.find(item => item._name === args[0]);
161
- if (command) {
162
- command.execute(args.slice(1));
163
- return this;
164
- }
165
- }
166
-
167
- this._opts = parseOptions(args, this._options);
168
- return this;
169
- }
170
- }
171
-
172
- function createProgram() {
173
- return new MiniProgram();
174
- }
175
-
176
- module.exports = {
177
- createProgram,
178
- program: createProgram()
179
- };