81 lines
2.1 KiB
TypeScript
81 lines
2.1 KiB
TypeScript
import { readdirSync, readFileSync, writeFileSync } from 'fs'
|
|
import { UserScriptMetaFull } from './types'
|
|
|
|
export interface readmeData {
|
|
meta: UserScriptMetaFull
|
|
error: string | null
|
|
}
|
|
|
|
export function updateReadmeFile(fileList: readmeData[]) {
|
|
let readmeFile = getReadmeFileName()
|
|
if (readmeFile !== null) {
|
|
let [readmeStart, readmeEnd] = readReadmeFile(readmeFile)
|
|
|
|
let installLinks = fileList.map(readmeDataToString).join('\n')
|
|
|
|
let installLinksAll = `
|
|
<!-- START INSTALL LINKS -->
|
|
## Installs
|
|
|
|
${installLinks}
|
|
<!-- END INSTALL LINKS -->
|
|
`
|
|
|
|
let content = [readmeStart, installLinksAll, readmeEnd].join('\n')
|
|
writeFileSync(readmeFile, content)
|
|
}
|
|
}
|
|
|
|
function readmeDataToString(readmeData: readmeData): string {
|
|
let { meta, error } = readmeData
|
|
let errStr = error !== null ? '~~' : ''
|
|
let errMsg = error !== null ? `\n - ${error}` : ''
|
|
return `- ${errStr}[${meta.name}](${meta.downloadURL})})${errStr}${errMsg}`
|
|
}
|
|
|
|
function getReadmeFileName(): string | null {
|
|
let files = readdirSync('.')
|
|
for (let name of files) {
|
|
if (/^readme\.md$/i.test(name)) {
|
|
return name
|
|
}
|
|
}
|
|
return null
|
|
}
|
|
|
|
function readReadmeFile(readmeFile: string): [string, string] {
|
|
const content = readFileSync(readmeFile).toString()
|
|
|
|
console.log(JSON.stringify(content))
|
|
const regex =
|
|
/\n{0,1}<!-- START INSTALL LINKS -->(?:.|\n)*?<!-- END INSTALL LINKS -->\n{0,1}/
|
|
const index = regex.exec(content)?.index
|
|
|
|
let contentPre = '',
|
|
contentPost = ''
|
|
|
|
if (index === undefined) {
|
|
contentPre = content
|
|
} else {
|
|
let content_replace = content.replace(regex, '')
|
|
contentPre = content_replace.slice(0, index)
|
|
contentPost = content_replace.slice(index)
|
|
}
|
|
|
|
if (!contentPre.endsWith('\n')) {
|
|
contentPre += '\n'
|
|
}
|
|
if (!contentPost.endsWith('\n')) {
|
|
contentPost += '\n'
|
|
}
|
|
|
|
console.log(
|
|
JSON.stringify(index),
|
|
JSON.stringify(content),
|
|
JSON.stringify(contentPre),
|
|
JSON.stringify(contentPost)
|
|
)
|
|
|
|
return [contentPre, contentPost]
|
|
}
|