fix(smartcli.helpers): Add robust getUserArgs helper and refactor Smartcli to use it; add MIT license and update documentation
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import * as plugins from './smartcli.plugins.js';
|
||||
import { getUserArgs } from './smartcli.helpers.js';
|
||||
|
||||
// interfaces
|
||||
export interface ICommandObservableObject {
|
||||
@@ -91,7 +92,8 @@ export class Smartcli {
|
||||
* getOption
|
||||
*/
|
||||
public getOption(optionNameArg: string) {
|
||||
const parsedYargs = plugins.yargsParser(process.argv);
|
||||
const userArgs = getUserArgs(process.argv);
|
||||
const parsedYargs = plugins.yargsParser(userArgs);
|
||||
return parsedYargs[optionNameArg];
|
||||
}
|
||||
|
||||
@@ -123,32 +125,10 @@ export class Smartcli {
|
||||
* start the process of evaluating commands
|
||||
*/
|
||||
public startParse(): void {
|
||||
const parsedYArgs = plugins.yargsParser([...process.argv]);
|
||||
|
||||
// lets handle commands
|
||||
// Filter out runtime executable and script path from arguments
|
||||
// Node.js: ["/path/to/node", "/path/to/script.js", ...args]
|
||||
// Deno: ["deno", "/path/to/script.ts", ...args]
|
||||
// Bun: ["/path/to/bun", "/path/to/script.ts", ...args]
|
||||
let counter = 0;
|
||||
let foundCommand = false;
|
||||
const runtimeNames = ['node', 'deno', 'bun', 'tsx', 'ts-node'];
|
||||
parsedYArgs._ = parsedYArgs._.filter((commandPartArg) => {
|
||||
counter++;
|
||||
if (typeof commandPartArg === 'number') {
|
||||
return true;
|
||||
}
|
||||
if (counter <= 2 && !foundCommand) {
|
||||
const isPath = commandPartArg.startsWith('/');
|
||||
const isRuntimeExecutable = runtimeNames.some(name =>
|
||||
commandPartArg === name || commandPartArg.endsWith(`/${name}`)
|
||||
);
|
||||
foundCommand = !isPath && !isRuntimeExecutable;
|
||||
return foundCommand;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
// Get user arguments, properly handling Node.js, Deno (run/compiled), and Bun
|
||||
// Pass process.argv explicitly to handle test scenarios where it's modified
|
||||
const userArgs = getUserArgs(process.argv);
|
||||
const parsedYArgs = plugins.yargsParser(userArgs);
|
||||
const wantedCommand = parsedYArgs._[0];
|
||||
|
||||
// lets handle some standards
|
||||
|
||||
Reference in New Issue
Block a user