Skip to content

Want command module to be extendable #2467

@vipzhicheng

Description

@vipzhicheng

yargs/lib/command.ts

Lines 104 to 116 in 888db19

const module = req(joined);
const visited = visit(module, joined, file);
if (visited) {
if (this.requireCache.has(joined)) continue;
else this.requireCache.add(joined);
// Infer command from directory structure if none is given:
if (!module.command) {
module.command = this.shim.path.basename(
joined,
this.shim.path.extname(joined)
);
}
this.addHandler(module);

For now:

When I add middleware in opts.visit (Why? Because I can get command object)

If the command module have no key: middleares, it will throw an error: (BTW, if the command do have a key: middleware, no error happens)

command.middlewares = [middleware];
                                        ^
TypeError: Cannot add property middlewares, object is not extensible

I think maybe we can change these lines code to be follows to solve this issue

        const module = req(joined);
+        const extendableModule = { ...module };
-         const visited = visit(module, joined, file);
+        const visited = visit(extendableModule, joined, file);
        if (visited) {
          if (this.requireCache.has(joined)) continue;
          else this.requireCache.add(joined);
          // Infer command from directory structure if none is given:

-            if (!module.command) {
-            module.command = this.shim.path.basename(
+          if (!extendableModule.command) {
+            extendableModule.command = this.shim.path.basename(
              joined,
              this.shim.path.extname(joined)
            );
          }
-          this.addHandler(module);
+         this.addHandler(extendableModule);

Do you think this makes sense?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions