-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateFileAddReadMe.js
88 lines (76 loc) · 2.12 KB
/
CreateFileAddReadMe.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
85
86
87
88
const fs = require('fs')
const path = require('path')
const exec = require('child_process').exec
capitalizeFirstLetter = string => {
return string.charAt(0).toUpperCase() + string.slice(1)
}
const createFile = (questionFolder, func) => {
const folderPath = path.join(__dirname, 'LeetCode')
fs.mkdir(path.join(folderPath, questionFolder), err => {
if (err) {
console.log(err)
}
fs.readFile(path.join(__dirname, 'leetcode.js'), 'utf8', (err, data) => {
if (err) {
console.log(err)
return
}
fs.writeFile(path.join(folderPath, questionFolder, capitalizeFirstLetter(func.name) + '.js'), data, err => {
if (err) {
console.log(err)
}
console.log('File created')
addToReadMe()
})
})
})
}
const addToReadMe = () => {
const command = 'git ls-files --others --exclude-standard'
exec(command, (err, stdout, stderr) => {
if (err || stderr) {
console.log(err || stderr)
return
}
const pathDesc = path.parse(stdout)
const lang = path.extname(stdout).trim()
const folderName = pathDesc.dir.split('LeetCode/')[1]
const fileName = pathDesc.name
if (folderName && fileName) {
addLine(folderName, fileName, lang)
} else {
console.log('No solution added')
}
})
}
const addLine = (problem, file, lang) => {
let number = problem.split('. ')[0]
let title = problem.split('. ')[1]
let language = ''
switch (lang) {
case '.js':
language = 'JavaScript'
break
case '.cs':
language = 'C#'
default:
break
}
let content = `|${number}|[${title}](https://leetcode.com/problems/${title
.split(' ')
.join('-')
.toString()
.toLowerCase()}/)|[${language}](https://github.com/berkansivri/LeetCode/blob/master/LeetCode/${number}.%20${title
.split(' ')
.join('%20')
.toString()}/${file}.js)|`
fs.open('README.md', 'a', 666, function (e, id) {
fs.write(id, content + '\r\n', null, 'utf8', function () {
fs.close(id, function () {
console.log('readme updated')
})
})
})
}
addToReadMe()
module.exports = createFile