chore(seed): Move seed script to tools

This commit is contained in:
Bouncey
2018-10-07 08:51:58 +01:00
committed by mrugesh mohapatra
parent 26750776ed
commit bc9b3b4ddd
13 changed files with 6433 additions and 1801 deletions

6401
tools/scripts/seed/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,27 @@
{
"name": "@freecodecamp/database-seeding",
"version": "0.0.1",
"description": "A script to seed challenges in to the database",
"main": "index.js",
"scripts": {
"test": "jest"
},
"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",
"debug": "^4.0.1",
"dotenv": "^6.0.0",
"jest": "^23.6.0",
"lodash": "^4.17.11",
"mongodb": "^3.1.6"
}
}

View File

@ -0,0 +1,77 @@
const path = require('path');
const fs = require('fs');
require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
const MongoClient = require('mongodb').MongoClient;
const { getChallengesForLang } = require('@freecodecamp/curriculum');
const { flatten } = require('lodash');
const debug = require('debug');
const { createPathMigrationMap } = require('./createPathMigrationMap');
const log = debug('fcc:tools:seedChallenges');
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);
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 {
log('challenge seed complete');
client.close();
log('generating path migration map');
const pathMap = createPathMigrationMap(curriculum);
const outputDir = path.resolve(
__dirname,
'../../../api-server/server/resources/pathMigration.json'
);
fs.writeFile(outputDir, JSON.stringify(pathMap), err => {
if (err) {
console.error('Oh noes!!');
console.error(err);
}
log('path migration map generated');
});
}
});
}
);