|
| 1 | +import {task} from 'gulp'; |
| 2 | +import {join, relative} from 'path'; |
| 3 | +import {readFileSync} from 'fs'; |
| 4 | +import {bold, red, green} from 'chalk'; |
| 5 | +import * as ts from 'typescript'; |
| 6 | +import * as tsutils from 'tsutils'; |
| 7 | + |
| 8 | +// Current version from the package.json. Splits it on the dash to ignore `-beta.x` suffixes. |
| 9 | +const packageVersion = require(join(process.cwd(), 'package.json')).version.split('-')[0]; |
| 10 | + |
| 11 | +// Regex used to extract versions from a string. |
| 12 | +const versionRegex = /\d+\.\d+\.\d+/; |
| 13 | + |
| 14 | +/** |
| 15 | + * Goes through all of the TypeScript files in the project and puts |
| 16 | + * together a summary of all of the pending and expired breaking changes. |
| 17 | + */ |
| 18 | +task('breaking-changes', () => { |
| 19 | + const cwd = process.cwd(); |
| 20 | + const configFile = ts.readJsonConfigFile(join(cwd, 'tsconfig.json'), ts.sys.readFile); |
| 21 | + const parsedConfig = ts.parseJsonSourceFileConfigFileContent(configFile, ts.sys, cwd); |
| 22 | + const summary: {[version: string]: string[]} = {}; |
| 23 | + |
| 24 | + // Go through all the TS files in the project. |
| 25 | + parsedConfig.fileNames.forEach(fileName => { |
| 26 | + const sourceFile = ts.createSourceFile(fileName, readFileSync(fileName, 'utf8'), |
| 27 | + configFile.languageVersion); |
| 28 | + const lineRanges = tsutils.getLineRanges(sourceFile); |
| 29 | + |
| 30 | + // Go through each of the comments of the file. |
| 31 | + tsutils.forEachComment(sourceFile, (file, range) => { |
| 32 | + const comment = file.substring(range.pos, range.end); |
| 33 | + const versionMatch = comment.match(versionRegex); |
| 34 | + |
| 35 | + // Don't do any extra work if the comment doesn't indicate a breaking change. |
| 36 | + if (!versionMatch || comment.indexOf('@breaking-change') === -1) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + // Use a path relative to the project root, in order to make the summary more tidy. |
| 41 | + // Also replace escaped Windows slashes with regular forward slashes. |
| 42 | + const pathInProject = relative(cwd, sourceFile.fileName).replace(/\\/g, '/'); |
| 43 | + const [version] = versionMatch; |
| 44 | + |
| 45 | + summary[version] = summary[version] || []; |
| 46 | + summary[version].push(` ${pathInProject}: ${formatMessage(comment, range, lineRanges)}`); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + // Go through the summary and log out all of the breaking changes. |
| 51 | + Object.keys(summary).forEach(version => { |
| 52 | + const isExpired = hasExpired(packageVersion, version); |
| 53 | + const status = isExpired ? red('(expired)') : green('(not expired)'); |
| 54 | + const header = bold(`Breaking changes for ${version} ${status}:`); |
| 55 | + const messages = summary[version].join('\n'); |
| 56 | + |
| 57 | + console.log(isExpired ? red(header) : header); |
| 58 | + console.log(isExpired ? red(messages) : messages, '\n'); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +/** |
| 63 | + * Formats a message to be logged out in the breaking changes summary. |
| 64 | + * @param comment Contents of the comment that contains the breaking change. |
| 65 | + * @param commentRange Object containing info on the position of the comment in the file. |
| 66 | + * @param lines Ranges of the lines of code in the file. |
| 67 | + */ |
| 68 | +function formatMessage(comment: string, commentRange: ts.CommentRange, lines: tsutils.LineRange[]) { |
| 69 | + const lineNumber = lines.findIndex(line => line.pos > commentRange.pos); |
| 70 | + const messageMatch = comment.match(/@deprecated(.*)|@breaking-change(.*)/); |
| 71 | + const message = messageMatch ? messageMatch[0] : ''; |
| 72 | + const cleanMessage = message |
| 73 | + .replace(/[\*\/\r\n]|@[\w-]+/g, '') |
| 74 | + .replace(versionRegex, '') |
| 75 | + .trim(); |
| 76 | + |
| 77 | + return `Line ${lineNumber}, ${cleanMessage || 'No message'}`; |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +/** Converts a version string into an object. */ |
| 82 | +function parseVersion(version: string) { |
| 83 | + const [major = 0, minor = 0, patch = 0] = version.split('.').map(segment => parseInt(segment)); |
| 84 | + return {major, minor, patch}; |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +/** |
| 89 | + * Checks whether a version has expired, based on the current version. |
| 90 | + * @param currentVersion Current version of the package. |
| 91 | + * @param breakingChange Version that is being checked. |
| 92 | + */ |
| 93 | +function hasExpired(currentVersion: string, breakingChange: string) { |
| 94 | + if (currentVersion === breakingChange) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + const current = parseVersion(currentVersion); |
| 99 | + const target = parseVersion(breakingChange); |
| 100 | + |
| 101 | + return target.major < current.major || |
| 102 | + (target.major === current.major && target.minor < current.minor) || |
| 103 | + ( |
| 104 | + target.major === current.major && |
| 105 | + target.minor === current.minor && |
| 106 | + target.patch < current.patch |
| 107 | + ); |
| 108 | +} |
0 commit comments