Create version file and delete loading package.json version#798
Conversation
Co-authored-by: camcam-lemon <omega.camcamlemon@gmail.com>
|
|
||
| - name: Update version file | ||
| run: | | ||
| echo "export const version = '${{env.RELEASE_VERSION}}'" > src/version.js | ||
| git add src/version.js | ||
|
|
There was a problem hiding this comment.
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
versioninpackage.jsonhas been updated (so we can safely doconst packageJson = JSON.parse(await readFile('package.json', 'utf8'))) - Before the
git commit/git tagis made.
It's also easier for you to test! You can try running npm version prepatch and see if it works!
There was a problem hiding this comment.
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.
Changed it because it is easier and more testable than doing it from github actions
aloisklink
left a comment
There was a problem hiding this comment.
I tested it locally with npm verison prepatch and it worked!
Thanks for the amazing and well-documented PR! If you're ever in the Kantō region of Japan, I owe you a coffee/beer as thanks 😄
|
And it's been released in v11.4.1! Feel free to look at the code at https://www.npmjs.com/package/@mermaid-js/mermaid-cli/v/11.4.1?activeTab=code This PR made it just in time for the release 😜 I hope this solves your issue, and if it doesn't, feel free to make another issue/PR! |
|
Thank you so much for quickly merging and publishing! |
📑 Summary
src/version.js.Resolves #796
📏 Design Decisions
Using JSON Modules proved difficult, so we decided to create a version constant file
📋 Tasks
Make sure you
masterbranch