node-red-contrib-homebridge-automation 0.2.1-beta.7 → 0.2.2-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,8 +9,6 @@ const child_process = require('node:child_process')
9
9
  const fs = require('node:fs')
10
10
  const process = require('node:process')
11
11
 
12
- const semver = require('semver')
13
-
14
12
  const BRANCH_VERSION_PATTERN = /^([A-Z]+)-(\d+\.\d+\.\d+)$/i
15
13
 
16
14
  // Load the contents of the package.json file
@@ -19,24 +17,29 @@ const packageJSON = JSON.parse(fs.readFileSync('package.json', 'utf8'))
19
17
  const refArgument = process.argv[2]
20
18
  const tagArgument = process.argv[3] || 'latest'
21
19
 
22
- if (refArgument == null) {
20
+ if (!refArgument) {
23
21
  console.error('ref argument is missing')
24
22
  console.error('Usage: npm-version-script.js <ref> [tag]')
25
23
  process.exit(1)
26
24
  }
27
25
 
28
26
  /**
29
- * Queries the NPM registry for the latest version for the provided tag.
30
- * @param tag The tag to query for.
31
- * @returns {string} Returns the version.
27
+ * Queries the NPM registry for the latest version for the provided base version and tag.
28
+ * If the tag is latest, then the base version is returned if it exists. For other tags, the latest
29
+ * version found for that base version and tag is returned.
30
+ * @param baseVersion The base version to query for, e.g. 2.0.0
31
+ * @param tag The tag to query for, e.g. beta or latest
32
+ * @returns {string} Returns the version, or '' if not found
32
33
  */
33
- function getTagVersionFromNpm(tag) {
34
+ function getTagVersionFromNpm(baseVersion, tag) {
34
35
  try {
35
- return child_process.execSync(`npm info ${packageJSON.name} version --tag="${tag}"`).toString('utf8').trim()
36
+ return JSON.parse(child_process.execSync(`npm info ${packageJSON.name} versions --json`).toString('utf8').trim())
37
+ .filter(v => tag === 'latest' ? v === baseVersion : v.startsWith(`${baseVersion}-${tag}.`)) // find all published versions for this base version and tag
38
+ .reduce((_, current) => current, '') // choose the last as they're sorted in ascending order, or '' if there are none
36
39
  } catch (e) {
37
- console.error(`Failed to query the npm registry for the latest version for tag: ${tag}`)
40
+ console.error(`Failed to query the npm registry for the latest version for tag: ${tag}`, e)
38
41
  // throw e;
39
- return '0.0.0'
42
+ return ''
40
43
  }
41
44
  }
42
45
 
@@ -50,7 +53,7 @@ function desiredTargetVersion(ref) {
50
53
  const branchName = ref.slice('refs/heads/'.length)
51
54
 
52
55
  const results = branchName.match(BRANCH_VERSION_PATTERN)
53
- if (results != null) {
56
+ if (results !== null) {
54
57
  if (results[1] !== tagArgument) {
55
58
  console.warn(`The base branch name (${results[1]}) differs from the tag name ${tagArgument}`)
56
59
  }
@@ -65,21 +68,30 @@ function desiredTargetVersion(ref) {
65
68
  const baseVersion = desiredTargetVersion(refArgument)
66
69
 
67
70
  // query the npm registry for the latest version of the provided tag name
68
- const latestReleasedVersion = getTagVersionFromNpm(tagArgument) // e.g. 0.7.0-beta.12
69
- const latestReleaseBase = semver.inc(latestReleasedVersion, 'patch') // will produce 0.7.0 (removing the preid, needed for the equality check below)
71
+ const latestReleasedVersion = getTagVersionFromNpm(baseVersion, tagArgument) // e.g. 0.7.0-beta.12
70
72
 
71
73
  let publishTag
72
- if (semver.eq(baseVersion, latestReleaseBase)) { // check if we are releasing another version for the latest beta or alpha
73
- publishTag = latestReleasedVersion // set the current latest beta or alpha to be incremented
74
+
75
+ if (latestReleasedVersion) {
76
+ console.warn(`Latest published version for ${baseVersion} with tag ${tagArgument} is ${latestReleasedVersion}`)
77
+ publishTag = latestReleasedVersion // set this released beta or alpha to be incremented
74
78
  } else {
75
- publishTag = baseVersion // start of with a new beta or alpha version
79
+ console.warn(`No published versions for ${baseVersion} with tag ${tagArgument}`)
80
+ publishTag = baseVersion // start off with a new beta or alpha version
76
81
  }
77
82
 
78
- // save the package.json
79
- packageJSON.version = publishTag
80
- fs.writeFileSync('package.json', JSON.stringify(packageJSON, null, 2))
83
+ if (packageJSON.version !== publishTag) {
84
+ // report the change for CI
85
+ console.warn(`Changing version in package.json from ${packageJSON.version} to ${publishTag}`)
86
+
87
+ // save the package.json
88
+ packageJSON.version = publishTag
89
+ fs.writeFileSync('package.json', JSON.stringify(packageJSON, null, 2))
81
90
 
82
- // perform the same change to the package-lock.json
83
- const packageLockJSON = JSON.parse(fs.readFileSync('package-lock.json', 'utf8'))
84
- packageLockJSON.version = publishTag
85
- fs.writeFileSync('package-lock.json', JSON.stringify(packageLockJSON, null, 2))
91
+ // perform the same change to the package-lock.json
92
+ const packageLockJSON = JSON.parse(fs.readFileSync('package-lock.json', 'utf8'))
93
+ packageLockJSON.version = publishTag
94
+ fs.writeFileSync('package-lock.json', JSON.stringify(packageLockJSON, null, 2))
95
+ } else {
96
+ console.warn(`Leaving version in package.json at ${packageJSON.version}`)
97
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-homebridge-automation",
3
- "version": "0.2.1-beta.7",
3
+ "version": "0.2.2-beta.0",
4
4
  "description": "NodeRED Automation for HomeBridge",
5
5
  "main": "src/HAP-NodeRed.js",
6
6
  "scripts": {