Skip to content

Commit

Permalink
feat: show proper error message on init
Browse files Browse the repository at this point in the history
Also check for common stuff under project and server config files.

See #28
  • Loading branch information
swashata committed Oct 30, 2018
1 parent b1d3c07 commit ea4c3a9
Show file tree
Hide file tree
Showing 17 changed files with 225 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ const pathToDummyWrong = path.resolve(
__dirname,
'../helpers/dummyConfigWrong.js'
);
const pathToDummyPCRight = path.resolve(
__dirname,
'../helpers/dummyPCRight.js'
);
const pathToDummyPCWrong = path.resolve(
__dirname,
'../helpers/dummyPCWrong.js'
);
const pathToDummySCRight = path.resolve(
__dirname,
'../helpers/dummySCRight.js'
);
const pathToDummySCWrong = path.resolve(
__dirname,
'../helpers/dummySCWrong.js'
);
const validCwd = path.resolve(__dirname, '../helpers');

describe('getProjectAndServerConfig', () => {
Expand All @@ -29,8 +45,8 @@ describe('getProjectAndServerConfig', () => {
test('works properly if module.exports is an object', () => {
expect(() => {
getProjectAndServerConfig(__dirname, {
projectConfig: pathToDummyRight,
serverConfig: pathToDummyRight,
projectConfig: pathToDummyPCRight,
serverConfig: pathToDummySCRight,
});
}).not.toThrow();
});
Expand All @@ -39,23 +55,23 @@ describe('getProjectAndServerConfig', () => {
test('throws error if file not found', () => {
expect(() => {
getProjectAndServerConfig(__dirname, {
projectConfig: pathToDummyRight,
projectConfig: pathToDummyPCRight,
});
}).toThrowError('not find server configuration');
});
test('throws error if module.exports not an object', () => {
expect(() => {
getProjectAndServerConfig(__dirname, {
projectConfig: pathToDummyRight,
projectConfig: pathToDummyPCRight,
serverConfig: pathToDummyWrong,
});
}).toThrow('Server configuration must export an object literal.');
});
test('works properly if module.exports is an object', () => {
expect(() => {
getProjectAndServerConfig(__dirname, {
projectConfig: pathToDummyRight,
serverConfig: pathToDummyRight,
projectConfig: pathToDummyPCRight,
serverConfig: pathToDummySCRight,
});
}).not.toThrow();
});
Expand Down
9 changes: 9 additions & 0 deletions packages/scripts/__tests__/helpers/dummyConfigRight.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
module.exports = {
appName: 'helloWorld',
files: [
{
entry: {
foo: './src/index.js',
},
name: 'app',
},
],
foo: 'bar',
};
11 changes: 11 additions & 0 deletions packages/scripts/__tests__/helpers/dummyPCRight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
appName: 'helloWorld',
files: [
{
entry: {
foo: './src/index.js',
},
name: 'app',
},
],
};
4 changes: 4 additions & 0 deletions packages/scripts/__tests__/helpers/dummyPCWrong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
// No appName
files: [],
};
4 changes: 4 additions & 0 deletions packages/scripts/__tests__/helpers/dummySCRight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
host: undefined,
proxy: 'http://localhost',
};
3 changes: 3 additions & 0 deletions packages/scripts/__tests__/helpers/dummySCWrong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
host: undefined,
};
1 change: 1 addition & 0 deletions packages/scripts/__tests__/helpers/wpackio.project.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
appName: 'helloWorld',
files: [
{
entry: {
Expand Down
1 change: 1 addition & 0 deletions packages/scripts/__tests__/helpers/wpackio.server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
host: undefined,
proxy: 'http://localhost',
};
3 changes: 2 additions & 1 deletion packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"access": "public"
},
"devDependencies": {
"dts-gen": "0.5.7"
"dts-gen": "0.5.7",
"fork-ts-checker-webpack-plugin": "^0.4.14"
}
}
5 changes: 3 additions & 2 deletions packages/scripts/src/bin/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Build } from '../scripts/Build';
import { ProgramOptions } from './index';
import {
endBuildInfo,
prettyPrintError,
resolveCWD,
watchEllipsis,
wpackLogoSmall,
Expand Down Expand Up @@ -77,8 +78,8 @@ export function build(options: ProgramOptions | undefined): void {
process.exit(1);
});
} catch (e) {
spinner.fail(`${wpackLogoSmall} could not start webpack compiler.`);
console.error(pe.render(e));
spinner.stop();
prettyPrintError(e, 'could not start webpack compiler.');
process.exit(1);
}
}
4 changes: 2 additions & 2 deletions packages/scripts/src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ program
program
.command('build')
.description('Build production files.')
.option('-c, --context', contextHelp)
.option('-c, --context [path]', contextHelp)
.option(
'-p, --project-config [path]',
'Path to project config. If it differs from ./wpackio.project.js'
Expand All @@ -101,7 +101,7 @@ program
program
.command('bootstrap')
.description('create project and/or server configuration files.')
.option('-c, --context', contextHelp)
.option('-c, --context [path]', contextHelp)
.action((options: ProgramOptions | undefined) => {
isValidCommand = true;
bootstrap(options, pkg.version);
Expand Down
8 changes: 4 additions & 4 deletions packages/scripts/src/bin/serve.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import chalk from 'chalk';
import figures from 'figures';
import logSymbols from 'log-symbols';
import ora from 'ora';
import path from 'path';
import PrettyError from 'pretty-error';
import clearConsole from 'react-dev-utils/clearConsole';
import { getProjectAndServerConfig } from '../config/getProjectAndServerConfig';
import { WpackioError } from '../errors/WpackioError';
import { Server } from '../scripts/Server';
import { ProgramOptions } from './index';
import {
endServeInfo,
prettyPrintError,
resolveCWD,
serverInfo,
watchEllipsis,
Expand All @@ -29,8 +32,6 @@ export function serve(options: ProgramOptions | undefined): void {
// Get project and server config JSONs.
const cwd = resolveCWD(options);
const relCwd = path.relative(process.cwd(), cwd);
// For pretty logging
const pe = new PrettyError();

// For spinner
const spinner = ora({
Expand Down Expand Up @@ -162,8 +163,7 @@ export function serve(options: ProgramOptions | undefined): void {
}
} catch (e) {
spinner.stop();
console.log(`${logSymbols.error} could not start server.`);
console.error(pe.render(e));
prettyPrintError(e, 'could not start server.');
process.exit(1);
}
}
27 changes: 27 additions & 0 deletions packages/scripts/src/bin/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import figlet from 'figlet';
import figures from 'figures';
import findUp from 'find-up';
import gradient from 'gradient-string';
import logSymbols from 'log-symbols';
import path from 'path';
import PrettyError from 'pretty-error';
import { WpackioError } from '../errors/WpackioError';

let isYarnCache: boolean | null = null;

Expand Down Expand Up @@ -207,3 +210,27 @@ To spread the ${chalk.red(figures.heart)} please tweet.`;
})
);
}

export function prettyPrintError(
e: Error | WpackioError,
errorMsg: string
): void {
const errorPrefix = ` ${chalk.dim.red(figures.pointer)} `;
console.log(chalk.dim('='.repeat(errorMsg.length + 2)));
console.log(`${logSymbols.error} ${errorMsg}`);
console.log(chalk.dim('='.repeat(errorMsg.length + 2)));
console.log('');
if (e instanceof WpackioError) {
console.log(chalk.bgRed.black(' please review the following errors '));
console.log('');
console.error(
errorPrefix +
e.message
.split('\n')
.reduce((acc, line) => `${acc}\n${errorPrefix}${line}`)
);
} else {
const pe = new PrettyError();
console.error(pe.render(e));
}
}
35 changes: 21 additions & 14 deletions packages/scripts/src/config/WebpackConfigHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import slugify from 'slugify';
import TimeFixPlugin from 'time-fix-plugin';
import webpack from 'webpack';
import WebpackAssetsManifest from 'webpack-assets-manifest';
import { WpackioError } from '../errors/WpackioError';
import {
BannerConfig,
FileConfig,
Expand Down Expand Up @@ -265,20 +266,26 @@ export class WebpackConfigHelper {
const tsconfigPath = path.resolve(this.cwd, './tsconfig.json');
if (this.fileExists(tsconfigPath)) {
// dynamic require forktschecker otherwise it will throw error
// tslint:disable-next-line:variable-name
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
plugins.push(
new ForkTsCheckerWebpackPlugin({
tsconfig: tsconfigPath,
tslint: undefined,
async: false,
silent: true,
formatter: 'codeframe',
formatterOptions: {
highlightCode: true,
},
})
);
try {
// tslint:disable-next-line:variable-name no-implicit-dependencies
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
plugins.push(
new ForkTsCheckerWebpackPlugin({
tsconfig: tsconfigPath,
tslint: undefined,
async: false,
silent: true,
formatter: 'codeframe',
formatterOptions: {
highlightCode: true,
},
})
);
} catch (e) {
throw new WpackioError(
'please install fork-ts-checker-webpack-plugin package'
);
}
}
// Add development specific plugins
if (this.isDev) {
Expand Down
Loading

0 comments on commit ea4c3a9

Please sign in to comment.