Skip to content

Commit a3e4823

Browse files
chore(release): 1.0.0 [skip ci]
# 1.0.0 (2024-03-27) ### Bug Fixes * build before release ([36a4632](36a4632)) * build before release ([bef4c0a](bef4c0a)) * **deps:** move lodash to dependencies ([#19](#19)) ([81c8098](81c8098)) * enables multiline regex mode ([#140](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/issues/140)) ([e14d13f](e14d13f)) * improve error message ([#3](#3)) ([b7871c6](b7871c6)) * **index.ts and index.test.ts:** refactored the plugin to support latest version of semantic release ([618cf92](618cf92)) * rename package ([d105286](d105286)) * switch to esm ([#222](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/issues/222)) ([d785268](d785268)) * trigger release ([5a2606b](5a2606b)) * trigger release ([d1112bd](d1112bd)) * trigger release ([a4bd09f](a4bd09f)) * update package.json ([90c2976](90c2976)) ### Features * add support for passing in `replace-in-file` functions ([#156](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/issues/156)) ([4bfa3c7](4bfa3c7)) * support more replace-in-file functionality ([#172](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/issues/172)) ([429ed59](429ed59)), closes [#167](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/issues/167) [#167](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/issues/167)
1 parent 46bd97f commit a3e4823

File tree

7 files changed

+1618
-2208
lines changed

7 files changed

+1618
-2208
lines changed

