diff --git a/README.md b/README.md index 91176090b0..d0b791bb74 100644 --- a/README.md +++ b/README.md @@ -54,19 +54,23 @@ The easiest way to get started is to clone the repository: # Get the latest snapshot git clone --depth=1 https://github.com/freecodecamp/freecodecamp.git freecodecamp +# Change directory cd freecodecamp # Install NPM dependencies npm install +# Install Gulp globally +npm install -g gulp + +# Install Bower globally +npm install -g bower + # Install Bower dependencies bower install # Create a .env file and populate it with the necessary API keys and secrets: touch .env - -# Install Gulp globally -npm install -g gulp ``` Edit your `.env` file with the following API keys accordingly (if you only use email login, only the `MONGOHQ_URL`, `SESSION_SECRET`, `MANDRILL_USER` and `MANDRILL_PASSWORD` fields are necessary. Keep in mind if you want to use more services you'll have to get your own API keys for those services. @@ -107,20 +111,19 @@ DEBUG=true ``` ```bash -# Start the mongo server +# Start the mongo server in a seperate terminal mongod -# Create your mongo database. -# Type "mongo" in your terminal to access the mongo shell -use freecodecamp -# Exit the mongo shell with control + d - -# Seed your database with the challenges -node seed/ +# Initialize Free Code Camp +# This will seed the database for the first time. +# This command should only be run once. +npm run init # start the application gulp ``` +Now navigate to your browser and open http://localhost:3001 +Congradulations! You did it! License ------- diff --git a/package.json b/package.json index 06d6a933ce..9a765dc44f 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,8 @@ "url": "https://github.com/freecodecamp/freecodecamp.git" }, "scripts": { + "init": "npm run create-rev && echo '\n\nseeding database\n\n' && node seed && node seed/nonprofits", + "create-rev": "test ! -e server/rev-manifest.json && echo 'creating manifest' && touch server/rev-manifest.json && echo '{}' >> server/rev-manifest.json", "build": "gulp build", "start": "babel-node server/server.js", "prestart-production": "bower cache clean && bower install && gulp build", diff --git a/seed/index.js b/seed/index.js index 464242c26e..8300b97245 100644 --- a/seed/index.js +++ b/seed/index.js @@ -1,55 +1,34 @@ /* eslint-disable no-process-exit */ require('babel/register'); require('dotenv').load(); + var fs = require('fs'), + Rx = require('rx'), _ = require('lodash'), path = require('path'), - app = require('../server/server'), - nonprofits = require('./nonprofits.json'), - jobs = require('./jobs.json'); + app = require('../server/server'); function getFilesFor(dir) { return fs.readdirSync(path.join(__dirname, '/' + dir)); } var Challenge = app.models.Challenge; -var Nonprofit = app.models.Nonprofit; -var Job = app.models.Job; -var counter = 0; var challenges = getFilesFor('challenges'); -// plus two accounts for nonprofits and jobs seed. -var numberToSave = challenges.length + 1; +var destroy = Rx.Observable.fromNodeCallback(Challenge.destroyAll, Challenge); +var create = Rx.Observable.fromNodeCallback(Challenge.create, Challenge); -function completionMonitor() { - // Increment counter - counter++; - - // Exit if all challenges have been checked - if (counter >= numberToSave) { - process.exit(0); - } - - // Log where in the seed order we're currently at - console.log('Call: ' + counter + '/' + numberToSave); -} - -Challenge.destroyAll(function(err, info) { - if (err) { - throw err; - } else { - console.log('Deleted ', info); - } - challenges.forEach(function(file) { +destroy() + .flatMap(function() { return Rx.Observable.from(challenges); }) + .flatMap(function(file) { var challengeSpec = require('./challenges/' + file); var order = challengeSpec.order; var block = challengeSpec.name; var isBeta = !!challengeSpec.isBeta; + console.log('parsed %s successfully', file); // challenge file has no challenges... if (challengeSpec.challenges.length === 0) { - console.log('file %s has no challenges', file); - completionMonitor(); - return; + return Rx.Observable.just([{ block: 'empty ' + block }]); } var challenges = challengeSpec.challenges @@ -73,50 +52,15 @@ Challenge.destroyAll(function(err, info) { return challenge; }); - Challenge.create( - challenges, - function(err) { - if (err) { - throw err; - } else { - console.log('Successfully parsed %s', file); - completionMonitor(err); - } - } - ); - }); -}); - -Nonprofit.destroyAll(function(err, info) { - if (err) { - console.error(err); - } else { - console.log('Deleted ', info); - } - Nonprofit.create(nonprofits, function(err, data) { - if (err) { - throw err; - } else { - console.log('Saved ', data); + return create(challenges); + }) + .subscribe( + function(challenges) { + console.log('%s successfully saved', challenges[0].block); + }, + function(err) { throw err; }, + function() { + console.log('challenge seed completed'); + process.exit(0); } - completionMonitor(err); - console.log('nonprofits'); - }); -}); - -Job.destroyAll(function(err, info) { - if (err) { - throw err; - } else { - console.log('Deleted ', info); - } - Job.create(jobs, function(err, data) { - if (err) { - console.log('error: ', err); - } else { - console.log('Saved ', data); - } - console.log('jobs'); - completionMonitor(err); - }); -}); + ); diff --git a/seed/nonprofits.js b/seed/nonprofits.js new file mode 100644 index 0000000000..a30cdcfbd2 --- /dev/null +++ b/seed/nonprofits.js @@ -0,0 +1,29 @@ +/* eslint-disable no-process-exit */ +require('babel/register'); +require('dotenv').load(); + +var Rx = require('rx'); +var app = require('../server/server'); + +var Nonprofits = app.models.Challenge; +var nonprofits = require('./nonprofits.json'); +var destroy = Rx.Observable.fromNodeCallback(Nonprofits.destroyAll, Nonprofits); +var create = Rx.Observable.fromNodeCallback(Nonprofits.create, Nonprofits); + +destroy() + .flatMap(function() { + if (!nonprofits) { + return Rx.Observable.throw(new Error('No nonprofits found')); + } + return create(nonprofits); + }) + .subscribe( + function(nonprofits) { + console.log('successfully saved %d nonprofits', nonprofits.length); + }, + function(err) { throw err; }, + function() { + console.log('nonprofit seed completed'); + process.exit(0); + } + );