|
| 1 | +// @flow |
| 2 | + |
| 3 | +const {danger, fail, warn} = require('danger'); |
| 4 | +const fs = require('fs'); |
| 5 | + |
| 6 | +// Takes a list of file paths, and converts it into clickable links |
| 7 | +const linkableFiles = (paths: Array<string>): string => { |
| 8 | + const repoURL = danger.github.pr.head.repo.html_url; |
| 9 | + const ref = danger.github.pr.head.ref; |
| 10 | + const links = paths.map(path => { |
| 11 | + return createLink(`${repoURL}/blob/${ref}/${path}`, path); |
| 12 | + }); |
| 13 | + return toSentence(links); |
| 14 | +}; |
| 15 | + |
| 16 | +// ["1", "2", "3"] to "1, 2 and 3" |
| 17 | +const toSentence = (array: Array<string>) : string => { |
| 18 | + if (array.length === 1) { return array[0]; } |
| 19 | + return array.slice(0, array.length - 1).join(', ') + ' and ' + array.pop(); |
| 20 | +}; |
| 21 | + |
| 22 | +// ("/href/thing", "name") to "<a href="/href/thing">name</a>" |
| 23 | +const createLink = (href: string, text: string): string => |
| 24 | + `<a href='${href}'>${text}</a>`; |
| 25 | + |
| 26 | +const newJsFiles = danger.git.created_files.filter(path => path.endsWith('js')); |
| 27 | + |
| 28 | +// New JS files should have the FB copyright header + flow |
| 29 | +const facebookLicenseHeaderComponents = [ |
| 30 | + 'Copyright \(c\) .*, Facebook, Inc. All rights reserved.', |
| 31 | + 'This source code is licensed under the BSD-style license found in the', |
| 32 | + 'LICENSE file in the root directory of this source tree. An additional grant', |
| 33 | + 'of patent rights can be found in the PATENTS file in the same directory.', |
| 34 | +]; |
| 35 | + |
| 36 | +const noFBCopyrightFiles = newJsFiles.filter(filepath => { |
| 37 | + const content = fs.readFileSync(filepath).toString(); |
| 38 | + for (const line of facebookLicenseHeaderComponents) { |
| 39 | + if (!content.match(new RegExp(line))) { |
| 40 | + return true; |
| 41 | + } |
| 42 | + } |
| 43 | + return false; |
| 44 | +}); |
| 45 | + |
| 46 | +if (noFBCopyrightFiles.length > 0) { |
| 47 | + const files = linkableFiles(noFBCopyrightFiles); |
| 48 | + fail(`New JS files do not have the Facebook copyright header: ${files}`); |
| 49 | +} |
| 50 | + |
| 51 | +// Ensure the use of Flow and 'use strict'; |
| 52 | +const noFlowFiles = newJsFiles.filter(filepath => { |
| 53 | + const content = fs.readFileSync(filepath).toString(); |
| 54 | + return content.includes('@flow') && content.includes('use strict'); |
| 55 | +}); |
| 56 | + |
| 57 | +if (noFlowFiles.length > 0) { |
| 58 | + const files = linkableFiles(noFlowFiles); |
| 59 | + const flow = '<code>@flow</code>'; |
| 60 | + const strict = "<code>'use strict'</code>"; |
| 61 | + warn(`Please ensure that ${flow} and ${strict} are enabled on: ${files}`); |
| 62 | +} |
| 63 | + |
| 64 | +// No merge from master commmits |
| 65 | +// TODO: blocked by https://github.com/danger/danger-js/issues/81 |
| 66 | + |
| 67 | +// Warns if there are changes to package.json without changes to yarn.lock. |
| 68 | + |
| 69 | +const packageChanged = danger.git.modified_files.includes('package.json'); |
| 70 | +const lockfileChanged = danger.git.modified_files.includes('yarn.lock'); |
| 71 | +if (packageChanged && !lockfileChanged) { |
| 72 | + const message = 'Changes were made to package.json, but not to yarn.lock'; |
| 73 | + const idea = 'Perhaps you need to run `yarn install`?'; |
| 74 | + warn(`${message} - <i>${idea}</i>`); |
| 75 | +} |
0 commit comments