Skip to content

Commit f0a519a

Browse files
committed
Update update_docs.ts
1 parent b209efc commit f0a519a

File tree

1 file changed

+46
-44
lines changed

1 file changed

+46
-44
lines changed

scripts/update_docs.ts

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
console.log('Starting');
1+
console.log("Starting");
22

33
console.log(`meta path: ${import.meta.path}`);
44
console.log(`meta dir: ${import.meta.dir}`);
@@ -8,72 +8,74 @@ console.log(`Bun.env.PWD: ${Bun.env.PWD}`);
88
// https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
99

1010
// import { $ } from 'bun';
11-
import { readdir } from 'node:fs/promises';
12-
import { urls, automatic, manual } from './commands.ts';
13-
14-
const branches = {
15-
sdxl: {
16-
name: 'Stable Diffusion XL',
17-
},
18-
pdxl: {
19-
name: 'Pony Diffusion XL',
20-
},
21-
};
11+
import { readdir } from "node:fs/promises";
12+
import { join } from "node:path";
13+
import { automatic, manual, urls } from "./commands.ts";
14+
15+
// const branches = {
16+
// sdxl: {
17+
// name: "Stable Diffusion XL",
18+
// },
19+
// pdxl: {
20+
// name: "Pony Diffusion XL",
21+
// },
22+
// };
2223

2324
// if (!Bun.env.GITHUB_REPOSITORY || !Bun.env.GITHUB_REF_NAME || !Bun.env.GITHUB_API_URL) {
2425
// console.log('This script must be run in a GitHub Action environment');
2526
// process.exit(1);
2627
// }
2728

28-
const repoOwner = Bun.env.GITHUB_REPOSITORY?.split('/')[0];
29-
const repoName = Bun.env.GITHUB_REPOSITORY?.split('/')[1];
30-
const branchName = Bun.env.GITHUB_REF_NAME || 'sdxl';
31-
const apiURL = Bun.env.GITHUB_API_URL;
29+
const repoOwner = Bun.env.GITHUB_REPOSITORY?.split("/")[0];
30+
const repoName = Bun.env.GITHUB_REPOSITORY?.split("/")[1];
31+
const branchName = Bun.env.GITHUB_REF_NAME || "sdxl";
32+
// const apiURL = Bun.env.GITHUB_API_URL;
3233
const rawUrl = `https://raw.githubusercontent.com/${repoOwner}/${repoName}/${branchName}/wildcards/`;
3334
const archiveUrl = `https://github.com/${repoOwner}/${repoName}/releases/latest/download/${repoName}-${branchName}.zip`;
3435

3536
const path = Bun.env.GITHUB_REPOSITORY ? Bun.env.PWD : import.meta.dir;
3637

3738
const wildcards = await readdir(`${path}/wildcards`);
38-
const sanitizedUrl = rawUrl.endsWith('/') ? rawUrl.slice(0, -1) : rawUrl;
39-
const filesList = `${wildcards.map((w) => `- [${w.split('.')[0]}](${sanitizedUrl}/${w})\n`).join('')}\n`;
39+
const sanitizedUrl = rawUrl.endsWith("/") ? rawUrl.slice(0, -1) : rawUrl;
40+
const filesList = `${wildcards.map((w) => `- [${w.split(".")[0]}](${sanitizedUrl}/${w})\n`).join("")}\n`;
4041

4142
const downloadMethod = (method: { type: string; tools: string[]; commands: string[] }) => {
42-
const header = `### Download${method.type === 'automatic' ? ' automatically' : ''} with ${method.tools.map((tool) => `[${tool.toUpperCase()}](${urls[tool]})`).join(' and ')}\n\n`;
43-
const code = `\`\`\`bash\n${method.commands.join(' && ')}\n\`\`\`\n`;
43+
const header = `### Download${method.type === "automatic" ? " automatically" : ""} with ${
44+
method.tools.map((tool) => `[${tool.toUpperCase()}](${urls[tool]})`).join(" and ")
45+
}\n\n`;
46+
const code = `\`\`\`bash\n${method.commands.join(" && ")}\n\`\`\`\n`;
4447

4548
return header + code;
4649
};
4750

