Skip to content

Commit

Permalink
Merge pull request #164 from conveyal/dev
Browse files Browse the repository at this point in the history
v3.7.2
  • Loading branch information
trevorgerhardt authored Apr 4, 2017
2 parents 191aaf6 + 4b27cde commit 5f44987
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 202 deletions.
2 changes: 1 addition & 1 deletion __tests__/bin/mastarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('mastarm cli', () => {
exec(`node ${mastarm} build ${mockDir}/index.js:${buildDir}/index.js ${mockDir}/index.css:${buildDir}/index.css`,
(err, stdout, stderr) => {
expect(err).toBeNull()
expect(stdout).toContain('updated css file')
expect(stdout).toContain('done building')
expect(stderr).toBe('')
expect(fs.existsSync(`${buildDir}/index.js`)).toBeTruthy()
expect(fs.existsSync(`${buildDir}/index.css`)).toBeTruthy()
Expand Down
29 changes: 19 additions & 10 deletions __tests__/lib/push-to-s3.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
/* global describe, it */

const BUILD_DIR = '__tests__/test-utils/tmp'
const MOCK_DIR = '__tests__/test-utils/mocks'
const fs = require('fs')
const path = require('path')

const BUILD_DIR = path.join(process.cwd(), '__tests__/test-utils/tmp')
const MOCK_DIR = path.join(process.cwd(), '__tests__/test-utils/mocks')
const files = [
[`${MOCK_DIR}/index.js`, `${BUILD_DIR}/index.js`],
[`${MOCK_DIR}/index.css`, `${BUILD_DIR}/index.css`]
]

describe('lib > push to s3', () => {
const configPush = require('../../lib/push-to-s3')
const build = require('../../lib/build')
const createPushToS3 = require('../../lib/push-to-s3')
const loadConfig = require('../../lib/load-config')
const createLogger = require('../../lib/logger')

it('should compile JavaScript and CSS and send to s3 via aws-sdk', () => {
const config = loadConfig(process.cwd(), 'configurations/default', 'development')
const push = configPush({
const push = createPushToS3({
env: 'development',
config,
log: createLogger(),
minify: false,
s3bucket: 'test-bucket'
})
return Promise.all([
push([`${MOCK_DIR}/index.js`, `${BUILD_DIR}/index.js`]),
push([`${MOCK_DIR}/index.css`, `${BUILD_DIR}/index.css`])
])
return build({
config,
env: 'development',
files
}).then(() =>
Promise.all(files.map((f) =>
push({body: fs.readFileSync(f[0]), outfile: f[0]}))))
})
})
83 changes: 54 additions & 29 deletions bin/mastarm
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env node

const commander = require('commander')
const fs = require('fs')
const path = require('path')

const loadConfig = require('../lib/load-config')
const logger = require('../lib/logger')
const util = require('../lib/util')

commander
Expand Down Expand Up @@ -42,11 +44,11 @@ commander
const build = require('../lib/build')
build(opts)
.then((results) => {
console.log('done building...')
logger.log('done building...')
if (!watch) process.exit(0)
})
.catch((err) => {
console.error(err)
logger.error(err)
if (!watch) process.exit(1)
})
}
Expand Down Expand Up @@ -75,52 +77,69 @@ commander
.action(function (entries, options) {
const commit = require('this-commit')()
const username = require('username')
const createLogger = require('../lib/logger')

const build = require('../lib/build')
const pkg = require('../lib/pkg')
const pushToS3 = require('../lib/push-to-s3')
const createPushToS3 = require('../lib/push-to-s3')

const url = pkg.repository.url.replace('.git', '')
const tag = `<${url}/commit/${commit}|${pkg.name}@${commit.slice(0, 6)}>:`
const user = username.sync()
const tag = `<${url}/commit/${commit}|${pkg.name}@${commit.slice(0, 6)}>`
const config = loadConfig(process.cwd(), commander.config, commander.env)
const get = util.makeGetFn([options, commander, config.settings])

const env = get('env') || 'development'
const s3bucket = get('s3bucket')
if (config.env.SLACK_WEBHOOK && config.env.SLACK_WEBHOOK.length > 0) {
logger.logToSlack({
channel: config.env.SLACK_CHANNEL || '#devops',
webhook: config.env.SLACK_WEBHOOK
})
}

const files = util.parseEntries([...entries, ...(get('entries') || [])])
util.assertEntriesExist(files)
const sourceFiles = files.map((f) => f[0])
const outfiles = [
...files.map((f) => f[1]),
...files.map((f) => `${f[1]}.map`)
]

const log = createLogger({channel: config.env.SLACK_CHANNEL || '#devops', webhook: config.env.SLACK_WEBHOOK})
const env = get('env') || 'development'
const minify = get('minify')
const cloudfront = get('cloudfront')
const push = pushToS3({
cloudfront,
const buildOpts = {
config,
env,
log,
minify,
s3bucket,
tag
files,
minify
}
const cloudfront = get('cloudfront')
const s3bucket = get('s3bucket')

const pushToS3 = createPushToS3({
cloudfront,
s3bucket
})

log(
`:construction: *${tag} deploy started by <@${user}>*
logger.log(
`:construction: *deploying: ${tag} by <@${username.sync()}>*
:cloud: *cloudfront:* ${cloudfront}
:hash: *commit:* ${commit}
:seedling: *env:* ${env}
:compression: *minify:* ${minify}
:package: *s3bucket:* ${s3bucket}`
).then(() => {
Promise
.all(files.map(push))
:package: *s3bucket:* ${s3bucket}
:hammer_and_wrench: *building:* ${sourceFiles.join(', ')}`
).then(() =>
build(buildOpts)
.then(() =>
log(`:rocket: ${tag} deploy finished!! :tada: :confetti_ball: :tada:`)
logger.log(`:rocket: *uploading:* ${sourceFiles.length * 2} file(s)`))
.then(() =>
Promise.all(outfiles.map((outfile) =>
readFile(outfile).then((body) =>
pushToS3({body, outfile})))))
.then(() =>
logger.log(`:tada: :confetti_ball: :tada: *deploy ${tag} complete* :tada: :confetti_ball: :tada:`)
.then(() => process.exit(0)))
.catch((err) =>
log(`:rotating_light: *error deploying ${tag} ${err.message}*`)
.then(() => process.exit(1)))
})
logger.log(`:rotating_light: *${tag} error deploying ${tag} ${err.message || err}*`)
.then(() => process.exit(1))))
})

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

if (errors.length > 0) {
console.log(`${errors.length} missing messages`)
logger.error(`${errors.length} missing messages`)
for (const [message, file, line] of errors) {
console.log(`${file} line ${line}: ${message} is not defined`)
logger.error(`${file} line ${line}: ${message} is not defined`)
}

process.exit(1)
} else {
console.log('No missing messages found! 💃')
logger.log('No missing messages found! 💃')
}
})

Expand Down Expand Up @@ -212,3 +231,9 @@ commander
})

