Skip to content

Replacements #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ Run with the `--verbose` flag to see debug information
| --old | The name of the branch to rename | master |
| --new | The new branch name | main |
| --confirm | Run without prompting for confirmation | false |

## Replacements

TODO document how to write replacements
19 changes: 19 additions & 0 deletions replacements/circleci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = function (owner, repo, old, target) {
return {
path: "./circleci/config.yml",
replacements: [
{
from: `- ${old}`,
to: `- ${target}`
},
{
from : `branch: ${old}`,
to: `branch: ${target}`
},
{
from: `only: ${old}`,
to: `only: ${target}`
}
]
};
}
15 changes: 15 additions & 0 deletions replacements/readme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = function (owner, repo, old, target) {
return {
path: "README.md",
replacements: [
{
from: `@${old}`,
to: `@${target}`,
},
{
from: `${owner}/${repo}.svg?branch=${old}`,
to: `${owner}/${repo}.svg?branch=${target}`,
},
],
};
}
32 changes: 18 additions & 14 deletions src/update-content.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
const fs = require('fs');
const util = require('util');
const replaceAll = require("string.prototype.replaceall");

const ls = util.promisify(fs.readdir);

module.exports = async function (
owner,
repo,
old,
target,
octokit,
isVerbose,
isDryRun
isDryRun,
) {
const replacements = {
"README.md": [
{
from: `@${old}`,
to: `@${target}`,
},
{
from: `${owner}/${repo}.svg?branch=${old}`,
to: `${owner}/${repo}.svg?branch=${target}`,
},
],
};

const files = (await ls(`${__dirname}/../replacements`)).filter((f) => f.endsWith('.js'));
const replacements = files.reduce((acc, next) => {
const { path, replacements } = require(`../replacements/${next}`)(
owner,
repo,
old,
target,
octokit,
isVerbose,
isDryRun,
);
return Object.assign(acc, { [path]: replacements });
}, {});
for (let path in replacements) {
try {
let file = await loadFile(owner, repo, path, octokit);
Expand Down