-
Notifications
You must be signed in to change notification settings - Fork 6
/
_initChallenge.mjs
executable file
·62 lines (52 loc) · 1.95 KB
/
_initChallenge.mjs
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
#!/usr/bin/env zx
// @ts-check
import 'zx/globals'
// console.log(argv)
const toKebabCase = str =>
str
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/\s+]/gi, '-')
.replace(/\-+/g, '-')
.replace(/^\-|\-$/g, '')
.toLowerCase()
let name = await question('Puzzle/question/challenge name: ')
if (!name) throw new Error('Name is required')
let url = ''
if (argv.leetcode) {
const matches = name.match(/\d+\. (.*)/)
if (!matches) throw new Error('Invalid leetcode name')
const nameWithoutId = matches[1]
url = `https://leetcode.com/problems/${toKebabCase(nameWithoutId)}`
} else {
url = await question('Full URL: ')
}
if (!url) throw new Error('URL is required')
const templates = ['ts', 'ts-full', 'js', 'js-codegolf', 'js-leetcode']
let template = ''
const argvTemplate = argv.template
if (argvTemplate) {
if (!templates.includes(argvTemplate)) throw new Error(`Invalid template: ${argvTemplate}`)
template = argvTemplate
}
if (!template) {
template =
(await question('Template to use (ts, ts-full, js, js-codegolf, js-leetcode) [js-leetcode]: ', {
choices: templates,
})) || 'js-leetcode'
}
name = toKebabCase(name)
if (url.includes('leetcode')) name = `leetcode-${name}`
else if (url.includes('codingame')) name = `codingame-${name}`
const challengePath = path.resolve(__dirname, 'challenges', name)
const challengeReadmePath = path.resolve(challengePath, 'README.md')
await fs.copy(path.resolve(__dirname, '_templates', template), challengePath)
let README = await fs.readFile(challengeReadmePath, { encoding: 'utf-8' })
README = README.replace('codingame-temperatures', name)
README = README.replace('https://www.codingame.com/training/easy/temperatures', url)
await fs.writeFile(challengeReadmePath, README, { encoding: 'utf-8' })
console.log()
console.log('name:', name)
console.log('url :', url)
console.log()
console.log(`Initialized challenge at challenges/${name}`)