-
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 passing custom config, add mocha reporter, stay silent in cas…
…e of continuous integration environment
- Loading branch information
Showing
17 changed files
with
180 additions
and
89 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,5 +1,5 @@ | ||
#!/usr/bin/env node | ||
|
||
var run = require('stricter').default; | ||
var run = require('stricter').cli; | ||
var result = run(); | ||
process.exit(result); |
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
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,5 +1,23 @@ | ||
// This is not the actual CLI deploye with the app. | ||
// The sole purpose of the file is to help debugging. | ||
let run = require('.').default; | ||
let result = run(); | ||
process.exit(result); | ||
import * as program from 'commander'; | ||
import * as isCi from 'is-ci'; | ||
import stricter from './stricter'; | ||
|
||
export default (): number => { | ||
program | ||
.version(process.env.STRICTER_VERSION as string) | ||
.option('-c, --config <path>', 'specify config location') | ||
.option( | ||
'-r, --reporter <console|mocha>', | ||
'specify reporter', | ||
/^(console|mocha)$/i, | ||
'console', | ||
) | ||
.parse(process.argv); | ||
const result = stricter({ | ||
configPath: program.config, | ||
reporter: program.reporter, | ||
silent: isCi, | ||
}); | ||
|
||
return result; | ||
}; |
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
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,5 @@ | ||
// This is not the actual CLI deploye with the app. | ||
// The sole purpose of the file is to help debugging. | ||
let run = require('.').cli; | ||
let result = run(); | ||
process.exit(result); |
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,28 +1,2 @@ | ||
import { getConfig } from './config'; | ||
import { getRuleDefinitions, getRuleApplications, filterFilesToProcess } from './rule'; | ||
import { applyProjectRules, readDependencies, readFilesData } from './processor'; | ||
import { consoleLogger } from './logger'; | ||
import { listFiles } from './utils'; | ||
|
||
export default (): number => { | ||
console.log('Stricter: Checking...'); | ||
const config = getConfig(); | ||
|
||
const fileList = listFiles(config.root); | ||
|
||
const ruleDefinitions = getRuleDefinitions(config); | ||
const ruleApplications = getRuleApplications(config, ruleDefinitions); | ||
const filesToProcess = filterFilesToProcess(config.root, fileList, ruleApplications); | ||
|
||
const filesData = readFilesData(filesToProcess); | ||
const dependencies = readDependencies(filesData, config); | ||
const projectResult = applyProjectRules(config.root, filesData, dependencies, ruleApplications); | ||
|
||
const result = consoleLogger(projectResult); | ||
|
||
if (result === 0) { | ||
console.log('Stricter: No errors'); | ||
} | ||
|
||
return result; | ||
}; | ||
export { default as stricter } from './stricter'; | ||
export { default as cli } from './cli'; |
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
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,20 +1,7 @@ | ||
import chalk from 'chalk'; | ||
import { RuleToRuleApplicationResult } from './../types'; | ||
import logToConsole from './console'; | ||
import { compactProjectLogs } from './flatten'; | ||
import { LogEntry } from './../types'; | ||
|
||
export const consoleLogger = (projectResult: RuleToRuleApplicationResult): number => { | ||
const projectLogs = compactProjectLogs(projectResult); | ||
|
||
if (projectLogs.length) { | ||
console.log(chalk.bgBlackBright('Project')); | ||
logToConsole(projectLogs); | ||
} | ||
|
||
const errorCount = Object.values(projectLogs).reduce( | ||
(acc, i) => acc + ((i.errors && i.errors.length) || 0), | ||
0, | ||
); | ||
|
||
return errorCount; | ||
}; | ||
export { default as consoleLogger } from './console'; | ||
export { default as mochaLogger } from './mocha'; | ||
export { compactProjectLogs } from './flatten'; | ||
export const getErrorCount = (projectLogs: LogEntry[]) => | ||
Object.values(projectLogs).reduce((acc, i) => acc + ((i.errors && i.errors.length) || 0), 0); |
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,47 @@ | ||
import * as fs from 'fs'; | ||
import { LogEntry } from './../types'; | ||
|
||
const reportFileName = 'stricter.json'; | ||
|
||
const encode = (str: string) => { | ||
const substitutions = { | ||
'&:': '&', | ||
'"': '"', | ||
"'": ''', | ||
'<': '<', | ||
'>': '>', | ||
}; | ||
|
||
const result = Object.entries(substitutions).reduce((acc, [original, substitution]) => { | ||
return acc.replace(new RegExp(original, 'g'), substitution); | ||
}, str); | ||
|
||
return result; | ||
}; | ||
|
||
export default (logs: LogEntry[]): void => { | ||
const now = new Date(); | ||
const failuresCount = logs.reduce((acc, i) => acc + ((i.errors && i.errors.length) || 0), 0); | ||
|
||
const report = { | ||
stats: { | ||
tests: failuresCount, | ||
passes: 0, | ||
failures: failuresCount, | ||
duration: 0, | ||
start: now, | ||
end: now, | ||
}, | ||
failures: logs.map(log => ({ | ||
title: log.rule, | ||
fullTitle: log.rule, | ||
duration: 0, | ||
errorCount: (log.errors && log.errors.length) || 0, | ||
error: log.errors && log.errors.map(i => encode(i)).join('\n'), | ||
})), | ||
passes: [], | ||
skipped: [], | ||
}; | ||
|
||
fs.writeFileSync(reportFileName, JSON.stringify(report, null, 2), 'utf-8'); | ||
}; |
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,48 @@ | ||
import { getConfig } from './config'; | ||
import { getRuleDefinitions, getRuleApplications, filterFilesToProcess } from './rule'; | ||
import { applyProjectRules, readDependencies, readFilesData } from './processor'; | ||
import { consoleLogger, mochaLogger, compactProjectLogs, getErrorCount } from './logger'; | ||
import { listFiles } from './utils'; | ||
import { StricterArguments, Reporter } from './types'; | ||
|
||
export default ({ | ||
silent = false, | ||
reporter = Reporter.CONSOLE, | ||
configPath, | ||
}: StricterArguments): number => { | ||
if (!silent) { | ||
console.log('Stricter: Checking...'); | ||
} | ||
|
||
const config = getConfig(configPath); | ||
|
||
const fileList = listFiles(config.root); | ||
|
||
const ruleDefinitions = getRuleDefinitions(config); | ||
const ruleApplications = getRuleApplications(config, ruleDefinitions); | ||
const filesToProcess = filterFilesToProcess(config.root, fileList, ruleApplications); | ||
|
||
const filesData = readFilesData(filesToProcess); | ||
const dependencies = readDependencies(filesData, config); | ||
const projectResult = applyProjectRules(config.root, filesData, dependencies, ruleApplications); | ||
|
||
const logs = compactProjectLogs(projectResult); | ||
|
||
if (reporter === Reporter.MOCHA) { | ||
mochaLogger(logs); | ||
} else { | ||
consoleLogger(logs); | ||
} | ||
|
||
const result = getErrorCount(logs); | ||
|
||
if (!silent) { | ||
if (result === 0) { | ||
console.log('Stricter: No errors'); | ||
} else { | ||
console.log(`Stricter: ${result} error${result > 1 ? 's' : ''}`); | ||
} | ||
} | ||
|
||
return result; | ||
}; |
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,3 +1,7 @@ | ||
declare module 'cosmiconfig'; // does not exist | ||
declare module 'babylon'; // existing libdef misses plugins we use | ||
declare module '@babel/traverse'; // existing libdef misses plugins we use | ||
declare module 'is-ci' { | ||
var isCi: boolean; | ||
export = isCi; | ||
} |
Oops, something went wrong.