From 0522ec8b13bd2174b56a0dfcb30759303a53349f Mon Sep 17 00:00:00 2001 From: Berkeley Martinez Date: Tue, 20 Oct 2015 19:53:08 -0700 Subject: [PATCH 1/4] Fix first start for new devs also removes job seed --- README.md | 25 ++++++------ package.json | 2 + seed/index.js | 98 ++++++++++------------------------------------ seed/nonprofits.js | 29 ++++++++++++++ 4 files changed, 66 insertions(+), 88 deletions(-) create mode 100644 seed/nonprofits.js 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); + } + ); From 321040fe22f5e35553b54ed2615a03bbcc9b7c79 Mon Sep 17 00:00:00 2001 From: Berkeley Martinez Date: Tue, 20 Oct 2015 23:25:32 -0700 Subject: [PATCH 2/4] Move gulp modules to dependencies --- package.json | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 9a765dc44f..d9b5a9bca4 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,13 @@ "forever": "~0.14.1", "frameguard": "^0.2.2", "github-api": "~0.7.0", + "gulp": "~3.8.8", + "gulp-eslint": "~0.9.0", + "gulp-inject": "~1.0.2", + "gulp-jsonlint": "^1.1.0", + "gulp-nodemon": "^2.0.3", + "gulp-notify": "^2.2.0", + "gulp-plumber": "^1.0.1", "gulp-less": "^3.0.3", "gulp-minify-css": "~0.5.1", "gulp-reduce-file": "0.0.1", @@ -116,13 +123,6 @@ "browserify": "^10.2.4", "chai": "~1.10.0", "envify": "^3.4.0", - "gulp": "~3.8.8", - "gulp-eslint": "~0.9.0", - "gulp-inject": "~1.0.2", - "gulp-jsonlint": "^1.1.0", - "gulp-nodemon": "^2.0.3", - "gulp-notify": "^2.2.0", - "gulp-plumber": "^1.0.1", "istanbul": "^0.3.15", "loopback-explorer": "^1.7.2", "loopback-testing": "^1.1.0", From 61715da5266447d55596ffbab9f263a55d67fdfa Mon Sep 17 00:00:00 2001 From: Berkeley Martinez Date: Wed, 21 Oct 2015 21:51:37 -0700 Subject: [PATCH 3/4] Fix nonprofit model is called nonprofit --- seed/nonprofits.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/seed/nonprofits.js b/seed/nonprofits.js index a30cdcfbd2..96ed0ba5c9 100644 --- a/seed/nonprofits.js +++ b/seed/nonprofits.js @@ -5,7 +5,7 @@ require('dotenv').load(); var Rx = require('rx'); var app = require('../server/server'); -var Nonprofits = app.models.Challenge; +var Nonprofits = app.models.Nonprofit; var nonprofits = require('./nonprofits.json'); var destroy = Rx.Observable.fromNodeCallback(Nonprofits.destroyAll, Nonprofits); var create = Rx.Observable.fromNodeCallback(Nonprofits.create, Nonprofits); From b85826a00f79dedd64607c928e2d2dd457a9b5a4 Mon Sep 17 00:00:00 2001 From: Berkeley Martinez Date: Wed, 21 Oct 2015 21:55:47 -0700 Subject: [PATCH 4/4] Change init to first-time --- README.md | 2 +- package.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d0b791bb74..1cf0a455de 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ mongod # Initialize Free Code Camp # This will seed the database for the first time. # This command should only be run once. -npm run init +npm run first-time # start the application gulp diff --git a/package.json b/package.json index d9b5a9bca4..1c6f77559a 100644 --- a/package.json +++ b/package.json @@ -6,8 +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", + "first-time": "npm run create-rev && echo '\n\nseeding database\n\n' && node seed && node seed/nonprofits", + "create-rev": "test ! -e server/rev-manifest.json && echo '\n\ncreating manifest\n\n' && 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",