Skip to content

Commit 50853a7

Browse files
authored
HCL doc update. Add YAML doc separator.
2 parents c844fa1 + 3c4dc3d commit 50853a7

7 files changed

Lines changed: 58 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Added
11+
- Add HCL resource type support
12+
13+
### Changed
14+
- YAML writing ensures --- document separator header
15+
16+
### Fixed
17+
- Fix YAML unit test data incorrectly provided JSON
18+
1019
## 4.0.0 - 2025-07-02
1120
### Changed
1221
- Switch release workflow to use release-action

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ Example `hcl` resource configuration:
167167
]
168168
}
169169

170+
NOTE: This requires
171+
170172
### Managing version on Makefile file
171173

172174
Example `makefile` resource configuration:

lib/resource-types/hcl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import hcl from 'hcl2-parser';
66

77
/**
88
* Set version value in the HCL resource's property (defined in dot-notation).
9-
* This will use hcledit CLI tool (https://github.com/minamijoyo/hcledit) to
9+
* This uses hcledit CLI tool (https://github.com/minamijoyo/hcledit) to
1010
* modify the HCL file because there is no node.js library that can write HCL
1111
* files in a safe way while preserving original formatting. NOTE: hcledit
1212
* must be installed and available in the PATH for this to work.

lib/resource-types/yaml.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,22 @@ function setVersion(version, resource, opts, cb) {
1717
const data = jsYaml.load(fs.readFileSync(resource.path, 'UTF-8'));
1818
setProperty(data, property, version);
1919
if (!opts.dryRun) {
20-
fs.writeFile(resource.path, jsYaml.dump(data, { indent: 2 }), cb);
20+
fs.writeFile(resource.path, _yamlDumpWithHeader(data), cb);
2121
} else {
2222
cb();
2323
}
2424
}
2525

26+
/**
27+
* Prepend YAML header (--- document separator) if missing.
28+
* @param {String} data: the YAML data
29+
* @returns {String} the YAML data with header
30+
*/
31+
function _yamlDumpWithHeader(data) {
32+
const _data = jsYaml.dump(data, { indent: 2 });
33+
return _data.startsWith('---\n') ? _data : `---\n${_data}`;
34+
}
35+
2636
/**
2737
* Get version value from the YAML resource's property (defined in dot-notation).
2838
*

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"keywords": [
66
"release",
77
"versioning",
8+
"hcl",
89
"json",
910
"makefile",
1011
"text",
@@ -54,6 +55,7 @@
5455
"@iarna/toml": "^2.2.5"
5556
},
5657
"devDependencies": {
58+
"esmock": "^2.7.3",
5759
"@sinonjs/referee": "^11.0.1",
5860
"sinon": "^21.0.0"
5961
},

test/resource-types/yaml.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ describe('yaml', function() {
4242
property: 'version'
4343
}
4444
};
45-
this.mockFs.expects('readFileSync').once().withExactArgs('someplaybook.yaml', 'UTF-8').returns('{ "version": "0.0.0" }');
46-
this.mockFs.expects('writeFile').once().withExactArgs('someplaybook.yaml', 'version: 1.2.3\n', sinon.match.func).callsArgWith(2, null);
45+
this.mockFs.expects('readFileSync').once().withExactArgs('someplaybook.yaml', 'UTF-8').returns('version: "0.0.0"');
46+
this.mockFs.expects('writeFile').once().withExactArgs('someplaybook.yaml', '---\nversion: 1.2.3\n', sinon.match.func).callsArgWith(2, null);
4747
function cb(err, result) {
4848
assert.equal(err, null);
4949
done();
@@ -58,14 +58,30 @@ describe('yaml', function() {
5858
property: 'versions[1].minor'
5959
}
6060
};
61-
this.mockFs.expects('readFileSync').once().withExactArgs('someplaybook.yaml', 'UTF-8').returns('{ "versions": [{ "major": 1, "minor": 2, "patch": 3 }, { "major": 8, "minor": 9, "patch": 0 }] }');
62-
this.mockFs.expects('writeFile').once().withExactArgs('someplaybook.yaml', 'versions:\n - major: 1\n minor: 2\n patch: 3\n - major: 8\n minor: 9\n patch: 0\n', sinon.match.func).callsArgWith(2, null);
61+
this.mockFs.expects('readFileSync').once().withExactArgs('someplaybook.yaml', 'UTF-8').returns('---\nversions:\n - major: 1\n minor: 2\n patch: 3\n - major: 8\n minor: 9\n patch: 0\n');
62+
this.mockFs.expects('writeFile').once().withExactArgs('someplaybook.yaml', '---\nversions:\n - major: 1\n minor: 2\n patch: 3\n - major: 8\n minor: 9\n patch: 0\n', sinon.match.func).callsArgWith(2, null);
6363
function cb(err, result) {
6464
assert.equal(err, null);
6565
done();
6666
}
6767
resourceType.setReleaseVersion(9, resource, { dryRun: false }, cb);
6868
});
69+
it('should not double up document separator YAML header when it already exists', function(done) {
70+
const resource = {
71+
path: 'someplaybook.yaml',
72+
type: 'yaml',
73+
params: {
74+
property: 'version'
75+
}
76+
};
77+
this.mockFs.expects('readFileSync').once().withExactArgs('someplaybook.yaml', 'UTF-8').returns('---\nversion: "0.0.0"');
78+
this.mockFs.expects('writeFile').once().withExactArgs('someplaybook.yaml', '---\nversion: 1.2.3\n', sinon.match.func).callsArgWith(2, null);
79+
function cb(err, result) {
80+
assert.equal(err, null);
81+
done();
82+
}
83+
resourceType.setReleaseVersion('1.2.3', resource, { dryRun: false }, cb);
84+
});
6985
});
7086

7187
describe('getVersion', function() {
@@ -77,7 +93,7 @@ describe('yaml', function() {
7793
property: 'version'
7894
}
7995
};
80-
this.mockFs.expects('readFile').once().withExactArgs('someplaybook.yaml', 'UTF-8', sinon.match.func).callsArgWith(2, null, '{ "version": "0.0.0" }');
96+
this.mockFs.expects('readFile').once().withExactArgs('someplaybook.yaml', 'UTF-8', sinon.match.func).callsArgWith(2, null, 'version: "0.0.0"');
8197
function cb(err, result) {
8298
assert.equal(err, null);
8399
assert.equal(result, '0.0.0');
@@ -93,7 +109,7 @@ describe('yaml', function() {
93109
property: 'versions[1].minor'
94110
}
95111
};
96-
this.mockFs.expects('readFile').once().withExactArgs('someplaybook.yaml', 'UTF-8', sinon.match.func).callsArgWith(2, null, '{ "versions": [{ "major": 1, "minor": 2, "patch": 3 }, { "major": 8, "minor": 9, "patch": 0 }] }');
112+
this.mockFs.expects('readFile').once().withExactArgs('someplaybook.yaml', 'UTF-8', sinon.match.func).callsArgWith(2, null, '---\nversions:\n - major: 1\n minor: 2\n patch: 3\n - major: 8\n minor: 9\n patch: 0\n');
97113
function cb(err, result) {
98114
assert.equal(err, null);
99115
assert.equal(result, 9);

0 commit comments

Comments
 (0)