Skip to content

Commit 15d955a

Browse files
committed
Removed recalibrate command, improved day detection
1 parent 41f7211 commit 15d955a

File tree

15 files changed

+137
-93
lines changed

15 files changed

+137
-93
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ npm i aoctimer -D
4949

5050
If you prefer a single binary file (Node.js included), you can download it for the most popular platforms:
5151

52-
- [aoctimer-linux64-v2.0.0.zip](https://github.com/caderek/aoctimer/releases/download/v2.0.0/aoctimer-linux64-v2.0.0.zip)
53-
- [aoctimer-macos-v2.0.0.zip](https://github.com/caderek/aoctimer/releases/download/v2.0.0/aoctimer-macos-v2.0.0.zip)
54-
- [aoctimer-windows64-v2.0.0.zip](https://github.com/caderek/aoctimer/releases/download/v2.0.0/aoctimer-windows64-v2.0.0.zip)
52+
- [aoctimer-linux64-v3.0.0.zip](https://github.com/caderek/aoctimer/releases/download/v3.0.0/aoctimer-linux64-v3.0.0.zip)
53+
- [aoctimer-macos-v3.0.0.zip](https://github.com/caderek/aoctimer/releases/download/v3.0.0/aoctimer-macos-v3.0.0.zip)
54+
- [aoctimer-windows64-v3.0.0.zip](https://github.com/caderek/aoctimer/releases/download/v3.0.0/aoctimer-windows64-v3.0.0.zip)
5555

5656
## Preparing your code
5757

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aoctimer",
3-
"version": "2.0.0",
3+
"version": "3.0.0",
44
"description": "AoC TIMER - compare your AoC solutions with others",
55
"repository": "https://github.com/caderek/aoctimer",
66
"homepage": "https://github.com/caderek/aoctimer",

src/actions/calibrate.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/actions/help.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const help = () => {
22
console.log(`
3-
AoC TIMER v.2.0.0
3+
AoC TIMER v.3.0.0
44
55
Commands:
66
init [language] [year] Calibrate and create local config
7-
calibrate Recalibrate the timer
87
summary Display the summary
98
[-d, --day <day>] <command> Run the timer for your command
109
--help, -h Show help page

src/actions/init.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as readline from "readline"
22
import { save } from "../config"
3-
import calibrate from "./calibrate"
3+
import getSystem from "../helpers/get-system"
4+
import benchmark from "../benchmark"
45

56
const ask = (rl, question): Promise<string> =>
67
new Promise((resolve) => rl.question(question, resolve))
@@ -15,25 +16,28 @@ const init = async ({ language, year }) => {
1516
output: process.stdout,
1617
})
1718

18-
if (year === undefined) {
19-
year = await ask(rl, `Year${yearData ? ` (default ${yearData[0]})` : ""}: `)
20-
}
21-
2219
if (language === undefined) {
2320
language = await ask(rl, "Language: ")
2421
}
2522

23+
if (year === undefined) {
24+
year = await ask(rl, `Year${yearData ? ` (default ${yearData[0]})` : ""}: `)
25+
}
26+
2627
rl.close()
2728

29+
console.log("Calibrating (it may take a while)...")
30+
const bench = benchmark()
31+
2832
save({
2933
version: "2.0.0",
3034
year: (year ? year : yearData[0]) as string,
3135
language: (language || "unknown") as string,
36+
system: getSystem().toString(),
37+
benchmark: bench,
3238
days: [],
3339
})
3440

35-
calibrate()
36-
3741
console.log("Done!")
3842
}
3943

src/actions/run.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { spawn } from "child_process"
1+
import { spawn, execSync } from "child_process"
22
import config, { check, save } from "../config"
33
import views from "../views"
44
import stats from "../stats"
@@ -24,7 +24,7 @@ Total 12345
2424
My solution has total time of 123.100 us.
2525
`
2626

27-
const runStats = (time: bigint, day: string) => {
27+
const runStats = (time: bigint, day: string, command: string | null) => {
2828
if (time !== null) {
2929
const data = stats.day(time)
3030

@@ -43,41 +43,34 @@ const runStats = (time: bigint, day: string) => {
4343
})
4444
}
4545

46-
console.log(views.day(day, data))
46+
return data
4747
} else {
4848
console.log(NO_TIME_MESSAGE)
4949
}
5050
}
5151

52-
const run = ({ day, exec, time }) => {
52+
const run = ({ day, command, time }) => {
5353
if (!check() || !config.benchmark) {
5454
console.log("Please run 'aoctimer init' first.")
5555
return
5656
}
5757

58-
day = day ?? getDay()
58+
day = day ?? getDay(command)
5959

6060
if (time !== null) {
61-
runStats(time, day)
61+
runStats(time, day, null)
6262
process.exit()
6363
}
6464

65-
let output = ""
66-
67-
const [command, ...args] = exec
68-
69-
const ps = spawn(command, args, {
70-
stdio: ["pipe", "pipe", process.stderr],
71-
})
72-
73-
ps.stdout.on("data", (data) => {
74-
output += data.toString()
75-
})
76-
77-
ps.once("close", () => {
65+
try {
66+
const output = execSync(command).toString()
7867
const time = extractTime(output)
79-
runStats(time, day)
80-
})
68+
console.log("Running")
69+
const data = runStats(time, day, command)
70+
console.log(views.day(day, data))
71+
} catch (e) {
72+
console.log(`Failed to execute the command: ${command}`)
73+
}
8174
}
8275

8376
export default run

src/actions/summary.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const summary = () => {
66
console.log("Please run 'aoctimer init' first.")
77
return
88
}
9+
910
console.log(views.summary(config.year, config.days))
1011
}
1112

src/config/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ type Data = {
1414
year?: string
1515
benchmark?: number
1616
language?: string
17+
system?: string
1718
days?: Day[]
1819
}
1920

20-
const fileName = `aoctimer.json`
21+
const fileName = `.aoctimer.json`
2122

2223
const configDir = process.cwd()
2324
const configFile = path.join(configDir, fileName)

src/helpers/get-day.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
const getDay = () => {
2-
const dayData = `${process.cwd()} ${process.argv.slice(3).join(" ")}`
3-
.replace(/20\d{2}/, "")
4-
.match(/\d?\d/)
1+
const getDay = (command: string | null) => {
2+
if (command === null) {
3+
return "??"
4+
}
55

6-
return dayData ? dayData[0].padStart(2, "0") : "??"
6+
const dayData = command.replace(/20\d{2}/, "").match(/\d?\d/g)
7+
8+
if (dayData === null) {
9+
return "??"
10+
}
11+
12+
const day = Number(dayData[dayData.length - 1])
13+
14+
if (day < 1 || day > 25) {
15+
return "??"
16+
}
17+
18+
return String(day).padStart(2, "0")
719
}
820

921
export default getDay

src/helpers/get-system.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as os from "os"
2+
3+
const OS = {
4+
aix: "AIX",
5+
darwin: "MacOS",
6+
freebsd: "FreeBSD",
7+
linux: "Linux",
8+
openbsd: "OpenBSD",
9+
sunos: "SunOS",
10+
win32: "Windows",
11+
}
12+
13+
const getSystem = () => {
14+
const cpu = os.cpus()[0].model
15+
const mem = Math.round(os.totalmem() / 10 ** 9)
16+
const platform = OS[os.platform()]
17+
const arch = os.arch()
18+
19+
return {
20+
cpu,
21+
mem,
22+
platform,
23+
arch,
24+
toString() {
25+
return `${cpu}|${arch}|${platform}|${mem}GB`
26+
},
27+
}
28+
}
29+
30+
export default getSystem

0 commit comments

Comments
 (0)