Skip to content

Commit 5f44987

Browse files
Merge pull request #164 from conveyal/dev
v3.7.2
2 parents 191aaf6 + 4b27cde commit 5f44987

13 files changed

+213
-202
lines changed

__tests__/bin/mastarm.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('mastarm cli', () => {
4040
exec(`node ${mastarm} build ${mockDir}/index.js:${buildDir}/index.js ${mockDir}/index.css:${buildDir}/index.css`,
4141
(err, stdout, stderr) => {
4242
expect(err).toBeNull()
43-
expect(stdout).toContain('updated css file')
43+
expect(stdout).toContain('done building')
4444
expect(stderr).toBe('')
4545
expect(fs.existsSync(`${buildDir}/index.js`)).toBeTruthy()
4646
expect(fs.existsSync(`${buildDir}/index.css`)).toBeTruthy()

__tests__/lib/push-to-s3.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
/* global describe, it */
22

3-
const BUILD_DIR = '__tests__/test-utils/tmp'
4-
const MOCK_DIR = '__tests__/test-utils/mocks'
3+
const fs = require('fs')
4+
const path = require('path')
5+
6+
const BUILD_DIR = path.join(process.cwd(), '__tests__/test-utils/tmp')
7+
const MOCK_DIR = path.join(process.cwd(), '__tests__/test-utils/mocks')
8+
const files = [
9+
[`${MOCK_DIR}/index.js`, `${BUILD_DIR}/index.js`],
10+
[`${MOCK_DIR}/index.css`, `${BUILD_DIR}/index.css`]
11+
]
512

613
describe('lib > push to s3', () => {
7-
const configPush = require('../../lib/push-to-s3')
14+
const build = require('../../lib/build')
15+
const createPushToS3 = require('../../lib/push-to-s3')
816
const loadConfig = require('../../lib/load-config')
9-
const createLogger = require('../../lib/logger')
1017

1118
it('should compile JavaScript and CSS and send to s3 via aws-sdk', () => {
1219
const config = loadConfig(process.cwd(), 'configurations/default', 'development')
13-
const push = configPush({
20+
const push = createPushToS3({
1421
env: 'development',
1522
config,
16-
log: createLogger(),
1723
minify: false,
1824
s3bucket: 'test-bucket'
1925
})
20-
return Promise.all([
21-
push([`${MOCK_DIR}/index.js`, `${BUILD_DIR}/index.js`]),
22-
push([`${MOCK_DIR}/index.css`, `${BUILD_DIR}/index.css`])
23-
])
26+
return build({
27+
config,
28+
env: 'development',
29+
files
30+
}).then(() =>
31+
Promise.all(files.map((f) =>
32+
push({body: fs.readFileSync(f[0]), outfile: f[0]}))))
2433
})
2534
})

bin/mastarm

+54-29
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#!/usr/bin/env node
22

33
const commander = require('commander')
4+
const fs = require('fs')
45
const path = require('path')
56

67
const loadConfig = require('../lib/load-config')
8+
const logger = require('../lib/logger')
79
const util = require('../lib/util')
810

911
commander
@@ -42,11 +44,11 @@ commander
4244
const build = require('../lib/build')
4345
build(opts)
4446
.then((results) => {
45-
console.log('done building...')
47+
logger.log('done building...')
4648
if (!watch) process.exit(0)
4749
})
4850
.catch((err) => {
49-
console.error(err)
51+
logger.error(err)
5052
if (!watch) process.exit(1)
5153
})
5254
}
@@ -75,52 +77,69 @@ commander
7577
.action(function (entries, options) {
7678
const commit = require('this-commit')()
7779
const username = require('username')
78-
const createLogger = require('../lib/logger')
80+
81+
const build = require('../lib/build')
7982
const pkg = require('../lib/pkg')
80-
const pushToS3 = require('../lib/push-to-s3')
83+
const createPushToS3 = require('../lib/push-to-s3')
8184

8285
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)}>`
8587
const config = loadConfig(process.cwd(), commander.config, commander.env)
8688
const get = util.makeGetFn([options, commander, config.settings])
8789

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+
}
9096

9197
const files = util.parseEntries([...entries, ...(get('entries') || [])])
9298
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+
]
93104

94-
const log = createLogger({channel: config.env.SLACK_CHANNEL || '#devops', webhook: config.env.SLACK_WEBHOOK})
105+
const env = get('env') || 'development'
95106
const minify = get('minify')
96-
const cloudfront = get('cloudfront')
97-
const push = pushToS3({
98-
cloudfront,
107+
const buildOpts = {
99108
config,
100109
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
105119
})
106120

107-
log(
108-
`:construction: *${tag} deploy started by <@${user}>*
121+
logger.log(
122+
`:construction: *deploying: ${tag} by <@${username.sync()}>*
109123
:cloud: *cloudfront:* ${cloudfront}
110124
:hash: *commit:* ${commit}
111125
:seedling: *env:* ${env}
112126
: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)
117131
.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:`)
119139
.then(() => process.exit(0)))
120140
.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))))
124143
})
125144

