Skip to content

Commit

Permalink
Support for passing configPath and coverageSummaryPath
Browse files Browse the repository at this point in the history
closes #1 closes #2
  • Loading branch information
Koleok committed May 24, 2017
1 parent 261c523 commit 4107eba
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 90 deletions.
16 changes: 7 additions & 9 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ module.exports = {
functions: 'ignore',
},
],
semi: ['error', 'never'],
'func-names': 0,
'global-require': 0,
'import/no-dynamic-require': 0,
'max-len': ['error', { code: 80, ignoreComments: true }],
'new-cap': 0,
'newline-per-chained-call': ['error', { ignoreChainWithDepth: 2 }],
'no-confusing-arrow': 0,
'no-console': 0,
'func-names': 0,
'import/extensions': 0,
'no-mixed-operators': 0,
'no-confusing-arrow': 0,
'import/no-extraneous-dependencies': 0,
'import/prefer-default-export': 0,
'import/no-dynamic-require': 0,
'new-cap': 0,
'no-underscore-dangle': 0,
semi: ['error', 'never'],
},
};
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
coverage
39 changes: 1 addition & 38 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,39 +1,2 @@
#!/usr/bin/env node
const R = require('ramda')
const F = require('fluture')
const { bold, green } = require('chalk')

const {
getNewThresholds,
ratchetThresholds,
writePackage,
thresholdLens,
formatJson,
} = require('./utils')

const projectDir = process.cwd()
const packagePath = `${projectDir}/package.json`
const packageJson = require(packagePath)
const existingThresholds = R.view(thresholdLens, packageJson)
const coverage = require(`${projectDir}/coverage/coverage-summary.json`)

const logResult = R.tap(x =>
console.log(`
${bold('new coverage thresholds:')}
${green(formatJson(x))}
`)
)

const program = x =>
F.of(x)
.map(getNewThresholds)
.map(R.pair(existingThresholds))
.map(ratchetThresholds)
.map(logResult)
.map(R.set(thresholdLens, R.__, packageJson))
.map(formatJson)
.chain(writePackage(packagePath))

