Skip to content

Commit b9dccc2

Browse files
authored
feat(updater): add YAML support (#137)
1 parent 7ebdacd commit b9dccc2

10 files changed

+245
-0
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ This is going to read and update only the `<Version>` tag in the file.
108108
commit-and-tag-version --packageFiles <YOUR-PROJECT-NAME>.csproj --bumpFiles <YOUR-PROJECT-NAME>.csproj
109109
```
110110

111+
### YAML Support
112+
113+
If you are using YAML files.
114+
This is going to read and update only the `version:` tag in the file.
115+
116+
```sh
117+
commit-and-tag-version --packageFiles file.yaml --bumpFiles file.yaml
118+
```
119+
111120
## Installing `commit-and-tag-version`
112121

113122
### As a local `npm run` script

lib/updaters/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const updatersByType = {
66
maven: require('./types/maven'),
77
gradle: require('./types/gradle'),
88
csproj: require('./types/csproj'),
9+
yaml: require('./types/yaml'),
910
};
1011
const PLAIN_TEXT_BUMP_FILES = ['VERSION.txt', 'version.txt'];
1112

@@ -33,6 +34,9 @@ function getUpdaterByFilename(filename) {
3334
if (filename.endsWith('.csproj')) {
3435
return getUpdaterByType('csproj');
3536
}
37+
if (/\.ya?ml$/.test(filename)) {
38+
return getUpdaterByType('yaml');
39+
}
3640
throw Error(
3741
`Unsupported file (${filename}) provided for bumping.\n Please specify the updater \`type\` or use a custom \`updater\`.`,
3842
);

lib/updaters/types/yaml.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const yaml = require('yaml');
2+
const detectNewline = require('detect-newline');
3+
4+
module.exports.readVersion = function (contents) {
5+
return yaml.parse(contents).version;
6+
};
7+
8+
module.exports.writeVersion = function (contents, version) {
9+
const newline = detectNewline(contents);
10+
const document = yaml.parseDocument(contents);
11+
12+
document.set('version', version);
13+
14+
return document.toString().replace(/\r?\n/g, newline);
15+
};

