Skip to content

Commit

Permalink
chore: improve codebase structure and fix workflow fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Anmol-Baranwal committed Mar 12, 2024
1 parent e1c55ce commit 6b94edc
Show file tree
Hide file tree
Showing 15 changed files with 432 additions and 367 deletions.
279 changes: 161 additions & 118 deletions dist/index.js

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions dist/utils/createArticlesReadme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createArticlesReadme = void 0;
const core = __importStar(require("@actions/core"));
const fs = __importStar(require("fs"));
const git_1 = require("./git");
const performGitActions_1 = require("./performGitActions");
const conventionalCommits = core.getInput("conventionalCommits") === "true" || true;
async function createArticlesReadme(articles, outputDir, branch) {
// Create content for README.md
let readmeContent = "";
const readmePath = `${outputDir}/README.md`;
if (fs.existsSync(readmePath)) {
readmeContent = fs.readFileSync(readmePath, "utf8");
}
const hasTableOfContentsHeading = readmeContent.includes("# Table of Contents\n\n");
// Set the commit message based on whether the heading exists
let commitMessage = hasTableOfContentsHeading
? "update readme with table of contents"
: "create readme with table of contents";
if (!hasTableOfContentsHeading) {
readmeContent = "# Table of Contents\n\n";
}
for (const article of articles) {
const fileName = (0, git_1.getFileNameFromTitle)(article.title).trim();
const fileLink = `./${fileName}.md`;
if (readmeContent.includes(`[${article.title}]`)) {
console.log(`Skipping "${article.title}" because it already exists in the table of contents.`);
continue;
}
// Add entry to README content
readmeContent += `- [${article.title}](${fileLink.replace(/ /g, "%20")})\n`;
}
// Write README.md
fs.writeFileSync(readmePath, readmeContent);
if (conventionalCommits) {
commitMessage = `chore: ${commitMessage.toLowerCase()}`;
}
(0, performGitActions_1.performGitActions)({
commitMessage,
path: readmePath,
branch,
noticeMessage: "README.md file created and committed"
});
}
exports.createArticlesReadme = createArticlesReadme;
82 changes: 16 additions & 66 deletions dist/utils/createMarkdownFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const fs = __importStar(require("fs"));
const git_1 = require("./git");
const parseMarkdownContent_1 = require("./parseMarkdownContent");
const fetchDevArticleUsingId_1 = require("./fetchDevArticleUsingId");
const performGitActions_1 = require("./performGitActions");
const createArticlesReadme_1 = require("./createArticlesReadme");
const conventionalCommits = core.getInput("conventionalCommits") === "true" || true;
async function createMarkdownFile(articles, outputDir, branch, apiKey) {
// output directory must exist
Expand All @@ -55,18 +57,13 @@ async function createMarkdownFile(articles, outputDir, branch, apiKey) {
const markdownContent = (0, parseMarkdownContent_1.parseMarkdownContent)(article);
// Write markdown content to file
fs.writeFileSync(filePath, markdownContent);
try {
await (0, git_1.gitConfig)();
await (0, git_1.gitAdd)(filePath);
await (0, git_1.gitCommit)(commitMessage, filePath);
await (0, git_1.gitPull)(branch);
await (0, git_1.gitPush)(branch);
core.notice(`Markdown file created and committed: ${filePath}`);
}
catch (error) {
core.setFailed(`Failed to commit and push changes: ${error.message}`);
}
core.notice(`Markdown file created: ${filePath}`);
(0, performGitActions_1.performGitActions)({
commitMessage,
path: filePath,
branch
// noticeMessage: "Markdown file created and committed"
});
// core.notice(`Markdown file created: ${filePath}`)
}
else {
const existingContent = fs.readFileSync(filePath, "utf8");
Expand All @@ -82,17 +79,12 @@ async function createMarkdownFile(articles, outputDir, branch, apiKey) {
if (conventionalCommits) {
commitMessage = `chore: ${commitMessage.toLowerCase()}`;
}
try {
await (0, git_1.gitConfig)();
await (0, git_1.gitAdd)(filePath);
await (0, git_1.gitCommit)(commitMessage, filePath);
await (0, git_1.gitPull)(branch);
await (0, git_1.gitPush)(branch);
core.notice(`Markdown file created and committed: ${filePath}`);
}
catch (error) {
core.setFailed(`Failed to commit and push changes: ${error.message}`);
}
(0, performGitActions_1.performGitActions)({
commitMessage,
path: filePath,
branch,
noticeMessage: "Markdown file created and committed"
});
}
else {
core.notice(`Markdown file already exists for "${article.title}" and it is not edited. Skipping.`);
Expand All @@ -101,49 +93,7 @@ async function createMarkdownFile(articles, outputDir, branch, apiKey) {
}
const tableOfContents = core.getInput("saveArticlesReadme") === "true" || false;
if (tableOfContents) {
await createArticlesReadme(articles, outputDir, branch);
await (0, createArticlesReadme_1.createArticlesReadme)(articles, outputDir, branch);
}
}
exports.createMarkdownFile = createMarkdownFile;
async function createArticlesReadme(articles, outputDir, branch) {
// Create content for README.md
let readmeContent = "";
const readmePath = `${outputDir}/README.md`;
if (fs.existsSync(readmePath)) {
readmeContent = fs.readFileSync(readmePath, "utf8");
}
const hasTableOfContentsHeading = readmeContent.includes("# Table of Contents\n\n");
// Set the commit message based on whether the heading exists
let commitMessage = hasTableOfContentsHeading
? "update readme with table of contents"
: "create readme with table of contents";
if (!hasTableOfContentsHeading) {
readmeContent = "# Table of Contents\n\n";
}
for (const article of articles) {
const fileName = (0, git_1.getFileNameFromTitle)(article.title).trim();
const fileLink = `./${fileName}.md`;
if (readmeContent.includes(`[${article.title}]`)) {
console.log(`Skipping "${article.title}" because it already exists in the table of contents.`);
continue;
}
// Add entry to README content
readmeContent += `- [${article.title}](${fileLink.replace(/ /g, "%20")})\n`;
}
// Write README.md
fs.writeFileSync(readmePath, readmeContent);
if (conventionalCommits) {
commitMessage = `chore: ${commitMessage.toLowerCase()}`;
}
try {
await (0, git_1.gitConfig)();
await (0, git_1.gitAdd)(readmePath);
await (0, git_1.gitCommit)(commitMessage, readmePath);
await (0, git_1.gitPull)(branch);
await (0, git_1.gitPush)(branch);
core.notice("README.md file created and committed");
}
catch (error) {
core.setFailed(`Failed to commit and push changes (readme articles): ${error}`);
}
}
19 changes: 7 additions & 12 deletions dist/utils/createReadingList.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.createReadingList = void 0;
const core = __importStar(require("@actions/core"));
const fs = __importStar(require("fs"));
const git_1 = require("./git");
const performGitActions_1 = require("./performGitActions");
async function createReadingList(articles, outputDir, branch) {
const readTime = core.getInput("readTime") === "true" || false;
const conventionalCommits = core.getInput("conventionalCommits") === "true" || true;
Expand Down Expand Up @@ -76,17 +76,12 @@ async function createReadingList(articles, outputDir, branch) {
}
}
fs.writeFileSync(readmePath, existingContent);
try {
await (0, git_1.gitConfig)();
await (0, git_1.gitAdd)(readmePath);
await (0, git_1.gitCommit)(commitMessage, readmePath);
await (0, git_1.gitPull)(branch);
await (0, git_1.gitPush)(branch);
core.notice(`reading list file created and committed`);
}
catch (error) {
core.setFailed(`Failed to commit and push changes: ${error.message}`);
}
(0, performGitActions_1.performGitActions)({
commitMessage,
path: readmePath,
branch,
noticeMessage: "Reading list file created and committed"
});
core.notice(`Reading list updated in README.md`);
}
exports.createReadingList = createReadingList;
25 changes: 0 additions & 25 deletions dist/utils/fetchDevToArticles.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchDevToArticles = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
const core = __importStar(require("@actions/core"));
async function fetchDevToArticles(apiKey, per_page) {
if (per_page === undefined)
per_page = 999; // default is 30
Expand All @@ -51,7 +27,6 @@ async function fetchDevToArticles(apiKey, per_page) {
articles = articles.concat(pageArticles);
page++;
}
core.notice("Articles fetched and saved successfully.");
return articles;
}
exports.fetchDevToArticles = fetchDevToArticles;
6 changes: 3 additions & 3 deletions dist/utils/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function gitAdd(filePath) {
await exec.exec("git", ["add", filePath]);
}
catch (error) {
core.setFailed(`Failed to complete git add: ${error.message}`);
core.notice(`Failed to complete git add: ${error.message}`);
}
}
exports.gitAdd = gitAdd;
Expand All @@ -58,7 +58,7 @@ async function gitCommit(message, filePath) {
await exec.exec("git", ["commit", "-m", message, filePath]);
}
catch (error) {
core.setFailed(`Failed to complete git commit: ${error.message}`);
core.notice(`Failed to complete git commit: ${error.message}`);
}
}
exports.gitCommit = gitCommit;
Expand Down Expand Up @@ -87,7 +87,7 @@ async function gitConfig() {
]);
}
catch (error) {
core.setFailed(`Failed to set up Git configuration: ${error.message}`);
core.notice(`Failed to set up Git configuration: ${error.message}`);
}
}
exports.gitConfig = gitConfig;
Expand Down
43 changes: 43 additions & 0 deletions dist/utils/performGitActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.performGitActions = void 0;
const core = __importStar(require("@actions/core"));
const git_1 = require("./git");
async function performGitActions({ commitMessage, path, branch, noticeMessage }) {
try {
await (0, git_1.gitConfig)();
await (0, git_1.gitAdd)(path);
await (0, git_1.gitCommit)(commitMessage, path);
await (0, git_1.gitPull)(branch);
await (0, git_1.gitPush)(branch);
if (noticeMessage)
core.notice(noticeMessage);
}
catch (error) {
core.notice(`Failed to commit and push changes: ${error.message}`);
}
}
exports.performGitActions = performGitActions;
19 changes: 7 additions & 12 deletions dist/utils/synchronizeReadingList.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.synchronizeReadingList = void 0;
const fs = __importStar(require("fs"));
const core = __importStar(require("@actions/core"));
const git_1 = require("./git");
const performGitActions_1 = require("./performGitActions");
async function synchronizeReadingList(readingList, outputDir, branch) {
const readmePath = `./${outputDir}/README.md`;
let commitMessage = "synchronize reading list";
Expand Down Expand Up @@ -67,20 +67,15 @@ async function synchronizeReadingList(readingList, outputDir, branch) {
console.log(`Removed these articles from the reading list: ${removedArticles.join(", ")}`);
}
fs.writeFileSync(readmePath, updatedContent);
try {
await (0, git_1.gitConfig)();
await (0, git_1.gitAdd)(readmePath);
await (0, git_1.gitCommit)(commitMessage, readmePath);
await (0, git_1.gitPull)(branch);
await (0, git_1.gitPush)(branch);
}
catch (error) {
core.setFailed(`Failed to commit and push changes: ${error.message}`);
}
(0, performGitActions_1.performGitActions)({
commitMessage,
path: readmePath,
branch
});
core.notice(`Reading list synchronized successfully.`);
}
catch (error) {
core.setFailed(`Failed to synchronize reading list: ${error.message}`);
core.notice(`Failed to synchronize reading list: ${error.message}`);
}
}
exports.synchronizeReadingList = synchronizeReadingList;
Loading

0 comments on commit 6b94edc

Please sign in to comment.