126145
commander
@@ -158,14 +177,14 @@ commander
158177
const errors = lintMessages(paths.length > 0 ? paths : ['lib'], config.messages)
159178

160179
if (errors.length > 0) {
161-
console.log(`${errors.length} missing messages`)
180+
logger.error(`${errors.length} missing messages`)
162181
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`)
164183
}
165184

166185
process.exit(1)
167186
} else {
168-
console.log('No missing messages found! 💃')
187+
logger.log('No missing messages found! 💃')
169188
}
170189
})
171190

@@ -212,3 +231,9 @@ commander
212231
})
213232

214233
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)))

lib/browserify.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ const uglifyify = require('uglifyify')
44

55
const transform = require('./js-transform')
66

7-
module.exports = function ({
7+
module.exports = browserifyIt
8+
9+
function browserifyIt ({
810
config,
911
entry,
1012
env,
1113
minify
1214
}) {
13-
const pipeline = browserify(entry, {
15+
return browserify(entry, {
1416
basedir: process.cwd(),
1517
cache: {},
1618
debug: true,
@@ -22,10 +24,10 @@ module.exports = function ({
2224
],
2325
transform: transform({config, env})
2426
})
27+
}
2528

26-
if (minify) {
27-
pipeline.transform(uglifyify, {global: true})
28-
}
29-
29+
module.exports.minify = function (opts) {
30+
const pipeline = browserifyIt(opts)
31+
pipeline.transform(uglifyify, {global: true})
3032
return pipeline
3133
}

lib/budo.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
const budo = require('budo')
22
const path = require('path')
33

4-
const transformJs = require('./js-transform')
54
const transformCss = require('./css-transform')
5+
const logger = require('./logger')
6+
const transformJs = require('./js-transform')
67

78
module.exports = function ({
89
config,
@@ -55,6 +56,6 @@ module.exports = function ({
5556
budo
5657
.cli(budoFiles, budoOpts)
5758
.on('error', function (err) {
58-
console.error(err.stack)
59+
logger.error(err.stack)
5960
})
6061
}

lib/build.js

+1-37
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
const fs = require('fs')
2-
const exorcist = require('exorcist')
3-
const mkdirp = require('mkdirp')
41
const path = require('path')
52

6-
const browserify = require('./browserify')
73
const buildCss = require('./css-transform')
4+
const buildJs = require('./js-build')
85

96
/**
107
* Takes a configuration object, array of file entries [entry, output], and other options.
@@ -24,36 +21,3 @@ module.exports = function ({
2421
? buildCss({config, entry, outfile, watch})
2522
: buildJs({config, entry, env, minify, outfile, watch})))
2623
}
27-
28-
/**
29-
*
30-
* @return Promise
31-
*/
32-
33-
function buildJs ({config, entry, env, minify, outfile, watch}) {
34-
const pipeline = browserify({config, entry, env, minify})
35-
const bundle = () => {
36-
return new Promise((resolve, reject) => {
37-
const stream = pipeline.bundle((err, buf) => {
38-
if (err) reject(err)
39-
else resolve(buf)
40-
})
41-
42-
if (outfile) {
43-
mkdirp.sync(path.dirname(outfile))
44-
stream
45-
.pipe(exorcist(`${outfile}.map`))
46-
.pipe(fs.createWriteStream(outfile))
47-
}
48-
})
49-
}
50-
51-
if (watch) {
52-
pipeline.plugin(require('watchify'), {poll: true})
53-
pipeline.plugin(require('errorify'))
54-
pipeline.on('update', bundle)
55-
pipeline.on('log', console.log)
56-
}
57-
58-
return bundle()
59-
}

lib/css-transform.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const postcssImport = require('postcss-import')
99
const postcssReporter = require('postcss-reporter')
1010
const postcssSafeParser = require('postcss-safe-parser')
1111

12+
const logger = require('./logger')
13+
1214
module.exports = function ({
1315
config,
1416
entry,
@@ -45,9 +47,11 @@ module.exports = function ({
4547
mkdirp.sync(path.dirname(outfile))
4648
fs.writeFileSync(outfile, results.css)
4749
if (results.map) {
48-
fs.writeFile(`${outfile}.map`, results.map, handleErr)
50+
fs.writeFileSync(`${outfile}.map`, results.map)
51+
}
52+
if (watch) {
53+
logger.log(`updated ${outfile}`)
4954
}
50-
console.log(`updated css file: ${outfile}`)
5155
}
5256
return results
5357
})
@@ -93,9 +97,3 @@ function getUrl (value) {
9397
const url = match[3]
9498
return url
9599
}
96-
97-
function handleErr (err) {
98-
if (err) {
99-
console.error(err.stack)
100-
}
101-
}

lib/flyle.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const mkdirp = require('mkdirp')
66
const path = require('path')
77
const parse = require('url').parse
88

9+
const logger = require('./logger')
10+
911
const DEFAULT_CACHE_DIRECTORY = `${process.env.HOME}/.flyle`
1012
const DEFAULT_PNG = path.resolve(__dirname, '../mastarm.png')
1113

@@ -43,7 +45,7 @@ module.exports = function (req, res) {
4345
}
4446

4547
function logAndSend ({err, res}) {
46-
console.error('flyle >> sending default image: ', err.message)
48+
logger.error('flyle >> sending default image: ', err.message)
4749
sendImg({
4850
path: DEFAULT_PNG,
4951
res

lib/js-build.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const fs = require('fs')
2+
const exorcist = require('exorcist')
3+
const mkdirp = require('mkdirp')
4+
const path = require('path')
5+
6+
const browserify = require('./browserify')
7+
const logger = require('./logger')
8+
9+
/**
10+
*
11+
* @return Promise
12+
*/
13+
14+
module.exports = function buildJs ({config, entry, env, minify, outfile, watch}) {
15+
const pipeline = minify
16+
? browserify.minify({config, entry, env})
17+
: browserify({config, entry, env})
18+
const bundle = () => new Promise((resolve, reject) => {
19+
if (outfile) {
20+
mkdirp.sync(path.dirname(outfile))
21+
pipeline.bundle()
22+
.pipe(exorcist(`${outfile}.map`))
23+
.pipe(fs.createWriteStream(outfile))
24+
.on('error', reject)
25+
.on('finish', resolve)
26+
} else {
27+
pipeline.bundle((err, buf) => {
28+
if (err) reject(err)
29+
else resolve(buf)
30+
})
31+
}
32+
})
33+
34+
if (watch) {
35+
pipeline.plugin(require('watchify'), {poll: true})
36+
pipeline.plugin(require('errorify'))
37+
pipeline.on('update', bundle)
38+
pipeline.on('log', logger.log)
39+
}
40+
41+
return bundle()
42+
}

0 commit comments

Comments
 (0)