-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for passing configPath and coverageSummaryPath
- Loading branch information
Showing
10 changed files
with
265 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.