Skip to content

Commit

Permalink
Fix overwriting greyhounds with same name
Browse files Browse the repository at this point in the history
  • Loading branch information
zachwhaley committed Jun 17, 2024
1 parent 6d30b1e commit 2a16c30
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions script/add-greyhound.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ function thisDate(dateString) {
}

module.exports = async ({github, context, core, inputs}) => {
const greyhound = slugify(inputs.name, { lower: true });
const filePath = `_greyhounds/${greyhound}.md`;

// Create greyhound data
const info = {
content: inputs.summary,
Expand All @@ -46,10 +43,22 @@ module.exports = async ({github, context, core, inputs}) => {
}
};

// Create a unique file path to avoid overwriting an existing greyhound's data
const greyhound = slugify(inputs.name, { lower: true });

let count = 1;
let nameId = greyhound;
let filePath = `_greyhounds/${nameId}.md`;
while (fs.existsSync(filePath)) {
count += 1;
nameId = greyhound + count;
filePath = `_greyhounds/${nameId}.md`;
}

// Save greyhound data
const data = matter.stringify(info.content, info.data, options);
await fs.writeFile(filePath, data);
console.log(`Saved ${filePath}`);

return `Adding ${info.data.title} to Available Hounds! 🌟`;
return `Adding ${info.data.title} to Available Hounds as ${nameId}! 🌟`;
}

0 comments on commit 2a16c30

Please sign in to comment.