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

Feature/add time #2

Merged
merged 2 commits into from
Apr 4, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Removed
### BREAKING CHANGES

## [1.0.0-beta.4] - 2020-04-04
### Added
- Option --time for the commands so you can put the time you want in them

## [1.0.0-beta.3] - 2020-04-01
### Added
- Option --debug so you can the puppeteer running in case you need to debug
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Khalid Zoabi
Copyright (c) 2020 Gabriel García Seco

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@ Stop commands updates the time when you stop the task

### Options

- --debug you can pass the option argument so you can see puppeteer running
| Option | Alias | Description |
| --- | --- | --- |
| --debug | -d | Debug puppeteer so you what happens under the hood |
| --time | -t | Give a time in hh:mm format so you can put the time you want for a day |
2 changes: 1 addition & 1 deletion package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "holded-bot",
"version": "1.0.0-beta.3",
"description": "Holded bot from terminal",
"version": "1.0.0-beta.4",
"description": "Holded bot so you can fill your time from terminal",
"author": "Gabriel García Seco <[email protected]>",
"keywords": [
"typescript",
"holded",
"cli",
"starter"
"lazydev"
],
"license": "MIT",
"main": "./dist/index.js",
Expand Down
31 changes: 25 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,57 @@
#!/usr/bin/env node

const packageJSON = require('../package.json'); // eslint-disable-line
import program from 'commander';
import input from '@inquirer/input';
import password from '@inquirer/password';
import keytar from 'keytar';
import { startWork, stopWork } from './holded';
import { Routes } from './routes';
const packageJSON = require('../package.json'); // eslint-disable-line

function executeCli({ email, company, password }) {
interface ExecuteCliArgs {
email: string;
company: string;
password: string;
}

function executeCli({ email, company, password }: ExecuteCliArgs): void {
const routes = Routes({ business: company });

program.version(packageJSON.version);

program.option('-debug, --debug', 'Debug puppeteer');
program.option('-d, --debug', 'Debug puppeteer');
program.option('-t, --time <time>', 'You can add the exact time here');

program
.command('start')
.description('writes into the holded the time')
.action(function () {
startWork({ email, password }, routes, { debug: program.debug });
startWork({ email, password }, routes, {
debug: program.debug,
time: program.time
});
});

program
.command('stop')
.description('updates holded time with the time')
.action(function () {
stopWork({ email, password }, routes, { debug: program.debug });
stopWork({ email, password }, routes, {
debug: program.debug,
time: program.time
});
});

program.parse(process.argv);
}

async function questions() {
interface QuestionArgs {
companyQuestion: string;
emailQuestion: string;
passwordQuestion: string;
}

async function questions(): Promise<QuestionArgs> {
const companyQuestion = await input({ message: 'Enter your company name' });
const emailQuestion = await input({ message: 'Enter your email' });
const passwordQuestion = await password({
Expand Down
11 changes: 7 additions & 4 deletions src/holded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ interface AccountArgs {
}

interface CliArgs {
debug: boolean;
debug?: boolean;
time?: boolean;
}

export async function startWork(
Expand All @@ -27,8 +28,9 @@ export async function startWork(
const { page, browser } = await startWithUrl(puppeteer, url, args);
const dayOfTheWeek = DateTime.local().weekday;

const time = DateTime.local().toLocaleString(DateTime.TIME_SIMPLE);
const timeEnd = DateTime.local()
const time =
args.time || DateTime.local().toLocaleString(DateTime.TIME_SIMPLE);
const timeEnd = DateTime.fromISO(time)
.plus({ minutes: 30 })
.toLocaleString(DateTime.TIME_SIMPLE);

Expand Down Expand Up @@ -59,7 +61,8 @@ export async function startWork(
export async function stopWork(account: AccountArgs, routes, args: CliArgs) {
const url = routes.LOGIN;
const { page, browser } = await startWithUrl(puppeteer, url, args);
const time = DateTime.local().toLocaleString(DateTime.TIME_SIMPLE);
const time =
args.time || DateTime.local().toLocaleString(DateTime.TIME_SIMPLE);
const date = DateTime.local().toFormat('dd-MM-yyyy');

login(page, {
Expand Down
12 changes: 6 additions & 6 deletions src/puppetter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export async function login(page, form) {
export async function login(page, form): Promise<void> {
try {
await page.type('#tpemail', form.username);
await page.type('#tppassword', form.password);
Expand All @@ -12,7 +12,7 @@ export async function login(page, form) {
export async function startWithUrl(
puppeteer,
url: string,
options: { debug: boolean }
options: { debug?: boolean }
) {
try {
const browser = await puppeteer.launch({
Expand All @@ -31,7 +31,7 @@ export async function startWithUrl(
}
}

export async function addTimeButton(page, dayOfTheWeek) {
export async function addTimeButton(page, dayOfTheWeek: number): Promise<void> {
try {
const links = await page.$$('div.timeline-additem');
const link = links[dayOfTheWeek - 1];
Expand All @@ -41,7 +41,7 @@ export async function addTimeButton(page, dayOfTheWeek) {
}
}

async function fillInput(page, options) {
async function fillInput(page, options): Promise<void> {
try {
await page.evaluate(
(selector, time) => {
Expand All @@ -61,7 +61,7 @@ async function fillInput(page, options) {
}
}

export async function fillInputs(page, options) {
export async function fillInputs(page, options): Promise<void> {
try {
await fillInput(page, {
selector: 'timerange-inputstart',
Expand All @@ -79,7 +79,7 @@ export async function fillInputs(page, options) {
}
}

export async function editLastTimeline(page, options) {
export async function editLastTimeline(page, options): Promise<void> {
try {
await page.evaluate(date => {
const selector = `td[data-date="${date}"] .timeline-item`;
Expand Down