2019-12-03 17:28:59 +02:00
|
|
|
import * as fs from 'fs'
|
|
|
|
import * as os from 'os'
|
|
|
|
import * as path from 'path'
|
|
|
|
import * as yaml from 'js-yaml'
|
|
|
|
|
|
|
|
//
|
|
|
|
// SUMMARY
|
|
|
|
//
|
|
|
|
// This script rebuilds the usage section in the README.md to be consistent with the action.yml
|
|
|
|
|
|
|
|
function updateUsage(
|
|
|
|
actionReference: string,
|
2021-10-19 17:52:57 +03:00
|
|
|
actionYamlPath = 'action.yml',
|
|
|
|
readmePath = 'README.md',
|
|
|
|
startToken = '<!-- start usage -->',
|
|
|
|
endToken = '<!-- end usage -->'
|
2019-12-03 17:28:59 +02:00
|
|
|
): void {
|
|
|
|
if (!actionReference) {
|
|
|
|
throw new Error('Parameter actionReference must not be empty')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the action.yml
|
|
|
|
const actionYaml = yaml.safeLoad(fs.readFileSync(actionYamlPath).toString())
|
|
|
|
|
|
|
|
// Load the README
|
|
|
|
const originalReadme = fs.readFileSync(readmePath).toString()
|
|
|
|
|
|
|
|
// Find the start token
|
|
|
|
const startTokenIndex = originalReadme.indexOf(startToken)
|
|
|
|
if (startTokenIndex < 0) {
|
|
|
|
throw new Error(`Start token '${startToken}' not found`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the end token
|
|
|
|
const endTokenIndex = originalReadme.indexOf(endToken)
|
|
|
|
if (endTokenIndex < 0) {
|
|
|
|
throw new Error(`End token '${endToken}' not found`)
|
|
|
|
} else if (endTokenIndex < startTokenIndex) {
|
|
|
|
throw new Error('Start token must appear before end token')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the new README
|
|
|
|
const newReadme: string[] = []
|
|
|
|
|
|
|
|
// Append the beginning
|
|
|
|
newReadme.push(originalReadme.substr(0, startTokenIndex + startToken.length))
|
|
|
|
|
|
|
|
// Build the new usage section
|
|
|
|
newReadme.push('```yaml', `- uses: ${actionReference}`, ' with:')
|
|
|
|
const inputs = actionYaml.inputs
|
|
|
|
let firstInput = true
|
|
|
|
for (const key of Object.keys(inputs)) {
|
|
|
|
const input = inputs[key]
|
|
|
|
|
|
|
|
// Line break between inputs
|
|
|
|
if (!firstInput) {
|
|
|
|
newReadme.push('')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constrain the width of the description
|
|
|
|
const width = 80
|
2020-03-11 21:55:17 +02:00
|
|
|
let description = (input.description as string)
|
|
|
|
.trimRight()
|
|
|
|
.replace(/\r\n/g, '\n') // Convert CR to LF
|
|
|
|
.replace(/ +/g, ' ') // Squash consecutive spaces
|
|
|
|
.replace(/ \n/g, '\n') // Squash space followed by newline
|
2019-12-03 17:28:59 +02:00
|
|
|
while (description) {
|
|
|
|
// Longer than width? Find a space to break apart
|
|
|
|
let segment: string = description
|
|
|
|
if (description.length > width) {
|
|
|
|
segment = description.substr(0, width + 1)
|
2020-03-11 21:55:17 +02:00
|
|
|
while (!segment.endsWith(' ') && !segment.endsWith('\n') && segment) {
|
2019-12-03 17:28:59 +02:00
|
|
|
segment = segment.substr(0, segment.length - 1)
|
|
|
|
}
|
2020-01-03 19:32:17 +02:00
|
|
|
|
|
|
|
// Trimmed too much?
|
|
|
|
if (segment.length < width * 0.67) {
|
|
|
|
segment = description
|
|
|
|
}
|
2019-12-03 17:28:59 +02:00
|
|
|
} else {
|
|
|
|
segment = description
|
|
|
|
}
|
|
|
|
|
2020-03-11 21:55:17 +02:00
|
|
|
// Check for newline
|
|
|
|
const newlineIndex = segment.indexOf('\n')
|
|
|
|
if (newlineIndex >= 0) {
|
|
|
|
segment = segment.substr(0, newlineIndex + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append segment
|
|
|
|
newReadme.push(` # ${segment}`.trimRight())
|
|
|
|
|
|
|
|
// Remaining
|
|
|
|
description = description.substr(segment.length)
|
2019-12-03 17:28:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (input.default !== undefined) {
|
2020-03-11 21:55:17 +02:00
|
|
|
// Append blank line if description had paragraphs
|
|
|
|
if ((input.description as string).trimRight().match(/\n[ ]*\r?\n/)) {
|
|
|
|
newReadme.push(` #`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default
|
2019-12-03 17:28:59 +02:00
|
|
|
newReadme.push(` # Default: ${input.default}`)
|
|
|
|
}
|
2020-03-11 21:55:17 +02:00
|
|
|
|
|
|
|
// Input name
|
2019-12-03 17:28:59 +02:00
|
|
|
newReadme.push(` ${key}: ''`)
|
|
|
|
|
|
|
|
firstInput = false
|
|
|
|
}
|
|
|
|
|
|
|
|
newReadme.push('```')
|
|
|
|
|
|
|
|
// Append the end
|
|
|
|
newReadme.push(originalReadme.substr(endTokenIndex))
|
|
|
|
|
|
|
|
// Write the new README
|
|
|
|
fs.writeFileSync(readmePath, newReadme.join(os.EOL))
|
|
|
|
}
|
|
|
|
|
|
|
|
updateUsage(
|
2022-03-01 19:46:45 +02:00
|
|
|
'actions/checkout@v3',
|
2019-12-03 17:28:59 +02:00
|
|
|
path.join(__dirname, '..', '..', 'action.yml'),
|
|
|
|
path.join(__dirname, '..', '..', 'README.md')
|
|
|
|
)
|