Skip to content

Commit aa7b1fa

Browse files
Merge pull request #46 from conveyal/dev
Merge dev branch into master
2 parents f4880d2 + 2d2d7c2 commit aa7b1fa

21 files changed

+559
-354
lines changed

README.md

+23-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Settings contain both Mastarm configuration settings and per environment setting
3333

3434
## Usage
3535

36-
Entries are in the format `input/file.js:output/file.js`. Not all options pertain to all commands.
36+
Not all options pertain to all commands. Entries are in the format `input/file.js:output/file.js`.
3737

3838
```shell
3939
$ mastarm --help
@@ -43,9 +43,9 @@ $ mastarm --help
4343

4444
Commands:
4545

46-
build [entries...] Bundle JavaScript & CSS
46+
build [entries...] [options] Bundle JavaScript & CSS
4747
commit Force intelligent commit messages.
48-
deploy [options] [entries...] Bundle & Deploy JavaScript & CSS
48+
deploy [entries...] [options] Bundle & Deploy JavaScript & CSS
4949
lint [paths...] Lint JavaScript [& CSS coming soon!]
5050
test [options] Run tests using Jest test runner
5151

@@ -67,6 +67,26 @@ $ mastarm --help
6767

6868
Compile JS, HTML, CSS, YAML, Markdown into a single `.js`. Utilizes [babel](https://babeljs.io/), [browserify](https://github.com/substack/node-browserify), [budo](https://github.com/mattdesl/budo), and [postcss](http://postcss.org/).
6969

70+
```shell
71+
$ mastarm build [entries...] [options]
72+
73+
Options:
74+
75+
-h, --help output usage information
76+
-F, --flyle Cache and serve tiles.
77+
-p, --proxy <address> Proxy calls through to target address.
78+
-s, --serve Serve with budo. Auto-matically rebuilds on changes.
79+
-w, --watch Automatically rebuild on changes.
80+
```
81+
82+
If no entries are provided, mastarm will attempt to find the entry file. It will first see if the `entry` option has been set in the `settings.yml` config file. If that setting or the file does not exist, it will attempt to build the file specified as `main` in your project's `package.json` file. And finally, if both of those options fail, mastarm will attempt to look for the `index.js` and `index.css` file in the current working directory and attempt to compile each into `assets/index.js` and `assets/index.css` respectively.
83+
84+
If entries are provided, mastarm will build only those files.
85+
86+
#### CSS Building
87+
88+
Starting with mastarm 1.0.0, CSS builds will occur separately from the browserify build. Thus, any CSS imports into a JavaScript file will cause a build error. Instead, to build CSS file(s), you must specify the file(s) as entries in the command. Also, when running in `serve` or `watch` mode, the CSS files will get automatically rebuilt, but a manual page refresh will be necessary.
89+
7090
### `commit`
7191

7292
Utilize best practices when forming a commit message using [Commitzen](http://commitizen.github.io/cz-cli/) & the [Conventional Changelog](https://github.com/conventional-changelog/conventional-changelog) standard.

bin/mastarm

+22-47
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const commander = require('commander')
44
const path = require('path')
55

66
const loadConfig = require('../lib/load-config')
7-
const pkg = require('../lib/pkg')
7+
const util = require('../lib/util')
88

99
commander
1010
.version(require('../package.json').version)
@@ -19,35 +19,41 @@ commander
1919
.option('-F, --flyle', 'Cache and serve tiles.')
2020
.option('-p, --proxy <address>', 'Proxy calls through to target address.')
2121
.option('-s, --serve', 'Serve with budo. Auto-matically rebuilds on changes.')
22-
.option('-w, --watch', 'Rebuild on changes with watchify.')
22+
.option('-w, --watch', 'Automatically rebuild on changes.')
2323
.action(function (entries, options) {
2424
checkDependencies()
2525
const config = loadConfig(process.cwd(), commander.config, commander.env)
26-
const get = (item) => val([options, commander, config.settings], item)
27-
const files = parseEntries(entries, get)
26+
const get = util.makeGetFn([options, commander, config.settings])
27+
const files = util.parseEntries(entries, get)
28+
if (files.length === 0) {
29+
console.log('No entry point found! Did you spell the filename correctly?')
30+
process.exit(1)
31+
}
2832
if (get('serve')) {
2933
const budo = require('../lib/budo')
30-
files.map(budo({
34+
budo({
3135
config,
36+
files,
3237
flyle: get('flyle'),
3338
proxy: get('proxy')
34-
}))
39+
})
3540
} else {
3641
const build = require('../lib/build')
37-
files.map(build({
42+
build({
3843
config,
3944
env: get('env'),
45+
files,
4046
minify: get('minify'),
4147
watch: get('watch')
42-
}))
48+
})
4349
}
4450
})
4551

4652
commander
4753
.command('commit')
4854
.description('Force intelligent commit messages.')
4955
.action(function () {
50-
popMastarmFromArgv()
56+
util.popMastarmFromArgv()
5157
const path = require('path')
5258
const bootstrap = require('commitizen/dist/cli/git-cz').bootstrap
5359
bootstrap({
@@ -67,8 +73,12 @@ commander
6773
checkDependencies()
6874
const pushToS3 = require('../lib/push-to-s3')
6975
const config = loadConfig(process.cwd(), commander.config, commander.env)
70-
const get = (item) => val([options, commander, config.settings], item)
71-
const files = parseEntries(entries, get)
76+
const get = util.makeGetFn([options, commander, config.settings])
77+
const files = util.parseEntries(entries, get)
78+
if (files.length === 0) {
79+
console.log('No file(s) found! Did you spell the filename correctly?')
80+
process.exit(1)
81+
}
7282
files.map(pushToS3({
7383
cloudfront: get('cloudfront'),
7484
config,
@@ -164,11 +174,7 @@ commander.parse(process.argv)
164174

165175
function checkDependencies () {
166176
if (!commander.skipCheckDependencies) {
167-
const checkDependenciesSync = require('check-dependencies').sync
168-
const results = checkDependenciesSync({
169-
install: true,
170-
packageDir: process.cwd()
171-
})
177+
const results = util.updateDependencies()
172178
if (results.status !== 0) {
173179
console.error(results.error)
174180
process.exit(results.status)
@@ -178,34 +184,3 @@ function checkDependencies () {
178184
}
179185
}
180186
}
181-
182-
function parseEntries (entries, get) {
183-
const files = entries.map((entry) => {
184-
entry = entry.split(':')
185-
if (entry.length === 1) {
186-
entry.push(`assets/${entry[0]}`)
187-
}
188-
return entry
189-
})
190-
191-
if (files.length === 0) {
192-
files.push([
193-
get('entry') || pkg.main || 'index.js',
194-
get('outfile') || 'assets/index.js'
195-
])
196-
}
197-
198-
return files
199-
}
200-
201-
/**
202-
* A lot of subcommands utilize exact argument lengths. Pop mastarm to handle it.
203-
*/
204-
function popMastarmFromArgv () {
205-
process.argv = process.argv.slice(0, 1).concat(process.argv.slice(2))
206-
}
207-
208-
function val (targets, i) {
209-
const ts = targets.filter((t) => t[i] !== undefined)
210-
if (ts.length > 0) return ts[0][i]
211-
}

lib/browserify.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ const browserify = require('browserify')
22
const path = require('path')
33
const uglifyify = require('uglifyify')
44

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

77
module.exports = function ({
88
config,
99
entry,
1010
env,
11-
minify,
12-
outfile
11+
minify
1312
}) {
1413
const pipeline = browserify(entry, {
1514
basedir: process.cwd(),
@@ -22,8 +21,7 @@ module.exports = function ({
2221
],
2322
transform: transform({
2423
config,
25-
env,
26-
outfile
24+
env
2725
})
2826
})
2927
.on('error', function (err) {

lib/budo.js

+62-49
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,76 @@
11
const budo = require('budo')
22
const path = require('path')
33

4-
const transform = require('./transform')
4+
const transformJs = require('./js-transform')
5+
const transformCss = require('./css-transform')
56

67
module.exports = function ({
78
config,
9+
files,
810
flyle,
911
proxy
1012
}) {
11-
return function ([entry, outfile]) {
12-
const budoOpts = {
13-
browserify: {
14-
debug: true,
15-
paths: [
16-
path.join(process.cwd(), '/node_modules'),
17-
path.join(__dirname, '/../node_modules')
18-
],
19-
transform: transform({
20-
config,
21-
env: 'development',
22-
outfile
23-
})
24-
},
25-
cors: true,
26-
middleware: [],
27-
pushstate: true
28-
}
29-
if (proxy) {
30-
const httpProxy = require('http-proxy')
31-
const proxyServer = httpProxy.createProxyServer({target: proxy})
32-
proxyServer.on('error', function (err, req, res) {
33-
console.error(err.stack)
34-
res.writeHead(500, {'Content-Type': 'text/plain'})
35-
res.end(err.message)
36-
})
37-
budoOpts.middleware.push(function (req, res, next) {
38-
if (req.url.indexOf('/api') === 0) {
39-
req.url = req.url.slice(4)
40-
proxyServer.web(req, res)
41-
} else {
42-
next()
43-
}
13+
const budoOpts = {
14+
browserify: {
15+
debug: true,
16+
paths: [
17+
path.join(process.cwd(), '/node_modules'),
18+
path.join(__dirname, '/../node_modules')
19+
],
20+
transform: transformJs({
21+
config,
22+
env: 'development'
4423
})
45-
}
46-
if (flyle) {
47-
const serveTiles = require('./flyle')
48-
budoOpts.middleware.push(function (req, res, next) {
49-
if (req.url.indexOf('/tile') === 0) {
50-
serveTiles(req, res)
51-
} else {
52-
next()
53-
}
24+
},
25+
cors: true,
26+
middleware: [],
27+
pushstate: true
28+
}
29+
if (proxy) {
30+
const httpProxy = require('http-proxy')
31+
const proxyServer = httpProxy.createProxyServer({target: proxy})
32+
proxyServer.on('error', function (err, req, res) {
33+
console.error(err.stack)
34+
res.writeHead(500, {'Content-Type': 'text/plain'})
35+
res.end(err.message)
36+
})
37+
budoOpts.middleware.push(function (req, res, next) {
38+
if (req.url.indexOf('/api') === 0) {
39+
req.url = req.url.slice(4)
40+
proxyServer.web(req, res)
41+
} else {
42+
next()
43+
}
44+
})
45+
}
46+
if (flyle) {
47+
const serveTiles = require('./flyle')
48+
budoOpts.middleware.push(function (req, res, next) {
49+
if (req.url.indexOf('/tile') === 0) {
50+
serveTiles(req, res)
51+
} else {
52+
next()
53+
}
54+
})
55+
}
56+
57+
const budoFiles = []
58+
59+
files.map((file) => {
60+
if (file[1].split('.').pop() === 'css') {
61+
transformCss({
62+
filename: file[0],
63+
outfile: file[1],
64+
watch: true
5465
})
66+
} else {
67+
budoFiles.push(file.join(':'))
5568
}
69+
})
5670

57-
budo
58-
.cli([entry + ':' + outfile], budoOpts)
59-
.on('error', function (err) {
60-
console.error(err.stack)
61-
})
62-
}
71+
budo
72+
.cli(budoFiles, budoOpts)
73+
.on('error', function (err) {
74+
console.error(err.stack)
75+
})
6376
}

lib/build.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ const fs = require('fs')
22
const exorcist = require('exorcist')
33

44
const browserify = require('./browserify')
5+
const transformCss = require('./css-transform')
56

67
module.exports = function ({
78
config,
89
env,
10+
files,
911
minify,
1012
watch
1113
}) {
12-
return function ([entry, outfile]) {
14+
function buildJs ([entry, outfile]) {
1315
const pipeline = browserify({
1416
config,
1517
entry,
@@ -33,4 +35,16 @@ module.exports = function ({
3335

3436
bundle()
3537
}
38+
39+
files.map((file) => {
40+
if (file[1].split('.').pop() === 'css') {
41+
transformCss({
42+
filename: file[0],
43+
outfile: file[1],
44+
watch
45+
})
46+
} else {
47+
buildJs(file)
48+
}
49+
})
3650
}

0 commit comments

Comments
 (0)