Merge pull request #4279 from FreeCodeCamp/feature/challenge-dev-mode

Feature in dev mode always pull from disk
This commit is contained in:
Rex Schrader
2015-11-10 10:19:10 -08:00
7 changed files with 75 additions and 15 deletions

View File

@ -54,6 +54,10 @@
"default": [],
"description": "prepended to user code"
},
"fileName": {
"type": "string",
"description": "filename challenge comes from. Used in dev mode"
},
"challengeSeed": {
"type": "array"
},

View File

@ -93,8 +93,7 @@ var paths = {
],
challenges: [
'seed/challenges/*.json',
'seed/under-construction/*.json'
'seed/challenges/*.json'
]
};
@ -399,10 +398,15 @@ var watchDependents = [
'pack-watch-manifest'
];
gulp.task('reload', function() {
notify({ message: 'test changed' });
reload();
});
gulp.task('watch', watchDependents, function() {
gulp.watch(paths.lessFiles, ['less']);
gulp.watch(paths.js, ['js']);
gulp.watch(paths.challenges, ['test-challenges']);
gulp.watch(paths.challenges, ['test-challenges', 'reload']);
gulp.watch(paths.js, ['js', 'dependents']);
gulp.watch(paths.dependents, ['dependents']);
gulp.watch(paths.manifest + '/*.json', ['build-manifest-watch']);

View File

@ -10,7 +10,10 @@ module.exports = function getChallenges() {
try {
return getFilesFor('challenges')
.map(function(file) {
return require('./challenges/' + file);
var challengeSpec = require('./challenges/' + file);
challengeSpec.fileName = file;
return challengeSpec;
});
} catch (e) {
console.log('error', e);

View File

@ -18,6 +18,7 @@ destroy()
var order = challengeSpec.order;
var block = challengeSpec.name;
var isBeta = !!challengeSpec.isBeta;
var fileName = challengeSpec.fileName;
console.log('parsed %s successfully', block);
// challenge file has no challenges...
@ -37,6 +38,8 @@ destroy()
.toLowerCase()
.replace(/\:/g, '')
.replace(/\s/g, '-');
challenge.fileName = fileName;
challenge.order = order;
challenge.suborder = index + 1;
challenge.block = block;

View File

@ -4,7 +4,15 @@ import moment from 'moment';
import { Observable, Scheduler } from 'rx';
import assign from 'object.assign';
import debugFactory from 'debug';
import utils from '../utils';
import {
dasherize,
unDasherize,
getMDNLinks,
randomVerb,
randomPhrase,
randomCompliment
} from '../utils';
import {
saveUser,
@ -16,6 +24,8 @@ import {
ifNoUserSend
} from '../utils/middleware';
import getFromDisk$ from '../utils/getFromDisk$';
const isDev = process.env.NODE_ENV !== 'production';
const isBeta = !!process.env.BETA;
const debug = debugFactory('freecc:challenges');
@ -31,10 +41,6 @@ const challengeView = {
7: 'coursewares/showStep'
};
const dasherize = utils.dasherize;
const unDasherize = utils.unDasherize;
const getMDNLinks = utils.getMDNLinks;
/*
function makeChallengesUnique(challengeArr) {
// clone and reverse challenges
@ -262,6 +268,12 @@ module.exports = function(app) {
return testChallengeName.test(challenge.name);
})
.last({ defaultValue: null })
.flatMap(challenge => {
if (challenge && isDev) {
return getFromDisk$(challenge);
}
return Observable.just(challenge);
})
.flatMap(challenge => {
// Handle not found
@ -311,11 +323,9 @@ module.exports = function(app) {
MDNlinks: getMDNLinks(challenge.MDNlinks),
// htmls specific
environment: utils.whichEnvironment(),
verb: utils.randomVerb(),
phrase: utils.randomPhrase(),
compliment: utils.randomCompliment()
verb: randomVerb(),
phrase: randomPhrase(),
compliment: randomCompliment()
});
})
.subscribe(

View File

@ -0,0 +1,37 @@
import _ from 'lodash';
import path from 'path';
import { Observable } from 'rx';
const basePath = process.cwd() + '/seed/challenges/';
export default function getFromDisk$(challenge) {
if (challenge && !challenge.fileName) {
throw new Error(
`Challenge ${challenge.name} has no fileName.
Did you remember run node seed?`
);
}
delete require.cache[require.resolve(
path.join(basePath, challenge.fileName)
)];
return Observable.just(require(path.join(basePath, challenge.fileName)))
.map(challengeSpec => challengeSpec.challenges[challenge.suborder - 1])
.map(challenge => {
challenge.head = challenge.head || [];
challenge.tail = challenge.tail || [];
challenge.challengeType = '' + challenge.challengeType;
challenge.name =
_.capitalize(challenge.type) +
': ' +
challenge.title.replace(/[^a-zA-Z0-9\s]/g, '');
challenge.dashedName = challenge.name
.toLowerCase()
.replace(/\:/g, '')
.replace(/\s/g, '-');
return challenge;
});
}

View File

@ -100,7 +100,6 @@ block content
common.challengeType = !{JSON.stringify(challengeType)};
common.dashedName = !{JSON.stringify(dashedName)};
common.prodOrDev = !{JSON.stringify(environment)};
common.started = Math.floor(Date.now());
include ../partials/challenge-footer