Initial commit

This commit is contained in:
richardrigutins 2023-07-02 14:39:21 +02:00
commit 1d7fd8e505
30 changed files with 23720 additions and 0 deletions

4
.eslintignore Normal file
View File

@ -0,0 +1,4 @@
dist/
lib/
node_modules/
jest.config.js

56
.eslintrc.json Normal file
View File

@ -0,0 +1,56 @@
{
"plugins": ["jest", "@typescript-eslint"],
"extends": ["plugin:github/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"project": "./tsconfig.json"
},
"rules": {
"i18n-text/no-en": "off",
"eslint-comments/no-use": "off",
"import/no-namespace": "off",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/explicit-member-accessibility": ["error", {"accessibility": "no-public"}],
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/array-type": "error",
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/ban-ts-comment": "error",
"camelcase": "off",
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/explicit-function-return-type": ["error", {"allowExpressions": true}],
"@typescript-eslint/func-call-spacing": ["error", "never"],
"@typescript-eslint/no-array-constructor": "error",
"@typescript-eslint/no-empty-interface": "error",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-extraneous-class": "error",
"@typescript-eslint/no-for-in-array": "error",
"@typescript-eslint/no-inferrable-types": "error",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/no-unnecessary-qualifier": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/no-useless-constructor": "error",
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/prefer-for-of": "warn",
"@typescript-eslint/prefer-function-type": "warn",
"@typescript-eslint/prefer-includes": "error",
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"@typescript-eslint/promise-function-async": "error",
"@typescript-eslint/require-array-sort-compare": "error",
"@typescript-eslint/restrict-plus-operands": "error",
"semi": "off",
"@typescript-eslint/semi": ["error", "always"],
"@typescript-eslint/type-annotation-spacing": "error",
"@typescript-eslint/unbound-method": "error",
"@typescript-eslint/trailingComma": "off"
},
"env": {
"node": true,
"es6": true,
"jest/globals": true
}
}

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
dist/** -diff linguist-generated=true

15
.github/dependabot.yml vendored Normal file
View File

@ -0,0 +1,15 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
commit-message:
prefix: "chore(deps): "
schedule:
interval: weekly
- package-ecosystem: npm
directory: /
commit-message:
prefix: "chore(deps): "
schedule:
interval: weekly

55
.github/workflows/check-dist.yml vendored Normal file
View File

@ -0,0 +1,55 @@
# `dist/index.js` is a special file in Actions.
# When you reference an action with `uses:` in a workflow,
# `index.js` is the code that will run.
# For our project, we generate this file through a build process from other source files.
# We need to make sure the checked-in `index.js` actually matches what we expect it to be.
name: Check dist/
on:
push:
branches:
- main
paths-ignore:
- '**.md'
- '**.yml'
pull_request:
paths-ignore:
- '**.md'
- '**.yml'
workflow_dispatch:
jobs:
check-dist:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set Node.js 16.x
uses: actions/setup-node@v3.6.0
with:
node-version: 16.x
- name: Install dependencies
run: npm ci
- name: Rebuild the dist/ directory
run: |
npm run build
npm run package
- name: Compare the expected and actual dist/ directories
run: |
if [ "$(git diff --ignore-space-at-eol dist/ | wc -l)" -gt "0" ]; then
echo "Detected uncommitted changes after build. See status below:"
git diff
exit 1
fi
id: diff
# If index.js was different than expected, upload the expected version as an artifact
- uses: actions/upload-artifact@v3
if: ${{ failure() && steps.diff.conclusion == 'failure' }}
with:
name: dist
path: dist/

64
.github/workflows/codeql-analysis.yml vendored Normal file
View File

@ -0,0 +1,64 @@
name: CodeQL
on:
push:
branches:
- main
pull_request:
# The branches below must be a subset of the branches above
branches:
- main
schedule:
- cron: '31 7 * * 3'
workflow_dispatch:
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [ 'TypeScript' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://git.io/codeql-language-support
steps:
- name: Checkout repository
uses: actions/checkout@v3
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
source-root: src
queries: security-extended,security-and-quality
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
# Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language
#- run: |
# make bootstrap
# make release
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2

99
.github/workflows/test-workflow.yml vendored Normal file
View File

@ -0,0 +1,99 @@
name: Build and Test
on:
push:
branches:
- main
paths-ignore:
- '**.md'
pull_request:
paths-ignore:
- '**.md'
jobs:
build: # make sure build/ci work properly
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: |
npm install
- run: |
npm run all
test: # make sure the action works on a clean machine without building
strategy:
matrix:
# os: [ ubuntu-latest, windows-latest, macos-latest ]
os: [ ubuntu-latest ] # only test on ubuntu for now
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Replace in Files - replace string
uses: ./
with:
search-text: '@VERSION@'
files: '**/*.txt'
replacement-text: '1.0.2'
exclude: '**/*.check.txt'
- name: Replace in Files - replace using special characters
uses: ./
with:
search-text: '{0}'
files: '**/*.txt'
replacement-text: 'world'
exclude: '**/*.check.txt'
- name: Replace in Files - pattern finds only one file
uses: ./
with:
search-text: '23'
files: '**/test2.txt'
replacement-text: '42'
exclude: '**/*.check.txt'
- name: Replace in Files - single file
uses: ./
with:
search-text: 'false'
files: 'test-files/test2.txt'
replacement-text: 'true'
exclude: '**/*.check.txt'
- name: Replace in Files - no files matching pattern
uses: ./
with:
search-text: 'foo'
files: '**/*.invalid'
replacement-text: 'bar'
exclude: '**/*.check.txt'
- name: Replace in Files - error on invalid encoding
uses: ./
continue-on-error: true
with:
search-text: 'foo'
files: '**/*.txt'
replacement-text: 'bar'
encoding: 'invalid'
exclude: '**/*.check.txt'
- name: Verify changes
run: |
echo " > Actual - test1.txt"
cat test-files/test1.txt
echo "=================="
echo " > Expected - test1.txt"
cat test-files/test1.check.txt
echo "=================="
echo " > Actual - test2.txt"
cat test-files/test2.txt
echo "=================="
echo " > Expected - test2.txt"
cat test-files/test2.check.txt
echo "=================="
diff test-files/test1.txt test-files/test1.check.txt >diff1.txt
diff test-files/test2.txt test-files/test2.check.txt >diff2.txt

