|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const glob = require('glob'); |
| 4 | +const config = require('config'); |
| 5 | +const path = require('path'); |
| 6 | +const fs = require('fs'); |
| 7 | +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 | + |
| 108 | + |
| 109 | +function stripCode(content) { |
| 110 | + return content.replace(/(^|\n[ \t]*)```(js|css|html)[\s\S]+\n[ \t]*```/gim, '\n'); |
| 111 | +} |
| 112 | + |
| 113 | +function stripYamlHeader(content) { |
| 114 | + let parts = content.split('\n---\n'); |
| 115 | + if (parts.length === 1) { |
| 116 | + return content; |
| 117 | + } |
| 118 | + |
| 119 | + return parts.slice(1).join('\n---\n'); |
| 120 | +} |
| 121 | + |
0 commit comments