Skip to content

Commit

Permalink
feat: add build-time error saving
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Aug 1, 2020
1 parent 655a0b8 commit 912074f
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 13 deletions.
4 changes: 4 additions & 0 deletions core/core/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ export const CURRENT_STORY = '.';
* store of stories information in memory after the loader is applied
*/
export interface Store {
/**
* build-time error string
*/
error?: string;
/**
* global configuration for config file
*/
Expand Down
5 changes: 5 additions & 0 deletions core/loader/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import {
} from '@component-controls/instrument';

export interface LoadingStore {
/**
* build-time error
*/
error?: string;

/**
* global configuration from project config file
*/
Expand Down
8 changes: 4 additions & 4 deletions core/store/src/create-pages/pages-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export const getHomePages = (store: Store): DocHomePagesPath[] => {
const { pages = {} } = store?.config || {};
if (pages) {
const docs = Object.keys(store.docs);
const paths: DocHomePagesPath[] = Object.keys(pages).map(
(type: DocType) => {
const paths: DocHomePagesPath[] = Object.keys(pages)
.map((type: DocType) => {
const page = pages[type];
const path = getDocTypePath(page) as string;

Expand All @@ -66,8 +66,8 @@ export const getHomePages = (store: Store): DocHomePagesPath[] => {
docId,
storyId,
};
},
);
})
.filter(({ path }) => path);
return paths;
}
return [];
Expand Down
2 changes: 2 additions & 0 deletions core/store/src/serialization/load-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const loadStore = (store: LoadingStore): Store => {
const globalStore: Store = { ...defaultStore };
try {
const {
error,
stores,
packages: loadedPackages,
components: loadedComponents,
Expand Down Expand Up @@ -77,6 +78,7 @@ export const loadStore = (store: LoadingStore): Store => {
});
}
});
globalStore.error = error;
globalStore.packages = loadedPackages;
globalStore.components = loadedComponents;
const { storySort } = globalStore.config || {};
Expand Down
31 changes: 24 additions & 7 deletions core/webpack-compile/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import webpack, {
HotModuleReplacementPlugin,
} from 'webpack';
import path from 'path';
import fs from 'fs';
const chalk = require('chalk');
import LoaderPlugin from '@component-controls/loader/plugin';
import {
Expand Down Expand Up @@ -96,24 +97,40 @@ export const runCompiler = (

props: CompileRunProps,
): Promise<CompileResults> => {
return new Promise((resolve, reject) => {
return new Promise(resolve => {
const compiler = webpack(createConfig(props));
run(compiler, (err, stats) => {
const bundleName = path.join(
stats.compilation.outputOptions.path,
stats.compilation.outputOptions.filename,
);
const bailError = (error: string) => {
fs.writeFileSync(
bundleName,
`
module.exports = ${JSON.stringify({
stores: [],
packages: [],
components: [],
error,
})}
`,
);
console.error(error);
return resolve({ bundleName, stats }); //reject(error);
};
if (err) {
return reject(err);
return bailError(err.toString());
}
if (stats.hasErrors() || stats.hasWarnings()) {
const error = stats.toString({
errorDetails: true,
warnings: true,
});
return reject(error);
return bailError(error);
}
const { store } = require('@component-controls/loader/store');
const bundleName = path.join(
stats.compilation.outputOptions.path,
stats.compilation.outputOptions.filename,
);
console.log(
chalk.bgRgb(
244,
Expand Down
1 change: 1 addition & 0 deletions integrations/gatsby-theme-stories/src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ exports.createPages = async (
: await compile(config);
if (bundleName) {
const store: Store = loadStore(require(bundleName));

//home page
const { docId = null, type = null } = getIndexPage(store) || {};
createPage({
Expand Down
4 changes: 2 additions & 2 deletions ui/app/src/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SEO } from '../SEO';
import { Header } from '../Header';
import { Footer } from '../Footer';
import { useAnalytics } from './useAnalytics';

import { AppError } from '../AppError';
export interface AppProps {
/**
* page title
Expand Down Expand Up @@ -47,13 +47,13 @@ export const App: FC<AppProps> = ({ title = '', children }) => {
const pageTitle = titleParts[titleParts.length - 1];
const pageDescription = useDocDescription(doc);
useAnalytics();

return (
<Fragment>
<SEO title={pageTitle} description={pageDescription} />
<SkipLinks items={items} />
<Box variant="app">
<Header toolbar={toolbar} />
<AppError error={store.error} />
{children}
<Footer />
</Box>
Expand Down
23 changes: 23 additions & 0 deletions ui/app/src/AppError/AppError.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** @jsx jsx */
import { jsx } from 'theme-ui';
import { ControlTypes } from '@component-controls/core';
import { AppError, AppErrorProps } from './AppError';

export default {
title: 'Application/AppError',
component: AppError,
};

export const overview = ({ error }: AppErrorProps) => (
<AppError error={error} />
);

overview.controls = {
error: {
type: ControlTypes.TEXT,
rows: 4,
value: `some error
text
`,
},
};
23 changes: 23 additions & 0 deletions ui/app/src/AppError/AppError.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** @jsx jsx */
import { FC } from 'react';
import { jsx, Box } from 'theme-ui';

export interface AppErrorProps {
/**
* Error text, "apperror" theme key.
*/
error?: string;
}
/**
* application build-time error container
*/
export const AppError: FC<AppErrorProps> = ({ error }) => {
return error ? (
<Box
variant="apperror"
dangerouslySetInnerHTML={{
__html: error.split('\n').join('<br />'),
}}
/>
) : null;
};
1 change: 1 addition & 0 deletions ui/app/src/AppError/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './AppError';
7 changes: 7 additions & 0 deletions ui/components/src/ThemeContext/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type ControlsTheme = {
documentitem: Record<string, ThemeUIStyleObject>;
taglist: Record<string, ThemeUIStyleObject>;
appsidebarpage: Record<string, ThemeUIStyleObject>;
apperror: ThemeUIStyleObject;
container: Record<string, ThemeUIStyleObject>;
documentslist: Record<string, ThemeUIStyleObject>;
} & Theme;
Expand Down Expand Up @@ -1002,6 +1003,12 @@ export const theme: ControlsTheme = {
position: 'relative',
},
},
apperror: {
bg: 'black',
color: 'red',
p: 4,
fontFamily: 'monospace',
},
container: {
container: {},
pagination: { py: 4 },
Expand Down

0 comments on commit 912074f

Please sign in to comment.