Skip to content

Commit

Permalink
feat(messages): add message-format for better messaging
Browse files Browse the repository at this point in the history
Because we care about people who use this thing.
  • Loading branch information
Kent C. Dodds committed Jan 29, 2017
1 parent 33d3f9c commit 608dee8
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 29 deletions.
13 changes: 5 additions & 8 deletions __mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
module.exports = {
readFile: jest.fn((filePath, encoding, callback) => {
callback(null, `console.log('this is a mock thing for file: ${filePath}')`)
}),
writeFile: jest.fn((filePath, contents, callback) => {
callback(null)
}),
}
module.exports = {readFile: jest.fn((filePath, encoding, callback) => {
callback(null, `console.log('this is a mock thing for file: ${filePath}')`)
}), writeFile: jest.fn((filePath, contents, callback) => {
callback(null)
})}
7 changes: 3 additions & 4 deletions cli-test/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ test(`prettier-eslint ${writeCommand}`, async () => {
pReadFile(example1Path, 'utf8'),
pReadFile(example2Path, 'utf8'),
])
expect({
example1Result,
example2Result,
}).toMatchSnapshot(`file contents: prettier-eslint ${writeCommand}`)
expect({example1Result, example2Result}).toMatchSnapshot(
`file contents: prettier-eslint ${writeCommand}`,
)
await Promise.all([pUnlink(example1Path), pUnlink(example2Path)])
})

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"find-up": "^2.1.0",
"get-stdin": "^5.0.1",
"glob": "^7.1.1",
"messageformat": "^1.0.2",
"prettier-eslint": "^3.0.0",
"rxjs": "^5.0.3",
"yargs": "^6.6.0"
Expand Down
26 changes: 18 additions & 8 deletions src/format-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Rx from 'rxjs/Rx'
import format from 'prettier-eslint'
import chalk from 'chalk'
import getStdin from 'get-stdin'
import * as messages from './messages'

