feat: watch challenges (#34321)

This commit is contained in:
Stuart Taylor
2018-11-16 18:22:52 +00:00
committed by mrugesh mohapatra
parent 82ec250c75
commit cee98aef43
12 changed files with 173 additions and 87 deletions

View File

@ -824,11 +824,6 @@
"to-fast-properties": "^2.0.0" "to-fast-properties": "^2.0.0"
} }
}, },
"@freecodecamp/curriculum": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/@freecodecamp/curriculum/-/curriculum-3.1.2.tgz",
"integrity": "sha512-buBBtAcGKagoUxSr3SuCycPlCySV9buou1Aod1uSbrKyNWS7hAIGdyloJiqPotEUfjh23uXm+ogvL9/Z4+QWhQ=="
},
"@freecodecamp/loopback-component-passport": { "@freecodecamp/loopback-component-passport": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/@freecodecamp/loopback-component-passport/-/loopback-component-passport-1.0.0.tgz", "resolved": "https://registry.npmjs.org/@freecodecamp/loopback-component-passport/-/loopback-component-passport-1.0.0.tgz",

View File

@ -27,7 +27,6 @@
}, },
"license": "(BSD-3-Clause AND CC-BY-SA-4.0)", "license": "(BSD-3-Clause AND CC-BY-SA-4.0)",
"dependencies": { "dependencies": {
"@freecodecamp/curriculum": "^3.1.1",
"@freecodecamp/loopback-component-passport": "^1.0.0", "@freecodecamp/loopback-component-passport": "^1.0.0",
"accepts": "^1.3.0", "accepts": "^1.3.0",
"auth0-js": "^9.5.1", "auth0-js": "^9.5.1",

View File

@ -1,6 +1,10 @@
const path = require('path'); const path = require('path');
const { buildChallenges } = require('./utils/buildChallenges'); const {
buildChallenges,
replaceChallengeNode,
localeChallengesRootDir
} = require('./utils/buildChallenges');
const { NODE_ENV: env, LOCALE: locale = 'english' } = process.env; const { NODE_ENV: env, LOCALE: locale = 'english' } = process.env;
@ -36,7 +40,9 @@ module.exports = {
resolve: 'fcc-source-challenges', resolve: 'fcc-source-challenges',
options: { options: {
name: 'challenges', name: 'challenges',
source: buildChallenges source: buildChallenges,
onSourceChange: replaceChallengeNode,
curriculumPath: localeChallengesRootDir
} }
}, },
{ {

View File

@ -13,7 +13,6 @@
"@fortawesome/free-regular-svg-icons": "^5.2.0", "@fortawesome/free-regular-svg-icons": "^5.2.0",
"@fortawesome/free-solid-svg-icons": "^5.2.0", "@fortawesome/free-solid-svg-icons": "^5.2.0",
"@fortawesome/react-fontawesome": "0.0.20", "@fortawesome/react-fontawesome": "0.0.20",
"@freecodecamp/curriculum": "0.0.0-next.4",
"@freecodecamp/react-bootstrap": "^0.32.3", "@freecodecamp/react-bootstrap": "^0.32.3",
"@reach/router": "^1.1.1", "@reach/router": "^1.1.1",
"axios": "^0.18.0", "axios": "^0.18.0",
@ -82,6 +81,7 @@
}, },
"devDependencies": { "devDependencies": {
"babel-plugin-transform-imports": "^1.5.0", "babel-plugin-transform-imports": "^1.5.0",
"chokidar": "^2.0.4",
"eslint": "^5.5.0", "eslint": "^5.5.0",
"eslint-config-freecodecamp": "^1.1.1", "eslint-config-freecodecamp": "^1.1.1",
"jest": "^23.6.0", "jest": "^23.6.0",

View File

@ -1,6 +1,6 @@
const crypto = require('crypto'); const crypto = require('crypto');
function createChallengeNodes(challenge, reporter) { function createChallengeNode(challenge, reporter) {
if (typeof challenge.description[0] !== 'string') { if (typeof challenge.description[0] !== 'string') {
reporter.warn(` reporter.warn(`
@ -35,4 +35,4 @@ function createChallengeNodes(challenge, reporter) {
); );
} }
exports.createChallengeNodes = createChallengeNodes; exports.createChallengeNode = createChallengeNode;

View File

@ -1,31 +1,70 @@
const { createChallengeNodes } = require('./create-Challenge-nodes'); const chokidar = require('chokidar');
const { createChallengeNode } = require('./create-Challenge-nodes');
exports.sourceNodes = function sourceChallengesSourceNodes( exports.sourceNodes = function sourceChallengesSourceNodes(
{ actions, reporter }, { actions, reporter },
pluginOptions pluginOptions
) { ) {
if (typeof pluginOptions.source !== 'function') { const { source, onSourceChange, curriculumPath } = pluginOptions;
if (typeof source !== 'function') {
reporter.panic(` reporter.panic(`
"source" is a required option for fcc-source-challenges. It must be a function "source" is a required option for fcc-source-challenges. It must be a
that delivers challenge files to the plugin function that delivers challenge objects to the plugin
`); `);
}
if (typeof onSourceChange !== 'function') {
reporter.panic(`
"onSourceChange" is a required option for fcc-source-challenges. It must be
a function that delivers a new challenge object to the plugin
`);
}
if (typeof curriculumPath !== 'string') {
reporter.panic(`
"curriculumPath" is a required option for fcc-source-challenges. It must be
a path to a curriculum directory
`);
} }
// TODO: Add live seed updates
const { createNode } = actions; const { createNode } = actions;
const watcher = chokidar.watch(curriculumPath, {
ignored: /(^|[\/\\])\../,
persistent: true
});
const { source } = pluginOptions; watcher.on('ready', sourceAndCreateNodes).on(
return source() 'change',
.then(challenges => filePath =>
challenges (/\.md$/).test(filePath)
.filter(challenge => challenge.superBlock !== 'Certificates') ? onSourceChange(filePath)
.map(challenge => createChallengeNodes(challenge, reporter)) .then(challenge => {
.map(node => createNode(node)) reporter.info(
) `File changed at ${filePath}, replacing challengeNode id ${
.catch(e => challenge.id
reporter.panic(`fcc-source-challenges }`
);
return createChallengeNode(challenge, reporter);
})
.then(createNode)
: null
);
function sourceAndCreateNodes() {
return source()
.then(challenges => Promise.all(challenges))
.then(challenges =>
challenges
.filter(
challenge => challenge.superBlock.toLowerCase() !== 'certificates'
)
.map(challenge => createChallengeNode(challenge, reporter))
.map(node => createNode(node))
)
.catch(e =>
reporter.panic(`fcc-source-challenges
${e.message} ${e.message}
`) `)
); );
}
}; };

View File

@ -1,7 +1,12 @@
const { getChallengesForLang } = require('@freecodecamp/curriculum'); const path = require('path');
const _ = require('lodash'); const _ = require('lodash');
const utils = require('../utils'); const {
getChallengesForLang,
createChallenge,
localeChallengesRootDir
} = require('../../curriculum/getChallenges');
const utils = require('./');
const { locale } = require('../config/env.json'); const { locale } = require('../config/env.json');
const dasherize = utils.dasherize; const dasherize = utils.dasherize;
@ -10,8 +15,18 @@ const nameify = utils.nameify;
const arrToString = arr => const arrToString = arr =>
Array.isArray(arr) ? arr.join('\n') : _.toString(arr); Array.isArray(arr) ? arr.join('\n') : _.toString(arr);
exports.localeChallengesRootDir = localeChallengesRootDir;
exports.replaceChallengeNode = function replaceChallengeNode(fullFilePath) {
const relativeChallengePath = fullFilePath.replace(
localeChallengesRootDir + path.sep,
''
);
return createChallenge(relativeChallengePath);
};
exports.buildChallenges = async function buildChallenges() { exports.buildChallenges = async function buildChallenges() {
const curriculum = await getChallengesForLang( locale ); const curriculum = await getChallengesForLang(locale);
const superBlocks = Object.keys(curriculum); const superBlocks = Object.keys(curriculum);
const blocks = superBlocks const blocks = superBlocks
.map(superBlock => curriculum[superBlock].blocks) .map(superBlock => curriculum[superBlock].blocks)
@ -20,54 +35,57 @@ exports.buildChallenges = async function buildChallenges() {
return blocks.concat(_.flatten(currentBlocks)); return blocks.concat(_.flatten(currentBlocks));
}, []); }, []);
const builtChallenges = blocks.filter(block => !block.isPrivate).map(({ meta, challenges }) => { const builtChallenges = blocks
const { .filter(block => !block.isPrivate)
order, .map(({ meta, challenges }) => {
time, const {
template, order,
required, time,
superBlock, template,
superOrder, required,
isPrivate, superBlock,
dashedName: blockDashedName, superOrder,
fileName isPrivate,
} = meta; dashedName: blockDashedName,
fileName
} = meta;
return challenges.map(challenge => { return challenges.map(challenge => {
challenge.name = nameify(challenge.title); challenge.name = nameify(challenge.title);
challenge.dashedName = dasherize(challenge.name); challenge.dashedName = dasherize(challenge.name);
if (challenge.files) { if (challenge.files) {
challenge.files = _.reduce( challenge.files = _.reduce(
challenge.files, challenge.files,
(map, file) => { (map, file) => {
map[file.key] = { map[file.key] = {
...file, ...file,
head: arrToString(file.head), head: arrToString(file.head),
contents: arrToString(file.contents), contents: arrToString(file.contents),
tail: arrToString(file.tail) tail: arrToString(file.tail)
}; };
return map; return map;
}, },
{} {}
); );
} }
challenge.fileName = fileName; challenge.fileName = fileName;
challenge.order = order; challenge.order = order;
challenge.block = blockDashedName; challenge.block = blockDashedName;
challenge.isPrivate = challenge.isPrivate || isPrivate; challenge.isPrivate = challenge.isPrivate || isPrivate;
challenge.isRequired = !!challenge.isRequired; challenge.isRequired = !!challenge.isRequired;
challenge.time = time; challenge.time = time;
challenge.superOrder = superOrder; challenge.superOrder = superOrder;
challenge.superBlock = superBlock challenge.superBlock = superBlock
.split('-') .split('-')
.map(word => _.capitalize(word)) .map(word => _.capitalize(word))
.join(' '); .join(' ');
challenge.required = required; challenge.required = required;
challenge.template = template; challenge.template = template;
return challenge; return challenge;
}); });
}).reduce((accu, current) => accu.concat(current), []) })
.reduce((accu, current) => accu.concat(current), []);
return builtChallenges; return builtChallenges;
}; };

View File

@ -1,12 +1,17 @@
const path = require('path'); const path = require('path');
const { findIndex } = require('lodash'); const { findIndex } = require('lodash');
const readDirP = require('readdirp-walk'); const readDirP = require('readdirp-walk');
const { parseMarkdown } = require('@freecodecamp/challenge-md-parser'); const { parseMarkdown } = require('@freecodecamp/challenge-md-parser');
const { dasherize } = require('./utils'); const { dasherize } = require('./utils');
const { locale } = require('../config/env.json');
const challengesDir = path.resolve(__dirname, './challenges'); const challengesDir = path.resolve(__dirname, './challenges');
const localeChallengesRootDir = path.resolve(challengesDir, `./${locale}`);
const metaDir = path.resolve(challengesDir, '_meta');
exports.challengesDir = challengesDir;
exports.localeChallengesRootDir = localeChallengesRootDir;
exports.metaDir = metaDir;
exports.getChallengesForLang = function getChallengesForLang(lang) { exports.getChallengesForLang = function getChallengesForLang(lang) {
let curriculum = {}; let curriculum = {};
@ -18,8 +23,7 @@ exports.getChallengesForLang = function getChallengesForLang(lang) {
}; };
async function buildCurriculum(file, curriculum) { async function buildCurriculum(file, curriculum) {
const { name, depth, path: filePath, stat } = file;
const { name, depth, path: filePath, fullPath, stat } = file;
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);
@ -41,9 +45,9 @@ async function buildCurriculum(file, curriculum) {
if (name === 'meta.json' || name === '.DS_Store') { if (name === 'meta.json' || name === '.DS_Store') {
return; return;
} }
const block = getBlockNameFromPath(filePath); const block = getBlockNameFromPath(filePath);
const { name: superBlock } = superBlockInfoFromPath(filePath); const { name: superBlock } = superBlockInfoFromPath(filePath);
const challenge = await parseMarkdown(fullPath);
let challengeBlock; let challengeBlock;
try { try {
challengeBlock = curriculum[superBlock].blocks[block]; challengeBlock = curriculum[superBlock].blocks[block];
@ -53,6 +57,26 @@ async function buildCurriculum(file, curriculum) {
process.exit(0); process.exit(0);
} }
const { meta } = challengeBlock; const { meta } = challengeBlock;
const challenge = await createChallenge(filePath, meta);
challengeBlock.challenges = [...challengeBlock.challenges, challenge];
}
async function createChallenge(challengeFilePath, maybeMeta) {
const fullPath = path.resolve(localeChallengesRootDir, challengeFilePath);
const metaPath = path.resolve(
metaDir,
`./${getBlockNameFromFullPath(fullPath)}/meta.json`
);
let meta;
if (maybeMeta) {
meta = maybeMeta;
} else {
meta = require(metaPath);
}
const { name: superBlock } = superBlockInfoFromPath(challengeFilePath);
const challenge = await parseMarkdown(fullPath);
const challengeOrder = findIndex( const challengeOrder = findIndex(
meta.challengeOrder, meta.challengeOrder,
([id]) => id === challenge.id ([id]) => id === challenge.id
@ -64,9 +88,12 @@ async function buildCurriculum(file, curriculum) {
challenge.superOrder = superOrder; challenge.superOrder = superOrder;
challenge.superBlock = superBlock; challenge.superBlock = superBlock;
challenge.challengeOrder = challengeOrder; challenge.challengeOrder = challengeOrder;
challengeBlock.challenges = [...challengeBlock.challenges, challenge];
return challenge;
} }
exports.createChallenge = createChallenge;
function superBlockInfoFromPath(filePath) { function superBlockInfoFromPath(filePath) {
const [maybeSuper] = filePath.split(path.sep); const [maybeSuper] = filePath.split(path.sep);
return superBlockInfo(maybeSuper); return superBlockInfo(maybeSuper);
@ -89,3 +116,8 @@ function getBlockNameFromPath(filePath) {
const [, block] = filePath.split(path.sep); const [, block] = filePath.split(path.sep);
return block; return block;
} }
function getBlockNameFromFullPath(fullFilePath) {
const [, block] = fullFilePath.split(path.sep).reverse();
return block;
}

View File

@ -14,11 +14,9 @@
"version": "0.0.0-next.4", "version": "0.0.0-next.4",
"main": "lib.js", "main": "lib.js",
"scripts": { "scripts": {
"build": "gulp build",
"develop": "gulp", "develop": "gulp",
"format": "prettier --write es5 './**/*.{js,json}' && npm run lint", "format": "prettier --write es5 './**/*.{js,json}' && npm run lint",
"lint": "eslint ./**/*.js --fix", "lint": "eslint ./**/*.js --fix",
"prepare": "npm run build",
"repack": "babel-node ./repack.js", "repack": "babel-node ./repack.js",
"semantic-release": "semantic-release", "semantic-release": "semantic-release",
"test": "mocha --delay --reporter progress --bail", "test": "mocha --delay --reporter progress --bail",

View File

@ -4,7 +4,7 @@
"scripts": { "scripts": {
"postinstall": "npm run bootstrap", "postinstall": "npm run bootstrap",
"prebootstrap": "npm run ensure-env", "prebootstrap": "npm run ensure-env",
"bootstrap": "lerna bootstrap && lerna run build --scope @freecodecamp/curriculum", "bootstrap": "lerna bootstrap",
"clean": "lerna clean", "clean": "lerna clean",
"develop": "npm-run-all -s ensure-env start-develop", "develop": "npm-run-all -s ensure-env start-develop",
"ensure-env": "cross-env DEBUG=fcc:* node ./tools/scripts/ensure-env.js", "ensure-env": "cross-env DEBUG=fcc:* node ./tools/scripts/ensure-env.js",

View File

@ -17,7 +17,6 @@
}, },
"homepage": "https://github.com/freeCodeCamp/freeCodeCamp#readme", "homepage": "https://github.com/freeCodeCamp/freeCodeCamp#readme",
"devDependencies": { "devDependencies": {
"@freecodecamp/curriculum": "0.0.0-next.4",
"debug": "^4.0.1", "debug": "^4.0.1",
"dotenv": "^6.0.0", "dotenv": "^6.0.0",
"jest": "^23.6.0", "jest": "^23.6.0",

View File

@ -2,10 +2,10 @@ const path = require('path');
const fs = require('fs'); const fs = require('fs');
require('dotenv').config({ path: path.resolve(__dirname, '../../../.env') }); require('dotenv').config({ path: path.resolve(__dirname, '../../../.env') });
const { MongoClient, ObjectID } = require('mongodb'); const { MongoClient, ObjectID } = require('mongodb');
const { getChallengesForLang } = require('@freecodecamp/curriculum');
const { flatten } = require('lodash'); const { flatten } = require('lodash');
const debug = require('debug'); const debug = require('debug');
const { getChallengesForLang } = require('../../../curriculum/getChallenges');
const { createPathMigrationMap } = require('./createPathMigrationMap'); const { createPathMigrationMap } = require('./createPathMigrationMap');
const log = debug('fcc:tools:seedChallenges'); const log = debug('fcc:tools:seedChallenges');