(content: string, config: UpdateConfig)
| 113 | } |
| 114 | |
| 115 | function updateMd(content: string, config: UpdateConfig) { |
| 116 | let previousHosts = ''; |
| 117 | |
| 118 | try { |
| 119 | previousHosts = fs.readFileSync(config.hostDest, 'utf-8'); |
| 120 | } catch (err) { |
| 121 | } |
| 122 | |
| 123 | const regMatch = previousHosts.match(/GitHub Host Start([\s\S]*)# Update at/); |
| 124 | const previousHostRule = regMatch ? regMatch[1].trim() : ''; |
| 125 | |
| 126 | const needUpdate = previousHostRule !== content; |
| 127 | |
| 128 | if (needUpdate) { |
| 129 | console.log('发现新的Hosts,即将更新文件!'); |
| 130 | |
| 131 | const updateTime = dayjs() |
| 132 | .tz('Asia/Shanghai') |
| 133 | .format('YYYY-MM-DD HH:mm:ss'); |
| 134 | |
| 135 | const nextHosts = hostsTpl(content, updateTime); |
| 136 | |
| 137 | if (config.mdTpl && config.mdDest) { |
| 138 | const template = fs.readFileSync(config.mdTpl, 'utf-8'); |
| 139 | |
| 140 | const replacedMdContent = template.replace('{hostContent}', nextHosts) |
| 141 | .replace('{update_time}', updateTime); |
| 142 | |
| 143 | fs.writeFileSync(config.mdDest, replacedMdContent); |
| 144 | } |
| 145 | |
| 146 | writeHosts(config.hostDest, nextHosts); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | interface IAlive { |
| 151 | time?: number; |
no test coverage detected