Skip to content

Commit

Permalink
chore: Apply linting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bouncey authored and raisedadead committed Feb 19, 2019
1 parent 5499042 commit f45c3d4
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 190 deletions.
2 changes: 1 addition & 1 deletion tools/challenge-md-parser/text-to-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tools/scripts/createRedirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
1 change: 1 addition & 0 deletions tools/scripts/ensure-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fs.access(migrationMapPath, err => {
})
.catch(err => {
console.error(err);
// eslint-disable-next-line
process.exit(1);
});
}
Expand Down
1 change: 1 addition & 0 deletions tools/scripts/ensure-path-migration-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ getChallengesForLang('english')
})
.catch(err => {
console.error(err);
// eslint-disable-next-line
process.exit(1);
});
2 changes: 1 addition & 1 deletion tools/scripts/seed/seedAuthUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
51 changes: 31 additions & 20 deletions tools/translation/translate-challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(/<code>/g, '<code class="notranslate">');
fileString = fileString.replace(
/<blockquote>/g,
Expand Down Expand Up @@ -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
Expand All @@ -87,8 +96,6 @@ function processFile(
})
.catch(err => {
reject(console.log('!!!!!', err));
if (err) {
}
});
}
});
Expand All @@ -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);
});
};

Expand All @@ -147,7 +156,7 @@ function processFile(
/<section id='tests'>(.|\n)*?<\/section>/,
translations[3]
);
fileString = fileString.replace(/ class=\"notranslate\"/g, ''); // remove 'notranslate' class
fileString = fileString.replace(/ class=\"notranslate\"/g, '');
writeFile(fileString, file, dir);
});
}
Expand All @@ -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!');
});
}
}
196 changes: 102 additions & 94 deletions tools/translation/translate-guide-old.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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, '<br>')
htmlArticle = htmlArticle.replace(/ /g, '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
htmlArticle = htmlArticle.replace(/ /g, '&nbsp;&nbsp;&nbsp;&nbsp;')
htmlArticle = htmlArticle.replace(/ /g, '&nbsp;&nbsp;')
var article = fileString.substring(i + 4);

return mdToHtml(article).then(htmlArticle => {
htmlArticle = htmlArticle.replace(/\n/g, '<br>');
htmlArticle = htmlArticle.replace(
/ {8}/g,
'&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);
})
});
}

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(/<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(/<\/code> <\/pre/g, '</code></pre')
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);
});
});
}
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(/<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(/<\/code> <\/pre/g, '</code></pre')
tempArticle = tempArticle.replace(/&nbsp;/g, ' ');

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);
});
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);
})
);
};
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);
})
);
}
Loading

0 comments on commit f45c3d4

Please sign in to comment.