Skip to content

feat(updater): add YAML support (#33, #748) #137

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

Merged
merged 1 commit into from
Apr 18, 2024
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ 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
```

## Installing `commit-and-tag-version`

### As a local `npm run` script
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'),
};
const PLAIN_TEXT_BUMP_FILES = ['VERSION.txt', 'version.txt'];

Expand Down Expand Up @@ -33,6 +34,9 @@ function getUpdaterByFilename(filename) {
if (filename.endsWith('.csproj')) {
return getUpdaterByType('csproj');
}
if (/\.ya?ml$/.test(filename)) {
return getUpdaterByType('yaml');
}
throw Error(
`Unsupported file (${filename}) provided for bumping.\n Please specify the updater \`type\` or use a custom \`updater\`.`,
);
Expand Down
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 @@ -1365,6 +1365,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
33 changes: 33 additions & 0 deletions test/mocks/pubspec-6.3.1-crlf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This is a comment that should be preserved
name: mock
description: "A mock YAML file"
version: 6.3.1

environment:
dart: ">=3.2.6 <4.0.0"
flutter: ">=1.17.0"

dependencies:
flutter:
sdk: flutter

dev_dependencies:
flutter_test:
sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter packages.
flutter:
uses-material-design: true
generate: true

# More comments here nested under one entry
#
# These comments should also be preserved.

# Another comment block, separated from the first one
assets:
- assets/icons/
- assets/images/
33 changes: 33 additions & 0 deletions test/mocks/pubspec-6.3.1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This is a comment that should be preserved
name: mock
description: "A mock YAML file"
version: 6.3.1

environment:
dart: ">=3.2.6 <4.0.0"
flutter: ">=1.17.0"

dependencies:
flutter:
sdk: flutter

dev_dependencies:
flutter_test:
sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter packages.
flutter:
uses-material-design: true
generate: true

# More comments here nested under one entry
#
# These comments should also be preserved.

# Another comment block, separated from the first one
assets:
- assets/icons/
- assets/images/
33 changes: 33 additions & 0 deletions test/mocks/pubspec-6.4.0-crlf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This is a comment that should be preserved
name: mock
description: "A mock YAML file"
version: 6.4.0

environment:
dart: ">=3.2.6 <4.0.0"
flutter: ">=1.17.0"

dependencies:
flutter:
sdk: flutter

dev_dependencies:
flutter_test:
sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter packages.
flutter:
uses-material-design: true
generate: true

# More comments here nested under one entry
#
# These comments should also be preserved.

# Another comment block, separated from the first one
assets:
- assets/icons/
- assets/images/
33 changes: 33 additions & 0 deletions test/mocks/pubspec-6.4.0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This is a comment that should be preserved
name: mock
description: "A mock YAML file"
version: 6.4.0

environment:
dart: ">=3.2.6 <4.0.0"
flutter: ">=1.17.0"

dependencies:
flutter:
sdk: flutter

dev_dependencies:
flutter_test:
sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter packages.
flutter:
uses-material-design: true
generate: true

# More comments here nested under one entry
#
# These comments should also be preserved.

# Another comment block, separated from the first one
assets:
- assets/icons/
- assets/images/