package-lock.json

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"jsdom": "^23.2.0",
5353
"semver": "^7.5.4",
5454
"w3c-xmlserializer": "^5.0.0",
55+
"yaml": "^2.4.1",
5556
"yargs": "^17.7.2"
5657
},
5758
"devDependencies": {

test/core.spec.js

+72
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,78 @@ describe('cli', function () {
13651365
verifyPackageVersion({ writeFileSyncSpy, expectedVersion: '1.1.0' });
13661366
});
13671367

1368+
it('bumps version in Dart `pubspec.yaml` file', async function () {
1369+
const expected = fs.readFileSync(
1370+
'./test/mocks/pubspec-6.4.0.yaml',
1371+
'utf-8',
1372+
);
1373+
1374+
const filename = 'pubspec.yaml';
1375+
mock({
1376+
bump: 'minor',
1377+
realTestFiles: [
1378+
{
1379+
filename,
1380+
path: './test/mocks/pubspec-6.3.1.yaml',
1381+
},
1382+
],
1383+
});
1384+
1385+
await exec({
1386+
packageFiles: [{ filename, type: 'yaml' }],
1387+
bumpFiles: [{ filename, type: 'yaml' }],
1388+
});
1389+
1390+
// filePath is the first arg passed to writeFileSync
1391+
const packageJsonWriteFileSynchCall = findWriteFileCallForPath({
1392+
writeFileSyncSpy,
1393+
filename,
1394+
});
1395+
1396+
if (!packageJsonWriteFileSynchCall) {
1397+
throw new Error(`writeFileSynch not invoked with path ${filename}`);
1398+
}
1399+
1400+
const calledWithContentStr = packageJsonWriteFileSynchCall[1];
1401+
expect(calledWithContentStr).toEqual(expected);
1402+
});
1403+
1404+
it('bumps version in Dart `pubspec.yaml` file with CRLF line endings', async function () {
1405+
const expected = fs.readFileSync(
1406+
'./test/mocks/pubspec-6.4.0-crlf.yaml',
1407+
'utf-8',
1408+
);
1409+
1410+
const filename = 'pubspec.yaml';
1411+
mock({
1412+
bump: 'minor',
1413+
realTestFiles: [
1414+
{
1415+
filename,
1416+
path: './test/mocks/pubspec-6.3.1-crlf.yaml',
1417+
},
1418+
],
1419+
});
1420+
1421+
await exec({
1422+
packageFiles: [{ filename, type: 'yaml' }],
1423+
bumpFiles: [{ filename, type: 'yaml' }],
1424+
});
1425+
1426+
// filePath is the first arg passed to writeFileSync
1427+
const packageJsonWriteFileSynchCall = findWriteFileCallForPath({
1428+
writeFileSyncSpy,
1429+
filename,
1430+
});
1431+
1432+
if (!packageJsonWriteFileSynchCall) {
1433+
throw new Error(`writeFileSynch not invoked with path ${filename}`);
1434+
}
1435+
1436+
const calledWithContentStr = packageJsonWriteFileSynchCall[1];
1437+
expect(calledWithContentStr).toEqual(expected);
1438+
});
1439+
13681440
describe('skip', function () {
13691441
it('allows bump and changelog generation to be skipped', async function () {
13701442
const changelogContent = 'legacy header format<a name="1.0.0">\n';

test/mocks/pubspec-6.3.1-crlf.yaml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This is a comment that should be preserved
2+
name: mock
3+
description: "A mock YAML file"
4+
version: 6.3.1
5+
6+
environment:
7+
dart: ">=3.2.6 <4.0.0"
8+
flutter: ">=1.17.0"
9+
10+
dependencies:
11+
flutter:
12+
sdk: flutter
13+
14+
dev_dependencies:
15+
flutter_test:
16+
sdk: flutter
17+
18+
# For information on the generic Dart part of this file, see the
19+
# following page: https://dart.dev/tools/pub/pubspec
20+
21+
# The following section is specific to Flutter packages.
22+
flutter:
23+
uses-material-design: true
24+
generate: true
25+
26+
# More comments here nested under one entry
27+
#
28+
# These comments should also be preserved.
29+
30+
# Another comment block, separated from the first one
31+
assets:
32+
- assets/icons/
33+
- assets/images/

test/mocks/pubspec-6.3.1.yaml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This is a comment that should be preserved
2+
name: mock
3+
description: "A mock YAML file"
4+
version: 6.3.1
5+
6+
environment:
7+
dart: ">=3.2.6 <4.0.0"
8+
flutter: ">=1.17.0"
9+
10+
dependencies:
11+
flutter:
12+
sdk: flutter
13+
14+
dev_dependencies:
15+
flutter_test:
16+
sdk: flutter
17+
18+
# For information on the generic Dart part of this file, see the
19+
# following page: https://dart.dev/tools/pub/pubspec
20+
21+
# The following section is specific to Flutter packages.
22+
flutter:
23+
uses-material-design: true
24+
generate: true
25+
26+
# More comments here nested under one entry
27+
#
28+
# These comments should also be preserved.
29+
30+
# Another comment block, separated from the first one
31+
assets:
32+
- assets/icons/
33+
- assets/images/

test/mocks/pubspec-6.4.0-crlf.yaml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This is a comment that should be preserved
2+
name: mock
3+
description: "A mock YAML file"
4+
version: 6.4.0
5+
6+
environment:
7+
dart: ">=3.2.6 <4.0.0"
8+
flutter: ">=1.17.0"
9+
10+
dependencies:
11+
flutter:
12+
sdk: flutter
13+
14+
dev_dependencies:
15+
flutter_test:
16+
sdk: flutter
17+
18+
# For information on the generic Dart part of this file, see the
19+
# following page: https://dart.dev/tools/pub/pubspec
20+
21+
# The following section is specific to Flutter packages.
22+
flutter:
23+
uses-material-design: true
24+
generate: true
25+
26+
# More comments here nested under one entry
27+
#
28+
# These comments should also be preserved.
29+
30+
# Another comment block, separated from the first one
31+
assets:
32+
- assets/icons/
33+
- assets/images/

test/mocks/pubspec-6.4.0.yaml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This is a comment that should be preserved
2+
name: mock
3+
description: "A mock YAML file"
4+
version: 6.4.0
5+
6+
environment:
7+
dart: ">=3.2.6 <4.0.0"
8+
flutter: ">=1.17.0"
9+
10+
dependencies:
11+
flutter:
12+
sdk: flutter
13+
14+
dev_dependencies:
15+
flutter_test:
16+
sdk: flutter
17+
18+
# For information on the generic Dart part of this file, see the
19+
# following page: https://dart.dev/tools/pub/pubspec
20+
21+
# The following section is specific to Flutter packages.
22+
flutter:
23+
uses-material-design: true
24+
generate: true
25+
26+
# More comments here nested under one entry
27+
#
28+
# These comments should also be preserved.
29+
30+
# Another comment block, separated from the first one
31+
assets:
32+
- assets/icons/
33+
- assets/images/

0 commit comments

Comments
 (0)