Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ The `versionPrefix` option (default: `''`) can be used in cases where you'd like

With the above configuration, if release-it determines the new version to be `1.0.0`, it'll be saved as `^1.0.0` in the targeted file.

For less structured files like text or php files, you can use `pattern` to replace the version number. The `pattern` is a regular expression that should match the version number. The first capturing group should match the version number. The `pattern` option only works for files with `type: text/*`:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's the second capturing group that should match, right?


```json
"plugins": {
"@release-it/bumper": {
"out": {
"file": "file.php",
"type": "text/php",
"pattern": "(\\* Version: )(\\d+(\\.\\d+){1,})"
}
}
}
```

## Command-line

Options for this plugin can be set from the command line. Some examples:
Expand Down
17 changes: 12 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const parseFileOption = option => {
const path = (typeof option !== 'string' && option.path) || 'version';
const consumeWholeFile = typeof option !== 'string' ? option.consumeWholeFile : false;
const versionPrefix = typeof option !== 'string' ? option.versionPrefix : null;
return { file, mimeType, path, consumeWholeFile, versionPrefix };
const pattern = typeof option !== 'string' ? option.pattern : null;
return { file, mimeType, path, consumeWholeFile, versionPrefix, pattern };
};

const getFileType = (file, mimeType) => {
Expand Down Expand Up @@ -110,7 +111,7 @@ class Bumper extends Plugin {

return Promise.all(
options.map(async out => {
const { file, mimeType, path, consumeWholeFile, versionPrefix = '' } = parseFileOption(out);
const { file, mimeType, path, consumeWholeFile, versionPrefix = '', pattern } = parseFileOption(out);
this.log.exec(`Writing version to ${file}`, isDryRun);
if (isDryRun) return noop;

Expand Down Expand Up @@ -141,9 +142,15 @@ class Bumper extends Plugin {
case 'ini':
return writeFileSync(file, ini.encode(parsed));
default:
const versionMatch = new RegExp(latestVersion || '', 'g');
const write = (parsed && !consumeWholeFile) ? parsed.replace(versionMatch, version) : version + EOL;
return writeFileSync(file, write);
if (pattern) {
const regex = new RegExp(pattern);
const write = (parsed && !consumeWholeFile) ? parsed.replace(regex, `$1${version}`) : version + EOL;
return writeFileSync(file, write);
} else {
const versionMatch = new RegExp(latestVersion || '', 'g');
const write = (parsed && !consumeWholeFile) ? parsed.replace(versionMatch, version) : version + EOL;
return writeFileSync(file, write);
}
}
})
);
Expand Down
21 changes: 20 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ mock({
'./VERSION-OLD2': `v0.9.0${EOL}`,
'./README.md': `Release v1.0.0${EOL}`,
'./foo.yaml': `version: v1.0.0${EOL}`,
'./invalid.toml': `/# -*- some invalid toml -*-${EOL}version = "1.0.0"${EOL}`
'./invalid.toml': `/# -*- some invalid toml -*-${EOL}version = "1.0.0"${EOL}`,
'./wp-plugin.php': `<?php${EOL}/**${EOL}* Plugin Name: WP plugin${EOL}* Version: 1.0.0${EOL}*/${EOL}<? echo <p>hello world</p>; ?>${EOL}`
});

const namespace = 'bumper';
Expand Down Expand Up @@ -340,3 +341,21 @@ test('should update version in JSON file with prefix', async () => {
await runTasks(plugin);
assert.equal(readFile('./bower.json'), '{\n "version": "^1.0.1"\n}\n');
});

test('should update version php plugin by regex', async () => {
const options = {
[namespace]: {
out: {
file: './wp-plugin.php',
pattern: '(\\* Version: )(\\d+(\\.\\d+){1,})'
}
}
};

const plugin = factory(Bumper, { namespace, options });
await runTasks(plugin);
assert.equal(
readFile('./wp-plugin.php'),
`<?php\n/**\n* Plugin Name: WP plugin\n* Version: 1.0.1\n*/\n<? echo <p>hello world</p>; ?>\n`
);
});