chore: Apply linting fixes

This commit is contained in:
Bouncey 2019-02-16 08:48:52 +00:00 committed by mrugesh mohapatra
parent 5499042ce6
commit f45c3d4fa5
8 changed files with 221 additions and 188 deletions

View File

@ -45,7 +45,7 @@ function textToData(sectionIds) {
const lines = child.value.split('\n'); const lines = child.value.split('\n');
if (lines.filter(Boolean).length > 0) { if (lines.filter(Boolean).length > 0) {
lines.forEach((line, index) => { lines.forEach((line, index) => {
if (/^\s*$/.test(line)) { if ((/^\s*$/).test(line)) {
currentParagraph = null; currentParagraph = null;
} else { } else {
if (!currentParagraph || index > 0) { if (!currentParagraph || index > 0) {

View File

@ -5,7 +5,7 @@ const { createRedirects } = require('./createRedirects');
const testLocations = { const testLocations = {
api: 'https://api.example.com', api: 'https://api.example.com',
news: 'https://news.example.com', news: 'https://news.example.com',
forum: 'https://forum.example.com', forum: 'https://forum.example.com'
}; };
describe('createRedirects', () => { describe('createRedirects', () => {

View File

@ -53,6 +53,7 @@ fs.access(migrationMapPath, err => {
}) })
.catch(err => { .catch(err => {
console.error(err); console.error(err);
// eslint-disable-next-line
process.exit(1); process.exit(1);
}); });
} }

View File

@ -25,5 +25,6 @@ getChallengesForLang('english')
}) })
.catch(err => { .catch(err => {
console.error(err); console.error(err);
// eslint-disable-next-line
process.exit(1); process.exit(1);
}); });

View File

@ -5,7 +5,7 @@ const ObjectId = require('mongodb').ObjectID;
const debug = require('debug'); const debug = require('debug');
const log = debug('fcc:tools:seedLocalAuthUser'); const log = debug('fcc:tools:seedLocalAuthUser');
const { MONGOHQ_URL, LOCALE: lang } = process.env; const { MONGOHQ_URL } = process.env;
function handleError(err, client) { function handleError(err, client) {
if (err) { if (err) {

View File

@ -9,7 +9,14 @@ var dir = dir1 + '/' + dir2;
fs.readdirSync('../../curriculum/challenges/english/' + dir).forEach(file => { fs.readdirSync('../../curriculum/challenges/english/' + dir).forEach(file => {
if (file.includes('.md') && dir) { if (file.includes('.md') && dir) {
let originalFileName = 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) { fs.exists(originalFileName, function(exists) {
if (!exists) { if (!exists) {
@ -28,10 +35,12 @@ fs.readdirSync('../../curriculum/challenges/english/' + dir).forEach(file => {
// Load in full text, description, instructions, and title // Load in full text, description, instructions, and title
function getFile(file, dir) { function getFile(file, dir) {
let originalFileName = '../../curriculum/challenges/english/' + dir + '/' + file; let originalFileName =
'../../curriculum/challenges/english/' + dir + '/' + file;
let fileString = fs.readFileSync(originalFileName).toString(); 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(/<code>/g, '<code class="notranslate">'); fileString = fileString.replace(/<code>/g, '<code class="notranslate">');
fileString = fileString.replace( fileString = fileString.replace(
/<blockquote>/g, /<blockquote>/g,
@ -67,7 +76,7 @@ function processFile(
) { ) {
const translateText = (text, target) => { const translateText = (text, target) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof text == 'object' && Object.keys(text).length === 0) { if (typeof text === 'object' && Object.keys(text).length === 0) {
resolve(['']); resolve(['']);
} else { } else {
// Imports the Google Cloud client library // Imports the Google Cloud client library
@ -87,8 +96,6 @@ function processFile(
}) })
.catch(err => { .catch(err => {
reject(console.log('!!!!!', err)); reject(console.log('!!!!!', err));
if (err) {
}
}); });
} }
}); });
@ -106,21 +113,23 @@ function processFile(
let testsArray = tests.split('\n'); let testsArray = tests.split('\n');
let testsToTranslate = []; let testsToTranslate = [];
testsArray.forEach((test, index) => { testsArray.forEach(test => {
if (test.includes('- text: ')) { if (test.includes('- text: ')) {
testsToTranslate.push(test.slice(10)); testsToTranslate.push(test.slice(10));
} }
}); });
translateText(testsToTranslate, lang).then(translation => { translateText(testsToTranslate, lang)
let transIndex = 0; .then(translation => {
testsArray.forEach((test, index) => { let transIndex = 0;
if (test.includes('- text')) { testsArray.forEach((test, index) => {
testsArray[index] = ' - text: ' + translation[transIndex]; if (test.includes('- text')) {
transIndex++; testsArray[index] = ' - text: ' + translation[transIndex];
} transIndex++;
}); }
resolve(testsArray.join('\n')); });
}); resolve(testsArray.join('\n'));
})
.catch(reject);
}); });
}; };
@ -147,7 +156,7 @@ function processFile(
/<section id='tests'>(.|\n)*?<\/section>/, /<section id='tests'>(.|\n)*?<\/section>/,
translations[3] translations[3]
); );
fileString = fileString.replace(/ class=\"notranslate\"/g, ''); // remove 'notranslate' class fileString = fileString.replace(/ class=\"notranslate\"/g, '');
writeFile(fileString, file, dir); writeFile(fileString, file, dir);
}); });
} }
@ -163,7 +172,9 @@ function writeFile(fileString, file, dir) {
langFull + langFull +
'.md'; '.md';
fs.writeFile(fullFileName, fileString, function(err) { fs.writeFile(fullFileName, fileString, function(err) {
if (err) throw err; if (err) {
throw err;
}
console.log('Saved!'); console.log('Saved!');
}); });
} }

View File

@ -8,10 +8,7 @@ const stringify2 = require('remark-stringify');
const remark = require('rehype-remark'); const remark = require('rehype-remark');
const path = require('path'); const path = require('path');
const readDirP = require('readdirp-walk'); const readDirP = require('readdirp-walk');
const { const { Translate } = require('@google-cloud/translate');
Translate
} = require('@google-cloud/translate');
const lang = 'es'; const lang = 'es';
const langFull = 'spanish'; const langFull = 'spanish';
@ -27,112 +24,123 @@ const htmlProcessor = unified()
.use(stringify2); .use(stringify2);
readDirP({ readDirP({
root: path.resolve(__dirname, `./test`) root: path.resolve(__dirname, './test')
}) }).on('data', translateChallenge);
.on('data', translateChallenge);
async function translateChallenge(file) { async function translateChallenge(file) {
const { const { name, fullPath, fullParentDir, stat } = file;
name, if (stat.isDirectory() || name === '.DS_Store' || file.depth === 1) {
depth, return null;
path: filePath, }
fullPath,
fullParentDir,
stat
} = file;
if (stat.isDirectory() || name === '.DS_Store' || file.depth == 1) return null;
const pathIndex = fullPath.indexOf('guide') + 6; const pathIndex = fullPath.indexOf('guide') + 6;
const outputDir = fullParentDir.substring(0, pathIndex) + `${langFull}/` + fullParentDir.substring(pathIndex + 5); const outputDir =
const outputPath = fullPath.substring(0, pathIndex) + `${langFull}/` + fullPath.substring(pathIndex + 5); fullParentDir.substring(0, pathIndex) +
if (fs.existsSync(outputPath)) return null; `${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); fs.ensureDirSync(outputDir);
const fileString = fs.readFileSync(fullPath).toString(); const fileString = fs.readFileSync(fullPath).toString();
var i = fileString.indexOf('---', 4) var i = fileString.indexOf('---', 4);
const meta = fileString.substring(0, i+4) const meta = fileString.substring(0, i + 4);
const title = fileString.split('\n')[1].split(': ')[1]; const title = fileString.split('\n')[1].split(': ')[1];
var article = fileString.substring(i+4) var article = fileString.substring(i + 4);
mdToHtml(article).then((htmlArticle) => { return mdToHtml(article).then(htmlArticle => {
htmlArticle = htmlArticle.replace(/\n/g, '<br>') htmlArticle = htmlArticle.replace(/\n/g, '<br>');
htmlArticle = htmlArticle.replace(/ /g, '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;') htmlArticle = htmlArticle.replace(
htmlArticle = htmlArticle.replace(/ /g, '&nbsp;&nbsp;&nbsp;&nbsp;') / {8}/g,
htmlArticle = htmlArticle.replace(/ /g, '&nbsp;&nbsp;') '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
);
htmlArticle = htmlArticle.replace(/ {4}/g, '&nbsp;&nbsp;&nbsp;&nbsp;');
htmlArticle = htmlArticle.replace(/ {2}/g, '&nbsp;&nbsp;');
translate(htmlArticle, title, meta, outputPath); translate(htmlArticle, title, meta, outputPath);
}) });
} }
function translate(htmlArticle, title, meta, outputPath) { function translate(htmlArticle, title, meta, outputPath) {
Promise.all([ Promise.all([translateText(title), translateText(htmlArticle)]).then(function(
translateText(title), translations
translateText(htmlArticle) ) {
]).then(function(translations) { // Replace English with translation
// Replace English with translation let translatedTitle = translations[0][0];
let translatedTitle = translations[0][0]; let tempArticle = translations[1][0];
let tempArticle = translations[1][0] tempArticle = tempArticle.replace(/<br>/g, '\n');
tempArticle = tempArticle.replace(/<br>/g, '\n') tempArticle = tempArticle.replace(/&#39;/g, "'");
tempArticle = tempArticle.replace(/&#39;/g, `'`)
// tempArticle = tempArticle.replace(/language-html">/g, 'language-html">\n')
// tempArticle = tempArticle.replace(/<pre> <code/g, '<pre><code')
// tempArticle = tempArticle.replace(/<\/code> <\/pre/g, '</code></pre')
tempArticle = tempArticle.replace(/&nbsp;/g, ` `)
htmlToMd(tempArticle).then((translatedArticle) => { // tempArticle = tempArticle.replace(/language-html">/g,'language-html">\n')
const i = meta.indexOf('---', 4); // tempArticle = tempArticle.replace(/<pre> <code/g, '<pre><code')
let translatedFile = meta.slice(0, i) + `localeTitle: ${translatedTitle}\n` + meta.slice(i) + translatedArticle; // tempArticle = tempArticle.replace(/<\/code> <\/pre/g, '</code></pre')
writeFile(translatedFile, outputPath); tempArticle = tempArticle.replace(/&nbsp;/g, ' ');
});
}); 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 createTranslateText = target => (text) => { const createTranslateText = target => text => {
if (!text) return ''; if (!text) {
const translate = new Translate(); return '';
}
return translate const translate = new Translate();
.translate(text, target)
.then(results => { return translate
let translations = results[0]; .translate(text, target)
translations = Array.isArray(translations) ? .then(results => {
translations : [translations]; let translations = results[0];
return translations; translations = Array.isArray(translations)
}) ? translations
.catch(err => { : [translations];
console.log(err); return translations;
}); })
} .catch(err => {
console.log(err);
});
};
const translateText = createTranslateText(lang); const translateText = createTranslateText(lang);
function writeFile(fileString, outputPath) { function writeFile(fileString, outputPath) {
fs.writeFile(outputPath, fileString, function(err) { fs.writeFile(outputPath, fileString, function(err) {
if (err) throw err; if (err) {
console.log('Saved:' + outputPath); throw err;
}); }
console.log('Saved:' + outputPath);
});
} }
function mdToHtml(file) { function mdToHtml(file) {
return new Promise((resolve, reject) => return new Promise((resolve, reject) =>
mdProcessor.process(file, function(err, file) { mdProcessor.process(file, function(err, file) {
if (err) { if (err) {
reject(err); reject(err);
} }
return resolve(file.contents); return resolve(file.contents);
}) })
); );
}; }
function htmlToMd(file) { function htmlToMd(file) {
return new Promise((resolve, reject) => return new Promise((resolve, reject) =>
htmlProcessor.process(file, function(err, file) { htmlProcessor.process(file, function(err, file) {
if (err) { if (err) {
reject(err); reject(err);
} }
return resolve(file.contents); return resolve(file.contents);
}) })
); );
}; }

View File

@ -1,102 +1,114 @@
const fs = require('fs-extra'); const fs = require('fs-extra');
var showdown = require('showdown'); var showdown = require('showdown');
const path = require('path'); const path = require('path');
const readDirP = require('readdirp-walk'); const readDirP = require('readdirp-walk');
const { const { Translate } = require('@google-cloud/translate');
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 converter = new showdown.Converter();
const lang = 'pt'; const lang = 'pt';
const langFull = 'portuguese'; const langFull = 'portuguese';
readDirP({ readDirP({
root: path.resolve(__dirname, `./english`) root: path.resolve(__dirname, './english')
}) }).on('data', translateChallenge);
.on('data', translateChallenge);
async function translateChallenge(file) { async function translateChallenge(file) {
const { const { name, fullPath, fullParentDir, stat } = file;
name, if (stat.isDirectory() || name === '.DS_Store' || file.depth === 1) {
depth, return null;
path: filePath, }
fullPath,
fullParentDir,
stat
} = file;
if (stat.isDirectory() || name === '.DS_Store' || file.depth == 1) return null;
const pathIndex = fullPath.indexOf('guide') + 6; const pathIndex = fullPath.indexOf('guide') + 6;
const outputDir = fullParentDir.substring(0, pathIndex) + `${langFull}/` + fullParentDir.substring(pathIndex + 8); const outputDir =
const outputPath = fullPath.substring(0, pathIndex) + `${langFull}/` + fullPath.substring(pathIndex + 8); fullParentDir.substring(0, pathIndex) +
if (fs.existsSync(outputPath)) return null; `${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); fs.ensureDirSync(outputDir);
const fileString = fs.readFileSync(fullPath).toString(); const fileString = fs.readFileSync(fullPath).toString();
var i = fileString.indexOf('---', 4) var i = fileString.indexOf('---', 4);
const meta = fileString.substring(0, i+4) const meta = fileString.substring(0, i + 4);
const title = fileString.split('\n')[1].split(': ')[1]; 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); var htmlArticle = converter.makeHtml(article);
htmlArticle = htmlArticle.replace(/\n/g, '<br>') htmlArticle = htmlArticle.replace(/\n/g, '<br>');
htmlArticle = htmlArticle.replace(/ /g, '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;') htmlArticle = htmlArticle.replace(
htmlArticle = htmlArticle.replace(/ /g, '&nbsp;&nbsp;&nbsp;&nbsp;') / {8}/g,
htmlArticle = htmlArticle.replace(/ /g, '&nbsp;&nbsp;') '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
Promise.all([ );
translateText(title), htmlArticle = htmlArticle.replace(/ {4}/g, '&nbsp;&nbsp;&nbsp;&nbsp;');
translateText(htmlArticle) htmlArticle = htmlArticle.replace(/ {2}/g, '&nbsp;&nbsp;');
]).then(function(translations) { return Promise.all([translateText(title), translateText(htmlArticle)]).then(
// Replace English with translation function(translations) {
let translatedTitle = translations[0][0]; // Replace English with translation
let tempArticle = translations[1][0] let translatedTitle = translations[0][0];
tempArticle = tempArticle.replace(/<br>/g, '\n') let tempArticle = translations[1][0];
tempArticle = tempArticle.replace(/&#39;/g, `'`) tempArticle = tempArticle.replace(/<br>/g, '\n');
tempArticle = tempArticle.replace(/&#39;/g, "'");
tempArticle = tempArticle.replace(/language-html">/g, 'language-html">\n')
tempArticle = tempArticle.replace(/<pre> <code/g, '<pre><code')
tempArticle = tempArticle.replace(/<\/pre> <\/code/g, '</pre></code')
tempArticle = tempArticle.replace(/&nbsp;/g, ` `)
let translatedArticle = turndownService.turndown(tempArticle);
translatedArticle = translatedArticle.replace(/\n\n\`\`\`\n/g, '\n\`\`\`\n')
let translatedFile = meta.slice(0, i) + `localeTitle: ${translatedTitle}\n` + meta.slice(i) + translatedArticle;
writeFile(translatedFile, outputPath); tempArticle = tempArticle.replace(
}); /language-html">/g,
'language-html">\n'
);
tempArticle = tempArticle.replace(/<pre> <code/g, '<pre><code');
tempArticle = tempArticle.replace(/<\/pre> <\/code/g, '</pre></code');
tempArticle = tempArticle.replace(/&nbsp;/g, ' ');
let translatedArticle = turndownService.turndown(tempArticle);
translatedArticle = translatedArticle.replace(/\n\n\`\`\`\n/g, '\n```\n');
let translatedFile =
meta.slice(0, i) +
`localeTitle: ${translatedTitle}\n` +
meta.slice(i) +
translatedArticle;
writeFile(translatedFile, outputPath);
}
);
} }
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 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); const translateText = createTranslateText(lang);
function writeFile(fileString, outputPath) { function writeFile(fileString, outputPath) {
fs.writeFile(outputPath, fileString, function(err) { fs.writeFile(outputPath, fileString, function(err) {
if (err) throw err; if (err) {
console.log('Saved:' + outputPath); throw err;
}); }
} console.log('Saved:' + outputPath);
});
}