4851
const wrapInDetails = (content: string) => {
49-
const parts = content.split('###');
52+
const parts = content.split("###");
5053
if (parts.length < 3) return content;
5154

5255
return (
5356
parts[0] +
54-
'###' +
57+
"###" +
5558
parts[1] +
56-
'<details>\n<summary>Show more commands</summary>\n\n###' +
57-
parts.slice(2).join('###') +
58-
'\n\nYou can find more ways to download the wildcards in [DOWNLOAD.md](docs/DOWNLOAD.md) file.\n\n</details>'
59+
"<details>\n<summary>Show more commands</summary>\n\n###" +
60+
parts.slice(2).join("###") +
61+
"\n\nYou can find more ways to download the wildcards in [DOWNLOAD.md](docs/DOWNLOAD.md) file.\n\n</details>"
5962
);
6063
};
6164

6265
// I need to use \\s in the middle of the regex because with one slash prettier removes it
63-
const emptyLinesInMarkdownLists = new RegExp('(?<=^- .*\n)\\s*\n(?=- )', 'gm');
66+
const emptyLinesInMarkdownLists = new RegExp("(?<=^- .*\n)\\s*\n(?=- )", "gm");
6467

6568
const replaceNonBranchContent = (content: string) => {
66-
const commentRegex = new RegExp('<!--[^]*?-->', 'gm');
6769
const regex = new RegExp(
68-
`<!--\\s*(?!${branchName})(\\w+)\\s*-->([\\s\\S]*?)<!--\\s*/\\1\\s*-->`,
69-
'gm',
70+
`<!-- ${branchName} -->(.*?)<!-- \/${branchName} -->`,
71+
"gms",
7072
);
71-
return content.replace(regex, '').replace(commentRegex, '');
73+
return content.replace(regex, "").replace(/\n{2,}/g, "\n");
7274
};
7375

74-
const automaticMethods = automatic.map((m) => downloadMethod(m)).join('\n');
76+
const automaticMethods = automatic.map((m) => downloadMethod(m)).join("\n");
7577

76-
const manualMethods = manual.map((m) => downloadMethod(m)).join('\n');
78+
const manualMethods = manual.map((m) => downloadMethod(m)).join("\n");
7779

7880
const docsFiles = await readdir(`${path}/src`);
7981

@@ -84,25 +86,25 @@ docsFiles.forEach(async (file) => {
8486

8587
let processedAutomaticMethods = automaticMethods;
8688

87-
if (file === 'README.md') {
89+
if (file === "README.md") {
8890
processedAutomaticMethods = wrapInDetails(automaticMethods);
8991
}
9092

9193
const processed = content
92-
.replaceAll('{{filesList}}', filesList)
93-
.replaceAll('{{archiveUrl}}', archiveUrl)
94-
.replaceAll('{{branch}}', branchName)
95-
.replaceAll('{{automaticMethods}}', processedAutomaticMethods)
96-
.replaceAll('{{manualMethods}}', manualMethods)
97-
.replaceAll('{{amount}}', wildcards.length.toString())
98-
.replace(emptyLinesInMarkdownLists, '')
99-
.replace(/^\n{2,}/gm, '\n');
100-
101-
if (file === 'README.md') {
94+
.replaceAll("{{filesList}}", filesList)
95+
.replaceAll("{{archiveUrl}}", archiveUrl)
96+
.replaceAll("{{branch}}", branchName)
97+
.replaceAll("{{automaticMethods}}", processedAutomaticMethods)
98+
.replaceAll("{{manualMethods}}", manualMethods)
99+
.replaceAll("{{amount}}", wildcards.length.toString())
100+
.replace(emptyLinesInMarkdownLists, "")
101+
.replace(/^\n{2,}/gm, "\n");
102+
103+
if (file === "README.md") {
102104
await Bun.write(`${path}/${file}`, processed);
103105
} else {
104106
await Bun.write(`${path}/docs/${file}`, processed);
105107
}
106108
});
107109

108-
console.log('Done');
110+
console.log("Done");

0 commit comments

Comments
 (0)