99
.gitignore vendored Normal file
View File

@ -0,0 +1,99 @@
# Dependency directory
node_modules
# Rest pulled from https://github.com/github/gitignore/blob/master/Node.gitignore
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# next.js build output
.next
# nuxt.js build output
.nuxt
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# OS metadata
.DS_Store
Thumbs.db
# Ignore built ts files
__tests__/runner/*
lib/**/*

3
.prettierignore Normal file
View File

@ -0,0 +1,3 @@
dist/
lib/
node_modules/

13
.prettierrc.json Normal file
View File

@ -0,0 +1,13 @@
{
"arrowParens": "avoid",
"bracketSpacing": true,
"endOfLine": "auto",
"jsxSingleQuote": true,
"printWidth": 80,
"quoteProps": "consistent",
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all",
"useTabs": false
}

3
CHANGELOG.md Normal file
View File

@ -0,0 +1,3 @@
# Changelog
Changelogs for each release are located on the [releases page](https://github.com/richardrigutins/replace-in-files/releases).

1
CODEOWNERS Normal file
View File

@ -0,0 +1 @@
* @richardrigutins

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Riccardo Rigutini
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

68
README.md Normal file
View File

@ -0,0 +1,68 @@
# Replace in files
This GitHub Action allows you to find and replace text in files by matching a string.
It can be useful for automating repetitive tasks such as updating version numbers, replacing placeholders, or modifying configuration files during deployment.
## Features
- Specify the files to be searched using a glob pattern. You can also specify files to be excluded from the search.
- Find and replace all the occurrences of a string in the repository files.
- Supports different file encodings, including UTF-8, UTF-16, and ASCII.
- Works on every platform that supports JavaScript actions, including Linux, macOS, and Windows.
## Inputs
- `files`:
(**Required**) The files to be searched. It can be the path to a single file, or a glob pattern matching one or more files (e.g. `**/*.txt`).
- `replacement-text`:
(**Required**) The text that will replace the matched text.
- `search-text`:
(**Required**) The text that will be replaced.
- `encoding`:
(Optional) The encoding of the files to be searched. The following values are supported: `utf8`, `utf16le`, `latin1`, `ascii`, `base64`, `hex`. Defaults to `utf8`.
- `exclude`:
(Optional) The files to be excluded from the search. It can be the path to a file or a glob pattern matching one or more files (e.g. `**/*.md`). Defaults to an empty string.
## Example usage
```yaml
# Replace all the occurrences of 'hello' with 'world' in all the txt files,
# excluding the node_modules folder
- name: Replace multiple files
uses: richardrigutins/replace-in-files@v1
with:
files: '**/*.txt'
search-text: 'hello'
replacement-text: 'world'
exclude: 'node_modules/**'
encoding: 'utf8'
# Replace all the occurrences of '{0}' with '42' in the README.md file
- name: Replace single file
uses: richardrigutins/replace-in-files@v1
with:
files: 'README.md'
search-text: '{0}'
replacement-text: '42'
```
## Contributing
Contributions are welcome! Here are some ways you can contribute:
- Report bugs and suggest new features by creating an issue.
- Improve the documentation by submitting a pull request.
- Fix bugs or implement new features by submitting a pull request.
Before submitting a pull request, please make sure that your changes are consistent with the project's coding style and that all tests pass. To build and run all the tests and linters, run the following command:
```bash
npm run all
```
Be sure to also include the updated `dist` folder in your pull request.

66
__tests__/utils.test.ts Normal file
View File

@ -0,0 +1,66 @@
import fs from 'fs';
import path from 'path';
import { getFiles, isValidEncoding, replaceTextInFile } from '../src/utils';
describe('isValidEncoding', () => {
it('should return true for valid encodings', () => {
expect(isValidEncoding('ascii')).toBe(true);
expect(isValidEncoding('utf8')).toBe(true);
expect(isValidEncoding('utf16le')).toBe(true);
expect(isValidEncoding('ucs2')).toBe(true);
expect(isValidEncoding('base64')).toBe(true);
expect(isValidEncoding('latin1')).toBe(true);
});
it('should return false for invalid encodings', () => {
expect(isValidEncoding('foo')).toBe(false);
expect(isValidEncoding('bar')).toBe(false);
expect(isValidEncoding('')).toBe(false);
expect(isValidEncoding('42')).toBe(false);
expect(isValidEncoding('true')).toBe(false);
});
});
describe('getFiles', () => {
it('should return an array of file paths that match the given pattern', async () => {
const pattern = path.join(__dirname, '*.test.ts');
const files = await getFiles(pattern);
expect(files).toContain(path.join(__dirname, 'utils.test.ts'));
});
});
describe('replaceTextInFile', () => {
const testFilePath = path.join(__dirname, 'test-file.txt');
const testFileContent = '{0}, world!';
beforeEach(async () => {
await fs.promises.writeFile(testFilePath, testFileContent);
});
afterEach(async () => {
await fs.promises.unlink(testFilePath);
});
it('should replace all instances of the given text with the given value in the file', async () => {
await replaceTextInFile(testFilePath, '{0}', 'Hello'); // Replace text with special characters
await replaceTextInFile(testFilePath, 'world', 'universe'); // Replace text with no special characters
const updatedContent = await fs.promises.readFile(testFilePath, 'utf-8');
expect(updatedContent).toBe('Hello, universe!');
});
it('should not modify the file if the search text is not found', async () => {
const searchText = 'nonexistent';
const replacementText = 'replacement';
await replaceTextInFile(testFilePath, searchText, replacementText);
const updatedContent = await fs.promises.readFile(testFilePath, 'utf-8');
expect(updatedContent).toBe(testFileContent);
});
it('should not modify the file if the search text is empty', async () => {
const searchText = '';
const replacementText = 'replacement';
await replaceTextInFile(testFilePath, searchText, replacementText);
const updatedContent = await fs.promises.readFile(testFilePath, 'utf-8');
expect(updatedContent).toBe(testFileContent);
});
});

40
action.yml Normal file
View File

@ -0,0 +1,40 @@
name: "Replace in files"
author: "richardrigutins"
description: |-
Find and replace text in files by matching strings.
inputs:
files:
description: |-
The files to be searched.
It can be the path to a file or a glob pattern (e.g. `**/*.txt`).
required: true
replacement-text:
description: |-
The text that will replace the matched text.
required: true
search-text:
description: |-
The text that will be replaced.
required: true
encoding:
description: |-
(Optional) The encoding of the files to be searched.
The following values are supported: `ascii`, `utf8`, `utf16le`, `ucs2`, `base64`, `latin1`.
Defaults to `utf8`.
default: "utf8"
required: false
exclude:
description: |-
(Optional) The files to be excluded from the search.
It can be the path to a file or a glob pattern (e.g. `**/*.md`).
default: ""
required: false
branding:
icon: "edit"
color: "blue"
runs:
using: "node16"
main: "dist/index.js"

10623
dist/index.js generated vendored Normal file

File diff suppressed because it is too large Load Diff

1
dist/index.js.map generated vendored Normal file

File diff suppressed because one or more lines are too long

258
dist/licenses.txt generated vendored Normal file
View File

@ -0,0 +1,258 @@
@actions/core
MIT
The MIT License (MIT)
Copyright 2019 GitHub
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@actions/http-client
MIT
Actions Http Client for Node.js
Copyright (c) GitHub, Inc.
All rights reserved.
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
balanced-match
MIT
(MIT)
Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
brace-expansion
MIT
MIT License
Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
glob
ISC
The ISC License
Copyright (c) 2009-2023 Isaac Z. Schlueter and Contributors
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
lru-cache
ISC
The ISC License
Copyright (c) 2010-2023 Isaac Z. Schlueter and Contributors
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
minimatch
ISC
The ISC License
Copyright (c) 2011-2023 Isaac Z. Schlueter and Contributors
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
minipass
ISC
The ISC License
Copyright (c) 2017-2023 npm, Inc., Isaac Z. Schlueter, and Contributors
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
path-scurry
BlueOak-1.0.0
# Blue Oak Model License
Version 1.0.0
## Purpose
This license gives everyone as much permission to work with
this software as possible, while protecting contributors
from liability.
## Acceptance
In order to receive this license, you must agree to its
rules. The rules of this license are both obligations
under that agreement and conditions to your license.
You must not do anything with this software that triggers
a rule that you cannot or will not follow.
## Copyright
Each contributor licenses you to do everything with this
software that would otherwise infringe that contributor's
copyright in it.
## Notices
You must ensure that everyone who gets a copy of
any part of this software from you, with or without
changes, also gets the text of this license or a link to
<https://blueoakcouncil.org/license/1.0.0>.
## Excuse
If anyone notifies you in writing that you have not
complied with [Notices](#notices), you can keep your
license by taking all practical steps to comply within 30
days after the notice. If you do not do so, your license
ends immediately.
## Patent
Each contributor licenses you to do everything with this
software that would otherwise infringe any patent claims
they can license or become able to license.
## Reliability
No contributor can revoke this license.
## No Liability
***As far as the law allows, this software comes as is,
without any warranty or condition, and no contributor
will be liable to anyone for any damages related to this
software or this license, under any kind of legal claim.***
tunnel
MIT
The MIT License (MIT)
Copyright (c) 2012 Koichi Kobayashi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
uuid
MIT
The MIT License (MIT)
Copyright (c) 2010-2020 Robert Kieffer and other contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1
dist/sourcemap-register.js generated vendored Normal file

File diff suppressed because one or more lines are too long

9
jest.config.js Normal file
View File

@ -0,0 +1,9 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true
}

11991
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

45
package.json Normal file
View File

@ -0,0 +1,45 @@
{
"name": "@richardrigutins/replace-in-files",
"version": "1.0.0",
"private": true,
"description": "Replace text in files by matching strings.",
"main": "lib/main.js",
"scripts": {
"build": "tsc",
"format": "prettier --write '**/*.ts'",
"format-check": "prettier --check '**/*.ts'",
"lint": "eslint src/**/*.ts",
"package": "ncc build --source-map --license licenses.txt",
"test": "jest",
"all": "npm run build && npm run format && npm run lint && npm run package && npm test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/richardrigutins/replace-in-files"
},
"keywords": [
"actions",
"replace"
],
"author": "richardrigutins",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.10.0",
"glob": "^10.3.0"
},
"devDependencies": {
"@types/jest": "^29.5.2",
"@types/lodash": "^4.14.195",
"@types/node": "^20.3.2",
"@typescript-eslint/parser": "^5.60.1",
"@vercel/ncc": "^0.36.1",
"eslint": "^8.43.0",
"eslint-plugin-github": "^4.8.0",
"eslint-plugin-jest": "^27.2.2",
"jest": "^29.5.0",
"js-yaml": "^4.1.0",
"prettier": "^2.8.8",
"ts-jest": "^29.1.0",
"typescript": "^5.1.3"
}
}

