Skip to content

Commit

Permalink
Change implementation of filling activities, using remove+add instead…
Browse files Browse the repository at this point in the history
… of add/replace
  • Loading branch information
DeKal committed Dec 11, 2020
1 parent 841fb43 commit 551423f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 116 deletions.
81 changes: 23 additions & 58 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,7 @@ const serializers = {
},
PushEvent: (item) => {
return `🔥 Pushed to ${item.repo.name}`;
}
},
};

Toolkit.run(
Expand Down Expand Up @@ -1686,70 +1686,34 @@ Toolkit.run(
tools.log.info("Found less than 5 activities");
}

if (startIdx !== -1 && endIdx === -1) {
// Add one since the content needs to be inserted just after the initial comment
startIdx++;
content.forEach((line, idx) =>
readmeContent.splice(startIdx + idx, 0, `${idx + 1}. ${line}`)
);

// Append <!--END_SECTION:activity--> comment
readmeContent.splice(
startIdx + content.length,
0,
"<!--END_SECTION:activity-->"
);
if (endIdx !== -1) {
const oldContent = readmeContent.slice(startIdx + 1, endIdx).join("\n");

// Update README
fs.writeFileSync("./README.md", readmeContent.join("\n"));
const newContent = content
.map((line, idx) => `${idx + 1}. ${line}`)
.join("\n");

// Commit to the remote repository
try {
await commitFile();
} catch (err) {
tools.log.debug("Something went wrong");
return tools.exit.failure(err);
if (oldContent.trim() === newContent.trim()) {
tools.exit.success("No changes detected");
}
tools.exit.success("Wrote to README");
// Remove old content
readmeContent.slice(startIdx, endIdx + 1).forEach((line) => {
readmeContent.splice(startIdx + 1, 1);
});
}

const oldContent = readmeContent.slice(startIdx + 1, endIdx).join("\n");
const newContent = content
.map((line, idx) => `${idx + 1}. ${line}`)
.join("\n");

if (oldContent.trim() === newContent.trim())
tools.exit.success("No changes detected");

// Add one since the content needs to be inserted just after the initial comment
startIdx++;
content.forEach((line, idx) =>
readmeContent.splice(startIdx + idx, 0, `${idx + 1}. ${line}`)
);

// Recent GitHub Activity content between the comments
const readmeActivitySection = readmeContent.slice(startIdx, endIdx);
if (!readmeActivitySection.length) {
content.some((line, idx) => {
// User doesn't have 5 public events
if (!line) {
return true;
}
readmeContent.splice(startIdx + idx, 0, `${idx + 1}. ${line}`);
});
tools.log.success("Wrote to README");
} else {
// It is likely that a newline is inserted after the <!--START_SECTION:activity--> comment (code formatter)
let count = 0;

readmeActivitySection.some((line, idx) => {
// User doesn't have 5 public events
if (!content[count]) {
return true;
}
if (line !== "") {
readmeContent[startIdx + idx] = `${count + 1}. ${content[count]}`;
count++;
}
});
tools.log.success("Updated README with the recent activity");
}
// Append <!--END_SECTION:activity--> comment
readmeContent.splice(
startIdx + content.length,
0,
"<!--END_SECTION:activity-->"
);

// Update README
fs.writeFileSync("./README.md", readmeContent.join("\n"));
Expand All @@ -1761,6 +1725,7 @@ Toolkit.run(
tools.log.debug("Something went wrong");
return tools.exit.failure(err);
}

tools.exit.success("Pushed to remote repository");
},
{
Expand Down
81 changes: 23 additions & 58 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const serializers = {
},
PushEvent: (item) => {
return `🔥 Pushed to ${item.repo.name}`;
}
},
};

Toolkit.run(
Expand Down Expand Up @@ -149,70 +149,34 @@ Toolkit.run(
tools.log.info("Found less than 5 activities");
}

if (startIdx !== -1 && endIdx === -1) {
// Add one since the content needs to be inserted just after the initial comment
startIdx++;
content.forEach((line, idx) =>
readmeContent.splice(startIdx + idx, 0, `${idx + 1}. ${line}`)
);
if (endIdx !== -1) {
const oldContent = readmeContent.slice(startIdx + 1, endIdx).join("\n");

// Append <!--END_SECTION:activity--> comment
readmeContent.splice(
startIdx + content.length,
0,
"<!--END_SECTION:activity-->"
);
const newContent = content
.map((line, idx) => `${idx + 1}. ${line}`)
.join("\n");

// Update README
fs.writeFileSync("./README.md", readmeContent.join("\n"));

// Commit to the remote repository
try {
await commitFile();
} catch (err) {
tools.log.debug("Something went wrong");
return tools.exit.failure(err);
if (oldContent.trim() === newContent.trim()) {
tools.exit.success("No changes detected");
}
tools.exit.success("Wrote to README");
// Remove old content
readmeContent.slice(startIdx, endIdx + 1).forEach((line) => {
readmeContent.splice(startIdx + 1, 1);
});
}

const oldContent = readmeContent.slice(startIdx + 1, endIdx).join("\n");
const newContent = content
.map((line, idx) => `${idx + 1}. ${line}`)
.join("\n");

if (oldContent.trim() === newContent.trim())
tools.exit.success("No changes detected");

// Add one since the content needs to be inserted just after the initial comment
startIdx++;
content.forEach((line, idx) =>
readmeContent.splice(startIdx + idx, 0, `${idx + 1}. ${line}`)
);

// Recent GitHub Activity content between the comments
const readmeActivitySection = readmeContent.slice(startIdx, endIdx);
if (!readmeActivitySection.length) {
content.some((line, idx) => {
// User doesn't have 5 public events
if (!line) {
return true;
}
readmeContent.splice(startIdx + idx, 0, `${idx + 1}. ${line}`);
});
tools.log.success("Wrote to README");
} else {
// It is likely that a newline is inserted after the <!--START_SECTION:activity--> comment (code formatter)
let count = 0;

readmeActivitySection.some((line, idx) => {
// User doesn't have 5 public events
if (!content[count]) {
return true;
}
if (line !== "") {
readmeContent[startIdx + idx] = `${count + 1}. ${content[count]}`;
count++;
}
});
tools.log.success("Updated README with the recent activity");
}
// Append <!--END_SECTION:activity--> comment
readmeContent.splice(
startIdx + content.length,
0,
"<!--END_SECTION:activity-->"
);

// Update README
fs.writeFileSync("./README.md", readmeContent.join("\n"));
Expand All @@ -224,6 +188,7 @@ Toolkit.run(
tools.log.debug("Something went wrong");
return tools.exit.failure(err);
}

tools.exit.success("Pushed to remote repository");
},
{
Expand Down

0 comments on commit 551423f

Please sign in to comment.