Skip to content

Commit f3b4fbc

Browse files
committed
it works on hello-world!
1 parent 00781df commit f3b4fbc

1 file changed

Lines changed: 71 additions & 2 deletions

File tree

all/img-previews/index.mts

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ for (const { path, image } of projects) {
9898
// console.log(path, repoName, repoBranch)
9999
}
100100

101+
const newBranch = `feat/add-img-previews-${Date.now()}`
101102
const repoPath = 'all/img-previews/repo/'
102103
for (const [repo, paths] of Object.entries(repos)) {
103104
const [name, branch] = repo.split('#')
@@ -110,11 +111,79 @@ for (const [repo, paths] of Object.entries(repos)) {
110111
await exec(
111112
`git clone --branch ${branch} --depth 1 https://github.com/SheepTester/${name}.git ${repoPath}`
112113
)
113-
await Promise.all(
114+
await exec(`git checkout -b ${newBranch}`, { cwd: repoPath })
115+
const results = await Promise.all(
114116
paths.map(async ({ filePath, imageUrl }) => {
115117
const fullPath = join(repoPath, filePath)
116118
const html = await fs.readFile(fullPath, 'utf-8')
117-
console.log(fullPath, html.length)
119+
if (html.includes('og:image')) {
120+
// console.warn(`! file already has og:image: ${filePath}`)
121+
return false
122+
}
123+
124+
const match =
125+
html.match(
126+
/[ \t]*<meta\s+name=(?:"description"|'description'|description)\s*content=(?:"[^"]+"|'[^']+'|[^/>]+)\s*\/?>\r?\n/
127+
) ??
128+
html.match(
129+
/[ \t]*<meta\s+name=(?:"[^"]+"|'[^']+'|[^>]+\s)\s*content=(?:"[^"]+"|'[^']+'|[^/>]+)\s*\/?>\r?\n/
130+
)
131+
if (!match) {
132+
console.error(
133+
`!!! unable to find existing meta tag in ${name} ${filePath}`
134+
)
135+
return false
136+
}
137+
138+
await fs.writeFile(
139+
fullPath,
140+
html.slice(0, match.index) +
141+
match[0] +
142+
match[0]
143+
.replace('name=', 'property=')
144+
.replace(
145+
/property=(?:"([^"]+)"|'([^']+)'|([^>]+)\s)/,
146+
(_, g1, g2, g3) =>
147+
g1 !== undefined
148+
? 'property="og:image"'
149+
: g2 !== undefined
150+
? "property='og:image'"
151+
: g3 !== undefined
152+
? 'property=og:image'
153+
: console.error(
154+
`!!! cannot set og:image in ${fullPath}`
155+
) ?? '???'
156+
)
157+
.replace(
158+
/content=(?:"([^"]+)"|'([^']+)'|([^/>]+))/,
159+
(_, g1, g2, g3) =>
160+
g1 !== undefined
161+
? `content="${imageUrl}"`
162+
: g2 !== undefined
163+
? `content='${imageUrl}'`
164+
: g3 !== undefined
165+
? `content=${imageUrl}`
166+
: console.error(
167+
`!!! cannot set image url in ${fullPath}`
168+
) ?? '???'
169+
) +
170+
html.slice((match.index ?? 0) + match[0].length)
171+
)
172+
return true
118173
})
119174
)
175+
if (!results.includes(true)) {
176+
console.warn(`! no files changed in ${name}`)
177+
continue
178+
}
179+
await exec(
180+
`git commit -am "feat: Add Open Graph images\n\nAutomated with https://github.com/SheepTester/sheeptester.github.io/blob/master/all/img-previews/index.mts"`,
181+
{ cwd: repoPath }
182+
)
183+
await exec(`git push origin ${newBranch}`, { cwd: repoPath })
184+
const { stdout: prUrl } = await exec(
185+
`gh pr create --head ${newBranch} --title "Add Open Graph images" --body "Automated with https://github.com/SheepTester/sheeptester.github.io/blob/master/all/img-previews/index.mts"`,
186+
{ cwd: repoPath }
187+
)
188+
console.log(`> ${prUrl.trim()}`)
120189
}

0 commit comments

Comments
 (0)