diff --git a/commands/function.ts b/commands/function.ts index d99c59e43..cfe7cfc02 100644 --- a/commands/function.ts +++ b/commands/function.ts @@ -1,16 +1,31 @@ -// @ts-nocheck -const { addGlobalOptions } = require('../lib/commonOpts'); -const list = require('./function/list'); +import { Argv } from 'yargs'; +import list from './function/list'; const deploy = require('./function/deploy'); const server = require('./function/server'); -const { i18n } = require('../lib/lang'); +import { i18n } from '../lib/lang'; +import { makeYargsBuilder } from '../lib/yargsUtils'; +import { YargsCommandModuleBucket } from '../types/Yargs'; -exports.command = ['function', 'functions']; -exports.describe = i18n(`commands.function.describe`); +export const command = ['function', 'functions']; +export const describe = i18n(`commands.function.describe`); -exports.builder = yargs => { - addGlobalOptions(yargs); +function functionBuilder(yargs: Argv): Argv { yargs.command(list).command(deploy).command(server).demandCommand(1, ''); - return yargs; +} + +const builder = makeYargsBuilder(functionBuilder, command, describe, { + useGlobalOptions: true, +}); + +const functionCommand: YargsCommandModuleBucket = { + command, + describe, + builder, + handler: () => {}, }; + +export default functionCommand; + +// TODO Remove this legacy export once we've migrated all commands to TS +module.exports = functionCommand; diff --git a/commands/function/list.ts b/commands/function/list.ts index 718204919..211816210 100644 --- a/commands/function/list.ts +++ b/commands/function/list.ts @@ -1,26 +1,36 @@ -// @ts-nocheck -const moment = require('moment'); -const { getRoutes } = require('@hubspot/local-dev-lib/api/functions'); -const { logger } = require('@hubspot/local-dev-lib/logger'); -const { logError, ApiErrorContext } = require('../../lib/errorHandlers/index'); -const { getTableContents, getTableHeader } = require('../../lib/ui/table'); -const { - addConfigOptions, - addAccountOptions, - addUseEnvironmentOptions, -} = require('../../lib/commonOpts'); -const { trackCommandUsage } = require('../../lib/usageTracking'); -const { i18n } = require('../../lib/lang'); - -const { EXIT_CODES } = require('../../lib/enums/exitCodes'); - -exports.command = ['list', 'ls']; -exports.describe = i18n('commands.function.subcommands.list.describe'); - -exports.handler = async options => { - const { derivedAccountId } = options; - - trackCommandUsage('functions-list', null, derivedAccountId); +import { Argv, ArgumentsCamelCase } from 'yargs'; +import moment from 'moment'; +import { getRoutes } from '@hubspot/local-dev-lib/api/functions'; +import { logger } from '@hubspot/local-dev-lib/logger'; + +import { logError, ApiErrorContext } from '../../lib/errorHandlers/index'; +import { getTableContents, getTableHeader } from '../../lib/ui/table'; +import { trackCommandUsage } from '../../lib/usageTracking'; +import { i18n } from '../../lib/lang'; +import { EXIT_CODES } from '../../lib/enums/exitCodes'; +import { + CommonArgs, + ConfigArgs, + AccountArgs, + EnvironmentArgs, + YargsCommandModule, +} from '../../types/Yargs'; +import { makeYargsBuilder } from '../../lib/yargsUtils'; + +const command = ['list', 'ls']; +const describe = i18n('commands.function.subcommands.list.describe'); + +type FunctionListArgs = CommonArgs & + ConfigArgs & + AccountArgs & + EnvironmentArgs & { json?: boolean }; + +async function handler( + args: ArgumentsCamelCase +): Promise { + const { derivedAccountId } = args; + + trackCommandUsage('function-list', undefined, derivedAccountId); logger.debug( i18n('commands.function.subcommands.list.debug.gettingFunctions') @@ -39,7 +49,7 @@ exports.handler = async options => { ); } - if (options.json) { + if (args.json) { return logger.log(routesResp.objects); } @@ -58,13 +68,9 @@ exports.handler = async options => { getTableHeader(['Route', 'Method', 'Secrets', 'Created', 'Updated']) ); return logger.log(getTableContents(functionsAsArrays)); -}; - -exports.builder = yargs => { - addConfigOptions(yargs); - addAccountOptions(yargs); - addUseEnvironmentOptions(yargs); +} +function functionListBuilder(yargs: Argv): Argv { yargs.options({ json: { describe: i18n( @@ -73,4 +79,26 @@ exports.builder = yargs => { type: 'boolean', }, }); + + return yargs as Argv; +} + +const builder = makeYargsBuilder( + functionListBuilder, + command, + describe, + { + useConfigOptions: true, + useAccountOptions: true, + useEnvironmentOptions: true, + } +); + +const functionListCommand: YargsCommandModule = { + command, + describe, + handler, + builder, }; + +export default functionListCommand;