feat: watch challenges (#34321)
This commit is contained in:
committed by
mrugesh mohapatra
parent
82ec250c75
commit
cee98aef43
5
api-server/package-lock.json
generated
5
api-server/package-lock.json
generated
@ -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",
|
||||||
|
@ -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",
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -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",
|
||||||
|
@ -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;
|
||||||
|
@ -1,24 +1,62 @@
|
|||||||
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(
|
||||||
|
'change',
|
||||||
|
filePath =>
|
||||||
|
(/\.md$/).test(filePath)
|
||||||
|
? onSourceChange(filePath)
|
||||||
|
.then(challenge => {
|
||||||
|
reporter.info(
|
||||||
|
`File changed at ${filePath}, replacing challengeNode id ${
|
||||||
|
challenge.id
|
||||||
|
}`
|
||||||
|
);
|
||||||
|
return createChallengeNode(challenge, reporter);
|
||||||
|
})
|
||||||
|
.then(createNode)
|
||||||
|
: null
|
||||||
|
);
|
||||||
|
|
||||||
|
function sourceAndCreateNodes() {
|
||||||
return source()
|
return source()
|
||||||
|
.then(challenges => Promise.all(challenges))
|
||||||
.then(challenges =>
|
.then(challenges =>
|
||||||
challenges
|
challenges
|
||||||
.filter(challenge => challenge.superBlock !== 'Certificates')
|
.filter(
|
||||||
.map(challenge => createChallengeNodes(challenge, reporter))
|
challenge => challenge.superBlock.toLowerCase() !== 'certificates'
|
||||||
|
)
|
||||||
|
.map(challenge => createChallengeNode(challenge, reporter))
|
||||||
.map(node => createNode(node))
|
.map(node => createNode(node))
|
||||||
)
|
)
|
||||||
.catch(e =>
|
.catch(e =>
|
||||||
@ -28,4 +66,5 @@ that delivers challenge files to the plugin
|
|||||||
|
|
||||||
`)
|
`)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
@ -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,7 +35,9 @@ 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
|
||||||
|
.filter(block => !block.isPrivate)
|
||||||
|
.map(({ meta, challenges }) => {
|
||||||
const {
|
const {
|
||||||
order,
|
order,
|
||||||
time,
|
time,
|
||||||
@ -68,6 +85,7 @@ exports.buildChallenges = async function buildChallenges() {
|
|||||||
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;
|
||||||
};
|
};
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
@ -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",
|
||||||
|
@ -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",
|
||||||
|
@ -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",
|
||||||
|
@ -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');
|
||||||
|
Reference in New Issue
Block a user