This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
80 lines (74 loc) · 2.32 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import z from 'zod'
import { fs, path } from 'zx'
import lazy from './utils/lazy.js'
const projectsConfig = lazy(() =>
readConfig('projects.json', z.object({
ignored: z.object({
names: z.array(z.string())
})
}), {
ignored: {
__description: 'A set of (exact) project names that will be excluded from prompts',
names: []
}
})
)
const startTimerRulesConfig = lazy(() => {
const argsSchema = z.object({
args: z.object({
project: z.union([
z.object({ id: z.number() }),
z.object({ name: z.string() }),
z.object({ nameContains: z.string() })
]),
task: z.union([
z.object({ id: z.number() }),
z.object({ name: z.string() }),
z.object({ nameContains: z.string() })
]),
notes: z.string().optional().default('')
})
})
const promptSchema = z.object({
prompt: z.string().min(1),
type: z.union([
z.literal('input'),
z.object({ name: z.literal('input'), defaultInput: z.string().min(1) })
]),
then: z.lazy(() => z.union([promptSchema, argsSchema]))
})
return readConfig('start-timer-rules.json', z.object({
rules: z.array(z.object({
ruleName: z.string(),
enabled: z.boolean().optional().default(true),
start: z.union([promptSchema, argsSchema])
})).default([])
}), {
rules: [{
ruleName: 'example',
enabled: false,
start: {
prompt: 'notes',
type: 'input',
then: {
args: {
project: { name: 'project name' },
task: { nameContains: 'task' },
notes: '{0}'
}
}
}
}]
})
})
const readConfig = async (jsonFile, schema, defaultConfig) => {
const file = path.join(__dirname, '.config', jsonFile)
if (!await fs.pathExists(file)) {
await fs.writeJSON(file, defaultConfig)
}
return schema.parse(await fs.readJson(file))
}
export default {
readProjects: () => projectsConfig.value(),
readStartTimerRules: () => startTimerRulesConfig.value()
}