-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
182 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.timeclock/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { IInvoiceHandler } from "../../domain/Invoice/IInvoiceHandler.ts"; | ||
|
||
export class InvoiceHandler implements IInvoiceHandler { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { IPunchHandler } from "../../domain/Punch/IPunchHandler.ts"; | ||
import { Punch } from "../../domain/Punch/Punch.ts"; | ||
import { PunchType } from "../../domain/Punch/PunchType.ts"; | ||
import { executeShellCommandAsync } from "../../infra/IO/Shell.ts"; | ||
import * as FileManager from "../../infra/IO/Files.ts"; | ||
import { Shift } from "../../domain/Shift/Shift.ts"; | ||
import { ShiftHandler } from "../Shift/ShiftHandler.ts"; | ||
|
||
export class PunchHandler implements IPunchHandler { | ||
constructor() { | ||
} | ||
|
||
async createPunchAsync(punch: Punch): Promise<void> { | ||
if (punch.type === PunchType.Sart) { | ||
await this.createStartPunchAsync(punch); | ||
} else { | ||
await this.createEndPunchAsync(punch); | ||
} | ||
await this.commitPunchAsync(punch); | ||
} | ||
|
||
private async commitPunchAsync(punch: Punch) { | ||
const msg = punch.type == PunchType.Sart ? "START" : "END"; | ||
(await executeShellCommandAsync("git", ["commit", "-m", `\"TIMECLOCK PUNCH ${msg} - ${punch.user}\"`])).verifyZeroReturnCode(); | ||
} | ||
|
||
private async createStartPunchAsync(punch: Punch) { | ||
await FileManager.createDirectoryAsync(punch.punchDir); | ||
await Deno.writeTextFile(punch.punchFilePath, Date.now().toString()); | ||
(await executeShellCommandAsync("git", ["add", punch.punchFilePath])).verifyZeroReturnCode(); | ||
} | ||
|
||
private async createEndPunchAsync(punch: Punch) { | ||
const nowUtcMs = Date.now().toString(); | ||
const punchStartUtcMs = await Deno.readTextFile(punch.punchFilePath); | ||
const shift: Shift = new Shift(punch.user, punchStartUtcMs, nowUtcMs); | ||
|
||
await Deno.remove(punch.punchFilePath); | ||
(await executeShellCommandAsync("git", ["add", punch.punchFilePath])).verifyZeroReturnCode(); | ||
|
||
await new ShiftHandler().createShiftAsync(shift); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { IShiftHandler } from "../../domain/Shift/IShiftHandler.ts"; | ||
import { Shift } from "../../domain/Shift/Shift.ts"; | ||
import * as FileManager from "../../infra/IO/Files.ts"; | ||
import { executeShellCommandAsync } from "../../infra/IO/Shell.ts"; | ||
|
||
export class ShiftHandler implements IShiftHandler { | ||
|
||
public async createShiftAsync(shift: Shift): Promise<void> { | ||
await FileManager.createDirectoryAsync(shift.shiftDir); | ||
await Deno.writeTextFile(shift.shiftFilePath, shift.diffHours.toString()); | ||
(await executeShellCommandAsync("git", ["add", shift.shiftFilePath])).verifyZeroReturnCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Punch } from "../domain/Punch/Punch.ts"; | ||
import { PunchType } from "../domain/Punch/PunchType.ts"; | ||
import { executeShellCommandAsync } from "../infra/IO/Shell.ts"; | ||
import { PunchHandler } from "./Punch/PunchHandler.ts"; | ||
|
||
async function validateCleanWorkingTreeAsync() { | ||
const statusOutput = (await executeShellCommandAsync("git", ["status"])).verifyZeroReturnCode(); | ||
if (!statusOutput.stdout.includes("nothing to commit, working tree clean")) { | ||
throw "punches must occur on a clean working tree"; | ||
} | ||
} | ||
const _punchHandler = new PunchHandler(); | ||
async function main() { | ||
const user: string = Deno.args[0]; | ||
const isEndPunch: boolean = Deno.args.some(x => x === "--end"); | ||
const punchType: PunchType = isEndPunch ? PunchType.End : PunchType.Sart; | ||
|
||
const punch = new Punch(punchType, user); | ||
|
||
await validateCleanWorkingTreeAsync(); | ||
await _punchHandler.createPunchAsync(punch); | ||
} | ||
|
||
await main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const ONE_HOUR_IN_MS: number = 3600000; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface IInvoiceHandler { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class Invoice { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Punch } from "./Punch.ts"; | ||
|
||
export interface IPunchHandler { | ||
createPunchAsync(punch: Punch): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { PunchType } from "./PunchType.ts"; | ||
|
||
export class Punch { | ||
type: PunchType; | ||
user: string; | ||
punchDir: string; | ||
punchFilePath: string; | ||
|
||
constructor(punchType: PunchType, user: string) { | ||
this.type = punchType; | ||
this.user = user; | ||
this.punchDir = `./.timeclock/punches/${user}`; | ||
this.punchFilePath = `${this.punchDir}/punch`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum PunchType { | ||
Sart, | ||
End | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Shift } from "./Shift.ts"; | ||
|
||
export interface IShiftHandler { | ||
createShiftAsync(shift: Shift): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as DomainConstants from "../Constants.ts"; | ||
|
||
export class Shift { | ||
user: string; | ||
diffHours: number | ||
shiftDir: string; | ||
shiftFilePath: string; | ||
|
||
constructor(user: string, punchStartUtcMs: string, punchEndUtcMs: string) { | ||
this.user = user; | ||
const diffMs: number = Number.parseInt(punchEndUtcMs) - Number.parseInt(punchStartUtcMs); | ||
this.diffHours = diffMs/DomainConstants.ONE_HOUR_IN_MS; | ||
this.shiftDir = `./.timeclock/shifts/${user}`; | ||
this.shiftFilePath = `${this.shiftDir}/shift_${crypto.randomUUID()}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {exists} from "https://deno.land/std@0.201.0/fs/mod.ts"; | ||
|
||
export async function createDirectoryAsync(path: string) { | ||
const dirExists = await exists(path); | ||
if (!dirExists) { | ||
await Deno.mkdir(path, { | ||
recursive: true | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ShellOutput } from "./ShellOutput.ts"; | ||
|
||
export async function executeShellCommandAsync(command: string, args: string[]): Promise<ShellOutput> { | ||
const output = await new Deno.Command(command, { args: args }).output(); | ||
return new ShellOutput(output); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export class ShellOutput { | ||
private _output: Deno.CommandOutput; | ||
|
||
constructor(output: Deno.CommandOutput) { | ||
this._output = output; | ||
} | ||
|
||
public get code(): number { | ||
return this._output.code; | ||
} | ||
|
||
public get stderr(): string { | ||
return new TextDecoder().decode(this._output.stderr); | ||
} | ||
|
||
public get stdout(): string { | ||
return new TextDecoder().decode(this._output.stdout); | ||
} | ||
|
||
public verifyZeroReturnCode(): ShellOutput { | ||
if (this._output.code !== 0) { | ||
// todo - enhance error handling | ||
console.log(this.stdout, this.stderr); | ||
throw `non-zero RC: ${this._output.code}`; | ||
} | ||
return this; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.