@@ -3,7 +3,7 @@ import fs from 'fs';
33import duration from 'pendel' ;
44import sort from 'lodash.sortby' ;
55import happyAsJson from 'happy-json' ;
6- import { errors } from './messages.js' ;
6+ // import {errors} from './messages.js';
77
88// help node 0.10 with Promises
99require ( 'native-promise-only' ) ;
@@ -13,42 +13,40 @@ require('native-promise-only');
1313 *
1414 * @returns {* } Promise
1515 */
16- export function getTimeCardData ( ) {
16+ export function processTimeCardData ( ) {
1717 const self = this ;
1818
1919 return new Promise ( ( resolve , reject ) => {
20- fs . readFile ( self . filepath , 'utf8' , ( err , data ) => {
21- if ( err ) {
22- reject ( err ) ;
23- } else {
24- self . timecardData = JSON . parse ( data ) ;
25- self . name = self . timecardData . name ;
26- const shifts = self . timecardData . shifts ;
20+ self . getTimeCard ( ) . then ( data => {
21+ self . timecardData = JSON . parse ( data ) ;
22+ self . name = self . timecardData . name ;
23+ const shifts = self . timecardData . shifts ;
2724
28- // reset to zero each time the timecard data is read
29- self . totalSeconds = 0 ;
25+ // reset to zero each time the timecard data is read
26+ self . totalSeconds = 0 ;
3027
31- if ( shifts . length > 0 ) {
32- self . shifts = sort ( shifts , shift => {
33- return shift . id ;
34- } ) ;
28+ if ( shifts . length > 0 ) {
29+ self . shifts = sort ( shifts , shift => {
30+ return shift . id ;
31+ } ) ;
3532
36- const lastShift = self . shifts [ self . shifts . length - 1 ] ;
37- if ( lastShift . hasOwnProperty ( 'startTime' ) && lastShift . hasOwnProperty ( 'endTime' ) === false ) {
38- self . clockoutIsPending = true ;
39- self . pendingClockoutIndex = lastShift . id ;
40- }
41- self . shifts . map ( item => {
42- if ( item . hasOwnProperty ( 'startTime' ) && item . hasOwnProperty ( 'endTime' ) ) {
43- self . totalSeconds += duration ( item . startTime , item . endTime ) . totalSeconds ;
44- }
45- } ) ;
46- resolve ( self . shifts ) ;
47- } else {
48- self . shifts = [ ] ;
49- resolve ( self . timecardData ) ;
33+ const lastShift = self . shifts [ self . shifts . length - 1 ] ;
34+ if ( lastShift . hasOwnProperty ( 'startTime' ) && lastShift . hasOwnProperty ( 'endTime' ) === false ) {
35+ self . clockoutIsPending = true ;
36+ self . pendingClockoutIndex = lastShift . id ;
5037 }
38+ self . shifts . map ( item => {
39+ if ( item . hasOwnProperty ( 'startTime' ) && item . hasOwnProperty ( 'endTime' ) ) {
40+ self . totalSeconds += duration ( item . startTime , item . endTime ) . totalSeconds ;
41+ }
42+ } ) ;
43+ resolve ( self . shifts ) ;
44+ } else {
45+ self . shifts = [ ] ;
46+ resolve ( self . timecardData ) ;
5147 }
48+ } ) . catch ( err => {
49+ reject ( err ) ;
5250 } ) ;
5351 } ) ;
5452}
@@ -71,6 +69,25 @@ export function writeTimeCard(data) {
7169 } ) ;
7270}
7371
72+ /**
73+ * Read the .timecard.json file from disk and return its contents
74+ *
75+ * @returns {* } Promise
76+ */
77+ export function getTimeCard ( ) {
78+ const self = this ;
79+
80+ return new Promise ( ( resolve , reject ) => {
81+ fs . readFile ( self . filepath , 'utf8' , ( err , data ) => {
82+ if ( err ) {
83+ reject ( err ) ;
84+ } else {
85+ resolve ( data ) ;
86+ }
87+ } ) ;
88+ } ) ;
89+ }
90+
7491/**
7592 * The blank .timecard.json value.
7693 */
@@ -82,15 +99,9 @@ export function blankTimecard(name) {
8299 * Check if .timecard.json is in a valid json format.
83100 */
84101export function checkForValidTimecard ( ) {
85- try {
86- var timecardData = require ( '../.timecard.json' ) ;
87-
88- if ( happyAsJson ( timecardData ) === false ) {
89- throw new Error ( 'Invalid timecard data.' ) ;
90- }
91- } catch ( err ) {
92- console . log ( errors . invalidTimeCardJSON ) ;
93- console . log ( ' ' + err ) ;
94- process . exit ( 1 ) ;
95- }
102+ this . getTimeCard ( ) . then ( data => {
103+ return happyAsJson ( data ) ;
104+ } ) . catch ( ( ) => {
105+ return false ;
106+ } ) ;
96107}
0 commit comments