UNPKG

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