From f45c3d4fa51bc591ef7fc5b63cd0c35c81d47bbc Mon Sep 17 00:00:00 2001 From: Bouncey Date: Sat, 16 Feb 2019 08:48:52 +0000 Subject: [PATCH] chore: Apply linting fixes --- tools/challenge-md-parser/text-to-data.js | 2 +- tools/scripts/createRedirects.test.js | 2 +- tools/scripts/ensure-env.js | 1 + tools/scripts/ensure-path-migration-map.js | 1 + tools/scripts/seed/seedAuthUser.js | 2 +- tools/translation/translate-challenges.js | 51 +++--- tools/translation/translate-guide-old.js | 196 +++++++++++---------- tools/translation/translate-guide.js | 158 +++++++++-------- 8 files changed, 223 insertions(+), 190 deletions(-) diff --git a/tools/challenge-md-parser/text-to-data.js b/tools/challenge-md-parser/text-to-data.js index 638b5835a713f3..70dc4a70e5cd7b 100644 --- a/tools/challenge-md-parser/text-to-data.js +++ b/tools/challenge-md-parser/text-to-data.js @@ -45,7 +45,7 @@ function textToData(sectionIds) { const lines = child.value.split('\n'); if (lines.filter(Boolean).length > 0) { lines.forEach((line, index) => { - if (/^\s*$/.test(line)) { + if ((/^\s*$/).test(line)) { currentParagraph = null; } else { if (!currentParagraph || index > 0) { diff --git a/tools/scripts/createRedirects.test.js b/tools/scripts/createRedirects.test.js index 8fbf3e04c9cf41..6cd8d3bbdd906d 100644 --- a/tools/scripts/createRedirects.test.js +++ b/tools/scripts/createRedirects.test.js @@ -5,7 +5,7 @@ const { createRedirects } = require('./createRedirects'); const testLocations = { api: 'https://api.example.com', news: 'https://news.example.com', - forum: 'https://forum.example.com', + forum: 'https://forum.example.com' }; describe('createRedirects', () => { diff --git a/tools/scripts/ensure-env.js b/tools/scripts/ensure-env.js index 5a9eab83178199..2e9829868e9a05 100644 --- a/tools/scripts/ensure-env.js +++ b/tools/scripts/ensure-env.js @@ -53,6 +53,7 @@ fs.access(migrationMapPath, err => { }) .catch(err => { console.error(err); + // eslint-disable-next-line process.exit(1); }); } diff --git a/tools/scripts/ensure-path-migration-map.js b/tools/scripts/ensure-path-migration-map.js index 82e9d6855a9711..e831813a282845 100644 --- a/tools/scripts/ensure-path-migration-map.js +++ b/tools/scripts/ensure-path-migration-map.js @@ -25,5 +25,6 @@ getChallengesForLang('english') }) .catch(err => { console.error(err); + // eslint-disable-next-line process.exit(1); }); diff --git a/tools/scripts/seed/seedAuthUser.js b/tools/scripts/seed/seedAuthUser.js index 52203a29f650bc..7c3c095050531e 100644 --- a/tools/scripts/seed/seedAuthUser.js +++ b/tools/scripts/seed/seedAuthUser.js @@ -5,7 +5,7 @@ const ObjectId = require('mongodb').ObjectID; const debug = require('debug'); const log = debug('fcc:tools:seedLocalAuthUser'); -const { MONGOHQ_URL, LOCALE: lang } = process.env; +const { MONGOHQ_URL } = process.env; function handleError(err, client) { if (err) { diff --git a/tools/translation/translate-challenges.js b/tools/translation/translate-challenges.js index 04f2513af1049d..c78e776166abcc 100644 --- a/tools/translation/translate-challenges.js +++ b/tools/translation/translate-challenges.js @@ -9,7 +9,14 @@ var dir = dir1 + '/' + dir2; fs.readdirSync('../../curriculum/challenges/english/' + dir).forEach(file => { if (file.includes('.md') && dir) { let originalFileName = - '../../curriculum/challenges/' + langFull + '/' + dir + '/' + file.slice(0, -10) + langFull + '.md'; + '../../curriculum/challenges/' + + langFull + + '/' + + dir + + '/' + + file.slice(0, -10) + + langFull + + '.md'; fs.exists(originalFileName, function(exists) { if (!exists) { @@ -28,10 +35,12 @@ fs.readdirSync('../../curriculum/challenges/english/' + dir).forEach(file => { // Load in full text, description, instructions, and title function getFile(file, dir) { - let originalFileName = '../../curriculum/challenges/english/' + dir + '/' + file; + let originalFileName = + '../../curriculum/challenges/english/' + dir + '/' + file; let fileString = fs.readFileSync(originalFileName).toString(); - // Add 'notranslate' class to code so Google Translate API will not translate code segments. + // Add 'notranslate' class to code so Google Translate API + // will not translate code segments. fileString = fileString.replace(//g, ''); fileString = fileString.replace( /
/g, @@ -67,7 +76,7 @@ function processFile( ) { const translateText = (text, target) => { return new Promise((resolve, reject) => { - if (typeof text == 'object' && Object.keys(text).length === 0) { + if (typeof text === 'object' && Object.keys(text).length === 0) { resolve(['']); } else { // Imports the Google Cloud client library @@ -87,8 +96,6 @@ function processFile( }) .catch(err => { reject(console.log('!!!!!', err)); - if (err) { - } }); } }); @@ -106,21 +113,23 @@ function processFile( let testsArray = tests.split('\n'); let testsToTranslate = []; - testsArray.forEach((test, index) => { + testsArray.forEach(test => { if (test.includes('- text: ')) { testsToTranslate.push(test.slice(10)); } }); - translateText(testsToTranslate, lang).then(translation => { - let transIndex = 0; - testsArray.forEach((test, index) => { - if (test.includes('- text')) { - testsArray[index] = ' - text: ' + translation[transIndex]; - transIndex++; - } - }); - resolve(testsArray.join('\n')); - }); + translateText(testsToTranslate, lang) + .then(translation => { + let transIndex = 0; + testsArray.forEach((test, index) => { + if (test.includes('- text')) { + testsArray[index] = ' - text: ' + translation[transIndex]; + transIndex++; + } + }); + resolve(testsArray.join('\n')); + }) + .catch(reject); }); }; @@ -147,7 +156,7 @@ function processFile( /
(.|\n)*?<\/section>/, translations[3] ); - fileString = fileString.replace(/ class=\"notranslate\"/g, ''); // remove 'notranslate' class + fileString = fileString.replace(/ class=\"notranslate\"/g, ''); writeFile(fileString, file, dir); }); } @@ -163,7 +172,9 @@ function writeFile(fileString, file, dir) { langFull + '.md'; fs.writeFile(fullFileName, fileString, function(err) { - if (err) throw err; + if (err) { + throw err; + } console.log('Saved!'); }); -} \ No newline at end of file +} diff --git a/tools/translation/translate-guide-old.js b/tools/translation/translate-guide-old.js index 0613119612d8b3..10379a7c749961 100644 --- a/tools/translation/translate-guide-old.js +++ b/tools/translation/translate-guide-old.js @@ -8,10 +8,7 @@ const stringify2 = require('remark-stringify'); const remark = require('rehype-remark'); const path = require('path'); const readDirP = require('readdirp-walk'); -const { - Translate - } = require('@google-cloud/translate'); - +const { Translate } = require('@google-cloud/translate'); const lang = 'es'; const langFull = 'spanish'; @@ -27,112 +24,123 @@ const htmlProcessor = unified() .use(stringify2); readDirP({ - root: path.resolve(__dirname, `./test`) - }) - .on('data', translateChallenge); + root: path.resolve(__dirname, './test') +}).on('data', translateChallenge); async function translateChallenge(file) { - const { - name, - depth, - path: filePath, - fullPath, - fullParentDir, - stat - } = file; - if (stat.isDirectory() || name === '.DS_Store' || file.depth == 1) return null; - - + const { name, fullPath, fullParentDir, stat } = file; + if (stat.isDirectory() || name === '.DS_Store' || file.depth === 1) { + return null; + } + const pathIndex = fullPath.indexOf('guide') + 6; - const outputDir = fullParentDir.substring(0, pathIndex) + `${langFull}/` + fullParentDir.substring(pathIndex + 5); - const outputPath = fullPath.substring(0, pathIndex) + `${langFull}/` + fullPath.substring(pathIndex + 5); - if (fs.existsSync(outputPath)) return null; + const outputDir = + fullParentDir.substring(0, pathIndex) + + `${langFull}/` + + fullParentDir.substring(pathIndex + 5); + const outputPath = + fullPath.substring(0, pathIndex) + + `${langFull}/` + + fullPath.substring(pathIndex + 5); + if (fs.existsSync(outputPath)) { + return null; + } fs.ensureDirSync(outputDir); const fileString = fs.readFileSync(fullPath).toString(); - var i = fileString.indexOf('---', 4) - const meta = fileString.substring(0, i+4) + var i = fileString.indexOf('---', 4); + const meta = fileString.substring(0, i + 4); const title = fileString.split('\n')[1].split(': ')[1]; - var article = fileString.substring(i+4) - - mdToHtml(article).then((htmlArticle) => { - htmlArticle = htmlArticle.replace(/\n/g, '
') - htmlArticle = htmlArticle.replace(/ /g, '        ') - htmlArticle = htmlArticle.replace(/ /g, '    ') - htmlArticle = htmlArticle.replace(/ /g, '  ') + var article = fileString.substring(i + 4); + + return mdToHtml(article).then(htmlArticle => { + htmlArticle = htmlArticle.replace(/\n/g, '
'); + htmlArticle = htmlArticle.replace( + / {8}/g, + '        ' + ); + htmlArticle = htmlArticle.replace(/ {4}/g, '    '); + htmlArticle = htmlArticle.replace(/ {2}/g, '  '); translate(htmlArticle, title, meta, outputPath); - }) + }); } function translate(htmlArticle, title, meta, outputPath) { - Promise.all([ - translateText(title), - translateText(htmlArticle) - ]).then(function(translations) { - // Replace English with translation - let translatedTitle = translations[0][0]; - let tempArticle = translations[1][0] - tempArticle = tempArticle.replace(/
/g, '\n') - tempArticle = tempArticle.replace(/'/g, `'`) - - // tempArticle = tempArticle.replace(/language-html">/g, 'language-html">\n') - // tempArticle = tempArticle.replace(/
  <\/pre/g, ' {
-            const i = meta.indexOf('---', 4);
-            let translatedFile = meta.slice(0, i) + `localeTitle: ${translatedTitle}\n` + meta.slice(i) + translatedArticle;
-            writeFile(translatedFile, outputPath);
-        });
-      });
-}
+  Promise.all([translateText(title), translateText(htmlArticle)]).then(function(
+    translations
+  ) {
+    // Replace English with translation
+    let translatedTitle = translations[0][0];
+    let tempArticle = translations[1][0];
+    tempArticle = tempArticle.replace(/
/g, '\n'); + tempArticle = tempArticle.replace(/'/g, "'"); + + // tempArticle = tempArticle.replace(/language-html">/g,'language-html">\n') + // tempArticle = tempArticle.replace(/
  <\/pre/g, ' (text) => {
-    if (!text) return '';
-    const translate = new Translate();
-    
-    return translate
-        .translate(text, target)
-        .then(results => {
-        let translations = results[0];
-        translations = Array.isArray(translations) ?
-            translations : [translations];
-        return translations;
-        })
-        .catch(err => {
-        console.log(err);
-        });
+    htmlToMd(tempArticle).then(translatedArticle => {
+      const i = meta.indexOf('---', 4);
+      let translatedFile =
+        meta.slice(0, i) +
+        `localeTitle: ${translatedTitle}\n` +
+        meta.slice(i) +
+        translatedArticle;
+      writeFile(translatedFile, outputPath);
+    });
+  });
 }
-    
-const translateText = createTranslateText(lang);
 
+const createTranslateText = target => text => {
+  if (!text) {
+    return '';
+  }
+  const translate = new Translate();
+
+  return translate
+    .translate(text, target)
+    .then(results => {
+      let translations = results[0];
+      translations = Array.isArray(translations)
+        ? translations
+        : [translations];
+      return translations;
+    })
+    .catch(err => {
+      console.log(err);
+    });
+};
+
+const translateText = createTranslateText(lang);
 
 function writeFile(fileString, outputPath) {
-    fs.writeFile(outputPath, fileString, function(err) {
-        if (err) throw err;
-        console.log('Saved:' + outputPath);
-      });
+  fs.writeFile(outputPath, fileString, function(err) {
+    if (err) {
+      throw err;
+    }
+    console.log('Saved:' + outputPath);
+  });
 }
 
 function mdToHtml(file) {
-    return new Promise((resolve, reject) =>
-      mdProcessor.process(file, function(err, file) {
-        if (err) {
-          reject(err);
-        }
-        return resolve(file.contents);
-      })
-    );
-  };
-  
-  function htmlToMd(file) {
-      return new Promise((resolve, reject) =>
-        htmlProcessor.process(file, function(err, file) {
-          if (err) {
-            reject(err);
-          }
-          return resolve(file.contents);
-        })
-      );
-    };
\ No newline at end of file
+  return new Promise((resolve, reject) =>
+    mdProcessor.process(file, function(err, file) {
+      if (err) {
+        reject(err);
+      }
+      return resolve(file.contents);
+    })
+  );
+}
+
+function htmlToMd(file) {
+  return new Promise((resolve, reject) =>
+    htmlProcessor.process(file, function(err, file) {
+      if (err) {
+        reject(err);
+      }
+      return resolve(file.contents);
+    })
+  );
+}
diff --git a/tools/translation/translate-guide.js b/tools/translation/translate-guide.js
index b7e8554d5e351a..34d6c1a747fd46 100644
--- a/tools/translation/translate-guide.js
+++ b/tools/translation/translate-guide.js
@@ -1,102 +1,114 @@
 const fs = require('fs-extra');
-var showdown  = require('showdown');
+var showdown = require('showdown');
 const path = require('path');
 const readDirP = require('readdirp-walk');
-const {
-    Translate
-  } = require('@google-cloud/translate');
+const { Translate } = require('@google-cloud/translate');
 
-var TurndownService = require('turndown')
+var TurndownService = require('turndown');
 
-var turndownService = new TurndownService({'codeBlockStyle': 'fenced', 'headingStyle': 'atx'})
+var turndownService = new TurndownService({
+  codeBlockStyle: 'fenced',
+  headingStyle: 'atx'
+});
 
 const converter = new showdown.Converter();
 
 const lang = 'pt';
 const langFull = 'portuguese';
 
-
 readDirP({
-    root: path.resolve(__dirname, `./english`)
-  })
-  .on('data', translateChallenge);
+  root: path.resolve(__dirname, './english')
+}).on('data', translateChallenge);
 
 async function translateChallenge(file) {
-  const {
-    name,
-    depth,
-    path: filePath,
-    fullPath,
-    fullParentDir,
-    stat
-  } = file;
-  if (stat.isDirectory() || name === '.DS_Store' || file.depth == 1) return null;
+  const { name, fullPath, fullParentDir, stat } = file;
+  if (stat.isDirectory() || name === '.DS_Store' || file.depth === 1) {
+    return null;
+  }
 
-  
   const pathIndex = fullPath.indexOf('guide') + 6;
-  const outputDir = fullParentDir.substring(0, pathIndex) + `${langFull}/` + fullParentDir.substring(pathIndex + 8);
-  const outputPath = fullPath.substring(0, pathIndex) + `${langFull}/` + fullPath.substring(pathIndex + 8);
-  if (fs.existsSync(outputPath)) return null;
+  const outputDir =
+    fullParentDir.substring(0, pathIndex) +
+    `${langFull}/` +
+    fullParentDir.substring(pathIndex + 8);
+  const outputPath =
+    fullPath.substring(0, pathIndex) +
+    `${langFull}/` +
+    fullPath.substring(pathIndex + 8);
+  if (fs.existsSync(outputPath)) {
+    return null;
+  }
   fs.ensureDirSync(outputDir);
 
   const fileString = fs.readFileSync(fullPath).toString();
-  var i = fileString.indexOf('---', 4)
-  const meta = fileString.substring(0, i+4)
+  var i = fileString.indexOf('---', 4);
+  const meta = fileString.substring(0, i + 4);
   const title = fileString.split('\n')[1].split(': ')[1];
-  var article = fileString.substring(i+4)
-  
+  var article = fileString.substring(i + 4);
+
   var htmlArticle = converter.makeHtml(article);
-  htmlArticle = htmlArticle.replace(/\n/g, '
') - htmlArticle = htmlArticle.replace(/ /g, '        ') - htmlArticle = htmlArticle.replace(/ /g, '    ') - htmlArticle = htmlArticle.replace(/ /g, '  ') - Promise.all([ - translateText(title), - translateText(htmlArticle) - ]).then(function(translations) { - // Replace English with translation - let translatedTitle = translations[0][0]; - let tempArticle = translations[1][0] - tempArticle = tempArticle.replace(/
/g, '\n') - tempArticle = tempArticle.replace(/'/g, `'`) - - tempArticle = tempArticle.replace(/language-html">/g, 'language-html">\n') - tempArticle = tempArticle.replace(/
  <\/code/g, '
'); + htmlArticle = htmlArticle.replace( + / {8}/g, + '        ' + ); + htmlArticle = htmlArticle.replace(/ {4}/g, '    '); + htmlArticle = htmlArticle.replace(/ {2}/g, '  '); + return Promise.all([translateText(title), translateText(htmlArticle)]).then( + function(translations) { + // Replace English with translation + let translatedTitle = translations[0][0]; + let tempArticle = translations[1][0]; + tempArticle = tempArticle.replace(/
/g, '\n'); + tempArticle = tempArticle.replace(/'/g, "'"); - writeFile(translatedFile, outputPath); - }); + tempArticle = tempArticle.replace( + /language-html">/g, + 'language-html">\n' + ); + tempArticle = tempArticle.replace(/
  <\/code/g, '
text => { + if (!text) { + return ''; + } + const translate = new Translate(); -const createTranslateText = target => (text) => { - if (!text) return ''; - const translate = new Translate(); - - return translate - .translate(text, target) - .then(results => { - let translations = results[0]; - translations = Array.isArray(translations) ? - translations : [translations]; - return translations; - }) - .catch(err => { - console.log(err); - }); -} - -const translateText = createTranslateText(lang); + return translate + .translate(text, target) + .then(results => { + let translations = results[0]; + translations = Array.isArray(translations) + ? translations + : [translations]; + return translations; + }) + .catch(err => { + console.log(err); + }); +}; +const translateText = createTranslateText(lang); function writeFile(fileString, outputPath) { - fs.writeFile(outputPath, fileString, function(err) { - if (err) throw err; - console.log('Saved:' + outputPath); - }); -} \ No newline at end of file + fs.writeFile(outputPath, fileString, function(err) { + if (err) { + throw err; + } + console.log('Saved:' + outputPath); + }); +}