fix(seed): Fix seed script for new curriculum pkg

This commit is contained in:
Bouncey
2018-10-06 02:25:11 +01:00
committed by mrugesh mohapatra
parent d3e4d88f0c
commit 26750776ed
5 changed files with 1374 additions and 166 deletions

View File

@ -3,6 +3,7 @@
"api-server",
"client",
"curriculum",
"seed",
"tools/challenge-md-parser"
],
"version": "independent"

View File

@ -7,6 +7,7 @@
"ensure-env": "node ./tools/scripts/ensure-env.js",
"lint": "echo 'Warning: TODO - Define Linting.'",
"pretest": "npm-run-all -s lint bootstrap",
"seed": "node seed/index.js",
"test": "npm-run-all -p test:*",
"test:client": "cd ./client && npm test && cd ../",
"test:curriculum": "echo 'Warning: TODO - Define Testing.'",

View File

@ -1,168 +1,59 @@
/* eslint-disable no-process-exit */
require('babel-register');
require('dotenv').load();
const adler32 = require('adler32');
const path = require('path');
require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
const MongoClient = require('mongodb').MongoClient;
const { getChallengesForLang } = require('@freecodecamp/curriculum');
const { flatten } = require('lodash');
const Rx = require('rx');
const _ = require('lodash');
const createDebugger = require('debug');
const utils = require('../server/utils');
const { getChallenges } = require('@freecodecamp/curriculum');
const { validateChallenge } = require(
'./schema/challengeSchema'
const { MONGOHQ_URL, LOCALE: lang } = process.env;
function handleError(err, client) {
if (err) {
console.error('Oh noes!!');
console.error(err);
try {
client.close();
} catch (e) {
// no-op
} finally {
/* eslint-disable-next-line no-process-exit */
process.exit(1);
}
}
}
MongoClient.connect(
MONGOHQ_URL,
{ useNewUrlParser: true },
function(err, client) {
handleError(err, client);
console.log('Connected successfully to mongo');
const db = client.db('freecodecamp');
const challenges = db.collection('challenges');
challenges.deleteMany({}, err => {
handleError(err, client);
const curriculum = getChallengesForLang(lang);
const allChallenges = Object.keys(curriculum)
.map(key => curriculum[key].blocks)
.reduce((challengeArray, superBlock) => {
const challengesForBlock = Object.keys(superBlock).map(
key => superBlock[key].challenges
);
return [...challengeArray, ...flatten(challengesForBlock)];
}, []);
try {
challenges.insertMany(allChallenges, { ordered: false });
} catch (e) {
handleError(e, client);
} finally {
console.log('challenge seed complete');
client.close();
}
});
}
);
const app = require('../server/server');
const log = createDebugger('fcc:seed');
// force logger to always output
// this may be brittle
log.enabled = true;
const dasherize = utils.dasherize;
const nameify = utils.nameify;
const Observable = Rx.Observable;
const Challenge = app.models.Challenge;
const destroyChallenges =
Observable.fromNodeCallback(Challenge.destroyAll, Challenge);
const createChallenges =
Observable.fromNodeCallback(Challenge.create, Challenge);
const Block = app.models.Block;
const destroyBlocks = Observable.fromNodeCallback(Block.destroyAll, Block);
const createBlocks = Observable.fromNodeCallback(Block.create, Block);
const arrToString = arr =>
Array.isArray(arr) ? arr.join('\n') : _.toString(arr);
Observable.combineLatest(
destroyChallenges(),
destroyBlocks()
)
.last()
.flatMap(function() { return Observable.from(getChallenges()); })
.flatMap(function(challengeSpec) {
const order = challengeSpec.order;
const blockName = challengeSpec.name;
const superBlock = challengeSpec.superBlock;
const superOrder = challengeSpec.superOrder;
const isBeta = !!challengeSpec.isBeta;
const isComingSoon = !!challengeSpec.isComingSoon;
const fileName = challengeSpec.fileName;
const helpRoom = challengeSpec.helpRoom || 'Help';
const time = challengeSpec.time;
const isLocked = !!challengeSpec.isLocked;
const message = challengeSpec.message;
const required = challengeSpec.required || [];
const template = challengeSpec.template;
const isPrivate = !!challengeSpec.isPrivate;
log('parsed %s successfully', blockName);
// challenge file has no challenges...
if (challengeSpec.challenges.length === 0) {
return Rx.Observable.just([{ block: 'empty ' + blockName }]);
}
const block = {
title: blockName,
name: nameify(blockName),
dashedName: dasherize(blockName),
superOrder,
superBlock,
superBlockMessage: message,
order,
time,
isLocked,
isPrivate
};
return createBlocks(block)
.map(block => {
log('successfully created %s block', block.name);
return challengeSpec.challenges
.map(function(challenge, index) {
challenge.name = nameify(challenge.title);
challenge.dashedName = dasherize(challenge.title);
challenge.checksum = adler32.sum(
Buffer(
challenge.title +
JSON.stringify(challenge.description) +
JSON.stringify(challenge.challengeSeed) +
JSON.stringify(challenge.tests)
)
);
if (challenge.files) {
challenge.files = _.reduce(challenge.files, (map, file) => {
map[file.key] = {
...file,
head: arrToString(file.head),
contents: arrToString(file.contents),
tail: arrToString(file.tail)
};
return map;
}, {});
}
challenge.fileName = fileName;
challenge.helpRoom = helpRoom;
challenge.order = order;
challenge.suborder = index + 1;
challenge.block = dasherize(blockName);
challenge.blockId = '' + block.id;
challenge.isBeta = challenge.isBeta || isBeta;
challenge.isComingSoon = challenge.isComingSoon || isComingSoon;
challenge.isLocked = challenge.isLocked || isLocked;
challenge.isPrivate = challenge.isPrivate || isPrivate;
challenge.time = challengeSpec.time;
challenge.superOrder = superOrder;
challenge.superBlock = superBlock
.split('-')
.map(function(word) {
return _.capitalize(word);
})
.join(' ');
challenge.required = (challenge.required || []).concat(required);
challenge.template = challenge.template || template;
return _.omit(
challenge,
[
'betaSolutions',
'betaTests',
'hints',
'MDNlinks',
'null',
'rawSolutions',
'react',
'reactRedux',
'redux',
'releasedOn',
'translations',
'type'
]
);
});
})
.flatMap(challenges => {
challenges.forEach(challenge => {
const result = validateChallenge(challenge);
if (result.error) {
console.log(result.value);
throw new Error(result.error);
}
});
return createChallenges(challenges);
});
})
.subscribe(
function(challenges) {
log('%s successfully saved', challenges[0].block);
},
function(err) { throw err; },
function() {
log('challenge seed completed');
process.exit(0);
}
);

1290
seed/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

25
seed/package.json Normal file
View File

@ -0,0 +1,25 @@
{
"name": "@freecodecamp/database-seeding",
"version": "0.0.1",
"description": "A script to seed challenges in to the database",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/freeCodeCamp/freeCodeCamp.git"
},
"author": "freeCodeCamp.org",
"license": "BSD-3-Clause",
"bugs": {
"url": "https://github.com/freeCodeCamp/freeCodeCamp/issues"
},
"homepage": "https://github.com/freeCodeCamp/freeCodeCamp#readme",
"devDependencies": {
"@freecodecamp/curriculum": "0.0.0-next.4",
"dotenv": "^6.0.0",
"lodash": "^4.17.11",
"mongodb": "^3.1.6"
}
}