I am using commandDir:
require('yargs')
.commandDir('cmds')
.demandCommand()
.help()
.argv
Under cmds there is one file with
const command = 'get <source> [proxy]'
const describe = 'make a get HTTP request'
const builder = {
banana: {
default: 'cool'
},
batman: {
default: 'sad'
}
}
const handler = function (argv) {
// do something with argv.
}
module.exports = { command, describe, builder, handler }
If the file is a .js file it gets correctly loaded by yargs. When I rename the file ending to .ts it is completely ignored :(
Edit: The following works:
require('yargs')
.command(require('./cmds/addFakeProvider'))
.demandCommand()
.help()
.argv
Am I missing something here?
And the yargs.command method
It works for me.
Maybe you should use the correct way to export in Typescript, example:
export const command = 'get <source> [proxy]'
export const describe = 'make a get HTTP request'
export const builder = {
banana: {
default: 'cool'
},
batman: {
default: 'sad'
}
}
export const handler = function (argv) {
// do something with argv.
}
Anyway, if you have the strict mode enabled, you should add types to all constants and parameters.
Another example that is working for me:
import { Argv, Arguments } from 'yargs';
export const command: string = 'serverless <command>';
export const aliases: string[] = ['sls'];
export const desc: string = 'Create or generate new serverless resources';
export const builder = (yargs: Argv) =>
yargs.usage('Usage: $0 serverless <command> [Options]')
.commandDir('serverless')
.demandCommand()
.version(false);
As you see, you can have multiple levels of commandDir
@Junkern, I think what you're looking for is the extensions option for commandDir():
.commandDir('./commands', {
extensions: process.env.NODE_ENV === 'development' ? ['js', 'ts'] : ['js'],
})
You need to tell yargs that it should recognize .ts files. (and, of course, you'll want to run with ts-node or an approprirate require hook in development).
Thanks for the answer! I am working with a different command line tool now, but I hope this helps other people ;)
I posted another solution here - https://github.com/yargs/yargs/issues/571#issuecomment-610769621
It'll work for anyone who wants to run the actual built JS files, instead of the TS source code, if the JS code is built using ES6 modules.
Most helpful comment
@Junkern, I think what you're looking for is the
extensionsoption forcommandDir():You need to tell yargs that it should recognize
.tsfiles. (and, of course, you'll want to run withts-nodeor an approprirate require hook in development).