diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7de901613..818bef5a9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -48,20 +48,3 @@ jobs: with: github_token: ${{ secrets.RELEASE_GH_TOKEN }} branch: next - - # quick assertion to validate that rdme CLI can be installed and run on ubuntu - postrelease: - name: Post-release checks - needs: release - runs-on: ubuntu-latest - if: ${{ github.ref }} == 'refs/heads/next' - steps: - - name: Set up Node.js - uses: actions/setup-node@v4 - with: - node-version: lts/* - - name: Install `rdme` from npm - run: npm install -g rdme@next - - name: Print rdme CLI version - run: rdme --version - timeout-minutes: 1 diff --git a/__tests__/commands/logout.test.ts b/__tests__/commands/logout.test.ts index 901e7dea9..b0fab24e6 100644 --- a/__tests__/commands/logout.test.ts +++ b/__tests__/commands/logout.test.ts @@ -1,6 +1,6 @@ import { describe, afterEach, beforeAll, it, expect } from 'vitest'; -import pkg from '../../package.json' with { type: 'json' }; +import pkg from '../../package.json'; import Command from '../../src/commands/logout.js'; import configStore from '../../src/lib/configstore.js'; import { runCommandAndReturnResult } from '../helpers/oclif.js'; diff --git a/__tests__/commands/open.test.ts b/__tests__/commands/open.test.ts index 3ab9829f0..80b961dd3 100644 --- a/__tests__/commands/open.test.ts +++ b/__tests__/commands/open.test.ts @@ -3,7 +3,7 @@ import type { Version } from '../../src/commands/versions/index.js'; import chalk from 'chalk'; import { describe, afterEach, beforeAll, it, expect } from 'vitest'; -import pkg from '../../package.json' with { type: 'json' }; +import pkg from '../../package.json'; import Command from '../../src/commands/open.js'; import configStore from '../../src/lib/configstore.js'; import { getAPIv1Mock } from '../helpers/get-api-mock.js'; diff --git a/__tests__/commands/whoami.test.ts b/__tests__/commands/whoami.test.ts index aecd0bbee..10c898255 100644 --- a/__tests__/commands/whoami.test.ts +++ b/__tests__/commands/whoami.test.ts @@ -1,6 +1,6 @@ import { describe, afterEach, it, expect, beforeAll } from 'vitest'; -import pkg from '../../package.json' with { type: 'json' }; +import pkg from '../../package.json'; import Command from '../../src/commands/whoami.js'; import configStore from '../../src/lib/configstore.js'; import { runCommandAndReturnResult } from '../helpers/oclif.js'; diff --git a/__tests__/helpers/get-gha-setup.ts b/__tests__/helpers/get-gha-setup.ts index cd382c773..528f6ca80 100644 --- a/__tests__/helpers/get-gha-setup.ts +++ b/__tests__/helpers/get-gha-setup.ts @@ -6,7 +6,7 @@ import { vi } from 'vitest'; import configstore from '../../src/lib/configstore.js'; import { git } from '../../src/lib/createGHA/index.js'; -import * as getPkgVersion from '../../src/lib/getPkg.js'; +import * as getPkgVersion from '../../src/lib/getPkgVersion.js'; import getGitRemoteMock from './get-git-mock.js'; diff --git a/__tests__/lib/createGHA.test.ts b/__tests__/lib/createGHA.test.ts index 943bb90f9..a1cd3d9b8 100644 --- a/__tests__/lib/createGHA.test.ts +++ b/__tests__/lib/createGHA.test.ts @@ -10,7 +10,7 @@ import { describe, beforeEach, afterEach, it, expect, vi, type MockInstance, bef import configstore from '../../src/lib/configstore.js'; import { getConfigStoreKey, getGHAFileName, git } from '../../src/lib/createGHA/index.js'; -import { getMajorPkgVersion } from '../../src/lib/getPkg.js'; +import { getMajorPkgVersion } from '../../src/lib/getPkgVersion.js'; import { after, before } from '../helpers/get-gha-setup.js'; import getGitRemoteMock from '../helpers/get-git-mock.js'; import ghaWorkflowSchema from '../helpers/github-workflow-schema.json' with { type: 'json' }; diff --git a/__tests__/lib/getPkgVersion.test.ts b/__tests__/lib/getPkgVersion.test.ts index ac3f6aff9..9bf6e7285 100644 --- a/__tests__/lib/getPkgVersion.test.ts +++ b/__tests__/lib/getPkgVersion.test.ts @@ -3,7 +3,7 @@ import semver from 'semver'; import { describe, beforeEach, afterEach, it, expect, vi, type MockInstance } from 'vitest'; import pkg from '../../package.json' with { type: 'json' }; -import { getNodeVersion, getPkgVersion, getPkgVersionFromNPM } from '../../src/lib/getPkg.js'; +import { getNodeVersion, getPkgVersion } from '../../src/lib/getPkgVersion.js'; describe('#getNodeVersion()', () => { it('should extract version that matches range in package.json', () => { @@ -27,7 +27,7 @@ describe('#getPkgVersion()', () => { }); it('should grab version from package.json by default', () => { - return expect(getPkgVersion()).toBe(pkg.version); + return expect(getPkgVersion()).resolves.toBe(pkg.version); }); it('should fetch version from npm registry', async () => { @@ -35,7 +35,7 @@ describe('#getPkgVersion()', () => { .get('/rdme') .reply(200, { 'dist-tags': { latest: '1.0' } }); - await expect(getPkgVersionFromNPM('latest')).resolves.toBe('1.0'); + await expect(getPkgVersion('latest')).resolves.toBe('1.0'); mock.done(); }); @@ -43,7 +43,7 @@ describe('#getPkgVersion()', () => { it('should fallback if npm registry fails', async () => { const mock = nock('https://registry.npmjs.com', { encodedQueryParams: true }).get('/rdme').reply(500); - await expect(getPkgVersionFromNPM('latest')).resolves.toBe(pkg.version); + await expect(getPkgVersion('latest')).resolves.toBe(pkg.version); mock.done(); }); diff --git a/bin/dev.js b/bin/dev.js index c87b65b4a..7122c2827 100755 --- a/bin/dev.js +++ b/bin/dev.js @@ -1,4 +1,4 @@ -#!/usr/bin/env npx tsx +#!/usr/bin/env -S npx tsx async function main() { const { execute } = await import('@oclif/core'); diff --git a/bin/run.js b/bin/run.js index 0875bc524..4a32f3b6b 100755 --- a/bin/run.js +++ b/bin/run.js @@ -1,4 +1,6 @@ -#!/usr/bin/env node +#!/usr/bin/env -S node --no-warnings=ExperimentalWarning +// ^ we need this env variable above to hide the ExperimentalWarnings +// source: https://github.com/nodejs/node/issues/10802#issuecomment-573376999 import stringArgv from 'string-argv'; diff --git a/bin/set-version-output.js b/bin/set-version-output.js index 0aee75ce5..41de6484b 100755 --- a/bin/set-version-output.js +++ b/bin/set-version-output.js @@ -3,7 +3,7 @@ import * as core from '@actions/core'; // eslint-disable-next-line import/extensions -import { getNodeVersion, getMajorPkgVersion } from '../dist/lib/getPkg.js'; +import { getNodeVersion, getMajorPkgVersion } from '../dist/lib/getPkgVersion.js'; /** * Sets output parameters for GitHub Actions workflow so we can do diff --git a/src/lib/configstore.ts b/src/lib/configstore.ts index d4517a480..7fc7fccc0 100644 --- a/src/lib/configstore.ts +++ b/src/lib/configstore.ts @@ -1,6 +1,6 @@ import Configstore from 'configstore'; -import { pkg } from './getPkg.js'; +import pkg from '../package.json' with { type: 'json' }; const configstore = new Configstore( /** diff --git a/src/lib/createGHA/index.ts b/src/lib/createGHA/index.ts index ffa744099..539c428ff 100644 --- a/src/lib/createGHA/index.ts +++ b/src/lib/createGHA/index.ts @@ -8,7 +8,7 @@ import prompts from 'prompts'; import { simpleGit } from 'simple-git'; import configstore from '../configstore.js'; -import { getMajorPkgVersion } from '../getPkg.js'; +import { getMajorPkgVersion } from '../getPkgVersion.js'; import isCI, { isNpmScript, isTest } from '../isCI.js'; import { info } from '../logger.js'; import promptTerminal from '../promptWrapper.js'; diff --git a/src/lib/getPkg.ts b/src/lib/getPkgVersion.ts similarity index 55% rename from src/lib/getPkg.ts rename to src/lib/getPkgVersion.ts index cf9c44266..335f7929b 100644 --- a/src/lib/getPkg.ts +++ b/src/lib/getPkgVersion.ts @@ -1,9 +1,9 @@ import type { Hook } from '@oclif/core'; -import { readFileSync } from 'node:fs'; - import semver from 'semver'; +import pkg from '../package.json' with { type: 'json' }; + import { error } from './logger.js'; const registryUrl = 'https://registry.npmjs.com/rdme'; @@ -13,15 +13,6 @@ const registryUrl = 'https://registry.npmjs.com/rdme'; */ type npmDistTag = 'latest'; -/** - * A synchronous function that reads the `package.json` file for use elsewhere. - * Until we drop support Node.js 20, we need to import this way to avoid ExperimentalWarning outputs. - * - * @see {@link https://nodejs.org/docs/latest-v20.x/api/esm.html#import-attributes} - * @see {@link https://www.stefanjudis.com/snippets/how-to-import-json-files-in-es-modules-node-js/} - */ -export const pkg = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), { encoding: 'utf-8' })); - /** * Return the major Node.js version specified in our `package.json` config. * @@ -36,18 +27,6 @@ export function getNodeVersion(): string { return parsedVersion.version; } -/** - * The current `rdme` version, as specified in the `package.json` - * or in the oclif hook context. - * - * @example "8.0.0" - * @note we mock this function in our snapshots - * @see {@link https://stackoverflow.com/a/54245672} - */ -export function getPkgVersion(this: Hook.Context | void): string { - return this?.config?.version || pkg.version; -} - /** * The current `rdme` version * @@ -55,20 +34,20 @@ export function getPkgVersion(this: Hook.Context | void): string { * the version from the `package.json` is returned. * @example "8.0.0" * @see {@link https://docs.npmjs.com/adding-dist-tags-to-packages} - * @note we mock this function in our snapshots + * @note we mock this function in our snapshots, hence it's not the default * @see {@link https://stackoverflow.com/a/54245672} */ -export async function getPkgVersionFromNPM(this: Hook.Context | void, npmDistTag?: npmDistTag): Promise { +export async function getPkgVersion(this: Hook.Context | void, npmDistTag?: npmDistTag): Promise { if (npmDistTag) { return fetch(registryUrl) .then(res => res.json() as Promise<{ 'dist-tags': Record }>) .then(body => body['dist-tags'][npmDistTag]) .catch(err => { error(`error fetching version from npm registry: ${err.message}`); - return getPkgVersion.call(this); + return pkg.version; }); } - return getPkgVersion.call(this); + return this?.config?.version || pkg.version; } /** @@ -77,5 +56,5 @@ export async function getPkgVersionFromNPM(this: Hook.Context | void, npmDistTag * @example 8 */ export async function getMajorPkgVersion(this: Hook.Context | void, npmDistTag?: npmDistTag): Promise { - return semver.major(await getPkgVersionFromNPM.call(this, npmDistTag)); + return semver.major(await getPkgVersion.call(this, npmDistTag)); } diff --git a/src/lib/readmeAPIFetch.ts b/src/lib/readmeAPIFetch.ts index 56aab4b4e..920ca2b79 100644 --- a/src/lib/readmeAPIFetch.ts +++ b/src/lib/readmeAPIFetch.ts @@ -5,10 +5,11 @@ import path from 'node:path'; import mime from 'mime-types'; import { ProxyAgent } from 'undici'; +import pkg from '../package.json' with { type: 'json' }; + import { APIv1Error } from './apiError.js'; import config from './config.js'; import { git } from './createGHA/index.js'; -import { getPkgVersion } from './getPkg.js'; import isCI, { ciName, isGHA } from './isCI.js'; import { debug, warn } from './logger.js'; @@ -89,7 +90,7 @@ function parseWarningHeader(header: string): WarningHeader[] { */ export function getUserAgent() { const gh = isGHA() ? '-github' : ''; - return `rdme${gh}/${getPkgVersion()}`; + return `rdme${gh}/${pkg.version}`; } /**