Skip to content

Commit

Permalink
Modify CPS script to fetch images (#1211)
Browse files Browse the repository at this point in the history
Co-authored-by: fill-the-fill <[email protected]>
  • Loading branch information
fill-the-fill and fickevics-makor authored Feb 13, 2024
1 parent 32149be commit d41a791
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ yarn-error.log*
/docs/governance/cardano-improvement-proposals/*
/docs/governance/cardano-problem-statements/*
/static/img/cip/*
/static/img/cps/*
/docs/native-tokens/token-registry/*
/docs/get-started/cardano-serialization-lib/*
**/changelog
Expand Down
32 changes: 26 additions & 6 deletions scripts/cps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,25 @@ import {
import { getDocTag } from "./reusable";

// Fetch markdown files from Github
async function fetchReadmeContent(folderUrl: string): Promise<string | null> {
async function fetchReadmeContent(folderUrl: string, folderName: string): Promise<string | null> {
try {
const response = await fetch(folderUrl);
const data = await response.json();
const readmeFile = data.find((file: any) => file.name === "README.md");
const otherFiles = data.filter((file: any) => file.name !== "README.md");

// Download and save other files
for (const file of otherFiles) {
const fileResponse = await fetch(file.download_url);
const fileBuffer = await fileResponse.buffer();
const filePath = path.join("static", "img", "cps", folderName, file.name);

// Ensure the directory exists before writing the file
const dir = path.dirname(filePath);
await fs.promises.mkdir(dir, { recursive: true });

await fs.promises.writeFile(filePath, fileBuffer);
}

if (readmeFile) {
const readmeResponse = await fetch(readmeFile.download_url);
Expand All @@ -26,7 +40,7 @@ async function fetchReadmeContent(folderUrl: string): Promise<string | null> {
return null;
}
} catch (error) {
console.error("Error fetching README:", error.message);
console.error("Error fetching README and other files:", error.message);
return null;
}
}
Expand Down Expand Up @@ -64,9 +78,16 @@ async function updateOrCreateReadmeFile(
")."
);

// Replace image paths
const newContentWithCorrectImagePaths = newContentWithInfo.replace(
/!\[.*?\]\(\.\/(.*?)\)/g,
`![same text](../../../static/img/cps/${folderName}/$1)`
);

const filePath = path.join(cps_target_folder, `${folderName}.md`);

try {
await fs.promises.writeFile(filePath, newContentWithInfo);
await fs.promises.writeFile(filePath, newContentWithCorrectImagePaths);
console.log(`Updated ${filePath}`);
} catch (error) {
console.error(`Error updating ${filePath}:`, error.message);
Expand All @@ -82,10 +103,9 @@ async function main() {
);

for (const folder of cpsFolders) {
const readmeContent = await fetchReadmeContent(folder.url);
const readmeContent = await fetchReadmeContent(folder.url, folder.name);
if (readmeContent) {
const folderName = folder.name;
await updateOrCreateReadmeFile(folderName, readmeContent);
await updateOrCreateReadmeFile(folder.name, readmeContent);
}
}
} catch (error) {
Expand Down

0 comments on commit d41a791

Please sign in to comment.