const fs = require('fs-extra'); const path = require('path'); const { omit, findLastIndex } = require('lodash'); const YAML = require('js-yaml'); const { dasherize } = require('./utils'); const { getChallenges } = require('./getChallenges'); const blackListedFieldNames = [ 'betaSolutions', 'betaTests', 'hints', 'MDNlinks', 'null', 'rawSolutions', 'react', 'reactRedux', 'redux', 'releasedOn', 'translations', 'type' ]; getChallenges().forEach(block => { const { name, order, challenges, time = '', superBlock, superOrder, template = '', required = [], ...restBlock } = block; const blockDashedName = dasherize(name); const blockMeta = { name, dashedName: blockDashedName, order, time, template, required, superBlock, superOrder, challengeOrder: challenges.map(({ id, title }) => [id, title]), ...restBlock }; const superOrderPrefix = `0${superOrder}`; const outputDir = path.resolve( __dirname, `./challenges/english/${superOrderPrefix}-${superBlock}/${blockDashedName}` ); fs.ensureDirSync(outputDir); challenges.forEach(challenge => { const { description: oldDescription = [], files = {}, tests = [], solutions = [], ...restChallenge } = challenge; const challengeMeta = omit(restChallenge, blackListedFieldNames); const challengeFileName = `${dasherize(challenge.title)}.english.md`; let description = ''; let instructions = ''; const hrIndex = findLastIndex(oldDescription, el => (/^$/).test(el) ); if (hrIndex && hrIndex !== -1) { description = oldDescription.slice(0, hrIndex).join('\n'); instructions = oldDescription.slice(hrIndex + 1).join('\n'); } else { description = oldDescription.join('\n'); } const md = `--- ${YAML.dump(challengeMeta, { lineWidth: 10000 })}--- ## Description
${description}
## Instructions
${instructions}
## Tests
\`\`\`yml ${YAML.dump({ tests }, { lineWidth: 10000 })} \`\`\`
## Challenge Seed
${generateChallengeSeed(files)}
## Solution
${ solutions.length === 0 ? `\`\`\`js // solution required \`\`\`` : solutions .map( solution => ` \`\`\`js ${solution} \`\`\` ` ) .join('\n') }
`; fs.writeFile(`${outputDir}/${challengeFileName}`, md); fs.writeFile(`${outputDir}/meta.json`, JSON.stringify(blockMeta, null, 2)); }); }); function generateChallengeSeed(files) { return Object.keys(files) .map(key => files[key]) .map(file => { const { ext, contents = [], head = [], tail = [] } = file; return `
\`\`\`${ext} ${contents.join('\n')} \`\`\`
${ head.length ? ` ### Before Test
\`\`\`${ext} ${head.join('\n')} \`\`\`
` : '' } ${ tail.length ? ` ### After Test
\`\`\`js console.info('after the test'); \`\`\`
` : '' } `; }) .join('\n'); }