|
| 1 | +"use strict"; |
| 2 | +import bag from 'bagofcli'; |
| 3 | +import {getProperty} from 'dot-prop'; |
| 4 | +import fs from 'fs'; |
| 5 | +import hcl from 'hcl2-parser'; |
| 6 | + |
| 7 | +/** |
| 8 | + * 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 |
| 10 | + * modify the HCL file because there is no node.js library that can write HCL |
| 11 | + * files in a safe way while preserving original formatting. NOTE: hcledit |
| 12 | + * must be installed and available in the PATH for this to work. |
| 13 | + * |
| 14 | + * @param {String} version: version value to set |
| 15 | + * @param {Object} resource: resource configuration which contains type, path, and params |
| 16 | + * @param {Object} opts: optional settings |
| 17 | + * - dryRun: when true, HCL file won't be modified |
| 18 | + * @param {Function} cb: standard cb(err, result) callback |
| 19 | + */ |
| 20 | +function setVersion(version, resource, opts, cb) { |
| 21 | + if (!opts.dryRun) { |
| 22 | + const property = resource.params.property; |
| 23 | + function execCb(err, stdOutOuput, stdErrOuput, result) { |
| 24 | + cb(err); |
| 25 | + }; |
| 26 | + bag.exec(`hcledit attribute set ${property} ${version} -f ${resource.path} -u`, false, execCb); |
| 27 | + } else { |
| 28 | + cb(); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Get version value from the HCL resource's property (defined in dot-notation). |
| 34 | + * |
| 35 | + * @param {Object} resource: resource configuration which contains type, path, and params |
| 36 | + * @param {Function} cb: standard cb(err, result) callback |
| 37 | + */ |
| 38 | +function getVersion(resource, cb) { |
| 39 | + const property = resource.params.property; |
| 40 | + |
| 41 | + function readCb(err, result) { |
| 42 | + let version; |
| 43 | + if (!err) { |
| 44 | + const data = hcl.parseToObject(result)[0]; |
| 45 | + version = getProperty(data, property); |
| 46 | + } |
| 47 | + cb(err, version); |
| 48 | + } |
| 49 | + fs.readFile(resource.path, 'UTF-8', readCb); |
| 50 | +} |
| 51 | + |
| 52 | +const exports = { |
| 53 | + setReleaseVersion: setVersion, |
| 54 | + setPostReleaseVersion: setVersion, |
| 55 | + getVersion: getVersion |
| 56 | +}; |
| 57 | + |
| 58 | +export { |
| 59 | + exports as default |
| 60 | +}; |
0 commit comments