program(coverage).fork(console.error, () =>
console.log(bold('coverage thresholds ratcheted 🔧'))
)
require('./src')
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
"ratchet"
],
"scripts": {
"local": "node .",
"jest": "./node_modules/.bin/jest",
"test": "yarn jest",
"test:watch": "yarn jest -- --watch",
"lint": "./node_modules/.bin/eslint . --ext .js --ext .jsx",
"lint:staged": "./node_modules/.bin/lint-staged",
"lint:errors": "yarn lint -- --quiet",
"lint:fix": "yarn lint -- --fix",
"format": "./node_modules/.bin/prettier-eslint \"*(both|client|server)/**/*.js\" --write",
"format": "./node_modules/.bin/prettier-eslint \"*src/**/*.js\" --write",
"prepush": "yarn test",
"precommit": "yarn lint:staged"
},
Expand All @@ -41,9 +42,11 @@
"babel-eslint": "^7.2.3",
"babel-jest": "^20.0.3",
"chalk": "^1.1.3",
"cliquer": "^0.1.5",
"fluture": "^6.1.0",
"jest": "^20.0.3",
"ramda": "^0.23.0"
"ramda": "^0.23.0",
"yargs": "^8.0.1"
},
"devDependencies": {
"babel-preset-es2015": "^6.24.1",
Expand Down
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { argv } = require('yargs')
const { resolve } = require('path')
const { logError, logSuccess } = require('./utils')
const program = require('./program')

const defaultCoveragePath = 'coverage/coverage-summary.json'
const defaultConfigPath = 'package.json'

const projectDir = process.cwd()
const coveragePath = resolve(projectDir, argv.coverage || defaultCoveragePath)
const configPath = resolve(projectDir, argv.config || defaultConfigPath)

program([coveragePath, configPath]).fork(logError, logSuccess)
33 changes: 33 additions & 0 deletions src/program.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const R = require('ramda')
const F = require('fluture')

const {
formatJson,
getCurrentThresholdsFromConfig,
getNewThresholdsFromSummary,
getPackageJson,
getPackageJsonPath,
logResult,
ratchetThresholds,
requireF,
thresholdLens,
writeFileF,
} = require('./utils')

module.exports = R.pipe(
F.of,
R.chain(([coveragePath, configPath]) =>
F.both(
requireF(coveragePath).map(getNewThresholdsFromSummary),
requireF(configPath).map(getCurrentThresholdsFromConfig)
)
),
R.map(ratchetThresholds),
R.map(logResult),
R.chain(newThresholds =>
getPackageJson().map(R.set(thresholdLens, newThresholds))
),
R.map(formatJson),
R.chain(thresholds => F.both(getPackageJsonPath(), F.of(thresholds))),
R.chain(R.apply(writeFileF))
)
88 changes: 88 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const fs = require('fs')
const R = require('ramda')
const F = require('fluture')
const { resolve } = require('path')
const { bold, green, red } = require('chalk')

// formatJson :: Object -> String
const formatJson = x => JSON.stringify(x, null, 2)

// log :: Object -> Object
const log = R.tap(x => console.log(formatJson(x)))

const logResult = R.tap(x =>
console.log(`
${bold('new coverage thresholds:')}
${green(formatJson(x))}
`)
)

const logError = err =>
console.error(`
${red.bold('There was a problem ratcheting thresholds')}
=========================================
${red(err)}
`)

const logSuccess = () => console.log(bold('coverage thresholds ratcheted 🔧'))

// getNewThresholdsFromSummary :: Object -> Object
const getNewThresholdsFromSummary = R.compose(
R.fromPairs,
R.map(R.over(R.lensIndex(1), R.prop('pct'))),
R.toPairs,
R.prop('total')
)

// takeMaxValues :: [(String, String)] -> Object
const takeMaxValues = R.reduce(
(result, [key, val]) =>
R.merge(result, { [key]: R.max(result[key] || 0, val) }),
{}
)

const configPath = ['coverageThreshold', 'global']

// thresholdLens :: Lens
const thresholdLens = R.lensPath(['jest', ...configPath])

// ratchetThresholds :: (Object, Object) -> Object
const ratchetThresholds = R.compose(takeMaxValues, R.unnest, R.map(R.toPairs))

// String -> Future a Error
const requireF = F.encase(require)

// getConfig :: String -> Future Object Error
const getCurrentThresholdsFromConfig = R.ifElse(
R.has('jest'),
R.path(['jest', ...configPath]),
R.path(configPath)
)

// getPackageJsonPath :: () -> Future String Error
const getPackageJsonPath = () =>
F.of(process.cwd()).map(rootDir => resolve(rootDir, 'package.json'))

// getPackageJson :: () -> Future Object Error
const getPackageJson = () => getPackageJsonPath().chain(requireF)

// writePackage :: String -> Object -> Future Object Error
const writeFileF = R.curry((path, x) =>
F.node(done => fs.writeFile(path, x, 'utf8', done))
)

module.exports = {
formatJson,
getCurrentThresholdsFromConfig,
getNewThresholdsFromSummary,
getPackageJson,
getPackageJsonPath,
log,
logError,
logResult,
logSuccess,
ratchetThresholds,
requireF,
thresholdLens,
writeFileF,
}
9 changes: 3 additions & 6 deletions utils.spec.js → src/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import {
getNewThresholds,
ratchetThresholds,
} from './utils'
import { getNewThresholdsFromSummary, ratchetThresholds } from './utils'

describe('#getNewThresholds', () => {
describe('#getNewThresholdsFromSummary', () => {
const branches = 0
const functions = 2
const lines = 6
Expand All @@ -18,7 +15,7 @@ describe('#getNewThresholds', () => {
}

it('should create summary object', () => {
expect(getNewThresholds(report)).toEqual({
expect(getNewThresholdsFromSummary(report)).toEqual({
branches,
functions,
lines,
Expand Down
34 changes: 0 additions & 34 deletions utils.js

This file was deleted.

Loading

0 comments on commit 4107eba

Please sign in to comment.