diff --git a/package.json b/package.json index 1b24bf5c5a..288de91194 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "postinstall": "npm run bootstrap", "seed": "npm-run-all -p seed:*", "seed:auth-user": "cross-env DEBUG=fcc:* node ./tools/scripts/seed/seedAuthUser", - "seed:challenges": "cross-env DEBUG=fcc:* node ./tools/scripts/seed/seedChallenges", + "seed:path-migration": "cross-env DEBUG=fcc:* node ./tools/scripts/seed/create-path-migration", "serve:client": "cd ./client && npm run serve", "start": "npm-run-all ensure-env -p develop:server serve:client", "test": "npm-run-all -p test:*", diff --git a/tools/scripts/seed/create-path-migration.js b/tools/scripts/seed/create-path-migration.js new file mode 100644 index 0000000000..e5e43621bf --- /dev/null +++ b/tools/scripts/seed/create-path-migration.js @@ -0,0 +1,27 @@ +const path = require('path'); +const fs = require('fs'); +require('dotenv').config({ path: path.resolve(__dirname, '../../../.env') }); +const debug = require('debug'); + +const { getChallengesForLang } = require('../../../curriculum/getChallenges'); +const { createPathMigrationMap } = require('./createPathMigrationMap'); + +const log = debug('fcc:tools:seedChallenges'); +const { LOCALE: lang = 'english' } = process.env; + +getChallengesForLang(lang).then(curriculum => { + 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('failed to save pathMigration'); + console.error(err); + } else { + log('path migration map generated'); + } + }); +}); diff --git a/tools/scripts/seed/seedChallenges.js b/tools/scripts/seed/seedChallenges.js deleted file mode 100644 index 7524b94549..0000000000 --- a/tools/scripts/seed/seedChallenges.js +++ /dev/null @@ -1,86 +0,0 @@ -const path = require('path'); -const fs = require('fs'); -require('dotenv').config({ path: path.resolve(__dirname, '../../../.env') }); -const { MongoClient, ObjectID } = require('mongodb'); -const { flatten } = require('lodash'); -const debug = require('debug'); - -const { getChallengesForLang } = require('../../../curriculum/getChallenges'); -const { createPathMigrationMap } = require('./createPathMigrationMap'); - -const log = debug('fcc:tools:seedChallenges'); -const { MONGOHQ_URL, LOCALE: lang = 'english' } = 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 at %s', MONGOHQ_URL); - - const db = client.db('freecodecamp'); - const challengeCollection = db.collection('challenge'); - - challengeCollection.deleteMany({}, async err => { - handleError(err, client); - - log('deleted all the challenges'); - - const curriculum = await 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)]; - }, []) - .map(challenge => { - const currentId = challenge.id.slice(0); - challenge._id = ObjectID(currentId); - delete challenge.id; - return challenge; - }); - - try { - challengeCollection.insertMany(allChallenges, { ordered: false }, err => { - handleError(err, client); - log('challenge seed complete'); - client.close(); - }); - } catch (e) { - handleError(e, client); - } finally { - 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'); - }); - } - }); -});