@@ -4,6 +4,7 @@ const path = require('path')
4
4
5
5
const commander = require ( 'commander' )
6
6
const execa = require ( 'execa' )
7
+ const fs = require ( 'fs-extra' )
7
8
const gitRepoIsUpToDate = require ( 'git-repo-is-up-to-date' )
8
9
const commit = require ( 'this-commit' ) ( )
9
10
const username = require ( 'username' )
@@ -26,8 +27,9 @@ commander
26
27
. option ( '-e, --env <environment>' , 'Environment to use.' )
27
28
. option ( '-m, --minify' , 'Minify built files.' )
28
29
. option ( '-O, --outdir <dir>' , 'Publish directory' , '' )
29
- . option ( '--cloudfront' , 'CloudFront Distribution ID to invalidate.' )
30
- . option ( '--s3bucket' , 'S3 Bucket to push to.' )
30
+ . option ( '--cloudfront <id>' , 'CloudFront Distribution ID to invalidate.' )
31
+ . option ( '--s3bucket <bucket>' , 'S3 Bucket to push to.' )
32
+ . option ( '--static-file-directory <dir>' , 'Directory of static files to deploy in lieu of building' )
31
33
. parse ( process . argv )
32
34
33
35
// each of these variables are also used in the logToMsTeams function and
@@ -52,18 +54,21 @@ async function deploy () {
52
54
process . exit ( 1 )
53
55
}
54
56
55
- // decrypt env file using sops to make sure old file is overwritten with
56
- // data from encoded sops file
57
- const configPath = path . resolve ( commander . config )
58
- console . log ( 'decrypting env file with sops' )
59
- const { stdout} = await execa (
60
- 'sops' ,
61
- [
62
- '-d' ,
63
- path . join ( configPath , 'env.enc.yml' )
64
- ]
65
- )
66
- await writeFile ( path . join ( configPath , 'env.yml' ) , stdout )
57
+ // no decryption needed during workflow to upload just static files
58
+ if ( ! commander . staticFileDirectory ) {
59
+ // decrypt env file using sops to make sure old file is overwritten with
60
+ // data from encoded sops file
61
+ const configPath = path . resolve ( commander . config )
62
+ console . log ( 'decrypting env file with sops' )
63
+ const { stdout} = await execa (
64
+ 'sops' ,
65
+ [
66
+ '-d' ,
67
+ path . join ( configPath , 'env.enc.yml' )
68
+ ]
69
+ )
70
+ await writeFile ( path . join ( configPath , 'env.yml' ) , stdout )
71
+ }
67
72
// at this point, we can be certain that the local configurations repo
68
73
// directory matches what has been committed and pushed to the remote repo
69
74
}
@@ -80,19 +85,8 @@ async function deploy () {
80
85
} )
81
86
}
82
87
83
- const files = util . parseEntries ( [ ...commander . args , ...( get ( 'entries' ) || [ ] ) ] )
84
- util . assertEntriesExist ( files )
85
- const sourceFiles = files . map ( f => f [ 0 ] )
86
- const outfiles = [ ...files . map ( f => f [ 1 ] ) , ...files . map ( f => `${ f [ 1 ] } .map` ) ]
87
-
88
88
env = get ( 'env' ) || 'development'
89
89
minify = get ( 'minify' )
90
- const buildOpts = {
91
- config,
92
- env,
93
- files,
94
- minify
95
- }
96
90
cloudfront = get ( 'cloudfront' )
97
91
s3bucket = get ( 's3bucket' )
98
92
@@ -108,18 +102,52 @@ async function deploy () {
108
102
:hash: *commit:* ${ commit }
109
103
:seedling: *env:* ${ env }
110
104
:compression: *minify:* ${ minify }
111
- :package: *s3bucket:* ${ s3bucket }
112
- :hammer_and_wrench: *building:* ${ sourceFiles . join ( ', ' ) } `
105
+ :package: *s3bucket:* ${ s3bucket } `
113
106
)
114
-
107
+ let outfiles
115
108
try {
116
- await build ( buildOpts )
117
- await logger . log ( `:rocket: *uploading:* ${ sourceFiles . length * 2 } file(s)` )
109
+ // If the flag staticFileDirectory is set, upload all files found in
110
+ // the base level of the given directory.
111
+ if ( commander . staticFileDirectory ) {
112
+ const staticDirPath = path . resolve ( commander . staticFileDirectory )
113
+ process . chdir ( staticDirPath )
114
+ outfiles = [ ]
115
+ const files = await fs . readdir ( staticDirPath )
116
+ await Promise . all ( files . map ( async file => {
117
+ const fileStats = await fs . stat ( file )
118
+ if ( ! fileStats . isDirectory ( ) ) {
119
+ outfiles . push ( file )
120
+ }
121
+ } ) )
122
+ await logger . log ( `:rocket: *uploading:* ${ outfiles . length } file(s)` )
123
+ } else {
124
+ // Otherwise, upload the files specified with the entries arg.
125
+ const files = util . parseEntries ( [ ...commander . args , ...( get ( 'entries' ) || [ ] ) ] )
126
+ // assert that the files exist if not uploading from static file directory
127
+ util . assertEntriesExist ( files )
128
+ // build files using mastarm build
129
+ outfiles = [ ...files . map ( f => f [ 1 ] ) , ...files . map ( f => `${ f [ 1 ] } .map` ) ]
130
+ const sourceFiles = files . map ( f => f [ 0 ] )
131
+ await logger . log ( `:hammer_and_wrench: *building:* ${ sourceFiles . join ( ', ' ) } ` )
132
+ const buildOpts = {
133
+ config,
134
+ env,
135
+ files,
136
+ minify
137
+ }
138
+ await build ( buildOpts )
139
+ await logger . log ( `:rocket: *uploading:* ${ sourceFiles . length * 2 } file(s)` )
140
+ }
141
+
142
+ // upload files to s3 and invalid cloudfront if needed
118
143
await Promise . all (
119
- outfiles . map ( outfile =>
120
- readFile ( outfile ) . then ( body => pushToS3 ( { body, outfile} ) )
121
- )
144
+ outfiles . map ( async outfile => {
145
+ const body = await readFile ( outfile )
146
+ await pushToS3 ( { body, outfile} )
147
+ } )
122
148
)
149
+
150
+ // pronounce success!
123
151
await logger . log (
124
152
`:tada: :confetti_ball: :tada: *deploy ${ tag } complete* :tada: :confetti_ball: :tada:`
125
153
)
@@ -152,7 +180,7 @@ function logToMsTeams ({ configCommit, configDir, configRemoteUrl, error }) {
152
180
153
181
const potentialAction = [ {
154
182
'@type' : 'OpenUri' ,
155
- name : ` View Commit on Github` ,
183
+ name : ' View Commit on Github' ,
156
184
targets : [
157
185
{
158
186
os : 'default' ,
@@ -163,7 +191,7 @@ function logToMsTeams ({ configCommit, configDir, configRemoteUrl, error }) {
163
191
if ( configCommit && configRemoteUrl ) {
164
192
potentialAction . push ( {
165
193
'@type' : 'OpenUri' ,
166
- name : ` View Config Commit on Github` ,
194
+ name : ' View Config Commit on Github' ,
167
195
targets : [
168
196
{
169
197
os : 'default' ,
@@ -185,7 +213,7 @@ function logToMsTeams ({ configCommit, configDir, configRemoteUrl, error }) {
185
213
📦 **s3bucket:** ${ s3bucket } \n
186
214
${ error
187
215
? `🚨 🚨 **error deploying ${ error . message || error } **`
188
- : ` 🎉 🎊 🎉 **deploy successful!** 🎉 🎊 🎉` } `
216
+ : ' 🎉 🎊 🎉 **deploy successful!** 🎉 🎊 🎉' } `
189
217
190
218
return logger . notifyMsTeams ( {
191
219
potentialAction,
0 commit comments