Skip to content

Commit 9b208a6

Browse files
author
Jonathan Felchlin
committed
Cleaning up the printHostingInstructions a bit
1 parent 51aaf3a commit 9b208a6

File tree

1 file changed

+73
-90
lines changed

1 file changed

+73
-90
lines changed

packages/react-dev-utils/printHostingInstructions.js

Lines changed: 73 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -21,122 +21,105 @@ function printHostingInstructions(
2121
buildFolder,
2222
useYarn
2323
) {
24-
const publicPathname = url.parse(publicPath).pathname;
25-
if (publicUrl && publicUrl.indexOf('.github.io/') !== -1) {
24+
if (publicUrl && publicUrl.includes('.github.io/')) {
2625
// "homepage": "http://user.github.io/project"
27-
console.log(
28-
`The project was built assuming it is hosted at ${chalk.green(
29-
publicPathname
30-
)}.`
31-
);
32-
console.log(
33-
`You can control this with the ${chalk.green(
34-
'homepage'
35-
)} field in your ${chalk.cyan('package.json')}.`
36-
);
37-
console.log();
38-
console.log(
39-
`The ${chalk.cyan(buildFolder)} folder is ready to be deployed.`
40-
);
41-
console.log(`To publish it at ${chalk.green(publicUrl)}, run:`);
42-
// If script deploy has been added to package.json, skip the instructions
43-
if (typeof appPackage.scripts.deploy === 'undefined') {
44-
console.log();
45-
if (useYarn) {
46-
console.log(` ${chalk.cyan('yarn')} add --dev gh-pages`);
47-
} else {
48-
console.log(` ${chalk.cyan('npm')} install --save-dev gh-pages`);
49-
}
50-
console.log();
51-
console.log(
52-
`Add the following script in your ${chalk.cyan('package.json')}.`
53-
);
54-
console.log();
55-
console.log(` ${chalk.dim('// ...')}`);
56-
console.log(` ${chalk.yellow('"scripts"')}: {`);
57-
console.log(` ${chalk.dim('// ...')}`);
58-
console.log(
59-
` ${chalk.yellow('"predeploy"')}: ${chalk.yellow(
60-
'"npm run build",'
61-
)}`
62-
);
63-
console.log(
64-
` ${chalk.yellow('"deploy"')}: ${chalk.yellow(
65-
'"gh-pages -d build"'
66-
)}`
67-
);
68-
console.log(' }');
69-
console.log();
70-
console.log('Then run:');
71-
}
72-
console.log();
73-
console.log(` ${chalk.cyan(useYarn ? 'yarn' : 'npm')} run deploy`);
74-
console.log();
26+
const publicPathname = url.parse(publicPath).pathname;
27+
const hasDeployScript = typeof appPackage.scripts.deploy !== 'undefined';
28+
printBaseMessage(buildFolder, publicPathname);
29+
30+
printDeployInstructions(publicUrl, hasDeployScript, useYarn);
31+
7532
} else if (publicPath !== '/') {
7633
// "homepage": "http://mywebsite.com/project"
34+
printBaseMessage(buildFolder, publicPath);
35+
36+
} else {
37+
// "homepage": "http://mywebsite.com"
38+
// or no homepage
39+
printBaseMessage(buildFolder, publicUrl);
40+
41+
printStaticServerInstructions(useYarn);
42+
}
43+
console.log();
44+
}
45+
46+
function printBaseMessage(buildFolder, hostingLocation) {
7747
console.log(
7848
`The project was built assuming it is hosted at ${chalk.green(
79-
publicPath
49+
hostingLocation || 'the server root'
8050
)}.`
8151
);
8252
console.log(
8353
`You can control this with the ${chalk.green(
8454
'homepage'
8555
)} field in your ${chalk.cyan('package.json')}.`
8656
);
87-
console.log();
88-
console.log(
89-
`The ${chalk.cyan(buildFolder)} folder is ready to be deployed.`
90-
);
91-
console.log();
92-
} else {
93-
if (publicUrl) {
94-
// "homepage": "http://mywebsite.com"
95-
console.log(
96-
`The project was built assuming it is hosted at ${chalk.green(
97-
publicUrl
98-
)}.`
99-
);
100-
console.log(
101-
`You can control this with the ${chalk.green(
102-
'homepage'
103-
)} field in your ${chalk.cyan('package.json')}.`
104-
);
105-
console.log();
106-
} else {
107-
// no homepage
108-
console.log(
109-
'The project was built assuming it is hosted at the server root.'
110-
);
111-
console.log(
112-
`To override this, specify the ${chalk.green(
113-
'homepage'
114-
)} in your ${chalk.cyan('package.json')}.`
115-
);
57+
58+
if (!hostingLocation) {
11659
console.log('For example, add this to build it for GitHub Pages:');
11760
console.log();
61+
11862
console.log(
11963
` ${chalk.green('"homepage"')} ${chalk.cyan(':')} ${chalk.green(
12064
'"http://myname.github.io/myapp"'
12165
)}${chalk.cyan(',')}`
12266
);
123-
console.log();
12467
}
68+
console.log();
69+
12570
console.log(
12671
`The ${chalk.cyan(buildFolder)} folder is ready to be deployed.`
12772
);
128-
console.log('You may serve it with a static server:');
129-
console.log();
130-
if (!fs.existsSync(`${globalModules}/serve`)) {
131-
if (useYarn) {
132-
console.log(` ${chalk.cyan('yarn')} global add serve`);
133-
} else {
134-
console.log(` ${chalk.cyan('npm')} install -g serve`);
135-
}
73+
}
74+
75+
function printDeployInstructions(publicUrl, hasDeployScript, useYarn) {
76+
console.log(`To publish it at ${chalk.green(publicUrl)}, run:`);
77+
console.log();
78+
79+
// If script deploy has been added to package.json, skip the instructions
80+
if (!hasDeployScript) {
81+
if (useYarn) {
82+
console.log(` ${chalk.cyan('yarn')} add --dev gh-pages`);
83+
} else {
84+
console.log(` ${chalk.cyan('npm')} install --save-dev gh-pages`);
13685
}
137-
console.log(` ${chalk.cyan('serve')} -s ${buildFolder}`);
13886
console.log();
87+
88+
console.log(`Add the following script in your ${chalk.cyan(
89+
'package.json'
90+
)}.`);
91+
console.log();
92+
93+
console.log(` ${chalk.dim('// ...')}`);
94+
console.log(` ${chalk.yellow('"scripts"')}: {`);
95+
console.log(` ${chalk.dim('// ...')}`);
96+
console.log(` ${chalk.yellow('"predeploy"')}: ${chalk.yellow(
97+
'"npm run build",'
98+
)}`);
99+
console.log(` ${chalk.yellow('"deploy"')}: ${chalk.yellow(
100+
'"gh-pages -d build"'
101+
)}`);
102+
console.log(' }');
103+
console.log();
104+
105+
console.log('Then run:');
106+
console.log();
107+
}
108+
console.log(` ${chalk.cyan(useYarn ? 'yarn' : 'npm')} run deploy`);
109+
}
110+
111+
function printStaticServerInstructions(useYarn) {
112+
console.log('You may serve it with a static server:');
113+
console.log();
114+
115+
if (!fs.existsSync(`${globalModules}/serve`)) {
116+
if (useYarn) {
117+
console.log(` ${chalk.cyan('yarn')} global add serve`);
118+
} else {
119+
console.log(` ${chalk.cyan('npm')} install -g serve`);
120+
}
139121
}
122+
console.log(` ${chalk.cyan('serve')} -s ${buildFolder}`);
140123
}
141124

142125
module.exports = printHostingInstructions;

0 commit comments

Comments
 (0)