1
1
var path = require ( 'path' ) ,
2
2
fs = require ( 'fs' ) ,
3
+ fse = require ( 'fs-extra' ) ,
3
4
ghpages = require ( 'gh-pages' ) ,
4
5
denodeify = require ( 'denodeify' ) ;
5
6
@@ -36,10 +37,10 @@ exports.run = function (options) {
36
37
37
38
// always clean the cache directory.
38
39
// avoids "Error: Remote url mismatch."
39
- if ( ! options . dryRun ) {
40
- ghpages . clean ( ) ;
41
- } else {
40
+ if ( options . dryRun ) {
42
41
console . info ( 'Dry-run / SKIPPED: cleaning of the cache directory' ) ;
42
+ } else {
43
+ ghpages . clean ( ) ;
43
44
}
44
45
45
46
var access = publish = denodeify ( fs . access ) ;
@@ -50,18 +51,35 @@ exports.run = function (options) {
50
51
}
51
52
52
53
return go ( )
53
- . then ( function ( ) {
54
+ . then ( function checkIfDistFolderExists ( ) {
54
55
return access ( dir , fs . F_OK )
55
56
} )
56
- . catch ( function ( error ) {
57
+ . catch ( function handleMissingDistFolder ( error ) {
57
58
console . error ( 'Dist folder does not exist. Check the dir --dir parameter or build the project first!\n' ) ;
58
59
return Promise . reject ( error ) ;
59
60
} )
60
- . then ( function ( ) {
61
- if ( ! options . dryRun ) {
62
- return publish ( dir , options )
63
- } else {
64
- console . info ( 'Dry-run / SKIPPED: publishing to "' + dir + '" with the following options:' , {
61
+ . then ( function createNotFoundPage ( ) {
62
+
63
+ if ( options . dryRun ) {
64
+ console . info ( 'Dry-run / SKIPPED: copying of index.html to 404.html' ) ;
65
+ return ;
66
+ }
67
+
68
+ // Note:
69
+ // There is no guarantee that there will be an index.html file,
70
+ // as the developer may specify a custom index file.
71
+ const indexHtml = path . join ( dir , 'index.html' ) ;
72
+ const notFoundPage = path . join ( dir , '404.html' ) ;
73
+
74
+ return fse . copy ( indexHtml , notFoundPage ) .
75
+ catch ( function ( ) {
76
+ console . info ( 'index.html could not be copied to 404.html. Continuing without an error.' ) ;
77
+ return ;
78
+ } )
79
+ } )
80
+ . then ( function publishViaGhPages ( ) {
81
+ if ( options . dryRun ) {
82
+ console . info ( 'Dry-run / SKIPPED: publishing to "' + dir + '" with the following options:' , {
65
83
dir : dir ,
66
84
repo : options . repo || 'undefined: current working directory (which must be a git repo in this case) will be used to commit & push' ,
67
85
message : options . message ,
@@ -71,12 +89,15 @@ exports.run = function (options) {
71
89
noDotfiles : options . noDotfiles || 'undefined: dotfiles are included by default' ,
72
90
dryRun : options . dryRun
73
91
} ) ;
92
+ return ;
74
93
}
94
+
95
+ return publish ( dir , options )
75
96
} )
76
- . then ( function ( ) {
97
+ . then ( function showSuccess ( ) {
77
98
console . log ( 'Successfully published!\n' ) ;
78
99
} )
79
- . catch ( function ( error ) {
100
+ . catch ( function showError ( error ) {
80
101
console . error ( 'An error occurred!\n' , error ) ;
81
102
return Promise . reject ( error ) ;
82
103
} ) ;
0 commit comments