-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve.js
73 lines (65 loc) Β· 1.61 KB
/
solve.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
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const concurrently = require('concurrently');
const { cp, mkdir } = require('shelljs');
const USAGE = `Usage
$ solve yyyy d
where
2015 <= yyyy <= ${new Date().getFullYear()}
1 <= d <= 25
`;
const validate = (y, d) => {
if (
!/^\d{4}$/.test(y) ||
Number(y) < 2015 ||
Number(y) > new Date().getFullYear() ||
!/^\d{2}$/.test(d) ||
Number(d) > 25
)
throw new Error();
};
let year = new Date().getFullYear().toString();
let day = new Date().getDate().toString();
try {
switch (process.argv.length) {
case 2:
break;
case 3:
// past year
if (year !== process.argv[2]) day = '01';
year = process.argv[2];
break;
case 4:
[year, day] = process.argv.slice(2);
break;
default:
throw new Error();
}
day = day.padStart(2, '0');
validate(year, day);
} catch (_) {
console.log(USAGE);
return -1;
}
const template = path.join(__dirname, 'template', '*');
const targetDir = path.join(__dirname, year, day);
if (!fs.existsSync(targetDir)) {
mkdir('-p', targetDir);
cp('-R', template, targetDir);
}
console.log(chalk`Preparing y{greenBright ${year}} d{greenBright ${day}}...\n`);
const pattern = path.join(year, day);
concurrently(
[
{
command: `npx nodemon --watch ${pattern} --watch utils -e "*" --exec "npx jest ${year}/${day}"`,
name: 'test',
},
{
command: `npx nodemon --watch ${pattern} --watch utils -e "*" "${pattern}"`,
name: 'main',
},
],
{ prefix: '[{time} {name}]', timestampFormat: 'HH:mm:ss' }
);