• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nguyenhuuit / adventofcode-runner / #4

10 Jun 2025 04:49PM UTC coverage: 9.492%. Remained the same
#4

push

nguyenhuuit
ci: publish coverage

20 of 137 branches covered (14.6%)

Branch coverage included in aggregate %.

36 of 453 relevant lines covered (7.95%)

0.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/hooks/useInputFile.ts
1
import fs from 'fs';
2
import { useStore } from 'zustand';
3

4
import { useCallback, useEffect, useState } from 'react';
5

6
import { aocClient } from '@utils/aocClient';
7
import { VALID_YEARS } from '@utils/constants';
8
import { logger } from '@utils/logger';
9

10
import { useExecuteAsStream } from '@hooks/useExecuteAsStream';
11
import { ExecutionStoreInstance } from '@hooks/useExecutionStore';
12
import { useWatcher } from '@hooks/useWatcher';
13

14
export const useInputFile = (executionStore: ExecutionStoreInstance): AppFile => {
×
15
  const [name, setName] = useState<string>('');
×
16
  const [size, setSize] = useState<number>(0);
×
17
  const { year, day, inputMode, getRelativeDir, getInputFile } = useStore(executionStore);
×
18
  const relativeDir = getRelativeDir();
×
19
  const inputFileName = getInputFile();
×
20

21
  const executeSolution = useExecuteAsStream(executionStore);
×
22

23
  const handleFileChange = useCallback(() => {
×
24
    executeSolution();
×
25
  }, [executionStore]);
26

27
  useWatcher({ filePath: name, onChange: handleFileChange });
×
28

29
  useEffect(() => {
×
30
    if (!fs.existsSync(relativeDir)) {
×
31
      fs.mkdirSync(relativeDir, { recursive: true });
×
32
    }
33
    if (!fs.existsSync(inputFileName)) {
×
34
      let data = '';
×
35
      fs.writeFileSync(inputFileName, data, { flag: 'as+' });
×
36
    }
37
    const stats = fs.statSync(inputFileName);
×
38
    setName(inputFileName);
×
39
    setSize(stats.size);
×
40
    if (!VALID_YEARS.includes(year)) {
×
41
      return;
×
42
    }
43
    if (stats.size === 0 && inputMode === 'input') {
×
44
      aocClient
×
45
        .fetchInput(year, day)
46
        .then((data) => {
47
          if (data) {
×
48
            fs.writeFileSync(inputFileName, data);
×
49
            const stats = fs.statSync(inputFileName);
×
50
            setSize(stats.size);
×
51
          }
52
        })
53
        .catch((error) => {
54
          logger.error(`Failed to fetch input: ${error}`);
×
55
        });
56
    }
57
    if (stats.size === 0 && inputMode === 'sample') {
×
58
      aocClient
×
59
        .fetchProblem(year, day)
60
        .then((html) => {
61
          if (html) {
×
62
            const sampleInput = aocClient.extractSampleInput(html);
×
63
            if (sampleInput) {
×
64
              fs.writeFileSync(inputFileName, sampleInput);
×
65
              const stats = fs.statSync(inputFileName);
×
66
              setSize(stats.size);
×
67
            }
68
          }
69
        })
70
        .catch((error) => {
71
          logger.error(`Failed to fetch problem: ${error}`);
×
72
        });
73
    }
74
  }, [relativeDir, inputFileName, year, day, inputMode]);
75

76
  return { name, size };
×
77
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc