diff --git a/README.md b/README.md index 6b1679a..91d6ad9 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,9 @@ Copy `config.example.yml` to `config.yml` and set it up. #### Running `yarn && yarn build && node ./dist/index.js` + +#### Custom arguments +```bash +node ./dist/index.js \ + --config ./config-dev.yml +``` diff --git a/package.json b/package.json index 65bed68..4a8c810 100644 --- a/package.json +++ b/package.json @@ -16,11 +16,13 @@ "ramda": "^0.27.1", "typescript": "^4.2.3", "winston": "^3.3.3", + "yargs": "^17.0.0-candidate.10", "yup": "^0.32.9" }, "devDependencies": { "@types/ramda": "^0.27.39", "@types/winston": "^2.4.4", + "@types/yargs": "^16.0.1", "@types/yup": "^0.29.11", "copyfiles": "^2.4.1" } diff --git a/src/config/index.ts b/src/config/index.ts index 0ea11be..91125d1 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -5,9 +5,11 @@ import { Config } from './types'; import { mergeRight } from 'ramda'; import { validateConfig } from './validate'; import logger from '../service/logger'; +import { getCmdArg } from '../utils/args'; +const configPath = getCmdArg('config') const defaultConfig = yaml.load(fs.readFileSync(path.join(__dirname, '../config.example.yml'), 'utf8')); -const userConfig = yaml.load(fs.readFileSync(path.join(__dirname, '../config.yml'), 'utf8')); +const userConfig = yaml.load(fs.readFileSync(configPath || path.join(__dirname, '../config.yml'), 'utf8')); const config = userConfig && mergeRight(defaultConfig, userConfig) || defaultConfig diff --git a/src/index.ts b/src/index.ts index aa5369c..c0e40aa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,6 @@ import { TelegramService } from './service/telegram'; import logger from './service/logger'; import { VkService } from './service/vk'; - try { const config = prepareConfig() const telegramService = new TelegramService(config.telegram) diff --git a/src/utils/args.ts b/src/utils/args.ts new file mode 100644 index 0000000..5dd04ec --- /dev/null +++ b/src/utils/args.ts @@ -0,0 +1,9 @@ +import yargs from 'yargs' + +const { hideBin } = require('yargs/helpers') + +export type CmdArgument = 'config'; + +export const getCmdArg = (name: CmdArgument) => ( + yargs(hideBin(process.argv)).argv[name] as string | undefined +) diff --git a/yarn.lock b/yarn.lock index aac2b7d..3fbf9b9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -42,6 +42,18 @@ dependencies: winston "*" +"@types/yargs-parser@*": + version "20.2.0" + resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-20.2.0.tgz#dd3e6699ba3237f0348cd085e4698780204842f9" + integrity sha512-37RSHht+gzzgYeobbG+KWryeAW8J33Nhr69cjTqSYymXVZEN9NbRYWoYlRtDhHKPVT1FyNKwaTPC1NynKZpzRA== + +"@types/yargs@^16.0.1": + version "16.0.1" + resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-16.0.1.tgz#5fc5d41f69762e00fbecbc8d4bf9dea47d8726f4" + integrity sha512-x4HABGLyzr5hKUzBC9dvjciOTm11WVH1NWonNjGgxapnTHu5SWUqyqn0zQ6Re0yQU0lsQ6ztLCoMAKDGZflyxA== + dependencies: + "@types/yargs-parser" "*" + "@types/yup@^0.29.11": version "0.29.11" resolved "https://registry.yarnpkg.com/@types/yup/-/yup-0.29.11.tgz#d654a112973f5e004bf8438122bd7e56a8e5cd7e" @@ -644,6 +656,19 @@ yargs@^16.1.0: y18n "^5.0.5" yargs-parser "^20.2.2" +yargs@^17.0.0-candidate.10: + version "17.0.0-candidate.10" + resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.0.0-candidate.10.tgz#8d764e4a3633cfd156e90b501124c55d033c89a6" + integrity sha512-vyorQzRrmtJySXUAYAa7oCnsWuWeJuFLhaP86iaUUwNpKYPSuWVrgCdv/cAiQOKSrzMUDIvPgMeahrxKBFOliQ== + dependencies: + cliui "^7.0.2" + escalade "^3.1.1" + get-caller-file "^2.0.5" + require-directory "^2.1.1" + string-width "^4.2.0" + y18n "^5.0.5" + yargs-parser "^20.2.2" + yup@^0.32.9: version "0.32.9" resolved "https://registry.yarnpkg.com/yup/-/yup-0.32.9.tgz#9367bec6b1b0e39211ecbca598702e106019d872"