Skip to content

Commit

Permalink
refactor(cli): discover package version at runtime
Browse files Browse the repository at this point in the history
Instead of relying on esbuild to set the package version of the CLI,
we now load it from `VERSION.json`.
`scipts/version.sh` and thus `npm version` update `VERSION.json`.

We could directly load `package.json`,
unfortunately ESbuild is not able to tree-shake JSON files.
I opened the following issue: evanw/esbuild#4000

We have to require NodeJS 20.10.0 or above for import attributes.
  • Loading branch information
Conaclos committed Dec 26, 2024
1 parent 763263d commit 7aa5782
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ This project adheres to [Semantic Versioning][semver].
The format of this changelog is [a variant][lib9-versionning] of [Keep a Changelog][keep-changelog].
New entries must be placed in a section entitled `Unreleased`.

## Unreleased

- BREAKING CHANGES: require Node.js 20.10.0 or above

This allows us to use [import attributes](https://nodejs.org/api/esm.html#import-attributes).

## 0.16.0 (2024-11-02)

- BREAKING CHANGES: require Node.js 20.0.0 or above
Expand Down
1 change: 1 addition & 0 deletions VERSION.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"0.16.0"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"url": "https://github.com/bare-ts/bare/issues"
},
"engines": {
"node": ">=20.0.0"
"node": ">=20.10.0"
},
"type": "module",
"bin": {
Expand Down
5 changes: 2 additions & 3 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ set -eu

# https://node.green/#ES2022
# https://kangax.github.io/compat-table/es2016plus
TARGET='node16.9.0'
VERSION=\""$npm_package_version"\"
TARGET='node20.10.0'

# build .d.ts
tsc --build src/

cp -f dist/index.d.ts dist/index.d.cts

# build ESM
esbuild 'src/**/*.ts' --target=$TARGET --define:VERSION=$VERSION --outdir=dist --log-level=warning
esbuild 'src/**/*.ts' --target=$TARGET --outdir=dist --log-level=warning

# build CommonJS (fallback)
esbuild src/index.ts --bundle --target=$TARGET --platform=node > dist/index.cjs
Expand Down
4 changes: 3 additions & 1 deletion scripts/version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ DATE="$(date -u +%Y-%m-%d)"
# set version and current date
sed -i "s/^## Unreleased$/## $npm_package_version ($DATE)/" CHANGELOG.md

git add CHANGELOG.md
echo "\"$npm_package_version\"" >| VERSION.json

git add CHANGELOG.md VERSION.json
6 changes: 2 additions & 4 deletions src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import * as fs from "node:fs"
import * as process from "node:process"
import * as util from "node:util"
import packageVersion from "../../VERSION.json" with { type: "json" }
import { CompilerError, Config, transform } from "../index.js"

// WARNING: This constant MUST be defined at build time.
declare const VERSION: string

const HELP_TEXT = `
Usage: bare [options] [schema]
Expand Down Expand Up @@ -82,7 +80,7 @@ function main(): void {
if (values.help) {
console.info(HELP_TEXT)
} else if (values.version) {
console.info(VERSION)
console.info(packageVersion)
} else {
if (positionals.length > 1 && positionals[0] === "compile") {
positionals.pop()
Expand Down

0 comments on commit 7aa5782

Please sign in to comment.