|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | const glob = require('glob'); |
4 | | -const config = require('config'); |
| 4 | +const config = require('../config'); |
| 5 | +const debug = require('debug')('countTranslation'); |
5 | 6 | const path = require('path'); |
6 | 7 | const fs = require('fs'); |
7 | 8 | const jsdiff = require('diff'); |
8 | | -const log = require('log')(); |
9 | | - |
10 | | -module.exports = function () { |
11 | | - |
12 | | - return function () { |
13 | | - let args = require('yargs') |
14 | | - .demand(['lang']) |
15 | | - .argv; |
16 | | - |
17 | | - return async function () { |
18 | | - |
19 | | - let root = path.dirname(config.tutorialRoot); |
20 | | - let rootEn = path.join(root, 'javascript-tutorial-en'); |
21 | | - let rootLang = path.join(root, 'javascript-tutorial-' + args.lang); |
22 | | - |
23 | | - log.debug(`Compare ${rootEn} vs ${rootLang}`); |
24 | | - let filesFrom = glob.sync('**/*.md', {cwd: rootEn}).filter(f => parseInt(f)); |
25 | | - |
26 | | - let filesTranslated = 0; |
27 | | - let filesMissing = 0; |
28 | | - let filesSimilar = 0; |
29 | | - |
30 | | - for (let relPath of filesFrom) { |
31 | | - let fileFrom = path.join(rootEn, relPath); |
32 | | - let fileLang = path.join(rootLang, relPath); |
33 | | - |
34 | | - log.debug(relPath); |
35 | | - if (!fs.existsSync(fileLang)) { |
36 | | - filesMissing++; |
37 | | - log.debug("MISSING"); |
38 | | - continue; |
39 | | - } |
40 | | - |
41 | | - if (fs.statSync(fileFrom).size === 0) { |
42 | | - // ignore zero files, don't count as translated |
43 | | - // this way new repo translation starts with zero |
44 | | - continue; |
45 | | - } |
46 | | - |
47 | | - let fileFromContent = fs.readFileSync(fileFrom, {encoding: 'utf8'}); |
48 | | - let fileLangContent = fs.readFileSync(fileLang, {encoding: 'utf8'}); |
49 | | - |
50 | | - fileFromContent = stripYamlHeader(fileFromContent); |
51 | | - fileLangContent = stripYamlHeader(fileLangContent); |
52 | | - |
53 | | - fileFromContent = stripCode(fileFromContent); |
54 | | - fileLangContent = stripCode(fileLangContent); |
55 | | - |
56 | | - fileFromContent = fileFromContent.trim(); |
57 | | - fileLangContent = fileLangContent.trim(); |
58 | | - // log.debug(fileFromContent); |
59 | | - |
60 | | - let linesTotal = (fileFromContent.match(/\n+/g) || []).length; |
61 | | - |
62 | | - /* |
63 | | -
|
64 | | - let added = 0; |
65 | | - let removed = 0; |
66 | | - try { |
67 | | - let cmd = `git diff --no-index --numstat --patience ${esc(fileFrom)} ${esc(fileLang)}`; |
68 | | - log.debug(cmd); |
69 | | - let compare = execSync(cmd, {encoding: 'utf8'}); |
70 | | - } catch(err) { |
71 | | - if (err.status !== 1) { |
72 | | - throw err; |
73 | | - } |
74 | | - [added, removed] = err.output[1].trim().split('\t'); |
75 | | - } |
76 | | -*/ |
77 | | - let diff = jsdiff.diffLines(fileFromContent, fileLangContent); |
78 | | - |
79 | | - |
80 | | - let added = 0; |
81 | | - let removed = 0; |
82 | | - for(let obj of diff) { |
83 | | - if (obj.added) added += obj.count; |
84 | | - if (obj.removed) removed += obj.count; |
85 | | - } |
86 | | - |
87 | | - if (added + removed > linesTotal / 3) { |
88 | | - log.debug("TRANSLATED", added, removed, linesTotal); |
89 | | - filesTranslated++; |
90 | | - } else { |
91 | | - log.debug("SIMILAR", added, removed, linesTotal); |
92 | | - filesSimilar++; |
93 | | - } |
94 | | - |
95 | | - // log.debug("RESULT", added, removed, linesTotal); |
96 | | - //return; |
97 | | - // if (added == 0 && removed == 0) |
98 | | - //log.debug("RESULT", added, removed, linesTotal); |
99 | | - |
100 | | - } |
101 | | - |
102 | | - log.info("TOTAL", { filesTranslated, filesMissing, filesSimilar }); |
103 | | - }(); |
104 | | - |
105 | | - }; |
106 | | -}; |
107 | 9 |
|
| 10 | +module.exports = async function(rootEn, rootLang) { |
| 11 | + |
| 12 | + debug(`Compare ${rootEn} vs ${rootLang}`); |
| 13 | + |
| 14 | + let filesFrom = glob.sync('**/*.md', {cwd: rootEn}).filter(f => parseInt(f)); |
| 15 | + |
| 16 | + debug("filesFrom", filesFrom); |
| 17 | + |
| 18 | + let filesTranslated = 0; |
| 19 | + let filesMissing = 0; |
| 20 | + let filesSimilar = 0; |
| 21 | + |
| 22 | + for (let relPath of filesFrom) { |
| 23 | + let fileFrom = path.join(rootEn, relPath); |
| 24 | + let fileLang = path.join(rootLang, relPath); |
| 25 | + |
| 26 | + debug(relPath); |
| 27 | + if (!fs.existsSync(fileLang)) { |
| 28 | + filesMissing++; |
| 29 | + debug("MISSING"); |
| 30 | + continue; |
| 31 | + } |
| 32 | + |
| 33 | + if (fs.statSync(fileFrom).size === 0) { |
| 34 | + // ignore zero files, don't count as translated |
| 35 | + // this way new repo translation starts with zero |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + let fileFromContent = fs.readFileSync(fileFrom, {encoding: 'utf8'}); |
| 40 | + let fileLangContent = fs.readFileSync(fileLang, {encoding: 'utf8'}); |
| 41 | + |
| 42 | + fileFromContent = stripYamlHeader(fileFromContent); |
| 43 | + fileLangContent = stripYamlHeader(fileLangContent); |
| 44 | + |
| 45 | + fileFromContent = stripCode(fileFromContent); |
| 46 | + fileLangContent = stripCode(fileLangContent); |
| 47 | + |
| 48 | + fileFromContent = fileFromContent.trim(); |
| 49 | + fileLangContent = fileLangContent.trim(); |
| 50 | + // debug(fileFromContent); |
| 51 | + |
| 52 | + let linesTotal = (fileFromContent.match(/\n+/g) || []).length; |
| 53 | + |
| 54 | + let diff = jsdiff.diffLines(fileFromContent, fileLangContent); |
| 55 | + |
| 56 | + |
| 57 | + let added = 0; |
| 58 | + let removed = 0; |
| 59 | + for (let obj of diff) { |
| 60 | + if (obj.added) added += obj.count; |
| 61 | + if (obj.removed) removed += obj.count; |
| 62 | + } |
| 63 | + |
| 64 | + if (added + removed > linesTotal / 3) { |
| 65 | + debug("TRANSLATED", added, removed, linesTotal); |
| 66 | + filesTranslated++; |
| 67 | + } else { |
| 68 | + debug("SIMILAR", added, removed, linesTotal); |
| 69 | + filesSimilar++; |
| 70 | + } |
| 71 | + |
| 72 | + // debug("RESULT", added, removed, linesTotal); |
| 73 | + //return; |
| 74 | + // if (added == 0 && removed == 0) |
| 75 | + //debug("RESULT", added, removed, linesTotal); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + let result = {filesTranslated, filesMissing, filesSimilar, filesTotal: filesFrom.length}; |
| 80 | + debug("TOTAL", result); |
| 81 | + |
| 82 | + return result; |
| 83 | +}; |
108 | 84 |
|
109 | 85 | function stripCode(content) { |
110 | 86 | return content.replace(/(^|\n[ \t]*)```(js|css|html)[\s\S]+\n[ \t]*```/gim, '\n'); |
|
0 commit comments