diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 954ea4be3..a8de1f700 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -10,13 +10,16 @@ jobs: - name: Checkout this repo uses: actions/checkout@v3 + - name: Install and build rdme deps + run: npm ci && npm run build + # Let's dynamically update our docs with the latest version of rdme! # Note that these next three steps are not required # in order to sync your docs to ReadMe. # First, we run a script that sets a few outputs: # our package version and our Node.js version. - - name: Retrieve version values from package.json + - name: Retrieve version values id: rdme-version run: ./bin/set-version-output diff --git a/__tests__/helpers/get-gha-setup.ts b/__tests__/helpers/get-gha-setup.ts index 406589f6c..7d49e8c60 100644 --- a/__tests__/helpers/get-gha-setup.ts +++ b/__tests__/helpers/get-gha-setup.ts @@ -3,8 +3,8 @@ import type { Response } from 'simple-git'; import fs from 'fs'; import configstore from '../../src/lib/configstore'; -import * as createGHAObject from '../../src/lib/createGHA'; import { git } from '../../src/lib/createGHA'; +import * as getPkgVersion from '../../src/lib/getPkgVersion'; import getGitRemoteMock from './get-git-mock'; @@ -33,8 +33,8 @@ export function before(writeFileSyncCb) { process.env.TEST_CREATEGHA = 'true'; - const spy = jest.spyOn(createGHAObject, 'getPkgVersion'); - spy.mockReturnValue('7.8.9'); + const spy = jest.spyOn(getPkgVersion, 'getPkgVersion'); + spy.mockReturnValue(Promise.resolve('7.8.9')); } /** diff --git a/__tests__/lib/createGHA.test.ts b/__tests__/lib/createGHA.test.ts index 1b987874e..45a15de1c 100644 --- a/__tests__/lib/createGHA.test.ts +++ b/__tests__/lib/createGHA.test.ts @@ -21,8 +21,8 @@ import createGHA, { getConfigStoreKey, getGHAFileName, getGitData, + getMajorRdmeVersion, git, - rdmeVersionMajor, } from '../../src/lib/createGHA'; import { after, before } from '../helpers/get-gha-setup'; import getGitRemoteMock from '../helpers/get-git-mock'; @@ -127,13 +127,13 @@ describe('#createGHA', () => { expect(fs.writeFileSync).toHaveBeenCalledWith(getGHAFileName(fileName), expect.any(String)); }); - it('should run if user is on an outdated package version', () => { + it('should run if user is on an outdated package version', async () => { const fileName = `rdme-${cmd}`; prompts.inject([true, 'some-branch', fileName]); const repoRoot = process.cwd(); - configstore.set(getConfigStoreKey(repoRoot), rdmeVersionMajor - 1); + configstore.set(getConfigStoreKey(repoRoot), (await getMajorRdmeVersion()) - 1); return expect(createGHA('', cmd, command.args, opts)).resolves.toMatch( 'Your GitHub Actions workflow file has been created!' @@ -156,7 +156,7 @@ describe('#createGHA', () => { ) ); - expect(configstore.get(getConfigStoreKey(repoRoot))).toBe(rdmeVersionMajor); + expect(configstore.get(getConfigStoreKey(repoRoot))).toBe(await getMajorRdmeVersion()); }); it('should not run if not a repo', () => { @@ -169,10 +169,10 @@ describe('#createGHA', () => { return expect(createGHA('success!', cmd, command.args, opts)).resolves.toBe('success!'); }); - it('should not run if user previously declined to set up GHA for current directory + pkg version', () => { + it('should not run if user previously declined to set up GHA for current directory + pkg version', async () => { const repoRoot = process.cwd(); - configstore.set(getConfigStoreKey(repoRoot), rdmeVersionMajor); + configstore.set(getConfigStoreKey(repoRoot), await getMajorRdmeVersion()); return expect(createGHA('success!', cmd, command.args, opts)).resolves.toBe('success!'); }); diff --git a/bin/set-version-output b/bin/set-version-output index d7fa534a1..b9b863772 100755 --- a/bin/set-version-output +++ b/bin/set-version-output @@ -1,5 +1,5 @@ #! /usr/bin/env node -const pkg = require('../package.json'); +const { getPkgVersion } = require('../dist/src/lib/getPkgVersion'); const getNodeVersion = require('../src/lib/getNodeVersion'); const name1 = 'RDME_VERSION'; @@ -9,5 +9,9 @@ const name2 = 'NODE_VERSION'; * Sets output parameters for GitHub Actions workflow * Docs: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter */ -console.log(`::set-output name=${name1}::${pkg.version}`); // eslint-disable-line no-console -console.log(`::set-output name=${name2}::${getNodeVersion()}`); // eslint-disable-line no-console +async function setOutputs() { + console.log(`::set-output name=${name1}::${await getPkgVersion('latest')}`); // eslint-disable-line no-console + console.log(`::set-output name=${name2}::${getNodeVersion()}`); // eslint-disable-line no-console +} + +setOutputs(); diff --git a/src/lib/createGHA/index.ts b/src/lib/createGHA/index.ts index 9e832c6bb..ae728a889 100644 --- a/src/lib/createGHA/index.ts +++ b/src/lib/createGHA/index.ts @@ -12,8 +12,8 @@ import simpleGit from 'simple-git'; import { transcludeString } from 'hercule/promises'; -import pkg from '../../../package.json'; import configstore from '../configstore'; +import { getPkgVersion } from '../getPkgVersion'; import isCI from '../isCI'; import { debug } from '../logger'; import promptTerminal from '../promptWrapper'; @@ -35,22 +35,12 @@ export const getConfigStoreKey = (repoRoot: string) => `createGHA.${repoRoot}`; const GITHUB_WORKFLOW_DIR = '.github/workflows'; const GITHUB_SECRET_NAME = 'README_API_KEY'; -/** - * The current `rdme` version - * - * @example "8.0.0" - * @note the reason why this is a function is - * because we want to mock it for our snapshots. - * @see {@link https://stackoverflow.com/a/54245672} - */ -export const getPkgVersion = () => pkg.version; - /** * The current major `rdme` version * * @example 8 */ -export const rdmeVersionMajor = semverMajor(getPkgVersion()); +export const getMajorRdmeVersion = async () => semverMajor(await getPkgVersion()); export const git = simpleGit(); @@ -186,6 +176,9 @@ export default async function createGHA( const configVal = configstore.get(getConfigStoreKey(repoRoot)); debug(`repo value in config: ${configVal}`); + const majorPkgVersion = await getMajorRdmeVersion(); + debug(`major pkg version: ${majorPkgVersion}`); + if (!opts.github) { if ( // not a repo @@ -193,7 +186,7 @@ export default async function createGHA( // in a CI environment isCI() || // user has previously declined to set up GHA for current repo and `rdme` package version - configVal === rdmeVersionMajor || + configVal === majorPkgVersion || // is a repo, but only contains non-GitHub remotes (isRepo && containsNonGitHubRemote && !containsGitHubRemote) || // not testing this function @@ -287,7 +280,7 @@ export default async function createGHA( if (!shouldCreateGHA) { // if the user says no, we don't want to bug them again // for this repo and version of `rdme - configstore.set(getConfigStoreKey(repoRoot), rdmeVersionMajor); + configstore.set(getConfigStoreKey(repoRoot), majorPkgVersion); throw new Error( 'GitHub Actions workflow creation cancelled. If you ever change your mind, you can run this command again with the `--github` flag.' ); @@ -298,7 +291,7 @@ export default async function createGHA( cleanCommand: cleanFileName(command), command, commandString: constructCmdString(command, args, opts), - rdmeVersion: getPkgVersion(), + rdmeVersion: await getPkgVersion(), timestamp: new Date().toISOString(), }; diff --git a/src/lib/getPkgVersion.ts b/src/lib/getPkgVersion.ts new file mode 100644 index 000000000..e5e4bfcc4 --- /dev/null +++ b/src/lib/getPkgVersion.ts @@ -0,0 +1,29 @@ +import fetch from 'node-fetch'; + +import pkg from '../../package.json'; + +const registryUrl = 'https://registry.npmjs.com/rdme'; + +/** + * The current `rdme` version + * + * @param npmDistTag the `npm` dist tag to retrieve. If this value is omitted, + * the version from the `package.json` is returned. + * @example "8.0.0" + * @see {@link https://docs.npmjs.com/cli/dist-tag} + * @note we mock this function in our snapshots + * @see {@link https://stackoverflow.com/a/54245672} + */ +export async function getPkgVersion(npmDistTag?: 'latest' | 'next'): Promise { + if (npmDistTag) { + return fetch(registryUrl) + .then(res => res.json()) + .then(body => body['dist-tags'][npmDistTag]) + .catch(() => { + // eslint-disable-next-line no-console + console.error('error fetching version from npm registry'); + return pkg.version; + }); + } + return pkg.version; +}