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

103 lines
2.6 KiB
JavaScript
Raw Normal View History

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-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-12-29 16:42:09 +03:00
const applyFunction = fn =>
async function(file) {
try {
if (file.error) {
return file;
}
const newFile = await fn.call(this, file);
if (typeof newFile !== 'undefined') {
return newFile;
2018-10-06 02:36:38 +03:00
}
2018-12-29 16:42:09 +03:00
return file;
} catch {
// file.error = e.message;
return file;
2018-10-06 02:36:38 +03:00
}
2018-12-29 16:42:09 +03:00
}.bind(this);
2018-10-06 02:36:38 +03:00
2018-12-29 16:42:09 +03:00
const composeFunctions = (...fns) =>
fns.map(applyFunction.bind(this)).reduce((f, g) => x => f(x).then(g));
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];
2018-12-29 16:42:09 +03:00
const toHtml = [jsToHtml, cssToHtml];
const pipeLine = composeFunctions(...throwers, ...transformers, ...toHtml);
2018-12-29 11:34:03 +03:00
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-12-29 16:42:09 +03:00
const pipeLine = composeFunctions(...throwers, ...transformers);
2018-11-26 02:17:38 +03:00
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 }
};
}