Skip to content

Commit 200e1aa

Browse files
authored
fix: allows ESM modules commands to be extensible using visit option (#2468)
1 parent 888db19 commit 200e1aa

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

‎lib/command.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,22 @@ export class CommandInstance {
102102
if (supportedExtension) {
103103
const joined = this.shim.path.join(fullDirPath, file);
104104
const module = req(joined);
105-
const visited = visit(module, joined, file);
105+
const extendableModule = Object.create(
106+
null,
107+
Object.getOwnPropertyDescriptors({...module})
108+
);
109+
const visited = visit(extendableModule, joined, file);
106110
if (visited) {
107111
if (this.requireCache.has(joined)) continue;
108112
else this.requireCache.add(joined);
109113
// Infer command from directory structure if none is given:
110-
if (!module.command) {
111-
module.command = this.shim.path.basename(
114+
if (!extendableModule.command) {
115+
extendableModule.command = this.shim.path.basename(
112116
joined,
113117
this.shim.path.extname(joined)
114118
);
115119
}
116-
this.addHandler(module);
120+
this.addHandler(extendableModule);
117121
}
118122
}
119123
}

‎test/command.mjs‎

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,14 +2297,15 @@ describe('Command', () => {
22972297
})
22982298
.parse()
22992299
);
2300-
commandObject.should.have
2301-
.property('command')
2300+
2301+
expect(commandObject)
2302+
.to.have.property('command')
23022303
.and.equal('dream [command] [opts]');
2303-
commandObject.should.have
2304-
.property('desc')
2304+
expect(commandObject)
2305+
.to.have.property('desc')
23052306
.and.equal('Go to sleep and dream');
2306-
commandObject.should.have.property('builder');
2307-
commandObject.should.have.property('handler');
2307+
expect(commandObject).to.have.property('builder');
2308+
expect(commandObject).to.have.property('handler');
23082309
pathToFile.should.contain(join('test', 'fixtures', 'cmddir', 'dream.js'));
23092310
filename.should.equal('dream.js');
23102311
r.exit.should.equal(true);
@@ -2498,5 +2499,36 @@ describe('Command', () => {
24982499
' --version Show version number [boolean]',
24992500
]);
25002501
});
2502+
2503+
it('allows ESM modules commands to be extensible using visit option', () => {
2504+
const r = checkOutput(() =>
2505+
yargs('remote --help')
2506+
.wrap(null)
2507+
.commandDir('fixtures/esm-command', {
2508+
visit(_commandObject, _pathToFile, _filename) {
2509+
if (!_commandObject.middlewares) {
2510+
_commandObject.middlewares = [];
2511+
}
2512+
return true;
2513+
},
2514+
})
2515+
.parse()
2516+
);
2517+
r.exit.should.equal(true);
2518+
r.errors.length.should.equal(0);
2519+
r.logs
2520+
.join('\n')
2521+
.split(/\n+/)
2522+
.should.deep.equal([
2523+
'usage remote <command>',
2524+
'Manage set of tracked repos',
2525+
'Commands:',
2526+
' usage remote add <name> <url> Add remote named <name> for repo at url <url>',
2527+
' usage remote prune <name> [names..] Delete tracked branches gone stale for remotes',
2528+
'Options:',
2529+
' --help Show help [boolean]',
2530+
' --version Show version number [boolean]',
2531+
]);
2532+
});
25012533
});
25022534
});

0 commit comments

Comments
 (0)