]>
Commit | Line | Data |
---|---|---|
1 | const { readdir } = require('fs/promises'); | |
2 | const { join, resolve } = require('path'); | |
3 | ||
4 | const internals = { | |
5 | ||
6 | dates: [ | |
7 | 'today', | |
8 | 'tomorrow', | |
9 | 'next friday', | |
10 | 'next wednesday', | |
11 | 'last thursday', | |
12 | 'last tuesday', | |
13 | 'the next day divisible by 7', | |
14 | 'this afternoon', | |
15 | 'next fiscal quarter', | |
16 | ], | |
17 | ||
18 | rules: null, | |
19 | currentIndex: 0, | |
20 | ||
21 | kRulesDirectory: resolve(process.cwd(), 'rules'), | |
22 | ||
23 | async readRules() { | |
24 | ||
25 | const ruleFiles = await readdir(internals.kRulesDirectory); | |
26 | const rules = ruleFiles | |
27 | .filter((file) => file.match(/.js$/)) | |
28 | .map((file) => require(join(internals.kRulesDirectory, file))); | |
29 | ||
30 | internals.rules = rules.sort(() => Math.random() - 0.5) | |
31 | } | |
32 | } | |
33 | ||
34 | module.exports = { | |
35 | async generate() { | |
36 | ||
37 | if (!internals.rules) { | |
38 | await internals.readRules(); | |
39 | } | |
40 | ||
41 | const rule = internals.rules[internals.currentIndex]; | |
42 | const time = internals.dates[Math.floor(Math.random() * internals.dates.length)]; | |
43 | internals.currentIndex = (internals.currentIndex + 1) % internals.rules.length | |
44 | return { | |
45 | rule: rule(), | |
46 | time | |
47 | } | |
48 | } | |
49 | } |