|
|
|
|
|
const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor'); |
|
|
const fs = require('fs'); |
|
|
const path = require('path'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const API_EXTRACTOR_YARN_COMMAND = 'pnpm api-extractor:build'; |
|
|
|
|
|
const config = loadExtractorConfig(); |
|
|
checkReportMatchesApi(config); |
|
|
checkNoErrorsAndWarnings(config); |
|
|
checkLineEndings(config); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function checkReportMatchesApi(config) { |
|
|
const result = Extractor.invoke(config, { |
|
|
localBuild: false, |
|
|
messageCallback: (message) => { |
|
|
|
|
|
message.handled = true; |
|
|
}, |
|
|
}); |
|
|
|
|
|
if (result.apiReportChanged) { |
|
|
exit( |
|
|
`The API Extractor report does not match the exported API.`, |
|
|
`Please run \`${API_EXTRACTOR_YARN_COMMAND}\` to generate an`, |
|
|
`updated report and commit it.`, |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function checkNoErrorsAndWarnings(config) { |
|
|
const result = Extractor.invoke(config, { |
|
|
localBuild: false, |
|
|
}); |
|
|
|
|
|
if (!result.succeeded) { |
|
|
exit( |
|
|
`API Extractor completed with ${result.errorCount} errors and`, |
|
|
`${result.warningCount} warnings.`, |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function checkLineEndings(config) { |
|
|
const report = fs.readFileSync(config.reportFilePath); |
|
|
|
|
|
const LF = '\n'; |
|
|
const CRLF = '\r\n'; |
|
|
|
|
|
const containsLf = report.includes(LF); |
|
|
const containsCrLf = report.includes(CRLF); |
|
|
|
|
|
const relativeReportPath = path.relative( |
|
|
process.cwd(), |
|
|
config.reportFilePath, |
|
|
); |
|
|
|
|
|
if (config.newlineKind === LF && containsCrLf) { |
|
|
exit( |
|
|
`${relativeReportPath} contains CRLF.`, |
|
|
`Please convert its line endings to LF.`, |
|
|
); |
|
|
} |
|
|
if (config.newlineKind === CRLF && containsLf && !containsCrLf) { |
|
|
exit( |
|
|
`${relativeReportPath} contains LF.`, |
|
|
`Please convert its line endings to CRLF.`, |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function loadExtractorConfig() { |
|
|
const rawConfig = ExtractorConfig.tryLoadForFolder({ |
|
|
startingFolder: process.cwd(), |
|
|
}); |
|
|
if (!rawConfig) { |
|
|
exit( |
|
|
`No API Extractor config could be found for the`, |
|
|
`current working directory.`, |
|
|
); |
|
|
} |
|
|
return ExtractorConfig.prepare(rawConfig); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function red(message) { |
|
|
return `\u001b[31m${message}\u001b[0m`; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function exit(...message) { |
|
|
|
|
|
console.log(`${red('FAILURE REASON')} ${message.join(' ')}`); |
|
|
process.exit(1); |
|
|
} |
|
|
|