Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docs): don't use beta tag #599

Merged
merged 4 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions __tests__/helpers/get-gha-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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'));
erunion marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
12 changes: 6 additions & 6 deletions __tests__/lib/createGHA.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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!'
Expand All @@ -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', () => {
Expand All @@ -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!');
});
Expand Down
10 changes: 7 additions & 3 deletions bin/set-version-output
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();
23 changes: 8 additions & 15 deletions src/lib/createGHA/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();

Expand Down Expand Up @@ -186,14 +176,17 @@ 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
!isRepo ||
// 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
Expand Down Expand Up @@ -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.'
);
Expand All @@ -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(),
};

Expand Down
29 changes: 29 additions & 0 deletions src/lib/getPkgVersion.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
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;
}