|
1 | 1 | // LICENSE : MIT
|
2 | 2 | "use strict";
|
3 | 3 | import {getKeywords} from "stemming-x-keywords";
|
4 |
| -import {getFilePathListAsync} from "gitbook-summary-to-path"; |
5 | 4 | import path from "path";
|
6 | 5 | import fs from "fs";
|
7 | 6 | import mdast from "mdast";
|
8 | 7 | import parents from "unist-util-parents";
|
9 | 8 | import select from "unist-util-select";
|
10 | 9 | import isUnist from "unist-util-is";
|
11 | 10 | import nlcstToString from "nlcst-to-string";
|
| 11 | + |
12 | 12 | const rootDir = path.join(__dirname, "..");
|
13 |
| -const OrganizationText = fs.readFileSync(__dirname + "/../ORGANIZATION.md", "utf-8"); |
| 13 | +const OrganizationText = fs.readFileSync(path.join(rootDir, "ORGANIZATION.md"), "utf-8"); |
14 | 14 | function isNotContain(content, keywords) {
|
15 | 15 | // 含んでないものだけを返す
|
16 | 16 | return keywords.filter(keyword => {
|
@@ -42,46 +42,48 @@ function isAlreadyCheckKeyword(list, keyword) {
|
42 | 42 | return list.indexOf(keyword) !== -1;
|
43 | 43 | }
|
44 | 44 |
|
45 |
| -// キーワードが書くコンテンツに含まれているかをテストする |
46 |
| -describe("keywords", function () { |
47 |
| - it("Each chapter contain the keyword", function () { |
48 |
| - let ast = mdast.parse(OrganizationText); |
49 |
| - let headerLinks = select(parents(ast), "heading link[href]"); |
50 |
| - let paragraphList = headerLinks.map(link => { |
51 |
| - let filePath = path.resolve(rootDir, link.href); |
52 |
| - let paragraphs = findAllAfter(ast, link.parent.node, "paragraph"); |
53 |
| - let results = { |
54 |
| - filePath: filePath, |
55 |
| - content: fs.readFileSync(filePath, "utf-8"), |
56 |
| - keywords: [] |
57 |
| - }; |
58 |
| - let keywords = paragraphs.map(p => { |
59 |
| - let text = nlcstToString(p); |
60 |
| - return getKeywords(text).then(a => { |
61 |
| - results.keywords = results.keywords.concat(a); |
62 |
| - }); |
63 |
| - }); |
64 |
| - return Promise.all(keywords).then(()=> { |
65 |
| - return results; |
| 45 | +function checkKeyword(text){ |
| 46 | + let ast = mdast.parse(text); |
| 47 | + let headerLinks = select(parents(ast), "heading link[href]"); |
| 48 | + let paragraphList = headerLinks.map(link => { |
| 49 | + let filePath = path.resolve(rootDir, link.href); |
| 50 | + let paragraphs = findAllAfter(ast, link.parent.node, "paragraph"); |
| 51 | + let results = { |
| 52 | + filePath: filePath, |
| 53 | + content: fs.readFileSync(filePath, "utf-8"), |
| 54 | + keywords: [] |
| 55 | + }; |
| 56 | + let keywords = paragraphs.map(p => { |
| 57 | + let text = nlcstToString(p); |
| 58 | + return getKeywords(text).then(a => { |
| 59 | + results.keywords = results.keywords.concat(a); |
66 | 60 | });
|
67 | 61 | });
|
68 |
| - let promises = Promise.all(paragraphList).then(results => { |
69 |
| - let confirmedKeywords = []; |
70 |
| - return results.forEach(({filePath, content, keywords}) => { |
71 |
| - let unusedKeywords = isNotContain(content, keywords); |
72 |
| - let isChecked = isAlreadyCheckKeyword.bind(null, confirmedKeywords); |
73 |
| - if (unusedKeywords.length === 0) { |
74 |
| - // 使用済みのキーワードを登録 |
75 |
| - confirmedKeywords = confirmedKeywords.concat(keywords); |
76 |
| - return; |
77 |
| - } |
78 |
| - if (unusedKeywords.every(isChecked)) { |
79 |
| - console.log(unusedKeywords.join(",") + "はチェック済み"); |
80 |
| - return; |
81 |
| - } |
82 |
| - throw new Error(`"${unusedKeywords.join(",")}" are not used in ${filePath}`); |
83 |
| - }); |
| 62 | + return Promise.all(keywords).then(()=> { |
| 63 | + return results; |
84 | 64 | });
|
85 |
| - return promises; |
| 65 | + }); |
| 66 | + return Promise.all(paragraphList).then(results => { |
| 67 | + let confirmedKeywords = []; |
| 68 | + return results.forEach(({filePath, content, keywords}) => { |
| 69 | + let unusedKeywords = isNotContain(content, keywords); |
| 70 | + let isChecked = isAlreadyCheckKeyword.bind(null, confirmedKeywords); |
| 71 | + if (unusedKeywords.length === 0) { |
| 72 | + // 使用済みのキーワードを登録 |
| 73 | + confirmedKeywords = confirmedKeywords.concat(keywords); |
| 74 | + return; |
| 75 | + } |
| 76 | + if (unusedKeywords.every(isChecked)) { |
| 77 | + console.log(unusedKeywords.join(",") + "はチェック済み"); |
| 78 | + return; |
| 79 | + } |
| 80 | + throw new Error(`"${unusedKeywords.join(",")}" are not used in ${filePath}`); |
| 81 | + }); |
| 82 | + }); |
| 83 | +} |
| 84 | +// キーワードが書くコンテンツに含まれているかをテストする |
| 85 | +describe("keywords", function () { |
| 86 | + it("Each chapter contain the keyword", function () { |
| 87 | + return checkKeyword(OrganizationText); |
86 | 88 | });
|
87 | 89 | });
|
0 commit comments