Skip to content

Commit 7b7a974

Browse files
authored
Merge pull request #70 from conveyal/dev
v1.3.0
2 parents 21b4dfa + 7ad6af9 commit 7b7a974

24 files changed

+265
-278
lines changed

.travis.yml

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ sudo: false
22
language: node_js
33
cache:
44
directories:
5-
- node_modules
5+
- ~/.yarn-cache
66
notifications:
77
email: false
88
node_js:
99
- '6'
1010
before_install:
11-
- npm i -g npm@^3.0.0
11+
- npm i -g codecov npm@^3 yarn
1212
install:
13-
- npm i
14-
- npm install -g codecov
15-
before_script:
16-
- npm prune
13+
- yarn
1714
script:
1815
- npm run cover
1916
- codecov

README.md

+24-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77

88
<p align="center">Conveyal's front-end JavaScript tool-belt. Build, run, lint and deploy front-end code.</p>
99

10+
## Table of Contents
11+
12+
* [Install](#install)
13+
* [Configuration](#configuration)
14+
* [CLI Usage](#cli-usage)
15+
* [Build](#build)
16+
* [Commit](#commit)
17+
* [Deploy](#deploy)
18+
* [Lint](#lint)
19+
* [Test](#test)
20+
* [React Utils](#react-utils)
21+
1022
## Install
1123

1224
With [node v6+ and npm 3+ installed](https://nodejs.org/en/download/current/):
@@ -31,7 +43,7 @@ This file should contain string messages to be used throughout the application.
3143

3244
Settings contain both Mastarm configuration settings and per environment settings to be used in the application. To override base settings, create an environments section in the `yml` file. [Example in Modeify](https://github.com/conveyal/modeify/blob/master/configurations/example/settings.yml#L40). Each section below will contain the settings that they can use.
3345

34-
## Usage
46+
## CLI Usage
3547

3648
Not all options pertain to all commands. Entries are in the format `input/file.js:output/file.js`.
3749

@@ -139,15 +151,20 @@ Run tests using Jest
139151

140152
Options:
141153

142-
-h, --help output usage information
143-
-u, --update-snapshots Force update of snapshots. USE WITH CAUTION.
144-
--coverage Run Jest with coverage reporting
145-
--no-cache Run Jest with cache
146-
--coverage-paths <paths> Extra paths to collect code coverage from
147-
--setup-files <paths> Setup files to run before each test
154+
-h, --help output usage information
155+
-u, --update-snapshots Force update of snapshots. USE WITH CAUTION.
156+
--coverage Run Jest with coverage reporting
157+
--no-cache Run Jest without cache (defaults to using cache)
158+
--coverage-paths <paths> Extra paths to collect code coverage from
159+
--setup-files <paths> Setup files to run before each test
160+
--test-path-ignore-patterns <patterns> File patterns to ignore when scanning for test files
148161

149162
```
150163

164+
## React Utils
165+
166+
Documentation coming soon
167+
151168
[npm-image]: https://img.shields.io/npm/v/mastarm.svg?maxAge=2592000&style=flat-square
152169
[npm-url]: https://www.npmjs.com/package/mastarm
153170
[travis-image]: https://img.shields.io/travis/conveyal/mastarm.svg?style=flat-square

tests/bin/mastarm.test.js __tests__/bin/mastarm.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const path = require('path')
66

77
const mastarm = path.resolve('./bin/mastarm')
88

9-
const util = require('../util.js')
9+
const util = require('../test-utils/util.js')
1010

1111
describe('mastarm cli', () => {
1212
it('should display usage with no args', (done) => {
@@ -21,17 +21,19 @@ describe('mastarm cli', () => {
2121
describe('build', function () {
2222
const buildFilePattern = 'built-bin-build'
2323
const cleanFn = util.makeCleanBuiltFilesFn(buildFilePattern)
24+
const mockDir = util.mockDir
25+
2426
beforeEach(cleanFn)
2527
afterEach(cleanFn)
2628

2729
it('should build a project', (done) => {
28-
exec(`node ${mastarm} build tests/mocks/mockComponent.js:tests/mocks/${buildFilePattern}.js tests/mocks/mock.css:tests/mocks/${buildFilePattern}.css`,
30+
exec(`node ${mastarm} build ${mockDir}/mockComponent.js:${mockDir}/${buildFilePattern}.js ${mockDir}/mock.css:${mockDir}/${buildFilePattern}.css`,
2931
(err, stdout, stderr) => {
3032
expect(err).toBeNull()
3133
expect(stdout).toBe('')
3234
expect(stderr).toBe('')
33-
expect(fs.existsSync(`tests/mocks/${buildFilePattern}.js`)).toBeTruthy()
34-
expect(fs.existsSync(`tests/mocks/${buildFilePattern}.css`)).toBeTruthy()
35+
expect(fs.existsSync(`${mockDir}/${buildFilePattern}.js`)).toBeTruthy()
36+
expect(fs.existsSync(`${mockDir}/${buildFilePattern}.css`)).toBeTruthy()
3537
done()
3638
}
3739
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
exports[`load-config should work without any config files present 1`] = `
2+
Object {
3+
"env": Object {},
4+
"environment": "test",
5+
"messages": Object {},
6+
"settings": Object {},
7+
"store": Object {},
8+
}
9+
`;

tests/lib/__snapshots__/test.test.js.snap __tests__/lib/__snapshots__/test.js.snap

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ Object {
2222
"setupFiles": Array [
2323
"beforeTestsSetup.js",
2424
],
25+
"testPathIgnorePatterns": Array [
26+
"<rootDir>/node_modules/",
27+
"<rootDir>/__tests__/test-utils",
28+
],
2529
}
2630
`;
27-
28-
exports[`test.js setupTestEnvironment should set MESSAGES environment variable if needed 1`] = `"{\"hi\":\"there\"}"`;

tests/lib/__snapshots__/util.test.js.snap __tests__/lib/__snapshots__/util.js.snap

+3-10
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,14 @@ exports[`util.js parseEntries should not find any paths if no matches 1`] = `Arr
33
exports[`util.js parseEntries should return inputted file paths 1`] = `
44
Array [
55
Array [
6-
"tests/mocks/mock.css",
7-
"assets/tests/mocks/mock.css",
6+
"__tests__/test-utils/mocks/mock.css",
7+
"assets/__tests__/test-utils/mocks/mock.css",
88
],
99
Array [
10-
"tests/mocks/mockComponent.js",
10+
"__tests__/test-utils/mocks/mockComponent.js",
1111
"blah",
1212
],
1313
]
1414
`;
1515

1616
exports[`util.js parseEntries should try to find default file paths 1`] = `Array []`;
17-
18-
exports[`util.js updateDependencies should run check-dependencies package 1`] = `
19-
Object {
20-
"depsWereOk": true,
21-
"status": 0,
22-
}
23-
`;

tests/lib/build.test.js __tests__/lib/build.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,29 @@ const fs = require('fs')
33

44
const build = require('../../lib/build')
55

6-
const util = require('../util.js')
6+
const util = require('../test-utils/util.js')
77

88
describe('build', () => {
99
const buildFilePattern = 'built-build'
1010
const cleanFn = util.makeCleanBuiltFilesFn(buildFilePattern)
11+
const mockDir = util.mockDir
12+
1113
beforeEach(cleanFn)
1214
afterEach(cleanFn)
1315

1416
it('should transform js and css', (done) => {
1517
build({
1618
config: {},
1719
files: [
18-
['tests/mocks/mockComponent.js', `tests/mocks/${buildFilePattern}.js`],
19-
['tests/mocks/mock.css', `tests/mocks/${buildFilePattern}.css`]
20+
[`${mockDir}/mockComponent.js`, `${mockDir}/${buildFilePattern}.js`],
21+
[`${mockDir}/mock.css`, `${mockDir}/${buildFilePattern}.css`]
2022
]
2123
})
2224

2325
// css transform happens asynchronously, so wait 1 second before checking for file
2426
setTimeout(() => {
25-
expect(fs.existsSync(`tests/mocks/${buildFilePattern}.js`)).toBeTruthy()
26-
expect(fs.existsSync(`tests/mocks/${buildFilePattern}.css`)).toBeTruthy()
27+
expect(fs.existsSync(`${mockDir}/${buildFilePattern}.js`)).toBeTruthy()
28+
expect(fs.existsSync(`${mockDir}/${buildFilePattern}.css`)).toBeTruthy()
2729
done()
2830
}, 1000)
2931
})

__tests__/lib/load-config.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* globals describe, expect, it */
2+
3+
const loadConfig = require('../../lib/load-config')
4+
5+
describe('load-config', () => {
6+
it('should work without any config files present', () => {
7+
const config = loadConfig(process.cwd(), '~/mastarm/configurations/default', 'test')
8+
expect(config.path).toContain('mastarm/configurations/default')
9+
delete config.path
10+
expect(config).toMatchSnapshot()
11+
})
12+
})

tests/lib/test.test.js __tests__/lib/test.js

-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,4 @@ describe('test.js', () => {
1818
delete jestCfg.scriptPreprocessor
1919
expect(jestCfg).toMatchSnapshot()
2020
})
21-
22-
it('setupTestEnvironment should set MESSAGES environment variable if needed', () => {
23-
testUtils.setupTestEnvironment({ messages: { hi: 'there' } })
24-
expect(process.env.MESSAGES).toMatchSnapshot()
25-
})
2621
})

__tests__/lib/util.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* globals describe, expect, it, jest */
2+
3+
jest.mock('../../lib/pkg', () => { return {} })
4+
5+
const util = require('../../lib/util')
6+
const testUtils = require('../test-utils/util')
7+
const mockDir = testUtils.mockDir
8+
9+
describe('util.js', () => {
10+
it('configureEnvironment should set process.env vars', () => {
11+
const env2 = 'mock-env2'
12+
const messages = { hi: 'there' }
13+
const settings = { someSetting: true }
14+
const store = {}
15+
const config = {
16+
env: 'mock-env',
17+
path: '/made/up/path',
18+
messages,
19+
settings,
20+
store
21+
}
22+
util.configureEnvironment({ config, env: env2 })
23+
expect(process.env.NODE_ENV).toEqual(env2)
24+
expect(JSON.parse(process.env.MESSAGES)).toEqual(messages)
25+
expect(JSON.parse(process.env.SETTINGS)).toEqual(settings)
26+
expect(JSON.parse(process.env.STORE)).toEqual(store)
27+
})
28+
29+
it('makeGetFn should find the right value', () => {
30+
expect(util.makeGetFn([{ a: 1 }, { a: 2 }])('a')).toEqual(1)
31+
})
32+
33+
describe('parseEntries', () => {
34+
const get = () => null
35+
it('should try to find default file paths', () => {
36+
const result = util.parseEntries([], get)
37+
expect(result.length).toBe(0)
38+
expect(result).toMatchSnapshot()
39+
})
40+
41+
it('should not find any paths if no matches', () => {
42+
const result = util.parseEntries(['fileDoesNotExist:blah'], get)
43+
expect(result.length).toBe(0)
44+
expect(result).toMatchSnapshot()
45+
})
46+
47+
it('should return inputted file paths', () => {
48+
const result = util.parseEntries([`${mockDir}/mock.css`, `${mockDir}/mockComponent.js:blah`], get)
49+
expect(result.length).toBe(2)
50+
expect(result).toMatchSnapshot()
51+
})
52+
})
53+
54+
it('popMastarmFromArgv should remove the mastarm command', () => {
55+
const argv1 = process.argv[1]
56+
expect(argv1).toContain('mastarm')
57+
util.popMastarmFromArgv()
58+
expect(process.argv[1]).not.toBe(argv1)
59+
})
60+
})
File renamed without changes.
File renamed without changes.

__tests__/test-utils/util.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const rimraf = require('rimraf')
2+
3+
const mockDir = '__tests__/test-utils/mocks'
4+
5+
exports.mockDir = mockDir
6+
7+
exports.makeCleanBuiltFilesFn = (filePattern) => {
8+
return (done) => {
9+
rimraf(`${mockDir}/${filePattern}*`, done)
10+
}
11+
}

bin/mastarm

+10-34
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ commander
1111
.option('-c, --config <path>', 'Path to configuration files.', path.join(process.cwd(), '/configurations/default'))
1212
.option('-e, --env <environment>', 'Environment to use.', process.env.NODE_ENV || 'development')
1313
.option('-m, --minify', 'Minify built files.')
14-
.option('-S, --skip-check-dependencies', 'Skip checking and installing out of date package.json dependencies.')
14+
.option('-S, --skip-check-dependencies', '**DEPRECATED**')
1515

1616
commander
1717
.command('build [entries...]')
@@ -21,7 +21,6 @@ commander
2121
.option('-s, --serve', 'Serve with budo. Auto-matically rebuilds on changes.')
2222
.option('-w, --watch', 'Automatically rebuild on changes.')
2323
.action(function (entries, options) {
24-
checkDependencies()
2524
const config = loadConfig(process.cwd(), commander.config, commander.env)
2625
const get = util.makeGetFn([options, commander, config.settings])
2726
const files = util.parseEntries(entries, get)
@@ -70,7 +69,6 @@ commander
7069
.option('--cloudfront', 'CloudFront Distribution ID to invalidate.')
7170
.option('--s3bucket', 'S3 Bucket to push to.')
7271
.action(function (entries, options) {
73-
checkDependencies()
7472
const pushToS3 = require('../lib/push-to-s3')
7573
const config = loadConfig(process.cwd(), commander.config, commander.env)
7674
const get = util.makeGetFn([options, commander, config.settings])
@@ -107,25 +105,15 @@ commander
107105
commander
108106
.command('prepublish [entries...]')
109107
.description('Transpile JavaScript down to ES5 with Babel')
110-
.action(function (entries) {
111-
checkDependencies()
108+
.action(function (entries, options) {
112109
const babel = require('babel-core')
113110
const fs = require('fs')
114111
const glob = require('glob')
115112
const mkdirp = require('mkdirp')
113+
const babelConfig = require('../lib/babel-config')
114+
const config = loadConfig(process.cwd(), commander.config, commander.env)
115+
const get = util.makeGetFn([options, commander, config.settings])
116116
const outdir = path.join(process.cwd(), 'build')
117-
const options = {
118-
presets: [
119-
require('babel-preset-es2015'),
120-
require('babel-preset-react'),
121-
require('babel-preset-stage-0')
122-
],
123-
plugins: [
124-
require('babel-plugin-add-module-exports'),
125-
require('babel-plugin-transform-runtime')
126-
],
127-
sourceMaps: true
128-
}
129117
entries.forEach((entry) => {
130118
if (fs.statSync(entry).isDirectory()) {
131119
transformDir(entry)
@@ -137,7 +125,7 @@ commander
137125
const filepath = path.join(outdir, filename)
138126
const results = babel.transform(
139127
fs.readFileSync(filename, 'utf8'),
140-
Object.assign({}, options, {filename})
128+
Object.assign({}, babelConfig(get('env')), {filename, sourceMaps: true})
141129
)
142130
mkdirp.sync(path.dirname(filepath))
143131
fs.writeFileSync(filepath, results.code + '\n//# sourceMappingURL=' + path.basename(filepath))
@@ -156,29 +144,17 @@ commander
156144
.description('Run tests using Jest')
157145
.option('-u, --update-snapshots', 'Force update of snapshots. USE WITH CAUTION.')
158146
.option('--coverage', 'Run Jest with coverage reporting')
159-
.option('--no-cache', 'Run Jest with cache')
147+
.option('--no-cache', 'Run Jest without cache (defaults to using cache)')
160148
.option('--coverage-paths <paths>', 'Extra paths to collect code coverage from')
161149
.option('--setup-files <paths>', 'Setup files to run before each test')
150+
.option('--test-path-ignore-patterns <patterns>', 'File patterns to ignore when scanning for test files')
162151
.action(function (patterns, options) {
163-
checkDependencies()
164152
const jest = require('jest')
165153
const config = loadConfig(process.cwd(), commander.config, commander.env)
154+
const get = util.makeGetFn([options, commander, config.settings])
166155
const testUtils = require('../lib/test')
167-
testUtils.setupTestEnvironment(config)
156+
util.configureEnvironment({config, env: get('env')})
168157
jest.run(testUtils.generateTestConfig(patterns, options))
169158
})
170159

171160
commander.parse(process.argv)
172-
173-
function checkDependencies () {
174-
if (!commander.skipCheckDependencies) {
175-
const results = util.updateDependencies()
176-
if (results.status !== 0) {
177-
console.error(results.error)
178-
process.exit(results.status)
179-
} else if (!results.depsWereOk) {
180-
console.log('Updated out of date dependencies found in package.json. Please try running the command again.')
181-
process.exit(results.status)
182-
}
183-
}
184-
}

0 commit comments

Comments
 (0)