Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify CLI #1677

Merged
merged 2 commits into from Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions .eslintrc
Expand Up @@ -5,10 +5,9 @@
},
"overrides": [
{
"files": "*.ts",
"files": ["*.ts", "*.mts"],
"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
21 changes: 6 additions & 15 deletions bin/telegraf.mts → src/cli.mts
@@ -1,20 +1,13 @@
#!/usr/bin/env node
/* global console, process */

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 +114,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 +143,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 +175,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/"]
}