52
src/main.ts Normal file
View File

@ -0,0 +1,52 @@
import { debug, getInput, info, setFailed, warning } from '@actions/core';
import {
Encoding,
getFiles,
isValidEncoding,
replaceTextInFile,
} from './utils';
async function run(): Promise<void> {
try {
const filesPattern = getInput('files');
const searchText = getInput('search-text');
const replaceText = getInput('replacement-text');
const excludePattern = getInput('exclude');
const inputEncoding = getInput('encoding');
if (!isValidEncoding(inputEncoding)) {
throw new Error(`Invalid encoding: ${inputEncoding}`);
}
const filePaths = await getFiles(filesPattern, excludePattern);
if (filePaths.length === 0) {
warning(`No files found for the given pattern.`);
return;
}
info(`Found ${filePaths.length} files for the given pattern.`);
info(`Replacing text...`);
const encoding = inputEncoding as Encoding;
const promises: Promise<void>[] = filePaths.map(
async (filePath: string) => {
debug(`Replacing text in file ${filePath}`);
await replaceTextInFile(filePath, searchText, replaceText, encoding);
},
);
await Promise.all(promises);
info(`Done!`);
} catch (err) {
if (err instanceof Error) {
setFailed(err.message);
} else {
const errorMessage =
'An error occurred. Run in debug mode for additional info.';
debug(`${JSON.stringify(err)}`);
setFailed(errorMessage);
}
}
}
run();

