Compare commits

..

1 commit

Author SHA1 Message Date
2db82b708d [WIP]
- Changed from js to ts
 - Added implementation for profiles
 - Can also be used as a node_module
2020-03-22 18:09:33 -04:00
2 changed files with 4 additions and 19 deletions

View file

@ -2,16 +2,12 @@ import Telme from './telme';
import { ArgParser, ERunMode, ISimpleMessageOptions, ITaskOptions, IInitProfileOptions } from './utils'; import { ArgParser, ERunMode, ISimpleMessageOptions, ITaskOptions, IInitProfileOptions } from './utils';
import Config, { IProfileConfig } from './config/config'; import Config, { IProfileConfig } from './config/config';
import Init from './flows/init' import Init from './flows/init'
const { version } = require('../package.json');
async function main() { async function main() {
const parsed = ArgParser.parse(process.argv); const parsed = ArgParser.parse(process.argv);
let config: IProfileConfig; let config: IProfileConfig;
let options: any; let options: any;
switch (parsed.mode) { switch (parsed.mode) {
case ERunMode.VERSION:
console.log(`[${Config.APP_NAME}] version ${version}`);
break;
case ERunMode.HELP: case ERunMode.HELP:
printHelp(); printHelp();
break; break;
@ -38,12 +34,11 @@ async function main() {
function printHelp() { function printHelp() {
const cliFlags = ArgParser.CLI_OPTIONS; const cliFlags = ArgParser.CLI_OPTIONS;
const message = const message =
`${Config.APP_NAME} v${version} - A CLI Telegram message tool `${Config.APP_NAME} - A CLI Telegram message tool
[Usage]: $ ${Config.APP_NAME} <telme_options> <?command> <arguments> [Usage]: $ ${Config.APP_NAME} <telme_options> <?command> <arguments>
Options: Options:
\t ${cliFlags.versionFlags.join(', ')} \t\t Print ${Config.APP_NAME} version.
\t ${cliFlags.helpFlags.join(', ')} \t\t This help page. \t ${cliFlags.helpFlags.join(', ')} \t\t This help page.
\t ${cliFlags.profileFlags.join(', ')} \t\t Specify a profile to use. This is optional. defaults to 'default' profile. \t ${cliFlags.profileFlags.join(', ')} \t\t Specify a profile to use. This is optional. defaults to 'default' profile.
\t ${cliFlags.initFlags.join(', ')} \t\t Will generate a config file for a given profile (defaults to 'default' profile). \t ${cliFlags.initFlags.join(', ')} \t\t Will generate a config file for a given profile (defaults to 'default' profile).

View file

@ -1,7 +1,6 @@
import InvalidArgumentsError from "../errors/invalid_arguments.error"; import InvalidArgumentsError from "../errors/invalid_arguments.error";
const CLI_OPTIONS = { const CLI_OPTIONS = {
versionFlags: ['--version', '-v'],
helpFlags: ['--help', '-h'], helpFlags: ['--help', '-h'],
initFlags: ['--init', '-i'], initFlags: ['--init', '-i'],
profileFlags: ['--profile', '-p'], profileFlags: ['--profile', '-p'],
@ -14,11 +13,7 @@ export class ArgParser {
static parse(testArgs: string[] = null): IRunOptions { static parse(testArgs: string[] = null): IRunOptions {
let cliArgs = testArgs ? testArgs.slice(2) : process.argv.slice(2); let cliArgs = testArgs ? testArgs.slice(2) : process.argv.slice(2);
let tokens = tokenize(cliArgs); let tokens = tokenize(cliArgs);
if (tokens.version) { if (tokens.help) {
return {
mode: ERunMode.VERSION
}
} else if (tokens.help) {
return { return {
mode: ERunMode.HELP mode: ERunMode.HELP
} }
@ -54,16 +49,13 @@ export class ArgParser {
function tokenize(args: string[]) { function tokenize(args: string[]) {
let cliArgs = args; let cliArgs = args;
if (!cliArgs.length) throw new InvalidArgumentsError('Missing a command to run'); if (!cliArgs.length) throw new InvalidArgumentsError('Missing a command to run');
const tokens: ITokenizeArgs = { init: false, help: false, version: false }; const tokens: ITokenizeArgs = { init: false, help: false };
// ['node', 'execCommand', ...]; // ['node', 'execCommand', ...];
let mode: ERunMode = ERunMode.TASK_MESSAGE; let mode: ERunMode = ERunMode.TASK_MESSAGE;
while (cliArgs.length > 0) { while (cliArgs.length > 0) {
let tokenCursor = 1; let tokenCursor = 1;
const token = cliArgs[0]; const token = cliArgs[0];
if (CLI_OPTIONS.versionFlags.indexOf(token) >= 0) { if (CLI_OPTIONS.helpFlags.indexOf(token) >= 0) {
tokens.version = true;
return tokens;
} else if (CLI_OPTIONS.helpFlags.indexOf(token) >= 0) {
tokens.help = true; tokens.help = true;
return tokens; return tokens;
} else if (CLI_OPTIONS.initFlags.indexOf(token) >= 0) { } else if (CLI_OPTIONS.initFlags.indexOf(token) >= 0) {
@ -97,7 +89,6 @@ function tokenize(args: string[]) {
} }
interface ITokenizeArgs { interface ITokenizeArgs {
version: boolean;
help: boolean; help: boolean;
profile?: string; profile?: string;
init: boolean; init: boolean;
@ -131,7 +122,6 @@ export interface ITaskOptions extends IBasicOptions {
export enum ERunMode { export enum ERunMode {
VERSION,
HELP, HELP,
INIT, INIT,
SIMPLE_MESSAGE, SIMPLE_MESSAGE,