From 451c9356249e16fe32a54a94ce4806aecfbd037c Mon Sep 17 00:00:00 2001 From: Vincent TAING Date: Thu, 15 Sep 2016 17:40:44 +0200 Subject: [PATCH 1/6] adds file paths to config/paths.js, try to retrieve required files when starting --- config/paths.js | 6 ++++++ scripts/start.js | 20 ++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/config/paths.js b/config/paths.js index bb1ba76125..17779bb769 100644 --- a/config/paths.js +++ b/config/paths.js @@ -38,7 +38,9 @@ var nodePaths = (process.env.NODE_PATH || '') // config after eject: we're in ./config/ module.exports = { appBuild: resolveApp('build'), + appFavico: resolveOwn('src/favicon.ico'), appHtml: resolveApp('index.html'), + appIndexJs: resolveApp('src/index.js'), appPackageJson: resolveApp('package.json'), appSrc: resolveApp('src'), testsSetup: resolveApp('src/setupTests.js'), @@ -55,7 +57,9 @@ function resolveOwn(relativePath) { // config before eject: we're in ./node_modules/react-scripts/config/ module.exports = { appBuild: resolveApp('build'), + appFavico: resolveApp('src/favicon.ico'), appHtml: resolveApp('index.html'), + appIndexJs: resolveApp('src/index.js'), appPackageJson: resolveApp('package.json'), appSrc: resolveApp('src'), testsSetup: resolveApp('src/setupTests.js'), @@ -69,7 +73,9 @@ module.exports = { // @remove-on-publish-begin module.exports = { appBuild: resolveOwn('../build'), + appFavico: resolveOwn('../template/src/favicon.ico'), appHtml: resolveOwn('../template/index.html'), + appIndexJs: resolveOwn('../template/src/index.js'), appPackageJson: resolveOwn('../package.json'), appSrc: resolveOwn('../template/src'), testsSetup: resolveOwn('../template/src/setupTests.js'), diff --git a/scripts/start.js b/scripts/start.js index be1d6f80e0..15761bb102 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -11,6 +11,7 @@ process.env.NODE_ENV = 'development'; +var fs = require('fs'); var path = require('path'); var chalk = require('chalk'); var webpack = require('webpack'); @@ -170,6 +171,20 @@ function openBrowser(port, protocol) { opn(protocol + '://localhost:' + port + '/'); } +function checkRequiredFiles() { + var filesPathToCheck = [paths.appHtml, paths.appIndexJs, paths.appFavico]; + filesPathToCheck.forEach(function(filePath) { + fs.access(filePath, fs.constants.F_OK, function(err) { + if(err) { + var fileName = path.basename(filePath); + console.log( + chalk.red(`Cannot find ${fileName} in ${filePath} directory`) + ); + process.exit(1); + } + }); + }); +} // We need to provide a custom onError function for httpProxyMiddleware. // It allows us to log custom error messages on the console. function onProxyError(proxy) { @@ -180,7 +195,7 @@ function onProxyError(proxy) { ' from ' + chalk.cyan(host) + ' to ' + chalk.cyan(proxy) + '.' ); console.log( - 'See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (' + + 'See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (' + chalk.cyan(err.code) + ').' ); console.log(); @@ -190,7 +205,7 @@ function onProxyError(proxy) { if (res.writeHead && !res.headersSent) { res.writeHead(500); } - res.end('Proxy error: Could not proxy request ' + req.url + ' from ' + + res.end('Proxy error: Could not proxy request ' + req.url + ' from ' + host + ' to ' + proxy + ' (' + err.code + ').' ); } @@ -304,6 +319,7 @@ function runDevServer(port, protocol) { function run(port) { var protocol = process.env.HTTPS === 'true' ? "https" : "http"; + checkRequiredFiles(); setupCompiler(port, protocol); runDevServer(port, protocol); } From 69b0794f6ea8809da3851d860715e5b580633575 Mon Sep 17 00:00:00 2001 From: Vincent TAING Date: Thu, 15 Sep 2016 18:33:00 +0200 Subject: [PATCH 2/6] change resolveOwn > resolveApp --- config/paths.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/paths.js b/config/paths.js index 17779bb769..187e7cc762 100644 --- a/config/paths.js +++ b/config/paths.js @@ -38,7 +38,7 @@ var nodePaths = (process.env.NODE_PATH || '') // config after eject: we're in ./config/ module.exports = { appBuild: resolveApp('build'), - appFavico: resolveOwn('src/favicon.ico'), + appFavico: resolveApp('src/favicon.ico'), appHtml: resolveApp('index.html'), appIndexJs: resolveApp('src/index.js'), appPackageJson: resolveApp('package.json'), From ee865ae0c2373ea3bca547803846c41a588920b0 Mon Sep 17 00:00:00 2001 From: Vincent TAING Date: Sat, 17 Sep 2016 23:24:31 +0200 Subject: [PATCH 3/6] tries to access synchronously the required files --- scripts/start.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/scripts/start.js b/scripts/start.js index 15761bb102..02149d1bb2 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -174,15 +174,15 @@ function openBrowser(port, protocol) { function checkRequiredFiles() { var filesPathToCheck = [paths.appHtml, paths.appIndexJs, paths.appFavico]; filesPathToCheck.forEach(function(filePath) { - fs.access(filePath, fs.constants.F_OK, function(err) { - if(err) { - var fileName = path.basename(filePath); - console.log( - chalk.red(`Cannot find ${fileName} in ${filePath} directory`) - ); - process.exit(1); - } - }); + try { + fs.accessSync(filePath, fs.F_OK); + } catch (err) { + var fileName = path.basename(filePath); + console.log( + chalk.red(`Cannot find ${fileName} in ${filePath} directory`) + ); + process.exit(1); + } }); } // We need to provide a custom onError function for httpProxyMiddleware. From 264096cbfd912dbae28cd6b9fc007efe08a311ed Mon Sep 17 00:00:00 2001 From: Vincent TAING Date: Sat, 17 Sep 2016 23:30:23 +0200 Subject: [PATCH 4/6] uses appIndexJs variable for specifying entry path in webpack configs --- config/webpack.config.dev.js | 2 +- config/webpack.config.prod.js | 2 +- scripts/start.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/webpack.config.dev.js b/config/webpack.config.dev.js index 86f592ba5b..a88409f2d0 100644 --- a/config/webpack.config.dev.js +++ b/config/webpack.config.dev.js @@ -49,7 +49,7 @@ module.exports = { // We ship a few polyfills by default. require.resolve('./polyfills'), // Finally, this is your app's code: - path.join(paths.appSrc, 'index') + paths.appIndexJs // We include the app code last so that if there is a runtime error during // initialization, it doesn't blow up the WebpackDevServer client, and // changing JS code would still trigger a refresh. diff --git a/config/webpack.config.prod.js b/config/webpack.config.prod.js index 2a482f9547..552ded4855 100644 --- a/config/webpack.config.prod.js +++ b/config/webpack.config.prod.js @@ -48,7 +48,7 @@ module.exports = { // In production, we only want to load the polyfills and the app code. entry: [ require.resolve('./polyfills'), - path.join(paths.appSrc, 'index') + paths.appIndexJs ], output: { // The build folder. diff --git a/scripts/start.js b/scripts/start.js index 02149d1bb2..185299052b 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -172,7 +172,7 @@ function openBrowser(port, protocol) { } function checkRequiredFiles() { - var filesPathToCheck = [paths.appHtml, paths.appIndexJs, paths.appFavico]; + var filesPathToCheck = [paths.appHtml, paths.appIndexJs]; filesPathToCheck.forEach(function(filePath) { try { fs.accessSync(filePath, fs.F_OK); From 8a1ca4edfd617e6779a29cac170da521064703e7 Mon Sep 17 00:00:00 2001 From: Vincent TAING Date: Sat, 17 Sep 2016 23:36:15 +0200 Subject: [PATCH 5/6] removes favico from the paths --- config/paths.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/config/paths.js b/config/paths.js index 187e7cc762..c3594e1451 100644 --- a/config/paths.js +++ b/config/paths.js @@ -38,7 +38,6 @@ var nodePaths = (process.env.NODE_PATH || '') // config after eject: we're in ./config/ module.exports = { appBuild: resolveApp('build'), - appFavico: resolveApp('src/favicon.ico'), appHtml: resolveApp('index.html'), appIndexJs: resolveApp('src/index.js'), appPackageJson: resolveApp('package.json'), @@ -57,7 +56,6 @@ function resolveOwn(relativePath) { // config before eject: we're in ./node_modules/react-scripts/config/ module.exports = { appBuild: resolveApp('build'), - appFavico: resolveApp('src/favicon.ico'), appHtml: resolveApp('index.html'), appIndexJs: resolveApp('src/index.js'), appPackageJson: resolveApp('package.json'), @@ -73,7 +71,6 @@ module.exports = { // @remove-on-publish-begin module.exports = { appBuild: resolveOwn('../build'), - appFavico: resolveOwn('../template/src/favicon.ico'), appHtml: resolveOwn('../template/index.html'), appIndexJs: resolveOwn('../template/src/index.js'), appPackageJson: resolveOwn('../package.json'), From d8d8797d7a697f1c162e9e0d9f4d6d9bb12ebded Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Sun, 18 Sep 2016 01:26:59 +0300 Subject: [PATCH 6/6] Add missing file check to npm run build too --- scripts/build.js | 3 +++ scripts/eject.js | 1 + scripts/start.js | 16 +------------- scripts/utils/checkRequiredFiles.js | 33 +++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 scripts/utils/checkRequiredFiles.js diff --git a/scripts/build.js b/scripts/build.js index e60b554466..71dcc798bb 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -21,9 +21,12 @@ var rimrafSync = require('rimraf').sync; var webpack = require('webpack'); var config = require('../config/webpack.config.prod'); var paths = require('../config/paths'); +var checkRequiredFiles = require('./utils/checkRequiredFiles'); var recursive = require('recursive-readdir'); var stripAnsi = require('strip-ansi'); +checkRequiredFiles(); + // Input: /User/dan/app/build/static/js/main.82be8.js // Output: /static/js/main.js function removeFileNameHash(fileName) { diff --git a/scripts/eject.js b/scripts/eject.js index 3e5e18af38..74c5c9ef99 100644 --- a/scripts/eject.js +++ b/scripts/eject.js @@ -44,6 +44,7 @@ prompt( path.join('config', 'jest', 'transform.js'), path.join('scripts', 'build.js'), path.join('scripts', 'start.js'), + path.join('scripts', 'utils', 'checkRequiredFiles.js'), path.join('scripts', 'utils', 'chrome.applescript'), path.join('scripts', 'utils', 'prompt.js'), path.join('scripts', 'utils', 'WatchMissingNodeModulesPlugin.js') diff --git a/scripts/start.js b/scripts/start.js index 185299052b..84d1e7c4b4 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -11,7 +11,6 @@ process.env.NODE_ENV = 'development'; -var fs = require('fs'); var path = require('path'); var chalk = require('chalk'); var webpack = require('webpack'); @@ -21,6 +20,7 @@ var httpProxyMiddleware = require('http-proxy-middleware'); var execSync = require('child_process').execSync; var opn = require('opn'); var detect = require('detect-port'); +var checkRequiredFiles = require('./utils/checkRequiredFiles'); var prompt = require('./utils/prompt'); var config = require('../config/webpack.config.dev'); var paths = require('../config/paths'); @@ -171,20 +171,6 @@ function openBrowser(port, protocol) { opn(protocol + '://localhost:' + port + '/'); } -function checkRequiredFiles() { - var filesPathToCheck = [paths.appHtml, paths.appIndexJs]; - filesPathToCheck.forEach(function(filePath) { - try { - fs.accessSync(filePath, fs.F_OK); - } catch (err) { - var fileName = path.basename(filePath); - console.log( - chalk.red(`Cannot find ${fileName} in ${filePath} directory`) - ); - process.exit(1); - } - }); -} // We need to provide a custom onError function for httpProxyMiddleware. // It allows us to log custom error messages on the console. function onProxyError(proxy) { diff --git a/scripts/utils/checkRequiredFiles.js b/scripts/utils/checkRequiredFiles.js new file mode 100644 index 0000000000..8110eb01cd --- /dev/null +++ b/scripts/utils/checkRequiredFiles.js @@ -0,0 +1,33 @@ +// @remove-on-eject-begin +/** + * 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. + */ +// @remove-on-eject-end + +const fs = require('fs'); +const path = require('path'); +const chalk = require('chalk'); +const paths = require('../../config/paths'); + +function checkRequiredFiles() { + const filesPathToCheck = [paths.appHtml, paths.appIndexJs]; + filesPathToCheck.forEach(filePath => { + try { + fs.accessSync(filePath, fs.F_OK); + } catch (err) { + const dirName = path.dirname(filePath); + const fileName = path.basename(filePath); + console.log(chalk.red('Could not find a required file.')); + console.log(chalk.red(' Name: ') + chalk.cyan(fileName)); + console.log(chalk.red(' Searched in: ') + chalk.cyan(dirName)); + process.exit(1); + } + }); +} + +module.exports = checkRequiredFiles;