Skip to content

Commit

Permalink
Add Cardano Problem Statements script (#1144)
Browse files Browse the repository at this point in the history
* Add CPS fetching script

* Adjust formatting

* Add folder for CPS markdown files

* Remove .gitkeep file

* Add .gitkeep file

* Fix comment typo

---------

Co-authored-by: fill-the-fill <[email protected]>
  • Loading branch information
fill-the-fill and fickevics-makor authored Sep 16, 2023
1 parent f016483 commit 410df6d
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ yarn-error.log*

# Auto generated content like CIPs, Rust Library, Token Registry and Changelog
/docs/governance/cardano-improvement-proposals/*
/docs/governance/cardano-problem-statements/*
/static/img/cip/*
/docs/native-tokens/token-registry/*
/docs/get-started/cardano-serialization-lib/*
Expand Down
1 change: 1 addition & 0 deletions docs/governance/cardano-problem-statements/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
File to push empty folder to github
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"docusaurus": "docusaurus",
"start": "docusaurus start",
"build": "ts-node scripts/cip.ts && ts-node scripts/token-registry.ts && ts-node scripts/rust-library.ts && docusaurus build",
"build": "ts-node scripts/cip.ts && ts-node scripts/cps.ts && ts-node scripts/token-registry.ts && ts-node scripts/rust-library.ts && docusaurus build",
"swizzle": "docusaurus swizzle",
"deploy": "docusaurus deploy",
"serve": "docusaurus serve",
Expand Down
64 changes: 51 additions & 13 deletions scripts/constants.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,66 @@
// CIP constants
const cip_readme_url: string = "/README.md";
const cip_readme_regex: RegExp = /\.\/CIP.?\/|\.\/CIP-.*\)/gm
const cip_readme_regex: RegExp = /\.\/CIP.?\/|\.\/CIP-.*\)/gm;
const cip_source_repo: string = "cardano-foundation/CIPs";
const cip_static_resource_path: string = "/static/img/cip/";
const cip_docs_path: string = "./docs/governance/cardano-improvement-proposals";
const cip_regex: RegExp = /\]\(.*?.png\)|\]\(.*?.jpg\)|\]\(.*?.jpeg\)|\]\(.*?.json\)/gm;
const cip_repo_base_url: string = "https://github.com/cardano-foundation/CIPs/tree/master/";
const cip_repo_raw_base_url: string = "https://raw.githubusercontent.com/cardano-foundation/CIPs/master/";
const cip_regex: RegExp =
/\]\(.*?.png\)|\]\(.*?.jpg\)|\]\(.*?.jpeg\)|\]\(.*?.json\)/gm;
const cip_repo_base_url: string =
"https://github.com/cardano-foundation/CIPs/tree/master/";
const cip_repo_raw_base_url: string =
"https://raw.githubusercontent.com/cardano-foundation/CIPs/master/";

// Rust Library constants
// CPS constants
const cps_repository_url: string =
"https://api.github.com/repos/cardano-foundation/CIPs/contents";
const cps_target_folder: string =
"./docs/governance/cardano-problem-statements";

// Rust Library constants
const rl_static_resource_path: string = "/tree/master/doc/getting-started";
const rl_docs_path: string = "./docs/get-started/cardano-serialization-lib";
const rl_repo_base_url: string = "https://github.com/Emurgo/cardano-serialization-lib";
const rl_raw_base_index_url: string = "https://raw.githubusercontent.com/Emurgo/cardano-serialization-lib/master/doc/index.rst";
const rl_repo_raw_base_url: string = "https://raw.githubusercontent.com/Emurgo/cardano-serialization-lib/master/doc/getting-started/";
const rl_repo_base_url: string =
"https://github.com/Emurgo/cardano-serialization-lib";
const rl_raw_base_index_url: string =
"https://raw.githubusercontent.com/Emurgo/cardano-serialization-lib/master/doc/index.rst";
const rl_repo_raw_base_url: string =
"https://raw.githubusercontent.com/Emurgo/cardano-serialization-lib/master/doc/getting-started/";

// Token Registry constants
const tr_docs_path: string = "./docs/native-tokens/token-registry";
const tr_github_wiki: string = "https://github.com/cardano-foundation/cardano-token-registry/wiki";
const tr_url: string = "https://github.com/cardano-foundation/cardano-token-registry/blob/master/";
const tr_raw_wiki_url: string = "https://raw.githubusercontent.com/wiki/cardano-foundation/cardano-token-registry/";
const tr_overview_url: string = "https://raw.githubusercontent.com/cardano-foundation/cardano-token-registry/master/README.md";
const tr_github_wiki: string =
"https://github.com/cardano-foundation/cardano-token-registry/wiki";
const tr_url: string =
"https://github.com/cardano-foundation/cardano-token-registry/blob/master/";
const tr_raw_wiki_url: string =
"https://raw.githubusercontent.com/wiki/cardano-foundation/cardano-token-registry/";
const tr_overview_url: string =
"https://raw.githubusercontent.com/cardano-foundation/cardano-token-registry/master/README.md";

// General constants
const custom_edit_url = "\ncustom_edit_url: null"

export {cip_readme_url, cip_readme_regex, cip_source_repo, cip_static_resource_path, cip_docs_path, cip_regex, cip_repo_base_url, cip_repo_raw_base_url, rl_static_resource_path, rl_docs_path, rl_repo_base_url, rl_raw_base_index_url, rl_repo_raw_base_url, tr_docs_path, tr_github_wiki, tr_url, tr_raw_wiki_url, tr_overview_url, custom_edit_url};
export {
cip_readme_url,
cip_readme_regex,
cip_source_repo,
cip_static_resource_path,
cip_docs_path,
cip_regex,
cip_repo_base_url,
cip_repo_raw_base_url,
cps_repository_url,
cps_target_folder,
rl_static_resource_path,
rl_docs_path,
rl_repo_base_url,
rl_raw_base_index_url,
rl_repo_raw_base_url,
tr_docs_path,
tr_github_wiki,
tr_url,
tr_raw_wiki_url,
tr_overview_url,
custom_edit_url,
};
96 changes: 96 additions & 0 deletions scripts/cps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import fetch from "node-fetch";
import * as fs from "fs";
import * as path from "path";
import {
custom_edit_url,
cps_repository_url,
cps_target_folder,
cip_source_repo,
cip_repo_base_url,
cip_readme_url,
} from "./constants";
import { getDocTag } from "./reusable";

// Fetch markdown files from Github
async function fetchReadmeContent(folderUrl: 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");

if (readmeFile) {
const readmeResponse = await fetch(readmeFile.download_url);
const readmeText = await readmeResponse.text();
return readmeText;
} else {
return null;
}
} catch (error) {
console.error("Error fetching README:", error.message);
return null;
}
}

// Update/Create CPS locally
async function updateOrCreateReadmeFile(
folderName: string,
content: string
): Promise<void> {
const title = getDocTag(content, "Title");
const cps = getDocTag(content, "CPS");
const sidebarLabel = `sidebar_label: "(${cps}) ${title}"`;
const status = getDocTag(content, "Status");
const creationDate = getDocTag(content, "Created");

// Sanitize title
const newContent = content.replace(
/Title: .+/,
`Title: "${title}"\n${sidebarLabel}${custom_edit_url}`
);

// Add Content Info at the bottom of the page
const newContentWithInfo = newContent.concat(
"\n" +
"## CPS Information \nThis CPS was created on **" +
creationDate +
"** and has the status: [" +
status +
"](../cardano-improvement-proposals/CIP-0001#cip-workflow). \nThis page was generated automatically from: [" +
cip_source_repo +
"](" +
cip_repo_base_url +
folderName +
cip_readme_url +
")."
);

const filePath = path.join(cps_target_folder, `${folderName}.md`);
try {
await fs.promises.writeFile(filePath, newContentWithInfo);
console.log(`Updated ${filePath}`);
} catch (error) {
console.error(`Error updating ${filePath}:`, error.message);
}
}

async function main() {
try {
const response = await fetch(cps_repository_url);
const data = await response.json();
const cpsFolders = data.filter(
(item: any) => item.type === "dir" && item.name.includes("CPS")
);

for (const folder of cpsFolders) {
const readmeContent = await fetchReadmeContent(folder.url);
if (readmeContent) {
const folderName = folder.name;
await updateOrCreateReadmeFile(folderName, readmeContent);
}
}
} catch (error) {
console.error("Error fetching repository contents:", error.message);
}
}

main();
11 changes: 11 additions & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ module.exports = {
},
],
},
{
type: "category",
label: "Cardano Problem Statements (CPS)",
items: [
{
// Generate sidebar automatically from docs/governance/cardano-problem-statements
type: "autogenerated",
dirName: "governance/cardano-problem-statements",
},
],
},
"governance/project-catalyst",
],
"Operate a Stake Pool": [
Expand Down

0 comments on commit 410df6d

Please sign in to comment.