UNPKG

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