Skip to content

Commit

Permalink
feat: add support for passing in replace-in-file functions (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
Borduhh authored Aug 30, 2021
1 parent d2e8b02 commit 4bfa3c7
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"rules": {
"eslint-comments/no-use": "off",
"import/no-namespace": "off",
"no-unused-vars": "error",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/explicit-member-accessibility": ["error", {"accessibility": "no-public"}],
"@typescript-eslint/array-type": "error",
Expand Down
20 changes: 20 additions & 0 deletions __tests__/prepare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,23 @@ test("replacements are global", async () => {

// Will throw if results do not match
});

test("prepare should replace using function", async () => {
const replacements = [
{
files: [path.join(d.name, "/*.py")],
from: '__VERSION__ = ".*"',
to: (match) => `__VERSION__ = 2`,
},
{
files: [path.join(d.name, "/build.gradle")],
from: "version = '.*'",
to: (match) => "version = 2",
},
];

await prepare({ replacements }, context);

await assertFileContentsContain("__init__.py", `__VERSION__ = 2`);
await assertFileContents("build.gradle", "version = 2");
});
10 changes: 9 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@ export interface Replacement {
*
* The context object is used to render the template. Additional values
* can be found at: https://semantic-release.gitbook.io/semantic-release/developer-guide/js-api#result
*
* For advacned replacement, pass in a function to replace non-standard variables
* ```
* {
* from: `__VERSION__ = 11`, // eslint-disable-line
* to: (matched) => `__VERSION: ${parseInt(matched.split('=')[1].trim()) + 1}`, // eslint-disable-line
* },
* ```
*/
to: string;
to: string | ((a: string) => string);
ignore?: string[];
allowEmptyPaths?: boolean;
countMatches?: boolean;
Expand Down
14 changes: 9 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.prepare = void 0;
var replace_in_file_1 = require("replace-in-file");
var lodash_1 = require("lodash");
var replace_in_file_1 = __importDefault(require("replace-in-file"));
var jest_diff_1 = __importDefault(require("jest-diff"));
function prepare(PluginConfig, context) {
return __awaiter(this, void 0, void 0, function () {
Expand All @@ -82,16 +83,19 @@ function prepare(PluginConfig, context) {
results = replacement.results;
delete replacement.results;
replaceInFileConfig = replacement;
replaceInFileConfig.to = lodash_1.template(replacement.to)(__assign({}, context));
replaceInFileConfig.from = new RegExp(replacement.from, "g");
return [4 /*yield*/, replace_in_file_1["default"](replaceInFileConfig)];
replaceInFileConfig.to =
typeof replacement.to === "function"
? replacement.to
: lodash_1.template(replacement.to)(__assign({}, context));
replaceInFileConfig.from = new RegExp(replacement.from, "gm");
return [4 /*yield*/, replace_in_file_1.replaceInFile(replaceInFileConfig)];
case 2:
actual = _b.sent();
if (results) {
results = results.sort();
actual = actual.sort();
if (!lodash_1.isEqual(actual.sort(), results.sort())) {
throw new Error("Results differed from actual! \n" + jest_diff_1["default"](results, actual));
throw new Error("Expected match not found!\n" + jest_diff_1["default"](actual, results));
}
}
_b.label = 3;
Expand Down
4 changes: 3 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[@google/semantic-release-replace-plugin](README.md)
**[@google/semantic-release-replace-plugin](README.md)**

> Globals
# @google/semantic-release-replace-plugin

Expand Down
10 changes: 6 additions & 4 deletions docs/interfaces/_index_.pluginconfig.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[@google/semantic-release-replace-plugin](../README.md)["index"](../modules/_index_.md)[PluginConfig](_index_.pluginconfig.md)
**[@google/semantic-release-replace-plugin](../README.md)**

> [Globals](../README.md) / ["index"](../modules/_index_.md) / PluginConfig
# Interface: PluginConfig

Expand Down Expand Up @@ -40,10 +42,10 @@ PluginConfig is used to provide multiple replacement.

## Properties

### replacements
### replacements

**replacements**: *[Replacement](_index_.replacement.md)[]*
**replacements**: [Replacement](_index_.replacement.md)[]

*Defined in [index.ts:93](https://github.com/google/semantic-release-replace-plugin/blob/master/src/index.ts#L93)*
*Defined in [index.ts:101](https://github.com/google/semantic-release-replace-plugin/blob/master/src/index.ts#L101)*

An array of replacements to be made.
86 changes: 48 additions & 38 deletions docs/interfaces/_index_.replacement.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[@google/semantic-release-replace-plugin](../README.md)["index"](../modules/_index_.md)[Replacement](_index_.replacement.md)
**[@google/semantic-release-replace-plugin](../README.md)**

> [Globals](../README.md) / ["index"](../modules/_index_.md) / Replacement
# Interface: Replacement

Expand All @@ -13,109 +15,117 @@ with the difference being the single string for `to` and `from`.

### Properties

* [allowEmptyPaths](_index_.replacement.md#optional-allowemptypaths)
* [countMatches](_index_.replacement.md#optional-countmatches)
* [disableGlobs](_index_.replacement.md#optional-disableglobs)
* [dry](_index_.replacement.md#optional-dry)
* [encoding](_index_.replacement.md#optional-encoding)
* [allowEmptyPaths](_index_.replacement.md#allowemptypaths)
* [countMatches](_index_.replacement.md#countmatches)
* [disableGlobs](_index_.replacement.md#disableglobs)
* [dry](_index_.replacement.md#dry)
* [encoding](_index_.replacement.md#encoding)
* [files](_index_.replacement.md#files)
* [from](_index_.replacement.md#from)
* [ignore](_index_.replacement.md#optional-ignore)
* [results](_index_.replacement.md#optional-results)
* [ignore](_index_.replacement.md#ignore)
* [results](_index_.replacement.md#results)
* [to](_index_.replacement.md#to)

## Properties

### `Optional` allowEmptyPaths
### allowEmptyPaths

**allowEmptyPaths**? : *boolean*
`Optional` **allowEmptyPaths**: boolean

*Defined in [index.ts:48](https://github.com/google/semantic-release-replace-plugin/blob/master/src/index.ts#L48)*
*Defined in [index.ts:56](https://github.com/Borduhh/semantic-release-replace-plugin/blob/6dd4918/src/index.ts#L56)*

___

### `Optional` countMatches
### countMatches

**countMatches**? : *boolean*
`Optional` **countMatches**: boolean

*Defined in [index.ts:49](https://github.com/google/semantic-release-replace-plugin/blob/master/src/index.ts#L49)*
*Defined in [index.ts:57](https://github.com/Borduhh/semantic-release-replace-plugin/blob/6dd4918/src/index.ts#L57)*

___

### `Optional` disableGlobs
### disableGlobs

**disableGlobs**? : *boolean*
`Optional` **disableGlobs**: boolean

*Defined in [index.ts:50](https://github.com/google/semantic-release-replace-plugin/blob/master/src/index.ts#L50)*
*Defined in [index.ts:58](https://github.com/Borduhh/semantic-release-replace-plugin/blob/6dd4918/src/index.ts#L58)*

___

### `Optional` dry
### dry

**dry**? : *boolean*
`Optional` **dry**: boolean

*Defined in [index.ts:52](https://github.com/google/semantic-release-replace-plugin/blob/master/src/index.ts#L52)*
*Defined in [index.ts:60](https://github.com/Borduhh/semantic-release-replace-plugin/blob/6dd4918/src/index.ts#L60)*

___

### `Optional` encoding
### encoding

**encoding**? : *string*
`Optional` **encoding**: string

*Defined in [index.ts:51](https://github.com/google/semantic-release-replace-plugin/blob/master/src/index.ts#L51)*
*Defined in [index.ts:59](https://github.com/Borduhh/semantic-release-replace-plugin/blob/6dd4918/src/index.ts#L59)*

___

### files
### files

**files**: *string[]*
**files**: string[]

*Defined in [index.ts:31](https://github.com/google/semantic-release-replace-plugin/blob/master/src/index.ts#L31)*
*Defined in [index.ts:31](https://github.com/Borduhh/semantic-release-replace-plugin/blob/6dd4918/src/index.ts#L31)*

files to search for replacements

___

### from
### from

**from**: *string*
**from**: string

*Defined in [index.ts:37](https://github.com/google/semantic-release-replace-plugin/blob/master/src/index.ts#L37)*
*Defined in [index.ts:37](https://github.com/Borduhh/semantic-release-replace-plugin/blob/6dd4918/src/index.ts#L37)*

The RegExp pattern to use to match.

Uses `String.replace(new RegExp(s, 'g'), to)` for implementation.

___

### `Optional` ignore
### ignore

**ignore**? : *string[]*
`Optional` **ignore**: string[]

*Defined in [index.ts:47](https://github.com/google/semantic-release-replace-plugin/blob/master/src/index.ts#L47)*
*Defined in [index.ts:55](https://github.com/Borduhh/semantic-release-replace-plugin/blob/6dd4918/src/index.ts#L55)*

___

### `Optional` results
### results

**results**? : *object[]*
`Optional` **results**: { file: string ; hasChanged: boolean ; numMatches?: number ; numReplacements?: number }[]

*Defined in [index.ts:57](https://github.com/google/semantic-release-replace-plugin/blob/master/src/index.ts#L57)*
*Defined in [index.ts:65](https://github.com/Borduhh/semantic-release-replace-plugin/blob/6dd4918/src/index.ts#L65)*

The results array can be passed to ensure that the expected replacements
have been made, and if not, throw and exception with the diff.

___

### to
### to

**to**: *string*
**to**: string \| (a: string) => string

*Defined in [index.ts:46](https://github.com/google/semantic-release-replace-plugin/blob/master/src/index.ts#L46)*
*Defined in [index.ts:54](https://github.com/Borduhh/semantic-release-replace-plugin/blob/6dd4918/src/index.ts#L54)*

The replacement value using a template of variables.

`__VERSION__ = "${nextRelease.version}"`

The context object is used to render the template. Additional values
can be found at: https://semantic-release.gitbook.io/semantic-release/developer-guide/js-api#result

For advacned replacement, pass in a function to replace non-standard variables
```
{
from: `__VERSION__ = 11`, // eslint-disable-line
to: (matched) => `__VERSION: ${parseInt(matched.split('=')[1].trim()) + 1}`, // eslint-disable-line
},
```
14 changes: 8 additions & 6 deletions docs/modules/_index_.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[@google/semantic-release-replace-plugin](../README.md)["index"](_index_.md)
**[@google/semantic-release-replace-plugin](../README.md)**

> [Globals](../README.md) / "index"
# Module: "index"

Expand All @@ -15,17 +17,17 @@

## Functions

### prepare
### prepare

**prepare**(`PluginConfig`: [PluginConfig](../interfaces/_index_.pluginconfig.md), `context`: Context): *Promisevoid*
**prepare**(`PluginConfig`: [PluginConfig](../interfaces/_index_.pluginconfig.md), `context`: Context): Promise<void\>

*Defined in [index.ts:96](https://github.com/google/semantic-release-replace-plugin/blob/master/src/index.ts#L96)*
*Defined in [index.ts:104](https://github.com/Borduhh/semantic-release-replace-plugin/blob/6dd4918/src/index.ts#L104)*

**Parameters:**
#### Parameters:

Name | Type |
------ | ------ |
`PluginConfig` | [PluginConfig](../interfaces/_index_.pluginconfig.md) |
`context` | Context |

**Returns:** *Promisevoid*
**Returns:** Promise<void\>
15 changes: 13 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,16 @@ export interface Replacement {
*
* The context object is used to render the template. Additional values
* can be found at: https://semantic-release.gitbook.io/semantic-release/developer-guide/js-api#result
*
* For advanced replacement (NOTE: only for use with `release.config.js` file version), pass in a function to replace non-standard variables
* ```
* {
* from: `__VERSION__ = 11`, // eslint-disable-line
* to: (matched) => `__VERSION: ${parseInt(matched.split('=')[1].trim()) + 1}`, // eslint-disable-line
* },
* ```
*/
to: string;
to: string | ((a: string) => string);
ignore?: string[];
allowEmptyPaths?: boolean;
countMatches?: boolean;
Expand Down Expand Up @@ -104,7 +112,10 @@ export async function prepare(

const replaceInFileConfig = replacement as ReplaceInFileConfig;

replaceInFileConfig.to = template(replacement.to)({ ...context });
replaceInFileConfig.to =
typeof replacement.to === "function"
? replacement.to
: template(replacement.to)({ ...context });
replaceInFileConfig.from = new RegExp(replacement.from, "gm");

let actual = await replaceInFile(replaceInFileConfig);
Expand Down

0 comments on commit 4bfa3c7

Please sign in to comment.