Skip to content

Add support for updating content in text files #12

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 1 commit into from
Jun 17, 2020
Merged

Conversation

mheap
Copy link
Owner

@mheap mheap commented Jun 16, 2020

Replace common strings in text files. This code currently updates README.md and matches the following patterns:

  • @<old> - For GitHub Actions repos
  • <org>/<repo>.svg?branch=<old> - Travis badges

It might be worth switching to this plugin if we edit multiple files to make all the changes in a single commit (and use less API requests)

This relates to #7 - @mathiasbynens are there any other patterns you'd like to ship in this version, or should we ship and add patterns as they come up in the future?

@mathiasbynens
Copy link
Contributor

Ooh, that was quick! Since your patch lays the groundwork for easily adding more patterns in the future, let's just ship this as-is? Up to you :)

Thanks for your work on this project!


let content = file.content;
for (let r of replacements[path]) {
content = content.replace(r.from, r.to);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You really want replaceAll here, but it's not yet available in Node.js. :( Then again, maybe just doing a single replacement is good enough for now?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed!

Copy link
Contributor

@mathiasbynens mathiasbynens Jun 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new code is:

        var re = new RegExp(r.from, "g");

It should be something like:

        var re = new RegExp(escapeRegExpPattern(r.from), "g");

Otherwise, for example, any special RegExp characters such as . or ? in the input are not interpreted as the raw characters anymore. As a result the ${owner}/${repo}.svg?branch=${old} replacement currently does not work.

An example escapeRegExpPattern implementation can be found here: https://stackoverflow.com/a/9310752/96656

@mathiasbynens
Copy link
Contributor

Other possible replacements (you'd probably want to do these globally, in all files):

  • https://github.com/<org>/<repo>/tree/<old> (example)
  • https://github.com/<org>/<repo>/blob/<old> (example)
  • https://raw.githubusercontent.com/<org>/<repo>/<old> (example)

I'm sure there's more.

@mheap mheap force-pushed the update-text-patterns branch from e9fd736 to c3331a0 Compare June 17, 2020 12:43
@mheap mheap merged commit 030c208 into main Jun 17, 2020
@mheap mheap deleted the update-text-patterns branch June 17, 2020 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants