printHostingInstructions.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. 'use strict';
  8. const chalk = require('chalk');
  9. const url = require('url');
  10. const globalModules = require('global-modules');
  11. const fs = require('fs');
  12. function printHostingInstructions(
  13. appPackage,
  14. publicUrl,
  15. publicPath,
  16. buildFolder,
  17. useYarn
  18. ) {
  19. if (publicUrl && publicUrl.includes('.github.io/')) {
  20. // "homepage": "http://user.github.io/project"
  21. const publicPathname = url.parse(publicPath).pathname;
  22. const hasDeployScript =
  23. typeof appPackage.scripts !== 'undefined' &&
  24. typeof appPackage.scripts.deploy !== 'undefined';
  25. printBaseMessage(buildFolder, publicPathname);
  26. printDeployInstructions(publicUrl, hasDeployScript, useYarn);
  27. } else if (publicPath !== '/') {
  28. // "homepage": "http://mywebsite.com/project"
  29. printBaseMessage(buildFolder, publicPath);
  30. } else {
  31. // "homepage": "http://mywebsite.com"
  32. // or no homepage
  33. printBaseMessage(buildFolder, publicUrl);
  34. printStaticServerInstructions(buildFolder, useYarn);
  35. }
  36. console.log();
  37. console.log('Find out more about deployment here:');
  38. console.log();
  39. console.log(` ${chalk.yellow('https://cra.link/deployment')}`);
  40. console.log();
  41. }
  42. function printBaseMessage(buildFolder, hostingLocation) {
  43. console.log(
  44. `The project was built assuming it is hosted at ${chalk.green(
  45. hostingLocation || 'the server root'
  46. )}.`
  47. );
  48. console.log(
  49. `You can control this with the ${chalk.green(
  50. 'homepage'
  51. )} field in your ${chalk.cyan('package.json')}.`
  52. );
  53. if (!hostingLocation) {
  54. console.log('For example, add this to build it for GitHub Pages:');
  55. console.log();
  56. console.log(
  57. ` ${chalk.green('"homepage"')} ${chalk.cyan(':')} ${chalk.green(
  58. '"http://myname.github.io/myapp"'
  59. )}${chalk.cyan(',')}`
  60. );
  61. }
  62. console.log();
  63. console.log(`The ${chalk.cyan(buildFolder)} folder is ready to be deployed.`);
  64. }
  65. function printDeployInstructions(publicUrl, hasDeployScript, useYarn) {
  66. console.log(`To publish it at ${chalk.green(publicUrl)} , run:`);
  67. console.log();
  68. // If script deploy has been added to package.json, skip the instructions
  69. if (!hasDeployScript) {
  70. if (useYarn) {
  71. console.log(` ${chalk.cyan('yarn')} add --dev gh-pages`);
  72. } else {
  73. console.log(` ${chalk.cyan('npm')} install --save-dev gh-pages`);
  74. }
  75. console.log();
  76. console.log(
  77. `Add the following script in your ${chalk.cyan('package.json')}.`
  78. );
  79. console.log();
  80. console.log(` ${chalk.dim('// ...')}`);
  81. console.log(` ${chalk.yellow('"scripts"')}: {`);
  82. console.log(` ${chalk.dim('// ...')}`);
  83. console.log(
  84. ` ${chalk.yellow('"predeploy"')}: ${chalk.yellow(
  85. `"${useYarn ? 'yarn' : 'npm run'} build",`
  86. )}`
  87. );
  88. console.log(
  89. ` ${chalk.yellow('"deploy"')}: ${chalk.yellow(
  90. '"gh-pages -d build"'
  91. )}`
  92. );
  93. console.log(' }');
  94. console.log();
  95. console.log('Then run:');
  96. console.log();
  97. }
  98. console.log(` ${chalk.cyan(useYarn ? 'yarn' : 'npm')} run deploy`);
  99. }
  100. function printStaticServerInstructions(buildFolder, useYarn) {
  101. console.log('You may serve it with a static server:');
  102. console.log();
  103. if (!fs.existsSync(`${globalModules}/serve`)) {
  104. if (useYarn) {
  105. console.log(` ${chalk.cyan('yarn')} global add serve`);
  106. } else {
  107. console.log(` ${chalk.cyan('npm')} install -g serve`);
  108. }
  109. }
  110. console.log(` ${chalk.cyan('serve')} -s ${buildFolder}`);
  111. }
  112. module.exports = printHostingInstructions;