fix: use location for language, not extension

Rather than relying on .lang.md this expects to find the English source
challenge in /curriculum/challenges/english/<translationPath>
This commit is contained in:
Oliver Eyton-Williams
2020-08-28 17:10:37 +02:00
parent 34f2c4ae32
commit 9089ddca5c
9 changed files with 138 additions and 166 deletions

View File

@ -16,9 +16,10 @@ describe('translation parser', () => {
return Promise.all([ return Promise.all([
parseMarkdown(path.resolve(__dirname, '__fixtures__/combined.md')), parseMarkdown(path.resolve(__dirname, '__fixtures__/combined.md')),
parseTranslation( parseTranslation(
path.resolve(__dirname, '__fixtures__/challenge.english.md'), path.resolve(__dirname, '__fixtures__/english/challenge.md'),
path.resolve(__dirname, '__fixtures__/challenge.chinese.md'), path.resolve(__dirname, '__fixtures__/chinese/challenge.md'),
SIMPLE_TRANSLATION SIMPLE_TRANSLATION,
'chinese'
) )
]).then(xs => expect(xs[1]).toEqual(xs[0])); ]).then(xs => expect(xs[1]).toEqual(xs[0]));
}); });
@ -30,10 +31,11 @@ describe('translation parser', () => {
parseTranslation( parseTranslation(
path.resolve( path.resolve(
__dirname, __dirname,
'__fixtures__/challenge-html-comments.english.md' '__fixtures__/english/challenge-html-comments.md'
), ),
path.resolve(__dirname, '__fixtures__/challenge.chinese.md'), path.resolve(__dirname, '__fixtures__/chinese/challenge.md'),
SIMPLE_TRANSLATION SIMPLE_TRANSLATION,
'chinese'
) )
]).then(xs => expect(xs[1]).toEqual(xs[0])); ]).then(xs => expect(xs[1]).toEqual(xs[0]));
}); });
@ -45,10 +47,11 @@ describe('translation parser', () => {
parseTranslation( parseTranslation(
path.resolve( path.resolve(
__dirname, __dirname,
'__fixtures__/challenge-jsx-comments.english.md' '__fixtures__/english/challenge-jsx-comments.md'
), ),
path.resolve(__dirname, '__fixtures__/challenge.chinese.md'), path.resolve(__dirname, '__fixtures__/chinese/challenge.md'),
SIMPLE_TRANSLATION SIMPLE_TRANSLATION,
'chinese'
) )
]).then(xs => expect(xs[1]).toEqual(xs[0])); ]).then(xs => expect(xs[1]).toEqual(xs[0]));
}); });
@ -60,10 +63,11 @@ describe('translation parser', () => {
parseTranslation( parseTranslation(
path.resolve( path.resolve(
__dirname, __dirname,
'__fixtures__/challenge-js-comments.english.md' '__fixtures__/english/challenge-js-comments.md'
), ),
path.resolve(__dirname, '__fixtures__/challenge.chinese.md'), path.resolve(__dirname, '__fixtures__/chinese/challenge.md'),
SIMPLE_TRANSLATION SIMPLE_TRANSLATION,
'chinese'
) )
]).then(xs => expect(xs[1]).toEqual(xs[0])); ]).then(xs => expect(xs[1]).toEqual(xs[0]));
}); });
@ -71,8 +75,8 @@ describe('translation parser', () => {
return Promise.all([ return Promise.all([
parseMarkdown(path.resolve(__dirname, '__fixtures__/combined.md')), parseMarkdown(path.resolve(__dirname, '__fixtures__/combined.md')),
parseTranslation( parseTranslation(
path.resolve(__dirname, '__fixtures__/challenge.english.md'), path.resolve(__dirname, '__fixtures__/english/challenge.md'),
path.resolve(__dirname, '__fixtures__/challenge-stripped.chinese.md'), path.resolve(__dirname, '__fixtures__/chinese/challenge-stripped.md'),
SIMPLE_TRANSLATION SIMPLE_TRANSLATION
) )
]).then(xs => expect(xs[1]).toEqual(xs[0])); ]).then(xs => expect(xs[1]).toEqual(xs[0]));

View File

@ -3,6 +3,7 @@ const { findIndex, reduce, toString } = require('lodash');
const readDirP = require('readdirp-walk'); const readDirP = require('readdirp-walk');
const { parseMarkdown } = require('../tools/challenge-md-parser'); const { parseMarkdown } = require('../tools/challenge-md-parser');
const fs = require('fs'); const fs = require('fs');
const util = require('util');
/* eslint-disable max-len */ /* eslint-disable max-len */
const { const {
mergeChallenges, mergeChallenges,
@ -17,6 +18,8 @@ const { createPoly } = require('../utils/polyvinyl');
const { blockNameify } = require('../utils/block-nameify'); const { blockNameify } = require('../utils/block-nameify');
const { supportedLangs } = require('./utils'); const { supportedLangs } = require('./utils');
const access = util.promisify(fs.access);
const challengesDir = path.resolve(__dirname, './challenges'); const challengesDir = path.resolve(__dirname, './challenges');
const metaDir = path.resolve(challengesDir, '_meta'); const metaDir = path.resolve(challengesDir, '_meta');
exports.challengesDir = challengesDir; exports.challengesDir = challengesDir;
@ -47,14 +50,15 @@ exports.getChallengesForLang = function getChallengesForLang(lang) {
readDirP({ root: getChallengesDirForLang(lang) }) readDirP({ root: getChallengesDirForLang(lang) })
.on('data', file => { .on('data', file => {
running++; running++;
buildCurriculum(file, curriculum).then(done); buildCurriculum(file, curriculum, lang).then(done);
}) })
.on('end', done); .on('end', done);
}); });
}; };
async function buildCurriculum(file, curriculum) { async function buildCurriculum(file, curriculum, lang) {
const { name, depth, path: filePath, fullPath, stat } = file; const { name, depth, path: filePath, stat } = file;
const createChallenge = createChallengeCreator(challengesDir, lang);
if (depth === 1 && stat.isDirectory()) { if (depth === 1 && stat.isDirectory()) {
// extract the superBlock info // extract the superBlock info
const { order, name: superBlock } = superBlockInfo(name); const { order, name: superBlock } = superBlockInfo(name);
@ -106,79 +110,85 @@ async function buildCurriculum(file, curriculum) {
} }
const { meta } = challengeBlock; const { meta } = challengeBlock;
const challenge = await createChallenge(fullPath, meta); const challenge = await createChallenge(filePath, meta);
challengeBlock.challenges = [...challengeBlock.challenges, challenge]; challengeBlock.challenges = [...challengeBlock.challenges, challenge];
} }
async function parseTranslation(engPath, transPath, dict) { async function parseTranslation(engPath, transPath, dict, lang) {
const engChal = await parseMarkdown(engPath); const engChal = await parseMarkdown(engPath);
const translatedChal = await parseMarkdown(transPath); const translatedChal = await parseMarkdown(transPath);
const engWithTranslatedComments = translateCommentsInChallenge( const engWithTranslatedComments = translateCommentsInChallenge(
engChal, engChal,
getChallengeLang(transPath), lang,
dict dict
); );
return mergeChallenges(engWithTranslatedComments, translatedChal); return mergeChallenges(engWithTranslatedComments, translatedChal);
} }
exports.parseTranslation = parseTranslation; function createChallengeCreator(basePath, lang) {
const hasEnglishSource = hasEnglishSourceCreator(basePath);
async function createChallenge(fullPath, maybeMeta) { return async function createChallenge(filePath, maybeMeta) {
let meta; let meta;
if (maybeMeta) { if (maybeMeta) {
meta = maybeMeta; meta = maybeMeta;
} else { } else {
const metaPath = path.resolve( const metaPath = path.resolve(
metaDir, metaDir,
`./${getBlockNameFromFullPath(fullPath)}/meta.json` `./${getBlockNameFromPath(filePath)}/meta.json`
);
meta = require(metaPath);
}
const { name: superBlock } = superBlockInfoFromPath(filePath);
if (!supportedLangs.includes(lang))
throw Error(`${lang} is not a accepted language.
Trying to parse ${filePath}`);
if (lang !== 'english' && !(await hasEnglishSource(filePath)))
throw Error(`Missing English challenge for
${filePath}
It should be in
${path.resolve(basePath, 'english', filePath)}
`);
// assumes superblock names are unique
// while the auditing is ongoing, we default to English for un-audited certs
// once that's complete, we can revert to using isEnglishChallenge(fullPath)
const useEnglish = lang === 'english' || !isAuditedCert(lang, superBlock);
const challenge = await (useEnglish
? parseMarkdown(path.resolve(basePath, 'english', filePath))
: parseTranslation(
path.resolve(basePath, 'english', filePath),
path.resolve(basePath, lang, filePath),
COMMENT_TRANSLATIONS,
lang
));
const challengeOrder = findIndex(
meta.challengeOrder,
([id]) => id === challenge.id
); );
meta = require(metaPath); const {
} name: blockName,
const { name: superBlock } = superBlockInfoFromFullPath(fullPath); order,
const lang = getChallengeLang(fullPath); superOrder,
if (!supportedLangs.includes(lang)) isPrivate,
throw Error(`${lang} is not a accepted language. required = [],
Trying to parse ${fullPath}`); template,
// assumes superblock names are unique time
// while the auditing is ongoing, we default to English for un-audited certs } = meta;
// once that's complete, we can revert to using isEnglishChallenge(fullPath) challenge.block = blockName;
const isEnglish = challenge.dashedName = dasherize(challenge.title);
isEnglishChallenge(fullPath) || !isAuditedCert(lang, superBlock); challenge.order = order;
if (isEnglish) fullPath = getEnglishPath(fullPath); challenge.superOrder = superOrder;
const challenge = await (isEnglish challenge.superBlock = superBlock;
? parseMarkdown(fullPath) challenge.challengeOrder = challengeOrder;
: parseTranslation( challenge.isPrivate = challenge.isPrivate || isPrivate;
getEnglishPath(fullPath), challenge.required = required.concat(challenge.required || []);
fullPath, challenge.template = template;
COMMENT_TRANSLATIONS challenge.time = time;
));
const challengeOrder = findIndex(
meta.challengeOrder,
([id]) => id === challenge.id
);
const {
name: blockName,
order,
superOrder,
isPrivate,
required = [],
template,
time
} = meta;
challenge.block = blockName;
challenge.dashedName = dasherize(challenge.title);
challenge.order = order;
challenge.superOrder = superOrder;
challenge.superBlock = superBlock;
challenge.challengeOrder = challengeOrder;
challenge.isPrivate = challenge.isPrivate || isPrivate;
challenge.required = required.concat(challenge.required || []);
challenge.template = template;
challenge.time = time;
return prepareChallenge(challenge); return prepareChallenge(challenge);
};
} }
// TODO: tests and more descriptive name. // TODO: tests and more descriptive name.
@ -203,8 +213,6 @@ function prepareChallenge(challenge) {
challenge.name = nameify(challenge.title); challenge.name = nameify(challenge.title);
if (challenge.files) { if (challenge.files) {
challenge.files = filesToObject(challenge.files); challenge.files = filesToObject(challenge.files);
// TODO: This should be something that can be folded into the above reduce
// EDIT: maybe not, now that we're doing the same for solutionFiles.
challenge.files = Object.keys(challenge.files) challenge.files = Object.keys(challenge.files)
.filter(key => challenge.files[key]) .filter(key => challenge.files[key])
.map(key => challenge.files[key]) .map(key => challenge.files[key])
@ -228,51 +236,23 @@ function prepareChallenge(challenge) {
return challenge; return challenge;
} }
exports.createChallenge = createChallenge; function hasEnglishSourceCreator(basePath) {
const englishRoot = path.resolve(__dirname, basePath, 'english');
function getEnglishPath(fullPath) { return async function(translationPath) {
const posix = path return await access(
.normalize(fullPath) path.join(englishRoot, translationPath),
.split(path.sep) fs.constants.F_OK
.join(path.posix.sep); )
const match = posix.match(/(.*curriculum\/challenges\/)([^/]*)(.*)(\2)(.*)/); .then(() => true)
const lang = getChallengeLang(fullPath); .catch(() => false);
if (!supportedLangs.includes(lang)) };
throw Error(`${lang} is not a accepted language.
Trying to parse ${fullPath}`);
if (match) {
return path.join(match[1], 'english', match[3] + 'english' + match[5]);
} else {
throw Error(`Malformed challenge path, ${fullPath} unable to parse.`);
}
} }
function getChallengeLang(fullPath) {
const match = fullPath.match(/\.(\w+)\.md$/);
if (!match || match.length < 2)
throw Error(`Missing language extension for
${fullPath}`);
return fullPath.match(/\.(\w+)\.md$/)[1];
}
function isEnglishChallenge(fullPath) {
return getChallengeLang(fullPath) === 'english';
}
exports.getChallengeLang = getChallengeLang;
exports.getEnglishPath = getEnglishPath;
exports.isEnglishChallenge = isEnglishChallenge;
function superBlockInfoFromPath(filePath) { function superBlockInfoFromPath(filePath) {
const [maybeSuper] = filePath.split(path.sep); const [maybeSuper] = filePath.split(path.sep);
return superBlockInfo(maybeSuper); return superBlockInfo(maybeSuper);
} }
function superBlockInfoFromFullPath(fullFilePath) {
const [, , maybeSuper] = fullFilePath.split(path.sep).reverse();
return superBlockInfo(maybeSuper);
}
function superBlockInfo(fileName) { function superBlockInfo(fileName) {
const [maybeOrder, ...superBlock] = fileName.split('-'); const [maybeOrder, ...superBlock] = fileName.split('-');
let order = parseInt(maybeOrder, 10); let order = parseInt(maybeOrder, 10);
@ -291,11 +271,10 @@ function getBlockNameFromPath(filePath) {
return block; return block;
} }
function getBlockNameFromFullPath(fullFilePath) {
const [, block] = fullFilePath.split(path.sep).reverse();
return block;
}
function arrToString(arr) { function arrToString(arr) {
return Array.isArray(arr) ? arr.join('\n') : toString(arr); return Array.isArray(arr) ? arr.join('\n') : toString(arr);
} }
exports.hasEnglishSourceCreator = hasEnglishSourceCreator;
exports.parseTranslation = parseTranslation;
exports.createChallengeCreator = createChallengeCreator;

View File

@ -1,68 +1,57 @@
/* global expect */ /* global expect beforeAll */
const { const {
createChallenge, challengesDir,
getChallengeLang, createChallengeCreator,
getEnglishPath, hasEnglishSourceCreator
isEnglishChallenge
} = require('./getChallenges'); } = require('./getChallenges');
/* eslint-disable max-len */ /* eslint-disable max-len */
const INVALID_PATH = 'not/challenge/path'; const REAL_PATH =
const ENGLISH_PATH = '01-responsive-web-design/applied-accessibility/add-a-text-alternative-to-images-for-visually-impaired-accessibility.english.md';
'curriculum/challenges/english/01-responsive-web-design/applied-accessibility/add-a-text-alternative-to-images-for-visually-impaired-accessibility.english.md'; const REAL_MISSING_PATH =
const CHINESE_PATH = '01-responsive-web-design/applied-accessibility/add-a-text-alternative-to-images-for-visually-impaired.md';
'curriculum/challenges/chinese/01-responsive-web-design/applied-accessibility/add-a-text-alternative-to-images-for-visually-impaired-accessibility.chinese.md';
const NOT_LANGUAGE_PATH =
'curriculum/challenges/chinese/01-responsive-web-design/applied-accessibility/add-a-text-alternative-to-images-for-visually-impaired-accessibility.notlang.md';
const MISSING_LANGUAGE_PATH =
'curriculum/challenges/chinese/01-responsive-web-design/applied-accessibility/add-a-text-alternative-to-images-for-visually-impaired-english.md';
const EXISTING_CHALLENGE_PATH = 'challenge.md';
const MISSING_CHALLENGE_PATH = 'no/challenge.md';
/* eslint-enable max-len */ /* eslint-enable max-len */
let hasEnglishSource;
let createChallenge;
const basePath = '__fixtures__';
describe('create non-English challenge', () => { describe('create non-English challenge', () => {
describe('createChallenge', () => { describe('createChallenge', () => {
it('throws if the filename includes an invalid language', async () => { it('throws if lang is an invalid language', async () => {
await expect(createChallenge(NOT_LANGUAGE_PATH)).rejects.toThrow( createChallenge = createChallengeCreator(basePath, 'notlang');
await expect(createChallenge(REAL_PATH)).rejects.toThrow(
'notlang is not a accepted language' 'notlang is not a accepted language'
); );
}); });
it('throws an error if the filename is missing a language', async () => { it('throws an error if the source challenge is missing', async () => {
await expect(createChallenge(MISSING_LANGUAGE_PATH)).rejects.toThrow( createChallenge = createChallengeCreator(challengesDir, 'chinese');
`Missing language extension for await expect(createChallenge(REAL_MISSING_PATH)).rejects.toThrow(
${MISSING_LANGUAGE_PATH}` `Missing English challenge for
${REAL_MISSING_PATH}
It should be in
`
); );
}); });
}); });
describe('getEnglishPath', () => { describe('hasEnglishSource', () => {
it('returns the full path of the English version of the challenge', () => { beforeAll(() => {
expect(getEnglishPath(CHINESE_PATH)).toBe(ENGLISH_PATH); hasEnglishSource = hasEnglishSourceCreator(basePath);
}); });
it('throws an error if the path has the wrong directory structure', () => { it('returns a boolean', async () => {
expect(() => getEnglishPath(INVALID_PATH)).toThrow(); const sourceExists = await hasEnglishSource(EXISTING_CHALLENGE_PATH);
expect(typeof sourceExists).toBe('boolean');
}); });
it('throws an error if the filename includes an invalid language', () => { it('returns true if the English challenge exists', async () => {
expect(() => getEnglishPath(NOT_LANGUAGE_PATH)).toThrow(); const sourceExists = await hasEnglishSource(EXISTING_CHALLENGE_PATH);
expect(sourceExists).toBe(true);
}); });
it('throws an error if the filename is missing a language', () => { it('returns false if the English challenge is missing', async () => {
expect(() => getEnglishPath(MISSING_LANGUAGE_PATH)).toThrow(); const sourceExists = await hasEnglishSource(MISSING_CHALLENGE_PATH);
}); expect(sourceExists).toBe(false);
});
describe('getChallengeLang', () => {
it("returns 'english' if the challenge is English", () => {
expect(getChallengeLang(ENGLISH_PATH)).toBe('english');
});
it("returns 'chinese' if the challenge is Chinese", () => {
expect(getChallengeLang(CHINESE_PATH)).toBe('chinese');
});
});
describe('isEnglishChallenge', () => {
it('returns true if the challenge is English', () => {
expect(isEnglishChallenge(ENGLISH_PATH)).toBe(true);
});
it('returns false if the challenge is not English', () => {
expect(isEnglishChallenge(CHINESE_PATH)).toBe(false);
}); });
}); });
}); });