Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: adamreisnz/replace-in-file
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2bec5b78f83848c318741f2bae4bc8f22fe25ca5
Choose a base ref
...
head repository: adamreisnz/replace-in-file
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: aafb9b1d644bc0414d8402dab246510c89a9d183
Choose a head ref
  • 4 commits
  • 6 files changed
  • 5 contributors

Commits on Dec 27, 2023

  1. Update packages

    adamreisnz committed Dec 27, 2023
    Copy the full SHA
    3b936d9 View commit details

Commits on Feb 14, 2024

  1. Custom processor with two arguments (#186)

    * Custom processor with additional argument which receives the file being processed
    
    * Update lib/process-file.spec.js
    
    Co-authored-by: Adam Reis <adam@getfrello.com>
    
    * Update lib/process-file.spec.js
    
    Co-authored-by: Adam Reis <adam@getfrello.com>
    
    * Update lib/helpers/run-processors.js
    
    Co-authored-by: Adam Reis <adam@getfrello.com>
    
    * Update README.md
    
    Co-authored-by: Adam Reis <adam@getfrello.com>
    
    * Update README.md
    
    Co-authored-by: Adam Reis <adam@getfrello.com>
    
    * cleanup
    
    ---------
    
    Co-authored-by: Kevin Sommer <kevin@pasoma.de>
    Co-authored-by: Adam Reis <adam@getfrello.com>
    3 people authored Feb 14, 2024
    Copy the full SHA
    e90c51f View commit details

Commits on May 26, 2024

  1. Copy the full SHA
    fd16534 View commit details
  2. Update package.json

    adamreisnz committed May 26, 2024
    Copy the full SHA
    aafb9b1 View commit details
Showing with 767 additions and 781 deletions.
  1. +8 −0 README.md
  2. +1 −1 lib/helpers/run-processors.js
  3. +19 −0 lib/process-file.spec.js
  4. +6 −6 package.json
  5. +15 −9 types/index.d.ts
  6. +718 −765 yarn.lock
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -205,6 +205,14 @@ const results = replace.sync({
processor: (input) => input.replace(/foo/g, 'bar'),
});
```
The custom processor will receive the path of the file being processed as a second parameter:

```js
const results = replace.sync({
files: 'path/to/files/*.html',
processor: (input, file) => input.replace(/foo/g, file),
});
```

### Array of custom processors

2 changes: 1 addition & 1 deletion lib/helpers/run-processors.js
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ module.exports = function runProcessors(contents, processor, file) {
const result = {file};

const newContents = processors.reduce((contents, processor) => {
return processor(contents);
return processor(contents, file);
}, contents);

result.hasChanged = (newContents !== contents);
19 changes: 19 additions & 0 deletions lib/process-file.spec.js
Original file line number Diff line number Diff line change
@@ -42,6 +42,13 @@ describe('Process a file', () => {
return config;
}

function appendFileProcessor(config) {
config.processor = (content, file) => {
return `${content}${file}`;
};
return config;
}

/**
* Async with promises
*/
@@ -754,6 +761,18 @@ describe('Process a file', () => {
expect(results[2].hasChanged).to.equal(false);
});

it('should pass filename to processor as second parameter', () => {
transform.sync(appendFileProcessor({
files: 'test*',
}));
const test1 = fs.readFileSync('test1', 'utf8');
const test2 = fs.readFileSync('test2', 'utf8');
const test3 = fs.readFileSync('test3', 'utf8');
expect(test1).to.equal(`${testData}test1`);
expect(test2).to.equal(`${testData}test2`);
expect(test3).to.equal('nopetest3');
});

describe('fs', () => {
it('reads and writes using a custom fs when provided', done => {
const before = 'a';
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "replace-in-file",
"version": "7.1.0",
"version": "7.2.0",
"description": "A simple utility to quickly replace text in one or more files.",
"homepage": "https://github.com/adamreisnz/replace-in-file#readme",
"author": {
@@ -38,13 +38,13 @@
"yargs": "^17.7.2"
},
"devDependencies": {
"@babel/core": "^7.15.8",
"@babel/cli": "^7.15.7",
"@babel/preset-env": "^7.15.8",
"@babel/register": "^7.15.3",
"@babel/core": "^7.23.6",
"@babel/cli": "^7.23.4",
"@babel/preset-env": "^7.23.6",
"@babel/register": "^7.22.15",
"babel-plugin-istanbul": "^6.1.1",
"bluebird": "^3.7.2",
"chai": "^4.3.4",
"chai": "^4.3.10",
"chai-as-promised": "^7.1.1",
"dirty-chai": "^2.0.1",
"mocha": "^10.2.0",
24 changes: 15 additions & 9 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

declare module 'replace-in-file' {
export function replaceInFile(config: ReplaceInFileConfig): Promise<ReplaceResult[]>;
export function replaceInFile(config: ReplaceInFileConfig, cb: (error: Error, results: ReplaceResult[]) => void): void;
export function replaceInFile(config: ReplaceInFileConfig & { from?: never, to?: never, processor: ReplaceInFileConfig["processor"] }): Promise<ReplaceResult[]>;
export function replaceInFile(config: ReplaceInFileConfig & { from?: never, to?: never, processor: ReplaceInFileConfig["processor"] }, cb: (error: Error, results: ReplaceResult[]) => void): void;

export function replaceInFile(config: ReplaceInFileConfig & { from: ReplaceInFileConfig["from"], to: ReplaceInFileConfig["to"], processor?: never }): Promise<ReplaceResult[]>;
export function replaceInFile(config: ReplaceInFileConfig & { from: ReplaceInFileConfig["from"], to: ReplaceInFileConfig["to"], processor?: never }, cb: (error: Error, results: ReplaceResult[]) => void): void;

export default replaceInFile;

export namespace replaceInFile {
@@ -20,14 +24,15 @@ declare module 'replace-in-file' {
export interface ReplaceInFileConfig {
files: string | string[];
ignore?: string | string[];
from: From | Array<From>;
to: To | Array<To>;
from?: From | Array<From>;
to?: To | Array<To>;
countMatches?: boolean;
allowEmptyPaths?: boolean,
disableGlobs?: boolean,
encoding?: string,
dry?:boolean
glob?:object
allowEmptyPaths?: boolean;
disableGlobs?: boolean;
encoding?: string;
dry?: boolean;
glob?: object;
processor?: ProcessorCallback | Array<ProcessorCallback>;
}

export interface ReplaceResult {
@@ -40,3 +45,4 @@ declare module 'replace-in-file' {

type FromCallback = (file: string) => string | RegExp | (RegExp | string)[];
type ToCallback = (match: string, file: string) => string | string[];
type ProcessorCallback = (input: string, file: string) => string;
Loading