-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.js
88 lines (71 loc) · 2.31 KB
/
template.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 puppeteer = require('puppeteer');
const cheerio = require('cheerio');
const { mkdirSync, existsSync, writeFileSync } = require('fs');
const TurndownService = require('turndown');
const problemNumber = process.argv[2];
(async () => {
const browser = await puppeteer.launch({
headless: true,
});
const page = await browser.newPage();
await page.setViewport({
width: 1920,
height: 800,
});
await page.goto(
`https://programmers.co.kr/learn/courses/30/lessons/${problemNumber}?language=javascript`
);
const content = await page.content();
const $ = cheerio.load(content);
const title = $('.algorithm-title').text().trim();
const turndownService = new TurndownService();
turndownService.keep(['table']);
const problemDescription = turndownService.turndown(
$('.guide-section-description').html()
);
const initSolution = $('.CodeMirror-line').text() + '\n\n';
let testCase = [];
await $('table')
.last()
.find('tbody > tr')
.each(function () {
const getTestCase = $(this)
.text()
.trim()
.split('\n')
.map(function (item) {
if (!isNaN(item)) {
return parseInt(item, 10);
}
return item;
});
testCase.push(getTestCase);
});
let testCode = `test('Test case', () => {`;
testCase.forEach((eachCase) => {
testCode += ' expect(solution(';
eachCase.forEach((each, index) => {
if (index === eachCase.length - 1) {
return (testCode += ')).toEqual(' + each + ');\n');
}
return (testCode += each + (index < eachCase.length - 2 ? ', ' : ''));
});
});
testCode += '});';
if (!existsSync(`programmers/${title}`)) {
mkdirSync(`programmers/${title}`);
writeFileSync(
`programmers/${title}/answer.test.js`,
initSolution + testCode
);
writeFileSync(
`programmers/${title}/README.md`,
`# [${title} / ${problemNumber}](https://programmers.co.kr/learn/courses/30/lessons/${problemNumber}?language=javascript)\n## What\n` +
problemDescription +
`\n> 출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/courses/30/lessons/${problemNumber}\n\n## How\n\n## Retrospective`
);
} else {
console.log('이미 폴더가 존재합니다.');
}
browser.close();
})();