fix: Add 'production' check

This commit is contained in:
Bouncey
2018-11-09 16:07:52 +00:00
committed by mrugesh mohapatra
parent fbee89b345
commit 01097385d6

View File

@ -9,7 +9,7 @@ const debug = require('debug');
const log = debug('fcc:tools:seedNewsArticles'); const log = debug('fcc:tools:seedNewsArticles');
const { MONGOHQ_URL } = process.env; const { MONGOHQ_URL, NODE_ENV: env } = process.env;
function handleError(err, client) { function handleError(err, client) {
if (err) { if (err) {
@ -26,30 +26,32 @@ function handleError(err, client) {
} }
} }
MongoClient.connect( if (env !== 'production') {
MONGOHQ_URL, MongoClient.connect(
{ useNewUrlParser: true }, MONGOHQ_URL,
async function(err, client) { { useNewUrlParser: true },
handleError(err, client); async function(err, client) {
handleError(err, client);
log('Connected successfully to mongo'); log('Connected successfully to mongo');
const db = client.db('freecodecamp'); const db = client.db('freecodecamp');
const articleCollection = db.collection('article'); const articleCollection = db.collection('article');
const articles = stubArticles(200); const articles = stubArticles(200);
await articleCollection await articleCollection
.deleteMany({}) .deleteMany({})
.catch(err => handleError(err, client)); .catch(err => handleError(err, client));
return articleCollection return articleCollection
.insertMany(articles) .insertMany(articles)
.then(({ insertedCount }) => { .then(({ insertedCount }) => {
log('inserted %d new articles', insertedCount); log('inserted %d new articles', insertedCount);
client.close(); client.close();
}) })
.catch(err => handleError(err, client)); .catch(err => handleError(err, client));
} }
); );
}
function stubArticles(numberOfArticles = 1) { function stubArticles(numberOfArticles = 1) {
return new Array(numberOfArticles).fill('').map(() => generateArticle()); return new Array(numberOfArticles).fill('').map(() => generateArticle());