Skip to content

Commit

Permalink
feat: throwing error messaging on node 12 being unsupported (#486)
Browse files Browse the repository at this point in the history
* feat: throwing error messaging on node 12 being unsupported

* revert: adding some optional chaining back in

* fix: removing optional chaining from the bin file

* fix: pr feedback

* fix: fixes
  • Loading branch information
erunion authored Apr 1, 2022
1 parent d848ff4 commit e991ef6
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 31 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ jobs:
- name: Run tests
run: npm test

build-node12:
name: Test Suite (Node 12)
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Use Node.js 12
uses: actions/setup-node@v3
with:
node-version: 12

- name: Install deps
run: npm ci

# rdme doesn't work on Node 12 but we just want to run this single test to make sure that
# our "we don't support node 12" error is shown.
- name: Run tests
run: npx jest __tests__/bin.test.js

action:
name: GitHub Action Dry Run
runs-on: ubuntu-latest
Expand Down
31 changes: 31 additions & 0 deletions __tests__/bin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { exec } = require('child_process');
const { isSupportedNodeVersion } = require('../src/lib/nodeVersionUtils');
const pkg = require('../package.json');

describe('bin', () => {
if (isSupportedNodeVersion(process.version)) {
it('should show our help screen', async () => {
expect.assertions(1);

await new Promise(done => {
exec(`node ${__dirname}/../bin/rdme`, (error, stdout) => {
expect(stdout).toContain('a utlity for interacting with ReadMe');
done();
});
});
});
} else {
it('should fail with a message', async () => {
expect.assertions(1);

await new Promise(done => {
exec(`node ${__dirname}/../bin/rdme`, (error, stdout, stderr) => {
expect(stderr).toContain(
`We're sorry, this release of rdme does not support Node.js v${process.version}. We support the following versions: ${pkg.engines.node}`
);
done();
});
});
});
}
});
11 changes: 0 additions & 11 deletions __tests__/lib/getNodeVersion.test.js

This file was deleted.

24 changes: 24 additions & 0 deletions __tests__/lib/nodeVersionUtils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { isSupportedNodeVersion, getNodeVersion } = require('../../src/lib/nodeVersionUtils');
const pkg = require('../../package.json');
const semver = require('semver');

describe('#isSupportedNodeVersion()', () => {
it('should return true for a supported version of node', () => {
expect(isSupportedNodeVersion('14.5.1')).toBe(true);
expect(isSupportedNodeVersion('16.0.0')).toBe(true);
});

it('should return false for an unsupported version of node', () => {
expect(isSupportedNodeVersion('10.0.0')).toBe(false);
expect(isSupportedNodeVersion('12.0.0')).toBe(false);
expect(isSupportedNodeVersion('18.0.0')).toBe(false);
});
});

describe('#getNodeVersion()', () => {
it('should extract version that matches range in package.json', () => {
const version = parseInt(getNodeVersion(), 10);
const cleanedVersion = semver.valid(semver.coerce(version));
expect(semver.satisfies(cleanedVersion, pkg.engines.node)).toBe(true);
});
});
22 changes: 17 additions & 5 deletions bin/rdme
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
#! /usr/bin/env node
/* eslint-disable no-console */
const chalk = require('chalk');
const core = require('@actions/core');

const updateNotifier = require('update-notifier');
const pkg = require('../package.json');

const isGHA = require('../src/lib/isGitHub');
const { isSupportedNodeVersion } = require('../src/lib/nodeVersionUtils');

updateNotifier({ pkg }).notify();

/**
* We use optional chaining throughout the library, which doesn't work on Node 12, so to curb
* support questions about why rdme is throwing an "Unexpected token '.'" error we should hard
* stop if we're being run with any Node version that we don't explicitly support.
*/
if (!isSupportedNodeVersion(process.version)) {
const message = `We're sorry, this release of rdme does not support Node.js v${process.version}. We support the following versions: ${pkg.engines.node}`;
console.error(chalk.red(`\n${message}\n`));
process.exit(1);
}

require('../src')(process.argv.slice(2))
.then(msg => {
// eslint-disable-next-line no-console
if (msg) console.log(msg);
return process.exit(0);
})
Expand All @@ -20,20 +32,20 @@ require('../src')(process.argv.slice(2))
'support@readme.io'
)}.`;

if (err?.message) {
if (err.message) {
message = err.message;
}

/**
* If we're in a GitHub Actions environment, log errors with that formatting instead.
* @link: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message
* @link: https://github.com/actions/toolkit/tree/main/packages/core#annotations
*
* @see {@link https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message}
* @see {@link https://github.com/actions/toolkit/tree/main/packages/core#annotations}
*/
if (isGHA()) {
return core.setFailed(message);
}

// eslint-disable-next-line no-console
console.error(chalk.red(`\n${message}\n`));
return process.exit(1);
});
15 changes: 0 additions & 15 deletions src/lib/getNodeVersion.js

This file was deleted.

21 changes: 21 additions & 0 deletions src/lib/nodeVersionUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const semver = require('semver');
const pkg = require('../../package.json');

module.exports = {
/**
* Determine if the current version of Node is one that we explicitly support.
*
*/
isSupportedNodeVersion(version) {
return semver.satisfies(semver.coerce(version), pkg.engines.node);
},

/**
* @example 14
* @returns {String} The maximum major Node.js version specified in the package.json
*/
getNodeVersion() {
const { node } = pkg.engines;
return Array.from(node.matchAll(/\d+/g)).pop();
},
};

0 comments on commit e991ef6

Please sign in to comment.