HISTORY.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 1.0.0 (2024-03-27)
2+
3+
4+
### Bug Fixes
5+
6+
* build before release ([36a4632](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/commit/36a46329f19069b380fea1a25a7d79cfafbd8735))
7+
* build before release ([bef4c0a](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/commit/bef4c0a468596d215eeeb185754e39a6292296f1))
8+
* **deps:** move lodash to dependencies ([#19](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/issues/19)) ([81c8098](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/commit/81c80989435be9073ca1dca65e24966e28db0639))
9+
* enables multiline regex mode ([#140](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/issues/140)) ([e14d13f](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/commit/e14d13f74a675a7aa83eab42172db7c8b3dcdf92))
10+
* improve error message ([#3](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/issues/3)) ([b7871c6](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/commit/b7871c6346bcaf4fce66c0820b20deebaf16286c))
11+
* **index.ts and index.test.ts:** refactored the plugin to support latest version of semantic release ([618cf92](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/commit/618cf9202e7d6707b70a1c69a927786d93918412))
12+
* rename package ([d105286](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/commit/d105286a34f2162c085537684a5f391f34b65f45))
13+
* switch to esm ([#222](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/issues/222)) ([d785268](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/commit/d78526808d0b471a112b0007e866af9d2ecc5aee))
14+
* trigger release ([5a2606b](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/commit/5a2606b0a5b2565e9788d544eea0df77450c9e4d))
15+
* trigger release ([d1112bd](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/commit/d1112bd1f426e659f1f89f3e1bc3d2cbaecc3e5c))
16+
* trigger release ([a4bd09f](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/commit/a4bd09f98ffc012077f19b1deeb028c272fb5b20))
17+
* update package.json ([90c2976](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/commit/90c29769dfe709821f9aa475c23068ac42d6182e))
18+
19+
20+
### Features
21+
22+
* add support for passing in `replace-in-file` functions ([#156](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/issues/156)) ([4bfa3c7](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/commit/4bfa3c72181c82f2b981517a30ebca46add5ee50))
23+
* support more replace-in-file functionality ([#172](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/issues/172)) ([429ed59](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/commit/429ed59bb78cbf5979913fc4a5bc45b7d2c342d4)), closes [#167](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/issues/167) [#167](https://github.com/centralnicgroup-opensource/rtldev-middleware-semantic-release-replace-plugin/issues/167)

dist/index.d.ts

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { VerifyReleaseContext as Context } from "semantic-release";
17+
export type From = FromCallback | RegExp | string;
18+
export type FromCallback = (filename: string, ...args: unknown[]) => RegExp | string;
19+
export type To = string | ToCallback;
20+
export type ToCallback = (match: string, ...args: unknown[]) => string;
21+
/**
22+
* Replacement is similar to the interface used by https://www.npmjs.com/package/replace-in-file
23+
* with the difference being the single string for `to` and `from`.
24+
*/
25+
export interface Replacement {
26+
/**
27+
* files to search for replacements
28+
*/
29+
files: string[];
30+
/**
31+
* The RegExp pattern to use to match.
32+
*
33+
* Uses `String.replace(new RegExp(s, 'gm'), to)` for implementation, if
34+
* `from` is a string.
35+
*
36+
* For advanced matching, i.e. when using a `release.config.js` file, consult
37+
* the documentation of the `replace-in-file` package
38+
* (https://github.com/adamreisnz/replace-in-file/blob/main/README.md) on its
39+
* `from` option. This allows explicit specification of `RegExp`s, callback
40+
* functions, etc.
41+
*
42+
* Multiple matchers may be provided as an array, following the same
43+
* conversion rules as mentioned above.
44+
*/
45+
from: From | From[];
46+
/**
47+
* The replacement value using a template of variables.
48+
*
49+
* `__VERSION__ = "${nextRelease.version}"`
50+
*
51+
* The context object is used to render the template. Additional values
52+
* can be found at: https://semantic-release.gitbook.io/semantic-release/developer-guide/js-api#result
53+
*
54+
* For advanced replacement (NOTE: only for use with `release.config.js` file version), pass in a function to replace non-standard variables
55+
* ```
56+
* {
57+
* from: `__VERSION__ = 11`, // eslint-disable-line
58+
* to: (matched) => `__VERSION: ${parseInt(matched.split('=')[1].trim()) + 1}`, // eslint-disable-line
59+
* },
60+
* ```
61+
*
62+
* The `args` for a callback function can take a variety of shapes. In its
63+
* simplest form, e.g. if `from` is a string, it's the filename in which the
64+
* replacement is done. If `from` is a regular expression the `args` of the
65+
* callback include captures, the offset of the matched string, the matched
66+
* string, etc. See the `String.replace` documentation for details
67+
*
68+
* Multiple replacements may be specified as an array. These can be either
69+
* strings or callback functions. Note that the amount of replacements needs
70+
* to match the amount of `from` matchers.
71+
*/
72+
to: To | To[];
73+
ignore?: string[];
74+
allowEmptyPaths?: boolean;
75+
countMatches?: boolean;
76+
disableGlobs?: boolean;
77+
encoding?: string;
78+
dry?: boolean;
79+
/**
80+
* The results array can be passed to ensure that the expected replacements
81+
* have been made, and if not, throw and exception with the diff.
82+
*/
83+
results?: {
84+
file: string;
85+
hasChanged: boolean;
86+
numMatches?: number;
87+
numReplacements?: number;
88+
}[];
89+
}
90+
/**
91+
* PluginConfig is used to provide multiple replacement.
92+
*
93+
* ```
94+
* [
95+
* "@google/semantic-release-replace-plugin",
96+
* {
97+
* "replacements": [
98+
* {
99+
* "files": ["foo/__init__.py"],
100+
* "from": "__VERSION__ = \".*\"",
101+
* "to": "__VERSION__ = \"${nextRelease.version}\"",
102+
* "results": [
103+
* {
104+
* "file": "foo/__init__.py",
105+
* "hasChanged": true,
106+
* "numMatches": 1,
107+
* "numReplacements": 1
108+
* }
109+
* ],
110+
* "countMatches": true
111+
* }
112+
* ]
113+
* }
114+
* ]
115+
* ```
116+
*/
117+
export interface PluginConfig {
118+
/** An array of replacements to be made. */
119+
replacements: Replacement[];
120+
}
121+
export declare function prepare(PluginConfig: PluginConfig, context: Context): Promise<void>;

dist/index.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import replace from "replace-in-file";
17+
import { isEqual, template } from "lodash-es";
18+
import { diff } from "jest-diff";
19+
/**
20+
* Wraps the `callback` in a new function that passes the `context` as the
21+
* final argument to the `callback` when it gets called.
22+
*/
23+
// eslint-disable-next-line @typescript-eslint/ban-types
24+
function applyContextToCallback(callback, context) {
25+
// eslint-disable-next-line prefer-spread
26+
return (...args) => callback.apply(null, args.concat(context));
27+
}
28+
/**
29+
* Applies the `context` to the replacement property `to` depending on whether
30+
* it is a string template or a callback function.
31+
*/
32+
function applyContextToReplacement(to, context) {
33+
return typeof to === "function"
34+
? applyContextToCallback(to, context)
35+
: template(to)({ ...context });
36+
}
37+
/**
38+
* Normalizes a `value` into an array, making it more straightforward to apply
39+
* logic to a single value of type `T` or an array of those values.
40+
*/
41+
function normalizeToArray(value) {
42+
return value instanceof Array ? value : [value];
43+
}
44+
export async function prepare(PluginConfig, context) {
45+
for (const replacement of PluginConfig.replacements) {
46+
let { results } = replacement;
47+
delete replacement.results;
48+
const replaceInFileConfig = replacement;
49+
// The `replace-in-file` package uses `String.replace` under the hood for
50+
// the actual replacement. If `from` is a string, this means only a
51+
// single occurrence will be replaced. This plugin intents to replace
52+
// _all_ occurrences when given a string to better support
53+
// configuration through JSON, this requires conversion into a `RegExp`.
54+
//
55+
// If `from` is a callback function, the `context` is passed as the final
56+
// parameter to the function. In all other cases, e.g. being a
57+
// `RegExp`, the `from` property does not require any modifications.
58+
//
59+
// The `from` property may either be a single value to match or an array of
60+
// values (in any of the previously described forms)
61+
replaceInFileConfig.from = normalizeToArray(replacement.from).map((from) => {
62+
switch (typeof from) {
63+
case "function":
64+
return applyContextToCallback(from, context);
65+
case "string":
66+
return new RegExp(from, "gm");
67+
default:
68+
return from;
69+
}
70+
});
71+
replaceInFileConfig.to =
72+
replacement.to instanceof Array
73+
? replacement.to.map((to) => applyContextToReplacement(to, context))
74+
: applyContextToReplacement(replacement.to, context);
75+
let actual = await replace(replaceInFileConfig);
76+
if (results) {
77+
results = results.sort();
78+
actual = actual.sort();
79+
if (!isEqual(actual.sort(), results.sort())) {
80+
throw new Error(`Expected match not found!\n${diff(actual, results)}`);
81+
}
82+
}
83+
}
84+
}

dist/index.test.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
export {};

0 commit comments

Comments
 (0)