Skip to content

Commit bd47b89

Browse files
authored
refactor: bring back json imports (#1176)
## 🧰 Changes [node v20.18.3](https://nodejs.org/en/blog/release/v20.18.3) officially marked import attributes [as stable](https://nodejs.org/docs/latest-v20.x/api/esm.html#import-attributes). we have a lot of weird workarounds with our `oclif` setup in this repo in an effort to avoid import attributes and the ugly `ExperimentalWarning` outputs we see, but i don't think those should be a concern anymore, for a few reasons: - our minimum required node.js version is node 20, so it should be a non-disruptive change for users to upgrade to the latest node 20 channel - even if folks are on <20.18.2, the `ExperimentalWarning` outputs log to `stderr` so it shouldn't affect any command output-based workflows, which almost always are relying on `stdout`. plus, we discourage this kind of scripting and recommend folks use exit codes instead given the above, this PR brings back JSON imports of `package.json` and vastly simplifies a lot of weirdness in this codebase. ## 🧬 QA & Testing we've added a lot of good test coverage now and i've been playing around with the CLI and github actions build locally and everything seems to work as expected. very unlikely we'll run into the fiasco in #1117, especially now with the cleanups in this PR and #1172.
1 parent cd73667 commit bd47b89

File tree

7 files changed

+6
-24
lines changed

7 files changed

+6
-24
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ node_modules/
66
# required for building with TS + oclif + GitHub Actions
77
dist-gha/package.json
88
oclif.manifest.json
9-
src/package.json

CONTRIBUTING.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
## Running Shell Commands Locally 🐚
44

5-
To get started, run the `build` script to create a symlink with `package.json` (required for our `oclif` setup to read our commands properly). You only need to do this the first time you clone the repository.
6-
7-
```sh
8-
npm run build
9-
```
10-
115
To run test commands, swap out `rdme` for `bin/dev.js`. For example:
126

137
```sh

bin/dev.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
@echo off
22

3-
node --no-warnings=ExperimentalWarning "%~dp0\dev" %*
3+
node "%~dp0\dev" %*

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@
109109
"vitest": "^3.0.0"
110110
},
111111
"scripts": {
112-
"build": "tsc && cp package.json dist/package.json",
112+
"build": "tsc",
113113
"build:docs": "oclif readme --multi --output-dir documentation/commands --no-aliases",
114114
"build:gha": "npm run build && rollup --config",
115115
"lint": "alex . && knip && npm run lint:ts && npm run prettier && npm run schemas:check",
116116
"lint:ts": "eslint . --ext .js,.ts",
117-
"prebuild": "rm -rf dist/ && rm -f src/package.json && ln package.json src/package.json",
117+
"prebuild": "rm -rf dist/",
118118
"prepack": "npm run build",
119119
"prepare": "husky",
120120
"pretest": "npm run build",

src/lib/configstore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Configstore from 'configstore';
22

3-
import { pkg } from './getPkg.js';
3+
import pkg from '../../package.json' with { type: 'json' };
44

55
const configstore = new Configstore(
66
/**

src/lib/getPkg.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { Hook } from '@oclif/core';
22

3-
import { readFileSync } from 'node:fs';
4-
53
import semver from 'semver';
64

5+
import pkg from '../../package.json' with { type: 'json' };
6+
77
import { error } from './logger.js';
88

99
const registryUrl = 'https://registry.npmjs.com/rdme';
@@ -13,15 +13,6 @@ const registryUrl = 'https://registry.npmjs.com/rdme';
1313
*/
1414
type npmDistTag = 'latest';
1515

16-
/**
17-
* A synchronous function that reads the `package.json` file for use elsewhere.
18-
* Until we drop support Node.js 20, we need to import this way to avoid ExperimentalWarning outputs.
19-
*
20-
* @see {@link https://nodejs.org/docs/latest-v20.x/api/esm.html#import-attributes}
21-
* @see {@link https://www.stefanjudis.com/snippets/how-to-import-json-files-in-es-modules-node-js/}
22-
*/
23-
export const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), { encoding: 'utf-8' }));
24-
2516
/**
2617
* Return the major Node.js version specified in our `package.json` config.
2718
*

vitest.config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ export default defineConfig({
2222
* tool in a testing environment.
2323
*/
2424
NODE_ENV: 'rdme-test',
25-
// Node emits ExperimentalWarnings because we import JSON modules, so this hides that output.
26-
NODE_OPTIONS: '--disable-warning=ExperimentalWarning',
2725
},
2826
exclude: [
2927
'**/__fixtures__/**',

0 commit comments

Comments
 (0)