102
src/utils.ts Normal file
View File

@ -0,0 +1,102 @@
import fs from 'fs';
import { glob } from 'glob';
const encodings = [
'ascii',
'utf8',
'utf16le',
'ucs2',
'base64',
'latin1',
] as const;
export type Encoding = (typeof encodings)[number];
/**
* Checks if the given encoding is supported.
* @param encoding The encoding to check.
* @returns `true` if the encoding is valid, `false` otherwise.
*/
export function isValidEncoding(encoding: string): boolean {
return encodings.includes(encoding as Encoding);
}
/**
* Returns an array of file paths that match the given pattern.
* @param filesPattern The file path or glob pattern to search for.
* @param exclude An optional glob pattern to exclude from the search.
* @returns A Promise that resolves to an array of file paths.
* @throws An error if there is an error getting the files.
*/
export async function getFiles(
filesPattern: string,
exclude?: string,
): Promise<string[]> {
try {
return await glob(filesPattern, { ignore: exclude });
} catch (error) {
throw new Error(`Error getting files: ${error}`);
}
}
/**
* Replaces all instances of the given text with the given value in the file.
* @param filePath The path of the file to modify.
* @param searchText The string to search for.
* @param replacementText The string to replace the search text with.
* @param encoding The encoding of the file.
* @returns A Promise that resolves when the file has been modified.
* @throws An error if there is an error reading or saving the file.
*/
export async function replaceTextInFile(
filePath: string,
searchText: string,
replacementText: string,
encoding: Encoding = 'utf8',
): Promise<void> {
// Don't do anything if the search text is empty
if (!searchText) {
return;
}
const fileContent = await readFileContent(filePath, encoding);
const updatedContent = fileContent.replace(searchText, replacementText);
await saveFileContent(filePath, updatedContent);
}
/**
* Reads the content of the file at the given path.
* @param filePath The path of the file to read.
* @param encoding The encoding of the file.
* @returns A Promise that resolves to the content of the file as a string.
* @throws An error if there is an error reading the file.
*/
async function readFileContent(
filePath: string,
encoding: Encoding,
): Promise<string> {
try {
const fileContentBuffer = await fs.promises.readFile(filePath, encoding);
return fileContentBuffer.toString();
} catch (error) {
throw new Error(`Error reading file content: ${error}`);
}
}
/**
* Saves the given content to the file at the given path.
* @param filePath The path of the file to save.
* @param content The content to save to the file.
* @returns A Promise that resolves when the file has been saved.
* @throws An error if there is an error saving the file.
*/
async function saveFileContent(
filePath: string,
content: string,
): Promise<void> {
try {
await fs.promises.writeFile(filePath, content);
} catch (error) {
throw new Error(`Error saving file content: ${error}`);
}
}

View File

@ -0,0 +1,4 @@
Version: 1.0.2.
Hello world!
This should be twenty-three: 23.
This line has been changed: false.

4
test-files/test1.txt Normal file
View File

@ -0,0 +1,4 @@
Version: @VERSION@.
Hello {0}!
This should be twenty-three: 23.
This line has been changed: false.

View File

@ -0,0 +1,3 @@
Hello again world!
21 * 2 = 42
This line has been changed: true.

3
test-files/test2.txt Normal file
View File

@ -0,0 +1,3 @@
Hello again {0}!
21 * 2 = 23
This line has been changed: false.

16
tsconfig.json Normal file
View File

@ -0,0 +1,16 @@
{
"compilerOptions": {
"alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"outDir": "./lib", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
},
"exclude": [
"node_modules",
"**/*.test.ts",
]
}