const rxGlob = Rx.Observable.bindNodeCallback(glob)
const rxReadFile = Rx.Observable.bindNodeCallback(fs.readFile)
Expand Down Expand Up @@ -95,22 +96,31 @@ async function formatFilesFromGlobs(

function onComplete() {
if (successes.length) {
const count = chalk.bold(successes.length)
const success = chalk.green('success')
console.log(
`${success} formatting ${count} files with prettier-eslint`,
messages.success({
success: chalk.green('success'),
count: successes.length,
countString: chalk.bold(successes.length),
}),
)
}
if (failures.length) {
const count = chalk.bold(failures.length)
const failure = chalk.red('failure')
console.log(
`${failure} formatting ${count} files with prettier-eslint`,
messages.failure({
failure: chalk.red('failure'),
count: failures.length,
countString: chalk.bold(failures.length),
}),
)
}
if (unchanged.length) {
const count = chalk.bold(unchanged.length)
console.log(`${count} files were ${chalk.gray('unchanged')}`)
console.log(
messages.unchanged({
unchanged: chalk.gray('unchanged'),
count: unchanged.length,
countString: chalk.bold(unchanged.length),
}),
)
}
resolve({successes, failures})
}
Expand Down
43 changes: 43 additions & 0 deletions src/message.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*eslint import/namespace: [2, { allowComputed: true }]*/
import * as messages from './messages'

const tests = {
success: [
{
input: {success: 'success', count: 1, countString: '1String'},
output: 'success formatting 1String file with prettier-eslint',
},
{
input: {success: 'success', count: 3, countString: '3String'},
output: 'success formatting 3String files with prettier-eslint',
},
],
failure: [
{
input: {failure: 'failure', count: 1, countString: '1String'},
output: 'failure formatting 1String file with prettier-eslint',
},
{
input: {failure: 'failure', count: 3, countString: '3String'},
output: 'failure formatting 3String files with prettier-eslint',
},
],
unchanged: [
{
input: {unchanged: 'unchanged', count: 1, countString: '1String'},
output: '1String file was unchanged',
},
{
input: {unchanged: 'unchanged', count: 3, countString: '3String'},
output: '3String files were unchanged',
},
],
}

Object.keys(tests).forEach(messageKey => {
tests[messageKey].forEach(({input, output}) => {
test(`${messageKey} ${JSON.stringify(input)}`, () => {
expect(messages[messageKey](input)).toEqual(output)
})
})
})
24 changes: 24 additions & 0 deletions src/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import MessageFormat from 'messageformat'

const mf = new MessageFormat('en')

export {success, failure, unchanged}

function success(data) {
const files = `{count, plural, one{file} other{files}}`
return mf.compile(
`{success} formatting {countString} ${files} with prettier-eslint`,
)(data)
}

function failure(data) {
const files = `{count, plural, one{file} other{files}}`
return mf.compile(
`{failure} formatting {countString} ${files} with prettier-eslint`,
)(data)
}

function unchanged(data) {
const files = `{count, plural, one{file was} other{files were}}`
return mf.compile(`{countString} ${files} {unchanged}`)(data)
}
45 changes: 36 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2285,6 +2285,17 @@ glob@7.1.1, glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.1.1:
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@~7.0.6:
version "7.0.6"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.0.6.tgz#211bafaf49e525b8cd93260d14ab136152b3f57a"
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^3.0.2"
once "^1.3.0"
path-is-absolute "^1.0.0"

global-modules@^0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/global-modules/-/global-modules-0.2.3.tgz#ea5a3bed42c6d6ce995a4f8a1269b5dae223828d"
Expand Down Expand Up @@ -3331,6 +3342,12 @@ loud-rejection@^1.0.0:
currently-unhandled "^0.4.1"
signal-exit "^3.0.0"

make-plural@~3.0.6:
version "3.0.6"
resolved "https://registry.yarnpkg.com/make-plural/-/make-plural-3.0.6.tgz#2033a03bac290b8f3bb91258f65b9df7e8b01ca7"
optionalDependencies:
minimist "^1.2.0"

makeerror@1.0.x:
version "1.0.11"
resolved "https://registry.yarnpkg.com/makeerror/-/makeerror-1.0.11.tgz#e01a5c9109f2af79660e4e8b9587790184f5a96c"
Expand Down Expand Up @@ -3393,6 +3410,20 @@ merge@^1.1.3, merge@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/merge/-/merge-1.2.0.tgz#7531e39d4949c281a66b8c5a6e0265e8b05894da"

messageformat-parser@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/messageformat-parser/-/messageformat-parser-1.0.0.tgz#3dea21419f141c50026a29d4b2239203ad613d3f"

messageformat@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/messageformat/-/messageformat-1.0.2.tgz#908f4691f29ff28dae35c45436a24cff93402388"
dependencies:
glob "~7.0.6"
make-plural "~3.0.6"
messageformat-parser "^1.0.0"
nopt "~3.0.6"
reserved-words "^0.1.1"

micromatch@^2.1.5, micromatch@^2.3.11, micromatch@^2.3.7:
version "2.3.11"
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-2.3.11.tgz#86677c97d1720b363431d04d0d15293bd38c1565"
Expand Down Expand Up @@ -4300,6 +4331,10 @@ require-uncached@^1.0.2:
caller-path "^0.1.0"
resolve-from "^1.0.0"

reserved-words@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/reserved-words/-/reserved-words-0.1.1.tgz#6f7c15e5e5614c50da961630da46addc87c0cef2"

resolve-dir@^0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/resolve-dir/-/resolve-dir-0.1.1.tgz#b219259a5602fac5c5c496ad894a6e8cc430261e"
Expand Down Expand Up @@ -4476,22 +4511,14 @@ shell-escape@0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/shell-escape/-/shell-escape-0.2.0.tgz#68fd025eb0490b4f567a027f0bf22480b5f84133"

shelljs@0.7.5:
shelljs@0.7.5, shelljs@^0.7.5:
version "0.7.5"
resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.7.5.tgz#2eef7a50a21e1ccf37da00df767ec69e30ad0675"
dependencies:
glob "^7.0.0"
interpret "^1.0.0"
rechoir "^0.6.2"

shelljs@^0.7.5:
version "0.7.6"
resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.7.6.tgz#379cccfb56b91c8601e4793356eb5382924de9ad"
dependencies:
glob "^7.0.0"
interpret "^1.0.0"
rechoir "^0.6.2"

shellwords@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.0.tgz#66afd47b6a12932d9071cbfd98a52e785cd0ba14"
Expand Down

0 comments on commit 608dee8

Please sign in to comment.