fix: throw on build errors

This commit is contained in:
Valeriy
2019-01-09 03:23:17 +03:00
committed by Stuart Taylor
parent dcca74ab92
commit 66f14c8329
2 changed files with 31 additions and 30 deletions

View File

@ -61,7 +61,7 @@ export const cssToHtml = cond([
// template: String, // template: String,
// files: [ polyVinyl ] // files: [ polyVinyl ]
// ) => String // ) => String
export function concatHtml(required, template, files = []) { export function concatHtml({ required = [], template, files = [] } = {}) {
const createBody = template ? _template(template) : defaultTemplate; const createBody = template ? _template(template) : defaultTemplate;
const head = required const head = required

View File

@ -1,11 +1,10 @@
import { throwers } from '../rechallenge/throwers'; import { throwers } from '../rechallenge/throwers';
import { import {
challengeFilesSelector, challengeFilesSelector,
isJSEnabledSelector,
challengeMetaSelector, challengeMetaSelector,
backendFormValuesSelector backendFormValuesSelector
} from '../redux'; } from '../redux';
import { transformers, testJS$JSX } from '../rechallenge/transformers'; import { transformers } from '../rechallenge/transformers';
import { cssToHtml, jsToHtml, concatHtml } from '../rechallenge/builders.js'; import { cssToHtml, jsToHtml, concatHtml } from '../rechallenge/builders.js';
const frameRunner = [ const frameRunner = [
@ -22,11 +21,6 @@ const globalRequires = [
} }
]; ];
function filterJSIfDisabled(state) {
const isJSEnabled = isJSEnabledSelector(state);
return file => !(testJS$JSX(file) && !isJSEnabled);
}
const applyFunction = fn => const applyFunction = fn =>
async function(file) { async function(file) {
try { try {
@ -39,13 +33,13 @@ const applyFunction = fn =>
} }
return file; return file;
} catch { } catch {
// file.error = e.message; // return { error };
return file; return file;
} }
}.bind(this); };
const composeFunctions = (...fns) => const composeFunctions = (...fns) =>
fns.map(applyFunction.bind(this)).reduce((f, g) => x => f(x).then(g)); fns.map(applyFunction).reduce((f, g) => x => f(x).then(g));
function buildSourceMap(files) { function buildSourceMap(files) {
return files.reduce((sources, file) => { return files.reduce((sources, file) => {
@ -54,6 +48,14 @@ function buildSourceMap(files) {
}, {}); }, {});
} }
function checkFilesErrors(files) {
const errors = files.filter(({ error }) => error).map(({ error }) => error);
if (errors.length) {
throw errors;
}
return files;
}
export function buildDOMChallenge(state) { export function buildDOMChallenge(state) {
const files = challengeFilesSelector(state); const files = challengeFilesSelector(state);
const { required = [], template } = challengeMetaSelector(state); const { required = [], template } = challengeMetaSelector(state);
@ -62,13 +64,13 @@ export function buildDOMChallenge(state) {
const pipeLine = composeFunctions(...throwers, ...transformers, ...toHtml); const pipeLine = composeFunctions(...throwers, ...transformers, ...toHtml);
const finalFiles = Object.keys(files) const finalFiles = Object.keys(files)
.map(key => files[key]) .map(key => files[key])
.filter(filterJSIfDisabled(state))
.filter(Boolean)
.map(pipeLine); .map(pipeLine);
return Promise.all(finalFiles).then(files => ({ return Promise.all(finalFiles)
build: concatHtml(finalRequires, template, files), .then(checkFilesErrors)
sources: buildSourceMap(files) .then(files => ({
})); build: concatHtml({ required: finalRequires, template, files }),
sources: buildSourceMap(files)
}));
} }
export function buildJSChallenge(state) { export function buildJSChallenge(state) {
@ -77,18 +79,17 @@ export function buildJSChallenge(state) {
const finalFiles = Object.keys(files) const finalFiles = Object.keys(files)
.map(key => files[key]) .map(key => files[key])
.map(pipeLine); .map(pipeLine);
return Promise.all(finalFiles).then(files => ({ return Promise.all(finalFiles)
build: files .then(checkFilesErrors)
.reduce( .then(files => ({
(body, file) => [ build: files
...body, .reduce(
file.head + '\n' + file.contents + '\n' + file.tail (body, file) => [...body, file.head, file.contents, file.tail],
], []
[] )
) .join('\n'),
.join('/n'), sources: buildSourceMap(files)
sources: buildSourceMap(files) }));
}));
} }
export function buildBackendChallenge(state) { export function buildBackendChallenge(state) {
@ -96,7 +97,7 @@ export function buildBackendChallenge(state) {
solution: { value: url } solution: { value: url }
} = backendFormValuesSelector(state); } = backendFormValuesSelector(state);
return { return {
build: concatHtml(frameRunner, ''), build: concatHtml({ required: frameRunner }),
sources: { url } sources: { url }
}; };
} }