Skip to content
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

Create version file and delete loading package.json version #798

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion .github/workflows/release-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ jobs:

- name: Prepare release
run: npm version --no-git-tag-version --allow-same-version ${{env.RELEASE_VERSION}}


- name: Update version file
run: |
echo "export const version = '${{env.RELEASE_VERSION}}'" > src/version.js
git add src/version.js

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmm, I don't think this will work, since the git commit is done in the npm version command, so it's already too late to call git add.

How about something like the following:

diff --git a/.github/workflows/release-publish.yml b/.github/workflows/release-publish.yml
index 85d3720..f07a2d1 100644
--- a/.github/workflows/release-publish.yml
+++ b/.github/workflows/release-publish.yml
@@ -43,11 +43,6 @@ jobs:
       - name: Prepare release
         run: npm version --no-git-tag-version --allow-same-version ${{env.RELEASE_VERSION}}

-      - name: Update version file
-        run: |
-          echo "export const version = '${{env.RELEASE_VERSION}}'" > src/version.js
-          git add src/version.js
-
       - name: Convert repository name to lower case
         run: echo "GITHUB_REPOSITORY_LOWER_CASE=$(echo $GITHUB_REPOSITORY | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV

diff --git a/package.json b/package.json
index d879480..3de1d76 100644
--- a/package.json
+++ b/package.json
@@ -25,6 +25,7 @@
     "prepare": "tsc && vite build",
     "prepack": "tsc && vite build",
     "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest",
+    "version": "node scripts/version.js",
     "lint": "standard",
     "lint-fix": "standard --fix"
   },
diff --git a/scripts/version.js b/scripts/version.js
new file mode 100755
index 0000000..0787efa
--- /dev/null
+++ b/scripts/version.js
@@ -0,0 +1,24 @@
+#!/usr/bin/env node
+
+/**
+ * This script reads the version from package.json and writes it to src/version.js.
+ * It also stages the changes to src/version.js in git.
+ *
+ * Should be used as an [`npm version` script](https://docs.npmjs.com/cli/v8/using-npm/scripts#npm-version)!
+ */
+import { execFile } from 'node:child_process'
+import { readFile, writeFile } from 'node:fs/promises'
+import { promisify } from 'node:util'
+
+async function main () {
+  const packageJson = JSON.parse(await readFile('package.json', 'utf8'))
+  const version = packageJson.version
+
+  await writeFile('src/version.js', `export const version = '${version}'\n`)
+  await promisify(execFile)('git', ['add', 'src/version.js'])
+}
+
+main().catch((error) => {
+  console.error(error)
+  process.exit(1)
+})

If you read https://docs.npmjs.com/cli/v10/commands/npm-version#description, it sounds like when calling npm version, the version script in our package.json file is called:

  • After version in package.json has been updated (so we can safely do const packageJson = JSON.parse(await readFile('package.json', 'utf8')))
  • Before the git commit/git tag is made.

It's also easier for you to test! You can try running npm version prepatch and see if it works!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I guess that’s true now that you bring it up.
Apparently, I had the wrong idea about how the npm version command behaves.
Your approach is simpler and makes testing easier perfect!!
I fix my code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! 82236e2

- name: Convert repository name to lower case
run: echo "GITHUB_REPOSITORY_LOWER_CASE=$(echo $GITHUB_REPOSITORY | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV

Expand Down
7 changes: 2 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import { resolve } from 'import-meta-resolve'
import path from 'path'
import puppeteer from 'puppeteer'
import url from 'url'
import { version } from './version.js'

// importing JSON is still experimental in Node.JS https://nodejs.org/docs/latest-v16.x/api/esm.html#json-modules
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const pkg = require('../package.json')
// __dirname is not available in ESM modules by default
const __dirname = url.fileURLToPath(new url.URL('.', import.meta.url))

Expand Down Expand Up @@ -108,7 +105,7 @@ function parseCommanderInt (value, _unused) {
async function cli () {
const commander = new Command()
commander
.version(pkg.version)
.version(version)
.addOption(new Option('-t, --theme [theme]', 'Theme of the chart').choices(['default', 'forest', 'dark', 'neutral']).default('default'))
.addOption(new Option('-w, --width [width]', 'Width of the page').argParser(parseCommanderInt).default(800))
.addOption(new Option('-H, --height [height]', 'Height of the page').argParser(parseCommanderInt).default(600))
Expand Down
1 change: 1 addition & 0 deletions src/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const version = '11.4.0'