Skip to content

Commit 72b6eb8

Browse files
GreenGremlingaearon
authored andcommitted
Cleaning up printHostingInstructions a bit (#3036)
* Replacing literal 'build' with `buildFolder` variable * Cleaning up the printHostingInstructions a bit * Fixing undefined variable
1 parent c162920 commit 72b6eb8

File tree

1 file changed

+73
-86
lines changed

1 file changed

+73
-86
lines changed

packages/react-dev-utils/printHostingInstructions.js

+73-86
Original file line numberDiff line numberDiff line change
@@ -19,118 +19,105 @@ function printHostingInstructions(
1919
buildFolder,
2020
useYarn
2121
) {
22-
const publicPathname = url.parse(publicPath).pathname;
23-
if (publicUrl && publicUrl.indexOf('.github.io/') !== -1) {
22+
if (publicUrl && publicUrl.includes('.github.io/')) {
2423
// "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();
24+
const publicPathname = url.parse(publicPath).pathname;
25+
const hasDeployScript = typeof appPackage.scripts.deploy !== 'undefined';
26+
printBaseMessage(buildFolder, publicPathname);
27+
28+
printDeployInstructions(publicUrl, hasDeployScript, useYarn);
29+
7130
} else if (publicPath !== '/') {
7231
// "homepage": "http://mywebsite.com/project"
32+
printBaseMessage(buildFolder, publicPath);
33+
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+
}
43+
44+
function printBaseMessage(buildFolder, hostingLocation) {
7345
console.log(
7446
`The project was built assuming it is hosted at ${chalk.green(
75-
publicPath
47+
hostingLocation || 'the server root'
7648
)}.`
7749
);
7850
console.log(
7951
`You can control this with the ${chalk.green(
8052
'homepage'
8153
)} field in your ${chalk.cyan('package.json')}.`
8254
);
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-
);
55+
56+
if (!hostingLocation) {
11057
console.log('For example, add this to build it for GitHub Pages:');
11158
console.log();
59+
11260
console.log(
11361
` ${chalk.green('"homepage"')} ${chalk.cyan(':')} ${chalk.green(
11462
'"http://myname.github.io/myapp"'
11563
)}${chalk.cyan(',')}`
11664
);
117-
console.log();
11865
}
66+
console.log();
67+
11968
console.log(
12069
`The ${chalk.cyan(buildFolder)} folder is ready to be deployed.`
12170
);
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-
}
71+
}
72+
73+
function printDeployInstructions(publicUrl, hasDeployScript, useYarn) {
74+
console.log(`To publish it at ${chalk.green(publicUrl)}, run:`);
75+
console.log();
76+
77+
// If script deploy has been added to package.json, skip the instructions
78+
if (!hasDeployScript) {
79+
if (useYarn) {
80+
console.log(` ${chalk.cyan('yarn')} add --dev gh-pages`);
81+
} else {
82+
console.log(` ${chalk.cyan('npm')} install --save-dev gh-pages`);
13083
}
131-
console.log(` ${chalk.cyan('serve')} -s ${buildFolder}`);
13284
console.log();
85+
86+
console.log(`Add the following script in your ${chalk.cyan(
87+
'package.json'
88+
)}.`);
89+
console.log();
90+
91+
console.log(` ${chalk.dim('// ...')}`);
92+
console.log(` ${chalk.yellow('"scripts"')}: {`);
93+
console.log(` ${chalk.dim('// ...')}`);
94+
console.log(` ${chalk.yellow('"predeploy"')}: ${chalk.yellow(
95+
'"npm run build",'
96+
)}`);
97+
console.log(` ${chalk.yellow('"deploy"')}: ${chalk.yellow(
98+
'"gh-pages -d build"'
99+
)}`);
100+
console.log(' }');
101+
console.log();
102+
103+
console.log('Then run:');
104+
console.log();
105+
}
106+
console.log(` ${chalk.cyan(useYarn ? 'yarn' : 'npm')} run deploy`);
107+
}
108+
109+
function printStaticServerInstructions(buildFolder, useYarn) {
110+
console.log('You may serve it with a static server:');
111+
console.log();
112+
113+
if (!fs.existsSync(`${globalModules}/serve`)) {
114+
if (useYarn) {
115+
console.log(` ${chalk.cyan('yarn')} global add serve`);
116+
} else {
117+
console.log(` ${chalk.cyan('npm')} install -g serve`);
118+
}
133119
}
120+
console.log(` ${chalk.cyan('serve')} -s ${buildFolder}`);
134121
}
135122

136123
module.exports = printHostingInstructions;

0 commit comments

Comments
 (0)