1
- import * as denodeify from 'denodeify' ;
2
1
import * as path from 'path' ;
3
2
import * as fs from 'fs' ;
4
3
import * as fse from 'fs-extra' ;
5
4
6
- import { GHPages } from './interfaces' ;
7
- import { Schema as RealDeployOptions } from './deploy/schema' ;
5
+ import { logging } from '@angular-devkit/core' ;
6
+ import { defaults } from './defaults' ;
7
+ import { GHPages } from '../interfaces' ;
8
+ import { Schema as RealDeployOptions } from '../deploy/schema' ;
8
9
9
10
const ghpages = require ( 'gh-pages' ) ;
10
- var access = denodeify ( fs . access ) ;
11
11
12
- function run ( options : RealDeployOptions ) {
12
+ export async function run ( dir : string , options : RealDeployOptions , logger : logging . LoggerApi ) {
13
13
14
- options = options || { } ;
14
+ options = prepareOptions ( options , logger ) ;
15
+
16
+ // always clean the cache directory.
17
+ // avoids "Error: Remote url mismatch."
18
+ if ( options . dryRun ) {
19
+ logger . info ( '*** Dry-run / SKIPPED: cleaning of the cache directory' ) ;
20
+ } else {
21
+ ghpages . clean ( ) ;
22
+ }
23
+
24
+ try {
25
+ checkIfDistFolderExists ( dir ) ;
26
+ await createNotFoundPage ( dir , options , logger ) ;
27
+ await createCnameFile ( dir , options , logger ) ;
28
+ await publishViaGhPages ( ghpages , dir , options , logger ) ;
29
+
30
+ logger . info ( '*** 🚀 Successfully published! Have a nice day!' ) ;
31
+ }
32
+ catch ( error ) {
33
+ logger . error ( '*** An error occurred!' , error ) ;
34
+ throw error ;
35
+ }
36
+ } ;
37
+
38
+
39
+ function prepareOptions ( options : RealDeployOptions , logger : logging . LoggerApi ) {
40
+
41
+ options = {
42
+ ...defaults ,
43
+ options
44
+ } ;
15
45
16
46
if ( options . dryRun ) {
17
- console . log ( '*** Dry-run: No changes are applied at all.' )
47
+ logger . info ( '*** Dry-run: No changes are applied at all.' ) ;
18
48
}
19
49
20
50
if ( options . name && options . email ) {
21
51
options . user = {
22
52
name : options . name ,
23
53
email : options . email
24
- }
54
+ } ;
25
55
} ;
26
56
27
- // gh-pages: forwards messages to console
28
- options . logger = function ( message ) { console . log ( message + "\n" ) ; }
29
-
30
- var dir = path . join ( process . cwd ( ) , options . dir ) ;
57
+ // gh-pages internal: forwards messages to logger
58
+ options . logger = function ( message ) { logger . info ( message ) ; } ;
31
59
32
60
if ( process . env . TRAVIS ) {
33
61
options . message += ' -- ' + process . env . TRAVIS_COMMIT_MESSAGE + ' \n\n' +
@@ -36,7 +64,7 @@ function run(options: RealDeployOptions) {
36
64
}
37
65
38
66
if ( process . env . CIRCLECI ) {
39
- options . message += ' -- \n\n' +
67
+ options . message += '\n\n' +
40
68
'Triggered by commit: https://github.com/' + process . env . CIRCLE_PROJECT_USERNAME + '/' + process . env . CIRCLE_PROJECT_REPONAME + '/commit/' + process . env . CIRCLE_SHA1 + '\n' +
41
69
'CircleCI build: ' + process . env . CIRCLE_BUILD_URL ;
42
70
}
@@ -46,87 +74,65 @@ function run(options: RealDeployOptions) {
46
74
options . repo = options . repo . replace ( 'GH_TOKEN' , process . env . GH_TOKEN ) ;
47
75
}
48
76
49
- // always clean the cache directory.
50
- // avoids "Error: Remote url mismatch."
51
- if ( options . dryRun ) {
52
- console . info ( '*** Dry-run / SKIPPED: cleaning of the cache directory' ) ;
53
- } else {
54
- ghpages . clean ( ) ;
55
- }
56
-
57
-
58
- var publish = denodeify ( ghpages . publish ) ;
59
-
60
-
61
- return Promise . resolve ( )
62
- . then ( ( ) => checkIfDistFolderExists ( dir ) )
63
- . catch ( ( error ) => handleMissingDistFolder ( error ) )
64
- . then ( ( ) => createNotFoundPage ( dir , options ) )
65
- . then ( ( ) => createCnameFile ( dir , options ) )
66
- . then ( ( ) => publishViaGhPages ( ghpages , dir , options ) )
67
- . then ( ( ) => showSuccess ( ) )
68
- . catch ( ( error ) => showError ( error ) ) ;
69
- } ;
70
-
71
-
72
- function checkIfDistFolderExists ( dir : string ) {
73
- const flag = fs [ 'F_OK' ] ;
74
- return access ( dir , flag ) ;
77
+ return options ;
75
78
}
76
79
77
- function handleMissingDistFolder ( error ) {
78
- console . error ( '*** Dist folder does not exist. Check the dir --dir parameter or build the project first!\n' ) ;
79
- return Promise . reject ( error ) ;
80
+ function checkIfDistFolderExists ( dir : string ) {
81
+ if ( ! fs . existsSync ( dir ) ) {
82
+ throw new Error ( '*** Dist folder does not exist. Check the dir --dir parameter or build the project first!' ) ;
83
+ }
80
84
}
81
85
82
- function createNotFoundPage ( dir : string , options : RealDeployOptions ) {
86
+ async function createNotFoundPage ( dir : string , options : RealDeployOptions , logger : logging . LoggerApi ) {
83
87
84
88
if ( options . dryRun ) {
85
- console . info ( '*** Dry-run / SKIPPED: copying of index.html to 404.html' ) ;
89
+ logger . info ( '*** Dry-run / SKIPPED: copying of index.html to 404.html' ) ;
86
90
return ;
87
91
}
88
92
89
93
// Note:
90
94
// There is no guarantee that there will be an index.html file,
91
95
// as the developer may specify a custom index file.
96
+ // TODO: respect setting in angular.json
92
97
const indexHtml = path . join ( dir , 'index.html' ) ;
93
98
const notFoundPage = path . join ( dir , '404.html' ) ;
94
99
95
- return fse . copy ( indexHtml , notFoundPage ) .
96
- catch ( function ( err ) {
97
- console . info ( 'index.html could not be copied to 404.html. Continuing without an error.' ) ;
98
- console . info ( '(Hint: are you sure that you have setup the --dir parameter correctly?)' ) ;
99
- console . dir ( err ) ;
100
- return ;
101
- } )
100
+ try {
101
+ return fse . copy ( indexHtml , notFoundPage ) ;
102
+ }
103
+ catch ( err ) {
104
+ logger . info ( 'index.html could not be copied to 404.html. This does not look like an angular project?!' ) ;
105
+ logger . info ( '(Hint: are you sure that you have setup the directory correctly?)' ) ;
106
+ logger . debug ( 'Diagnostic info' , err ) ;
107
+ return ;
108
+ }
102
109
}
103
110
104
- function createCnameFile ( dir : string , options : RealDeployOptions ) {
111
+ async function createCnameFile ( dir : string , options : RealDeployOptions , logger : logging . LoggerApi ) {
105
112
106
113
if ( ! options . cname ) {
107
114
return ;
108
115
}
109
116
110
117
const cnameFile = path . join ( dir , 'CNAME' ) ;
111
118
if ( options . dryRun ) {
112
- console . info ( '*** Dry-run / SKIPPED: creating of CNAME file with content: ' + options . cname ) ;
119
+ logger . info ( '*** Dry-run / SKIPPED: creating of CNAME file with content: ' + options . cname ) ;
113
120
return ;
114
121
}
115
122
116
- return fse . writeFile ( cnameFile , options . cname )
117
- . then ( function ( ) {
118
- console . log ( '*** CNAME file created' ) ;
119
- } )
120
- . catch ( function ( err ) {
121
- console . info ( '*** CNAME file could not be created. Stopping execution.' ) ;
122
- throw err ;
123
- } )
123
+ try {
124
+ await fse . writeFile ( cnameFile , options . cname ) ;
125
+ logger . info ( '*** CNAME file created' ) ;
126
+ }
127
+ catch ( err ) {
128
+ logger . error ( '*** CNAME file could not be created. Stopping execution.' ) ;
129
+ throw err ;
130
+ }
124
131
}
125
132
126
-
127
- async function publishViaGhPages ( ghPages : GHPages , dir : string , options : RealDeployOptions ) {
133
+ async function publishViaGhPages ( ghPages : GHPages , dir : string , options : RealDeployOptions , logger : logging . LoggerApi ) {
128
134
if ( options . dryRun ) {
129
- console . info ( '*** Dry-run / SKIPPED: publishing to "' + dir + '" with the following options:' , {
135
+ logger . info ( '*** Dry-run / SKIPPED: publishing folder "' + dir + '" with the following options:' , {
130
136
dir : dir ,
131
137
repo : options . repo || 'undefined: current working directory (which must be a git repo in this case) will be used to commit & push' ,
132
138
message : options . message ,
@@ -136,19 +142,9 @@ async function publishViaGhPages(ghPages: GHPages, dir: string, options: RealDep
136
142
noDotfiles : options . noDotfiles || 'undefined: dotfiles are included by default' ,
137
143
dryRun : options . dryRun ,
138
144
cname : options . cname || 'undefined: no CNAME file will be created' ,
139
- } ) ;
145
+ } as any ) ;
140
146
return ;
141
147
}
142
148
143
149
return await ghPages . publish ( dir , options )
144
150
}
145
-
146
- function showSuccess ( ) {
147
- console . log ( '*** Successfully published!\n' ) ;
148
- }
149
-
150
- function showError ( error ) {
151
- console . error ( '*** An error occurred!\n' ) ;
152
- console . dir ( error ) ;
153
- return Promise . reject ( error ) ;
154
- }
0 commit comments