|
1 | 1 | #!/usr/bin/env node
|
2 | 2 |
|
3 | 3 | const commander = require('commander')
|
| 4 | +const fs = require('fs') |
4 | 5 | const path = require('path')
|
5 | 6 |
|
6 | 7 | const loadConfig = require('../lib/load-config')
|
| 8 | +const logger = require('../lib/logger') |
7 | 9 | const util = require('../lib/util')
|
8 | 10 |
|
9 | 11 | commander
|
@@ -42,11 +44,11 @@ commander
|
42 | 44 | const build = require('../lib/build')
|
43 | 45 | build(opts)
|
44 | 46 | .then((results) => {
|
45 |
| - console.log('done building...') |
| 47 | + logger.log('done building...') |
46 | 48 | if (!watch) process.exit(0)
|
47 | 49 | })
|
48 | 50 | .catch((err) => {
|
49 |
| - console.error(err) |
| 51 | + logger.error(err) |
50 | 52 | if (!watch) process.exit(1)
|
51 | 53 | })
|
52 | 54 | }
|
@@ -75,52 +77,69 @@ commander
|
75 | 77 | .action(function (entries, options) {
|
76 | 78 | const commit = require('this-commit')()
|
77 | 79 | const username = require('username')
|
78 |
| - const createLogger = require('../lib/logger') |
| 80 | + |
| 81 | + const build = require('../lib/build') |
79 | 82 | const pkg = require('../lib/pkg')
|
80 |
| - const pushToS3 = require('../lib/push-to-s3') |
| 83 | + const createPushToS3 = require('../lib/push-to-s3') |
81 | 84 |
|
82 | 85 | const url = pkg.repository.url.replace('.git', '')
|
83 |
| - const tag = `<${url}/commit/${commit}|${pkg.name}@${commit.slice(0, 6)}>:` |
84 |
| - const user = username.sync() |
| 86 | + const tag = `<${url}/commit/${commit}|${pkg.name}@${commit.slice(0, 6)}>` |
85 | 87 | const config = loadConfig(process.cwd(), commander.config, commander.env)
|
86 | 88 | const get = util.makeGetFn([options, commander, config.settings])
|
87 | 89 |
|
88 |
| - const env = get('env') || 'development' |
89 |
| - const s3bucket = get('s3bucket') |
| 90 | + if (config.env.SLACK_WEBHOOK && config.env.SLACK_WEBHOOK.length > 0) { |
| 91 | + logger.logToSlack({ |
| 92 | + channel: config.env.SLACK_CHANNEL || '#devops', |
| 93 | + webhook: config.env.SLACK_WEBHOOK |
| 94 | + }) |
| 95 | + } |
90 | 96 |
|
91 | 97 | const files = util.parseEntries([...entries, ...(get('entries') || [])])
|
92 | 98 | util.assertEntriesExist(files)
|
| 99 | + const sourceFiles = files.map((f) => f[0]) |
| 100 | + const outfiles = [ |
| 101 | + ...files.map((f) => f[1]), |
| 102 | + ...files.map((f) => `${f[1]}.map`) |
| 103 | + ] |
93 | 104 |
|
94 |
| - const log = createLogger({channel: config.env.SLACK_CHANNEL || '#devops', webhook: config.env.SLACK_WEBHOOK}) |
| 105 | + const env = get('env') || 'development' |
95 | 106 | const minify = get('minify')
|
96 |
| - const cloudfront = get('cloudfront') |
97 |
| - const push = pushToS3({ |
98 |
| - cloudfront, |
| 107 | + const buildOpts = { |
99 | 108 | config,
|
100 | 109 | env,
|
101 |
| - log, |
102 |
| - minify, |
103 |
| - s3bucket, |
104 |
| - tag |
| 110 | + files, |
| 111 | + minify |
| 112 | + } |
| 113 | + const cloudfront = get('cloudfront') |
| 114 | + const s3bucket = get('s3bucket') |
| 115 | + |
| 116 | + const pushToS3 = createPushToS3({ |
| 117 | + cloudfront, |
| 118 | + s3bucket |
105 | 119 | })
|
106 | 120 |
|
107 |
| - log( |
108 |
| -`:construction: *${tag} deploy started by <@${user}>* |
| 121 | + logger.log( |
| 122 | +`:construction: *deploying: ${tag} by <@${username.sync()}>* |
109 | 123 | :cloud: *cloudfront:* ${cloudfront}
|
110 | 124 | :hash: *commit:* ${commit}
|
111 | 125 | :seedling: *env:* ${env}
|
112 | 126 | :compression: *minify:* ${minify}
|
113 |
| -:package: *s3bucket:* ${s3bucket}` |
114 |
| - ).then(() => { |
115 |
| - Promise |
116 |
| - .all(files.map(push)) |
| 127 | +:package: *s3bucket:* ${s3bucket} |
| 128 | +:hammer_and_wrench: *building:* ${sourceFiles.join(', ')}` |
| 129 | + ).then(() => |
| 130 | + build(buildOpts) |
117 | 131 | .then(() =>
|
118 |
| - log(`:rocket: ${tag} deploy finished!! :tada: :confetti_ball: :tada:`) |
| 132 | + logger.log(`:rocket: *uploading:* ${sourceFiles.length * 2} file(s)`)) |
| 133 | + .then(() => |
| 134 | + Promise.all(outfiles.map((outfile) => |
| 135 | + readFile(outfile).then((body) => |
| 136 | + pushToS3({body, outfile}))))) |
| 137 | + .then(() => |
| 138 | + logger.log(`:tada: :confetti_ball: :tada: *deploy ${tag} complete* :tada: :confetti_ball: :tada:`) |
119 | 139 | .then(() => process.exit(0)))
|
120 | 140 | .catch((err) =>
|
121 |
| - log(`:rotating_light: *error deploying ${tag} ${err.message}*`) |
122 |
| - .then(() => process.exit(1))) |
123 |
| - }) |
| 141 | + logger.log(`:rotating_light: *${tag} error deploying ${tag} ${err.message || err}*`) |
| 142 | + .then(() => process.exit(1)))) |
124 | 143 | })
|
125 | 144 |
|
126 | 145 | commander
|
@@ -158,14 +177,14 @@ commander
|
158 | 177 | const errors = lintMessages(paths.length > 0 ? paths : ['lib'], config.messages)
|
159 | 178 |
|
160 | 179 | if (errors.length > 0) {
|
161 |
| - console.log(`${errors.length} missing messages`) |
| 180 | + logger.error(`${errors.length} missing messages`) |
162 | 181 | for (const [message, file, line] of errors) {
|
163 |
| - console.log(`${file} line ${line}: ${message} is not defined`) |
| 182 | + logger.error(`${file} line ${line}: ${message} is not defined`) |
164 | 183 | }
|
165 | 184 |
|
166 | 185 | process.exit(1)
|
167 | 186 | } else {
|
168 |
| - console.log('No missing messages found! 💃') |
| 187 | + logger.log('No missing messages found! 💃') |
169 | 188 | }
|
170 | 189 | })
|
171 | 190 |
|
@@ -212,3 +231,9 @@ commander
|
212 | 231 | })
|
213 | 232 |
|
214 | 233 | commander.parse(process.argv)
|
| 234 | + |
| 235 | +const readFile = (f) => |
| 236 | + new Promise((resolve, reject) => |
| 237 | + fs.readFile(f, (err, data) => err |
| 238 | + ? reject(err) |
| 239 | + : resolve(data))) |
0 commit comments