Last active
March 3, 2020 23:56
-
-
Save manakuro/0627f8e93b93d5535f460c7677a6648b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// dangerfile.ts | |
//... | |
const createLink = (href: string, text: string): string => | |
`<a href='${href}'>${text}</a>`; | |
const toLinkList = (files: string[]): string => { | |
const repoURL = danger.github.pr.head.repo.html_url; | |
const ref = danger.github.pr.head.ref; | |
return files | |
.map(f => createLink(`${repoURL}/blob/${ref}/${f}`, f)) | |
.map(a => `- ${a}`) | |
.join('\n'); | |
}; | |
const isAppFile = (file: string) => /^(?!.*\.d\.ts).*?\.(ts|js|tsx|jsx)$/.test(file); | |
const isOnlyFiles = (file: string) => fs.existsSync(file) && fs.lstatSync(file).isFile() | |
const modifiedOrCreatedFiles = [ | |
...danger.git.modified_files, | |
...danger.git.created_files, | |
] | |
.filter((p: string) => p.includes('src/')) | |
.filter((p: string) => isOnlyFiles(p) && isAppFile(p)); | |
const untestedFiles = modifiedOrCreatedFiles | |
.filter(m => !/(test|spec|snap)/.test(m)) | |
.map(file => ({ | |
file, | |
testFile: `${path.basename(file, path.extname(file))}.test${path.extname(file)}`, | |
})) | |
.filter(m => !modifiedOrCreatedFiles.find(f => f.includes(m.testFile))); | |
const hasAppChanges = modifiedOrCreatedFiles.length; | |
const hasUntestedFiles = untestedFiles.length; | |
if (hasAppChanges && hasUntestedFiles) { | |
const list = toLinkList(untestedFiles.map(u => u.file)); | |
warn( | |
'App files should get test files' + | |
`\n\n${list}` | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment