MCPcopy
hub / github.com/ineo6/hosts / updateMd

Function updateMd

src/hosts.ts:115–148  ·  view source on GitHub ↗
(content: string, config: UpdateConfig)

Source from the content-addressed store, hash-verified

113}
114
115function 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
150interface IAlive {
151 time?: number;

Callers 1

saveHostsFunction · 0.85

Calls 2

hostsTplFunction · 0.85
writeHostsFunction · 0.85

Tested by

no test coverage detected