diff --git a/.eslintrc b/.eslintrc index 6550a91ee..7cb7f394d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -20,7 +20,10 @@ * We should be returning Promise-wrapped values in our main command functions * so we can write robust tests and take advantage of `bin/rdme`, * which we use for printing function outputs and returning correct exit codes. + * + * Furthermore, we should also be using our custom loggers (see src/lib/logger.js) + * instead of using console.info() or console.warn() statements. */ - "no-console": ["warn", { "allow": ["info", "warn"] }] + "no-console": ["warn"] } } diff --git a/__tests__/cmds/docs.test.js b/__tests__/cmds/docs.test.js index bf2b23d2f..fd2cb3640 100644 --- a/__tests__/cmds/docs.test.js +++ b/__tests__/cmds/docs.test.js @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ const nock = require('nock'); const chalk = require('chalk'); const fs = require('fs'); diff --git a/__tests__/cmds/openapi.test.js b/__tests__/cmds/openapi.test.js index 0ace20b41..927d12951 100644 --- a/__tests__/cmds/openapi.test.js +++ b/__tests__/cmds/openapi.test.js @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ const nock = require('nock'); const chalk = require('chalk'); const config = require('config'); diff --git a/__tests__/cmds/validate.test.js b/__tests__/cmds/validate.test.js index bd0f59355..9218ff9d3 100644 --- a/__tests__/cmds/validate.test.js +++ b/__tests__/cmds/validate.test.js @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ const fs = require('fs'); const chalk = require('chalk'); const Command = require('../../src/cmds/validate'); diff --git a/__tests__/lib/fetch.test.js b/__tests__/lib/fetch.test.js index a92c69236..29d10ad2b 100644 --- a/__tests__/lib/fetch.test.js +++ b/__tests__/lib/fetch.test.js @@ -9,6 +9,7 @@ describe('#fetch()', () => { // List of all GitHub Actions env variables: // https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables beforeEach(() => { + process.env.GITHUB_ACTION = '__repo-owner_name-of-action-repo'; process.env.GITHUB_ACTIONS = 'true'; process.env.GITHUB_REPOSITORY = 'octocat/Hello-World'; process.env.GITHUB_RUN_ATTEMPT = '3'; @@ -18,6 +19,7 @@ describe('#fetch()', () => { }); afterEach(() => { + delete process.env.GITHUB_ACTION; delete process.env.GITHUB_ACTIONS; delete process.env.GITHUB_REPOSITORY; delete process.env.GITHUB_RUN_ATTEMPT; diff --git a/package-lock.json b/package-lock.json index 64fa574e2..cb3937c7d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "MIT", "dependencies": { "@actions/core": "^1.6.0", + "@npmcli/ci-detect": "^2.0.0", "chalk": "^4.1.2", "cli-table": "^0.3.1", "command-line-args": "^5.2.0", @@ -1266,6 +1267,14 @@ "node": ">= 8" } }, + "node_modules/@npmcli/ci-detect": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@npmcli/ci-detect/-/ci-detect-2.0.0.tgz", + "integrity": "sha512-8yQtQ9ArHh/TzdUDKQwEvwCgpDuhSWTDAbiKMl3854PcT+Dk4UmWaiawuFTLy9n5twzXOBXVflWe+90/ffXQrA==", + "engines": { + "node": "^12.13.0 || ^14.15.0 || >=16" + } + }, "node_modules/@pkgr/utils": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/@pkgr/utils/-/utils-2.3.0.tgz", @@ -13183,6 +13192,11 @@ "fastq": "^1.6.0" } }, + "@npmcli/ci-detect": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@npmcli/ci-detect/-/ci-detect-2.0.0.tgz", + "integrity": "sha512-8yQtQ9ArHh/TzdUDKQwEvwCgpDuhSWTDAbiKMl3854PcT+Dk4UmWaiawuFTLy9n5twzXOBXVflWe+90/ffXQrA==" + }, "@pkgr/utils": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/@pkgr/utils/-/utils-2.3.0.tgz", diff --git a/package.json b/package.json index 385af1348..a1926f699 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ }, "dependencies": { "@actions/core": "^1.6.0", + "@npmcli/ci-detect": "^2.0.0", "chalk": "^4.1.2", "cli-table": "^0.3.1", "command-line-args": "^5.2.0", diff --git a/src/cmds/docs/edit.js b/src/cmds/docs/edit.js index 58578457e..2fccae025 100644 --- a/src/cmds/docs/edit.js +++ b/src/cmds/docs/edit.js @@ -6,7 +6,7 @@ const APIError = require('../../lib/apiError'); const { getProjectVersion } = require('../../lib/versionSelect'); const fetch = require('../../lib/fetch'); const { cleanHeaders, handleRes } = require('../../lib/fetch'); -const { debug } = require('../../lib/logger'); +const { debug, info } = require('../../lib/logger'); const writeFile = promisify(fs.writeFile); const readFile = promisify(fs.readFile); @@ -101,7 +101,7 @@ module.exports = class EditDocsCommand { if (res.error) { return reject(new APIError(res)); } - console.info('Doc successfully updated. Cleaning up local file.'); + info('Doc successfully updated. Cleaning up local file.'); await unlink(filename); debug('file unlinked'); // Normally we should resolve with a value that is logged to the console, diff --git a/src/cmds/openapi.js b/src/cmds/openapi.js index aff820ca2..913b6a955 100644 --- a/src/cmds/openapi.js +++ b/src/cmds/openapi.js @@ -2,7 +2,7 @@ const APIError = require('../lib/apiError'); const chalk = require('chalk'); const { cleanHeaders } = require('../lib/fetch'); const config = require('config'); -const { debug, oraOptions } = require('../lib/logger'); +const { debug, warn, oraOptions } = require('../lib/logger'); const fetch = require('../lib/fetch'); const { handleRes } = require('../lib/fetch'); const { getProjectVersion } = require('../lib/versionSelect'); @@ -66,12 +66,10 @@ module.exports = class OpenAPICommand { } if (version && id) { - console.warn( - chalk.yellow( - `⚠️ Warning! We'll be using the version associated with the \`--${ - opts.token ? 'token' : 'id' - }\` option, so the \`--version\` option will be ignored.` - ) + warn( + `We'll be using the version associated with the \`--${ + opts.token ? 'token' : 'id' + }\` option, so the \`--version\` option will be ignored.` ); } diff --git a/src/cmds/swagger.js b/src/cmds/swagger.js index ade7ccea3..e25053698 100644 --- a/src/cmds/swagger.js +++ b/src/cmds/swagger.js @@ -1,6 +1,5 @@ -const chalk = require('chalk'); const OpenAPICommand = require('./openapi'); -const { debug } = require('../lib/logger'); +const { debug, warn } = require('../lib/logger'); module.exports = class SwaggerCommand extends OpenAPICommand { constructor() { @@ -16,7 +15,7 @@ module.exports = class SwaggerCommand extends OpenAPICommand { debug(`command: ${this.command}`); debug(`opts: ${JSON.stringify(opts)}`); - console.warn(chalk.yellow('⚠️ Warning! `rdme swagger` has been deprecated. Please use `rdme openapi` instead.')); + warn('`rdme swagger` has been deprecated. Please use `rdme openapi` instead.'); return super.run(opts); } }; diff --git a/src/lib/isGitHub.js b/src/lib/isGitHub.js index a8c9fb102..c12e4435c 100644 --- a/src/lib/isGitHub.js +++ b/src/lib/isGitHub.js @@ -1,7 +1,9 @@ +const ciDetect = require('@npmcli/ci-detect'); + /** * Small env check to determine if we're in a GitHub Actions environment * @link https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables */ module.exports = function isGHA() { - return process.env.GITHUB_ACTIONS === 'true'; + return ciDetect() === 'github-actions'; }; diff --git a/src/lib/logger.js b/src/lib/logger.js index c3abc4cdd..6b888f10a 100644 --- a/src/lib/logger.js +++ b/src/lib/logger.js @@ -1,3 +1,4 @@ +const chalk = require('chalk'); const config = require('config'); const core = require('@actions/core'); const debugPackage = require('debug')(config.get('cli')); @@ -13,6 +14,28 @@ module.exports.debug = function debug(input) { return debugPackage(input); }; +/** + * Wrapper for warn statements. + * @param {String} input + */ +module.exports.warn = function warn(input) { + /* istanbul ignore next */ + if (isGHA() && process.env.NODE_ENV !== 'testing') return core.warning(input); + // eslint-disable-next-line no-console + return console.warn(chalk.yellow(`⚠️ Warning! ${input}`)); +}; + +/** + * Wrapper for info/notice statements. + * @param {String} input + */ +module.exports.info = function info(input) { + /* istanbul ignore next */ + if (isGHA() && process.env.NODE_ENV !== 'testing') return core.notice(input); + // eslint-disable-next-line no-console + return console.info(input); +}; + module.exports.oraOptions = function oraOptions() { // Disables spinner in tests so it doesn't pollute test output const opts = { isSilent: process.env.NODE_ENV === 'testing' }; diff --git a/src/lib/prepareOas.js b/src/lib/prepareOas.js index f03a5be67..339e35afd 100644 --- a/src/lib/prepareOas.js +++ b/src/lib/prepareOas.js @@ -3,7 +3,7 @@ const fs = require('fs'); const OASNormalize = require('oas-normalize'); const ora = require('ora'); -const { debug, oraOptions } = require('./logger'); +const { debug, info, oraOptions } = require('./logger'); /** * Normalizes, validates, and (optionally) bundles an OpenAPI definition. @@ -27,7 +27,7 @@ module.exports = async function prepare(path, command) { return; } - console.info( + info( chalk.yellow(`We found ${file} and are attempting to ${command === 'openapi' ? 'upload' : 'validate'} it.`) ); resolve(file);