-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaffold.js
84 lines (65 loc) · 2.38 KB
/
scaffold.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
81
82
83
84
import fs from 'fs'
import path from 'path'
const CODE_TEMPLATE = `
import { readFile } from '../shared'
export function parseInput(input) {
// Code to parse txt input
}
export function solvePartOne(input) {
// Code to solve the exercise
}
export function solvePartTwo(input) {
// Code to solve the exercise
}
export function main() {
const input = readFile(__dirname, 'input.txt')
const parsed = parseInput(input)
console.log('🎄 Part one result:', solvePartOne(parsed))
console.log('🎄 Part two result:', solvePartTwo(parsed))
}
`
const TESTS_TEMPLATE = `
import { readFile } from '../shared'
import { solvePartOne, solvePartTwo, parseInput } from './'
describe('Day [[day]]', () => {
it('should "parseInput" parse input into valid data structure', () => {
const parsed = parseInput(readFile(__dirname, 'example.txt'))
// Check data structure
})
it('should solve part one return expected result', () => {
const parsed = parseInput(readFile(__dirname, 'example.txt'))
const result = solvePartOne(parsed)
expect(result).toBe(/* expected part one example result */)
})
it.todo('should solve part two return expected result')
})
`
function createFile(filePath, content = '') {
fs.writeFileSync(filePath, content, 'utf-8')
}
function main(day) {
if (!day || Number.isNaN(day)) {
console.error('❌ Day argument is needed! Example: yarn scaffold 1')
process.exit(1)
}
const dayPadded = String(day).padStart(2, 0)
const dirName = `day-${dayPadded}`
if (fs.existsSync(dirName)) {
console.error('❌ Day already exists, scaffold files will be not generated.')
process.exit(1)
}
const input = path.resolve(__dirname, dirName, 'input.txt')
const example = path.resolve(__dirname, dirName, 'example.txt')
const code = path.resolve(__dirname, dirName, 'index.js')
const tests = path.resolve(__dirname, dirName, 'index.test.js')
fs.mkdirSync(path.resolve(__dirname, dirName))
createFile(input)
console.log('✅ %s/input.txt file created.', dirName)
createFile(example)
console.log('✅ %s/example.txt file created.', dirName)
createFile(code, CODE_TEMPLATE)
console.log('✅ %s/index.js file created.', dirName)
createFile(tests, TESTS_TEMPLATE.replace('[[day]]', dayPadded))
console.log('✅ %s/index.test.js file created.', dirName)
}
main(Number(process.argv[2]))