Skip to content

Commit

Permalink
Pulled out app paths to seperate config and added a check for require…
Browse files Browse the repository at this point in the history
…d files in start script
  • Loading branch information
christophior committed Jul 23, 2016
1 parent e395498 commit ff1951b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 15 deletions.
32 changes: 32 additions & 0 deletions config/appPaths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

var path = require('path');

module.exports = function(relativePath) {

// TODO: hide this behind a flag and eliminate dead code on eject.
// This shouldn't be exposed to the user.
var isInNodeModules = 'node_modules' ===
path.basename(path.resolve(path.join(__dirname, '..', '..')));

var srcPath = path.resolve(__dirname, relativePath, 'src');
var nodeModulesPath = path.join(__dirname, '..', 'node_modules');
var indexHtmlPath = path.resolve(__dirname, relativePath, 'index.html');
var faviconPath = path.resolve(__dirname, relativePath, 'favicon.ico');
var buildPath = path.join(__dirname, isInNodeModules ? '../../..' : '..', 'build');

return {
srcPath: srcPath,
nodeModulesPath: nodeModulesPath,
indexHtmlPath: indexHtmlPath,
faviconPath: faviconPath,
buildPath: buildPath
};
};
14 changes: 7 additions & 7 deletions config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ var autoprefixer = require('autoprefixer');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');

// TODO: hide this behind a flag and eliminate dead code on eject.
// This shouldn't be exposed to the user.
var isInNodeModules = 'node_modules' ===
path.basename(path.resolve(path.join(__dirname, '..', '..')));
var relativePath = isInNodeModules ? '../../..' : '..';
Expand All @@ -23,11 +21,13 @@ var isInDebugMode = process.argv.some(arg =>
if (isInDebugMode) {
relativePath = '../template';
}
var srcPath = path.resolve(__dirname, relativePath, 'src');
var nodeModulesPath = path.join(__dirname, '..', 'node_modules');
var indexHtmlPath = path.resolve(__dirname, relativePath, 'index.html');
var faviconPath = path.resolve(__dirname, relativePath, 'favicon.ico');
var buildPath = path.join(__dirname, isInNodeModules ? '../../..' : '..', 'build');

var appPaths = require(path.join(__dirname, 'appPaths.js'))(relativePath);
var srcPath = appPaths.srcPath;
var nodeModulesPath = appPaths.nodeModulesPath;
var indexHtmlPath = appPaths.indexHtmlPath;
var faviconPath = appPaths.faviconPath;
var buildPath = appPaths.buildPath;

module.exports = {
devtool: 'eval',
Expand Down
16 changes: 8 additions & 8 deletions config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

// TODO: hide this behind a flag and eliminate dead code on eject.
// This shouldn't be exposed to the user.
var isInNodeModules = 'node_modules' ===
path.basename(path.resolve(path.join(__dirname, '..', '..')));
path.basename(path.resolve(path.join(__dirname, '..', '..')));
var relativePath = isInNodeModules ? '../../..' : '..';
if (process.argv[2] === '--debug-template') {
relativePath = '../template';
}
var srcPath = path.resolve(__dirname, relativePath, 'src');
var nodeModulesPath = path.join(__dirname, '..', 'node_modules');
var indexHtmlPath = path.resolve(__dirname, relativePath, 'index.html');
var faviconPath = path.resolve(__dirname, relativePath, 'favicon.ico');
var buildPath = path.join(__dirname, isInNodeModules ? '../../..' : '..', 'build');

var appPaths = require(path.join(__dirname, 'appPaths.js'))(relativePath);
var srcPath = appPaths.srcPath;
var nodeModulesPath = appPaths.nodeModulesPath;
var indexHtmlPath = appPaths.indexHtmlPath;
var faviconPath = appPaths.faviconPath;
var buildPath = appPaths.buildPath;

module.exports = {
bail: true,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"eslint-plugin-import": "1.10.3",
"eslint-plugin-react": "5.2.2",
"extract-text-webpack-plugin": "1.0.1",
"file-exists": "^1.0.0",
"file-loader": "0.9.0",
"fs-extra": "^0.30.0",
"html-webpack-plugin": "2.22.0",
Expand Down
15 changes: 15 additions & 0 deletions scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var WebpackDevServer = require('webpack-dev-server');
var config = require('../config/webpack.config.dev');
var execSync = require('child_process').execSync;
var opn = require('opn');
var fileExists = require('file-exists');

// TODO: hide this behind a flag and eliminate dead code on eject.
// This shouldn't be exposed to the user.
Expand Down Expand Up @@ -121,6 +122,19 @@ compiler.plugin('done', function (stats) {
}
});

function checkRequiredFiles() {
var indexHtmlPath = path.join(__dirname, '../index.html');
var faviconPath = path.join(__dirname, '../favicon.ico');
var indexJsPath = path.join(__dirname, '../src/index.js');

[indexHtmlPath, faviconPath, indexJsPath]
.forEach(function(requiredFile) {
if (!fileExists(requiredFile)) {
console.log(chalk.red('ERROR: ', requiredFile, ' is missing!'));
}
});
}

function openBrowser() {
if (process.platform === 'darwin') {
try {
Expand Down Expand Up @@ -154,6 +168,7 @@ new WebpackDevServer(compiler, {

clearConsole();
console.log(chalk.cyan('Starting the development server...'));
checkRequiredFiles();
console.log();
openBrowser();
});

0 comments on commit ff1951b

Please sign in to comment.