Skip to content

Commit

Permalink
refactor: use the same implementation pattern as the 'yaml' updater
Browse files Browse the repository at this point in the history
  • Loading branch information
ixuz committed Apr 18, 2024
2 parents 4ae1d5f + b9dccc2 commit 65cd7b7
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 11 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ This is going to read and update only the `<Version>` tag in the file.
commit-and-tag-version --packageFiles <YOUR-PROJECT-NAME>.csproj --bumpFiles <YOUR-PROJECT-NAME>.csproj
```

### YAML Support

If you are using YAML files.
This is going to read and update only the `version:` tag in the file.

```sh
commit-and-tag-version --packageFiles file.yaml --bumpFiles file.yaml
```
### OpenAPI Support

If you are using OpenAPI, then just point to your `openapi.yaml` file.
Expand Down
4 changes: 4 additions & 0 deletions lib/updaters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const updatersByType = {
maven: require('./types/maven'),
gradle: require('./types/gradle'),
csproj: require('./types/csproj'),
yaml: require('./types/yaml'),
openapi: require('./types/openapi'),
};
const PLAIN_TEXT_BUMP_FILES = ['VERSION.txt', 'version.txt'];
Expand Down Expand Up @@ -34,6 +35,9 @@ function getUpdaterByFilename(filename) {
if (filename.endsWith('.csproj')) {
return getUpdaterByType('csproj');
}
if (/\.ya?ml$/.test(filename)) {
return getUpdaterByType('yaml');
}
if (/openapi.yaml/.test(filename)) {
return getUpdaterByType('openapi');
}
Expand Down
22 changes: 11 additions & 11 deletions lib/updaters/types/openapi.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const versionRegex = /(^.*?\r?\ninfo:\r?\n.*?version: ")([\d\w\-.]+)(".*?(?=\r?\n\w).*$)/s;
const yaml = require('yaml');
const detectNewline = require('detect-newline');

module.exports.readVersion = function (contents) {
const matches = versionRegex.exec(contents);
if (matches === null || matches.length !== 4) {
throw new Error(
'Failed to read the version field in your openapi file - is it present?',
);
}
return matches[2];
}
return yaml.parse(contents).info.version;
};

module.exports.writeVersion = function (contents, version) {
return contents.replace(versionRegex, `$1${version}$3`);
}
const newline = detectNewline(contents);
const document = yaml.parseDocument(contents);

document.get('info').set('version', version);

return document.toString().replace(/\r?\n/g, newline);
};
15 changes: 15 additions & 0 deletions lib/updaters/types/yaml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const yaml = require('yaml');
const detectNewline = require('detect-newline');

module.exports.readVersion = function (contents) {
return yaml.parse(contents).version;
};

module.exports.writeVersion = function (contents, version) {
const newline = detectNewline(contents);
const document = yaml.parseDocument(contents);

document.set('version', version);

return document.toString().replace(/\r?\n/g, newline);
};
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"jsdom": "^23.2.0",
"semver": "^7.5.4",
"w3c-xmlserializer": "^5.0.0",
"yaml": "^2.4.1",
"yargs": "^17.7.2"
},
"devDependencies": {
Expand Down
72 changes: 72 additions & 0 deletions test/core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,78 @@ describe('cli', function () {
verifyPackageVersion({ writeFileSyncSpy, expectedVersion: '1.1.0' });
});

it('bumps version in Dart `pubspec.yaml` file', async function () {
const expected = fs.readFileSync(
'./test/mocks/pubspec-6.4.0.yaml',
'utf-8',
);

const filename = 'pubspec.yaml';
mock({
bump: 'minor',
realTestFiles: [
{
filename,
path: './test/mocks/pubspec-6.3.1.yaml',
},
],
});

await exec({
packageFiles: [{ filename, type: 'yaml' }],
bumpFiles: [{ filename, type: 'yaml' }],
});

// filePath is the first arg passed to writeFileSync
const packageJsonWriteFileSynchCall = findWriteFileCallForPath({
writeFileSyncSpy,
filename,
});

if (!packageJsonWriteFileSynchCall) {
throw new Error(`writeFileSynch not invoked with path ${filename}`);
}

const calledWithContentStr = packageJsonWriteFileSynchCall[1];
expect(calledWithContentStr).toEqual(expected);
});

it('bumps version in Dart `pubspec.yaml` file with CRLF line endings', async function () {
const expected = fs.readFileSync(
'./test/mocks/pubspec-6.4.0-crlf.yaml',
'utf-8',
);

const filename = 'pubspec.yaml';
mock({
bump: 'minor',
realTestFiles: [
{
filename,
path: './test/mocks/pubspec-6.3.1-crlf.yaml',
},
],
});

await exec({
packageFiles: [{ filename, type: 'yaml' }],
bumpFiles: [{ filename, type: 'yaml' }],
});

// filePath is the first arg passed to writeFileSync
const packageJsonWriteFileSynchCall = findWriteFileCallForPath({
writeFileSyncSpy,
filename,
});

if (!packageJsonWriteFileSynchCall) {
throw new Error(`writeFileSynch not invoked with path ${filename}`);
}

const calledWithContentStr = packageJsonWriteFileSynchCall[1];
expect(calledWithContentStr).toEqual(expected);
});

describe('skip', function () {
it('allows bump and changelog generation to be skipped', async function () {
const changelogContent = 'legacy header format<a name="1.0.0">\n';
Expand Down

0 comments on commit 65cd7b7

Please sign in to comment.