Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a feature for overwriting the whole output file #32

Merged
merged 3 commits into from
Jul 29, 2023
Merged
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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ To replace all occurences of the current version with the new version in any tex
}
```

:warning: the operation is a search-and-replace; if the current version is not found in the file, the new version cannot be written out.

To instead always consume the entire file, that is, the whole and only content of the file is the version number, set `consumeWholeFile: true` for the `out` option:

```json
"plugins": {
"@release-it/bumper": {
"out": {
"file": "VERSION",
"type": "text/plain",
"consumeWholeFile": true
}
}
}
```

The version number is then written to the output file, overwriting it completely instead of a search-and-replace.

:bulb: Setting `consumeWholeFile: true` precludes the use of prefixes, such as `v1.0.1` in the output file.

The `out` option can also be an array of files:

```json
Expand Down
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const parseFileOption = option => {
const file = isString(option) ? option : option.file;
const mimeType = typeof option !== 'string' ? option.type : null;
const path = (typeof option !== 'string' && option.path) || 'version';
return { file, mimeType, path };
const consumeWholeFile = typeof option !== 'string' ? option.consumeWholeFile : false;
return { file, mimeType, path, consumeWholeFile };
};

const getFileType = (file, mimeType) => {
Expand Down Expand Up @@ -63,7 +64,7 @@ class Bumper extends Plugin {
async getLatestVersion() {
const { in: option } = this.options;
if (!option) return;
const { file, mimeType, path } = parseFileOption(option);
const { file, mimeType, path, consumeWholeFile } = parseFileOption(option);
if (file) {
const type = getFileType(file, mimeType);
let data;
Expand Down Expand Up @@ -109,7 +110,7 @@ class Bumper extends Plugin {

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

Expand Down Expand Up @@ -141,7 +142,7 @@ class Bumper extends Plugin {
return writeFileSync(file, ini.encode(parsed));
default:
const versionMatch = new RegExp(latestVersion || '', 'g');
const write = parsed ? parsed.replace(versionMatch, version) : version + EOL;
const write = (parsed && !consumeWholeFile) ? parsed.replace(versionMatch, version) : version + EOL;
return writeFileSync(file, write);
}
})
Expand Down
67 changes: 67 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ mock({
'./foo.toml': `[tool.test]${EOL}version = "1.0.0"${EOL}`,
'./foo.ini': `path.version=1.0.0${EOL}path.name=fake${EOL}`,
'./VERSION': `v1.0.0${EOL}`,
'./VERSION-OLD': `v0.9.0${EOL}`,
'./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}`
Expand Down Expand Up @@ -257,3 +259,68 @@ test('should give precedence to mime type over file extension', async () => {
await runTasks(plugin);
assert.equal(readFile('./invalid.toml'), `/# -*- some invalid toml -*-${EOL}version = "1.0.1"${EOL}`);
});

test('should read from plain text file, overwrite out-of-date plain version text file, completely', async () => {
const options = {
[namespace]: {
in: { file: 'VERSION', type: 'text/plain' },
out: [
{
file: './VERSION-OLD',
type: 'text/plain',
consumeWholeFile: true
},
{
file: './VERSION',
type: 'text/plain'
}
] }
};
const plugin = factory(Bumper, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./VERSION'), `v1.0.1${EOL}`);
assert.equal(readFile('./VERSION-OLD'), `1.0.1${EOL}`);
});

test('should read from plain text file, not update out-of-date plain version text file', async () => {
const options = {
[namespace]: {
in: { file: 'VERSION', type: 'text/plain' },
out: [
{
file: './VERSION-OLD2',
type: 'text/plain',
consumeWholeFile: false
},
{
file: './VERSION',
type: 'text/plain'
}
] }
};
const plugin = factory(Bumper, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./VERSION'), `v1.0.1${EOL}`);
assert.equal(readFile('./VERSION-OLD2'), `v0.9.0${EOL}`);
});

test('should read from plain text file, not update out-of-date plain version text file (default implied)', async () => {
const options = {
[namespace]: {
in: { file: 'VERSION', type: 'text/plain' },
out: [
{
file: './VERSION-OLD2',
type: 'text/plain'
},
{
file: './VERSION',
type: 'text/plain'
}
] }
};
const plugin = factory(Bumper, { namespace, options });
await runTasks(plugin);
assert.equal(readFile('./VERSION'), `v1.0.1${EOL}`);
assert.equal(readFile('./VERSION-OLD2'), `v0.9.0${EOL}`);
});
Loading