|
| 1 | +// @remove-on-eject-begin |
| 2 | +/** |
| 3 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * This source code is licensed under the BSD-style license found in the |
| 7 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 8 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 9 | + */ |
| 10 | +// @remove-on-eject-end |
| 11 | + |
| 12 | +// Do this as the first thing so that any code reading it knows the right env. |
| 13 | +process.env.NODE_ENV = 'staging'; |
| 14 | + |
| 15 | +// Load environment variables from .env file. Suppress warnings using silent |
| 16 | +// if this file is missing. dotenv will never modify any environment variables |
| 17 | +// that have already been set. |
| 18 | +// https://github.com/motdotla/dotenv |
| 19 | +require('dotenv').config({silent: true}); |
| 20 | + |
| 21 | +var chalk = require('chalk'); |
| 22 | +var fs = require('fs-extra'); |
| 23 | +var rimrafSync = require('rimraf').sync; |
| 24 | +var webpack = require('webpack'); |
| 25 | +var config = require('../config/webpack.config.staging'); |
| 26 | +var paths = require('../config/paths'); |
| 27 | +var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); |
| 28 | +var recursive = require('recursive-readdir'); |
| 29 | + |
| 30 | +// Warn and crash if required files are missing |
| 31 | +if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) { |
| 32 | + process.exit(1); |
| 33 | +} |
| 34 | + |
| 35 | +recursive(paths.appStaging, (err, fileNames) => { |
| 36 | + // Remove all content but keep the directory so that |
| 37 | + // if you're in it, you don't end up in Trash |
| 38 | + rimrafSync(paths.appStaging + '/*'); |
| 39 | + |
| 40 | + // Start the webpack build |
| 41 | + build(); |
| 42 | + |
| 43 | + // Merge with the public folder |
| 44 | + copyPublicFolder(); |
| 45 | +}); |
| 46 | + |
| 47 | + |
| 48 | +// Create the staging build and print the deployment instructions. |
| 49 | +function build() { |
| 50 | + console.log('Creating a staging build...'); |
| 51 | + webpack(config).run((err, stats) => { |
| 52 | + if (err) { |
| 53 | + console.error('Failed to create a staging build. Reason:'); |
| 54 | + console.error(err.message || err); |
| 55 | + process.exit(1); |
| 56 | + } |
| 57 | + |
| 58 | + console.log(chalk.green('Compiled successfully.')); |
| 59 | + console.log(); |
| 60 | + |
| 61 | + var openCommand = process.platform === 'win32' ? 'start' : 'open'; |
| 62 | + var homepagePath = require(paths.appPackageJson).homepage; |
| 63 | + var publicPath = config.output.publicPath; |
| 64 | + if (publicPath !== '/') { |
| 65 | + // "homepage": "http://mywebsite.com/project" |
| 66 | + console.log('The project was built assuming it is hosted at ' + chalk.green(publicPath) + '.'); |
| 67 | + console.log('You can control this with the ' + chalk.green('homepage') + ' field in your ' + chalk.cyan('package.json') + '.'); |
| 68 | + console.log(); |
| 69 | + console.log('The ' + chalk.cyan('staging') + ' folder is ready to be deployed.'); |
| 70 | + console.log(); |
| 71 | + } else { |
| 72 | + // no homepage or "homepage": "http://mywebsite.com" |
| 73 | + console.log('The project was built assuming it is hosted at the server root.'); |
| 74 | + if (homepagePath) { |
| 75 | + // "homepage": "http://mywebsite.com" |
| 76 | + console.log('You can control this with the ' + chalk.green('homepage') + ' field in your ' + chalk.cyan('package.json') + '.'); |
| 77 | + console.log(); |
| 78 | + } |
| 79 | + console.log('The ' + chalk.cyan('staging') + ' folder is ready to be deployed.'); |
| 80 | + console.log('You may also serve it locally with a static server:') |
| 81 | + console.log(); |
| 82 | + console.log(' ' + chalk.cyan('npm') + ' install -g pushstate-server'); |
| 83 | + console.log(' ' + chalk.cyan('pushstate-server') + ' staging'); |
| 84 | + console.log(' ' + chalk.cyan(openCommand) + ' http://localhost:9000'); |
| 85 | + console.log(); |
| 86 | + } |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +function copyPublicFolder() { |
| 91 | + fs.copySync(paths.appPublic, paths.appStaging, { |
| 92 | + dereference: true, |
| 93 | + filter: file => file !== paths.appHtml |
| 94 | + }); |
| 95 | +} |
0 commit comments