commander.parse(process.argv)

const readFile = (f) =>
new Promise((resolve, reject) =>
fs.readFile(f, (err, data) => err
? reject(err)
: resolve(data)))
14 changes: 8 additions & 6 deletions lib/browserify.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ const uglifyify = require('uglifyify')

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

module.exports = function ({
module.exports = browserifyIt

function browserifyIt ({
config,
entry,
env,
minify
}) {
const pipeline = browserify(entry, {
return browserify(entry, {
basedir: process.cwd(),
cache: {},
debug: true,
Expand All @@ -22,10 +24,10 @@ module.exports = function ({
],
transform: transform({config, env})
})
}

if (minify) {
pipeline.transform(uglifyify, {global: true})
}

module.exports.minify = function (opts) {
const pipeline = browserifyIt(opts)
pipeline.transform(uglifyify, {global: true})
return pipeline
}
5 changes: 3 additions & 2 deletions lib/budo.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const budo = require('budo')
const path = require('path')

const transformJs = require('./js-transform')
const transformCss = require('./css-transform')
const logger = require('./logger')
const transformJs = require('./js-transform')

module.exports = function ({
config,
Expand Down Expand Up @@ -55,6 +56,6 @@ module.exports = function ({
budo
.cli(budoFiles, budoOpts)
.on('error', function (err) {
console.error(err.stack)
logger.error(err.stack)
})
}
38 changes: 1 addition & 37 deletions lib/build.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const fs = require('fs')
const exorcist = require('exorcist')
const mkdirp = require('mkdirp')
const path = require('path')

const browserify = require('./browserify')
const buildCss = require('./css-transform')
const buildJs = require('./js-build')

/**
* Takes a configuration object, array of file entries [entry, output], and other options.
Expand All @@ -24,36 +21,3 @@ module.exports = function ({
? buildCss({config, entry, outfile, watch})
: buildJs({config, entry, env, minify, outfile, watch})))
}

/**
*
* @return Promise
*/

function buildJs ({config, entry, env, minify, outfile, watch}) {
const pipeline = browserify({config, entry, env, minify})
const bundle = () => {
return new Promise((resolve, reject) => {
const stream = pipeline.bundle((err, buf) => {
if (err) reject(err)
else resolve(buf)
})

if (outfile) {
mkdirp.sync(path.dirname(outfile))
stream
.pipe(exorcist(`${outfile}.map`))
.pipe(fs.createWriteStream(outfile))
}
})
}

if (watch) {
pipeline.plugin(require('watchify'), {poll: true})
pipeline.plugin(require('errorify'))
pipeline.on('update', bundle)
pipeline.on('log', console.log)
}

return bundle()
}
14 changes: 6 additions & 8 deletions lib/css-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const postcssImport = require('postcss-import')
const postcssReporter = require('postcss-reporter')
const postcssSafeParser = require('postcss-safe-parser')

const logger = require('./logger')

module.exports = function ({
config,
entry,
Expand Down Expand Up @@ -45,9 +47,11 @@ module.exports = function ({
mkdirp.sync(path.dirname(outfile))
fs.writeFileSync(outfile, results.css)
if (results.map) {
fs.writeFile(`${outfile}.map`, results.map, handleErr)
fs.writeFileSync(`${outfile}.map`, results.map)
}
if (watch) {
logger.log(`updated ${outfile}`)
}
console.log(`updated css file: ${outfile}`)
}
return results
})
Expand Down Expand Up @@ -93,9 +97,3 @@ function getUrl (value) {
const url = match[3]
return url
}

function handleErr (err) {
if (err) {
console.error(err.stack)
}
}
4 changes: 3 additions & 1 deletion lib/flyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const mkdirp = require('mkdirp')
const path = require('path')
const parse = require('url').parse

const logger = require('./logger')

const DEFAULT_CACHE_DIRECTORY = `${process.env.HOME}/.flyle`
const DEFAULT_PNG = path.resolve(__dirname, '../mastarm.png')

Expand Down Expand Up @@ -43,7 +45,7 @@ module.exports = function (req, res) {
}

function logAndSend ({err, res}) {
console.error('flyle >> sending default image: ', err.message)
logger.error('flyle >> sending default image: ', err.message)
sendImg({
path: DEFAULT_PNG,
res
Expand Down
42 changes: 42 additions & 0 deletions lib/js-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const fs = require('fs')
const exorcist = require('exorcist')
const mkdirp = require('mkdirp')
const path = require('path')

const browserify = require('./browserify')
const logger = require('./logger')

/**
*
* @return Promise
*/

module.exports = function buildJs ({config, entry, env, minify, outfile, watch}) {
const pipeline = minify
? browserify.minify({config, entry, env})
: browserify({config, entry, env})
const bundle = () => new Promise((resolve, reject) => {
if (outfile) {
mkdirp.sync(path.dirname(outfile))
pipeline.bundle()
.pipe(exorcist(`${outfile}.map`))
.pipe(fs.createWriteStream(outfile))
.on('error', reject)
.on('finish', resolve)
} else {
pipeline.bundle((err, buf) => {
if (err) reject(err)
else resolve(buf)
})
}
})

if (watch) {
pipeline.plugin(require('watchify'), {poll: true})
pipeline.plugin(require('errorify'))
pipeline.on('update', bundle)
pipeline.on('log', logger.log)
}

return bundle()
}
Loading

0 comments on commit 5f44987

Please sign in to comment.