Skip to content

Commit

Permalink
Simplify CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
wojpawlik committed Aug 26, 2022
1 parent 0a114c6 commit 96af9b2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 121 deletions.
1 change: 0 additions & 1 deletion .eslintrc
Expand Up @@ -8,7 +8,6 @@
"files": "*.ts",
"extends": ["plugin:prettier/recommended"],
"rules": {
"sort-imports": ["error", { "ignoreCase": true }],
"@typescript-eslint/ban-ts-comment": "warn",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-explicit-any": "warn",
Expand Down
11 changes: 0 additions & 11 deletions bin/tsconfig.json

This file was deleted.

103 changes: 19 additions & 84 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions package.json
Expand Up @@ -40,11 +40,11 @@
"future.*"
],
"bin": {
"telegraf": "bin/telegraf.mjs"
"telegraf": "lib/cli.mjs"
},
"scripts": {
"prepare": "npm run --silent build",
"build": "tsc && tsc -p bin/tsconfig.json",
"build": "tsc",
"build:docs": "typedoc src/index.ts",
"pretest": "npm run build",
"test": "ava test/*",
Expand All @@ -61,9 +61,7 @@
"dependencies": {
"abort-controller": "^3.0.0",
"debug": "^4.3.3",
"es-main": "^1.2.0",
"minimist": "^1.2.6",
"module-alias": "^2.2.2",
"mri": "^1.2.0",
"node-fetch": "^2.6.7",
"p-timeout": "^4.1.0",
"safe-compare": "^1.1.4",
Expand All @@ -72,8 +70,6 @@
},
"devDependencies": {
"@types/debug": "^4.1.7",
"@types/minimist": "^1.2.2",
"@types/module-alias": "^2.0.1",
"@types/node": "^17.0.14",
"@types/node-fetch": "^2.5.12",
"@types/safe-compare": "^1.1.0",
Expand All @@ -87,7 +83,6 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-standard": "^5.0.0",
"prettier": "^2.5.1",
"typedoc": "^0.23.10",
"typescript": "^4.7.4"
Expand Down
20 changes: 5 additions & 15 deletions bin/telegraf.mts → src/cli.mts
@@ -1,20 +1,12 @@
#!/usr/bin/env node

import debug from 'debug'
import parse from 'mri'
import path from 'path'
import parse from 'minimist'
import { addAlias } from 'module-alias'

// @ts-expect-error Telegraf doesn't build JS and d.ts in the same place, that needs to be fixed
import { Telegraf as _Telegraf } from '../lib/index.js'
import type { Telegraf as Tf, Context, Middleware } from '../typings/index.js'
import type { RequestListener } from 'http'
import type { TlsOptions } from 'tls'

import esMain from 'es-main'

// hack to type Telegraf correctly
const Telegraf: typeof Tf = _Telegraf
import { Telegraf, type Context, type Middleware } from './index.js'

const log = debug('telegraf:cli')

Expand Down Expand Up @@ -121,6 +113,7 @@ export async function main(argv: string[], env: Env = {}) {
path.resolve(process.cwd(), 'package.json')
)) as { main?: string }
file = packageJson.main || 'index.js'
// eslint-disable-next-line no-empty
} catch (err) {}
}

Expand Down Expand Up @@ -149,13 +142,12 @@ export async function main(argv: string[], env: Env = {}) {
try {
if (args.logs) debug.enable('telegraf:*')

addAlias('telegraf', path.join(import.meta.url, '../'))
const mod: Mod = await import(file)
const botHandler = mod.botHandler || mod.default
const httpHandler = mod.httpHandler
const tlsOptions = mod.tlsOptions

const config: Tf.LaunchOptions = {}
const config: Telegraf.LaunchOptions = {}
if (domain) {
config.webhook = {
domain,
Expand All @@ -182,6 +174,4 @@ export async function main(argv: string[], env: Env = {}) {
return 0
}

// run main if called from command line and not imported from another file
if (esMain(import.meta))
process.exitCode = await main(process.argv, process.env as Env)
process.exitCode = await main(process.argv, process.env as Env)
5 changes: 3 additions & 2 deletions tsconfig.json
@@ -1,9 +1,10 @@
{
"compilerOptions": {
"module": "commonjs",
"module": "Node16",
"declaration": true,
"declarationMap": true,
"declarationDir": "typings/",
"esModuleInterop": true,
"outDir": "lib/",
"rootDir": "src/",
"target": "ES2019",
Expand All @@ -24,5 +25,5 @@
"out": "docs/build/",
"readme": "README.md"
},
"include": ["src/**/*.ts"]
"include": ["src/"]
}

0 comments on commit 96af9b2

Please sign in to comment.