Files
freeCodeCamp/client/src/templates/Challenges/utils/build.js

123 lines
2.9 KiB
JavaScript
Raw Normal View History

import { flow } from 'lodash';
import { throwers } from '../rechallenge/throwers';
import {
challengeFilesSelector,
isJSEnabledSelector,
challengeMetaSelector,
backendFormValuesSelector
} from '../redux';
import { transformers, testJS$JSX } from '../rechallenge/transformers';
import { cssToHtml, jsToHtml, concatHtml } from '../rechallenge/builders.js';
2018-10-06 02:36:38 +03:00
import { isPromise } from './polyvinyl';
2018-12-29 11:34:03 +03:00
const frameRunner = [
{
src: '/js/frame-runner.js'
}
];
const globalRequires = [
{
link:
'https://cdnjs.cloudflare.com/' +
'ajax/libs/normalize/4.2.0/normalize.min.css'
}
];
function filterJSIfDisabled(state) {
const isJSEnabled = isJSEnabledSelector(state);
return file => !(testJS$JSX(file) && !isJSEnabled);
}
2018-10-06 02:36:38 +03:00
const applyFunction = fn => file => {
if (file.error) {
return file;
}
try {
let newFile = fn(file);
if (typeof newFile !== 'undefined') {
if (isPromise(newFile)) {
newFile = newFile.catch(() => {
// file.error = e.message;
return file;
});
}
return newFile;
}
return file;
} catch {
// file.error = e.message;
return file;
}
};
const applyFunctions = fns => file =>
fns.reduce((file, fn) => {
2018-10-06 02:36:38 +03:00
if (isPromise(file)) {
return file.then(applyFunction(fn));
}
2018-10-06 02:36:38 +03:00
return applyFunction(fn)(file);
}, file);
const toHtml = [jsToHtml, cssToHtml];
const pipeLine = flow(
applyFunctions(throwers),
applyFunctions(transformers),
applyFunctions(toHtml)
);
2018-12-29 11:34:03 +03:00
function buildSourceMap(files) {
return files.reduce((sources, file) => {
sources[file.name] = file.source || file.contents;
return sources;
}, {});
}
export function buildDOMChallenge(state) {
const files = challengeFilesSelector(state);
const { required = [], template } = challengeMetaSelector(state);
2018-12-29 11:34:03 +03:00
const finalRequires = [...globalRequires, ...required, ...frameRunner];
const finalFiles = Object.keys(files)
.map(key => files[key])
.filter(filterJSIfDisabled(state))
2018-12-29 11:34:03 +03:00
.filter(Boolean)
.map(pipeLine);
return Promise.all(finalFiles).then(files => ({
build: concatHtml(finalRequires, template, files),
sources: buildSourceMap(files)
}));
}
2018-12-29 11:34:03 +03:00
export function buildJSChallenge(state) {
const files = challengeFilesSelector(state);
2018-11-26 02:17:38 +03:00
const pipeLine = flow(
applyFunctions(throwers),
applyFunctions(transformers)
);
const finalFiles = Object.keys(files)
.map(key => files[key])
.map(pipeLine);
2018-12-29 11:34:03 +03:00
return Promise.all(finalFiles).then(files => ({
build: files
2018-11-26 02:17:38 +03:00
.reduce(
(body, file) => [
...body,
file.head + '\n' + file.contents + '\n' + file.tail
],
[]
)
2018-12-29 11:34:03 +03:00
.join('/n'),
sources: buildSourceMap(files)
2018-11-26 02:17:38 +03:00
}));
}
export function buildBackendChallenge(state) {
const {
solution: { value: url }
} = backendFormValuesSelector(state);
return {
2018-12-29 11:34:03 +03:00
build: concatHtml(frameRunner, ''),
sources: { url }
};
}