diff --git a/__tests__/cmds/categories/create.test.ts b/__tests__/cmds/categories/create.test.ts index 99a35450a..7429792f4 100644 --- a/__tests__/cmds/categories/create.test.ts +++ b/__tests__/cmds/categories/create.test.ts @@ -22,11 +22,11 @@ describe('rdme categories:create', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(categoriesCreate.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no title provided', () => { diff --git a/__tests__/cmds/categories/index.test.ts b/__tests__/cmds/categories/index.test.ts index 0c9bf8956..ddcc0d658 100644 --- a/__tests__/cmds/categories/index.test.ts +++ b/__tests__/cmds/categories/index.test.ts @@ -22,11 +22,11 @@ describe('rdme categories', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(categories.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should return all categories for a single page', async () => { diff --git a/__tests__/cmds/changelogs/index.test.ts b/__tests__/cmds/changelogs/index.test.ts index b655ba02d..f7e65e87f 100644 --- a/__tests__/cmds/changelogs/index.test.ts +++ b/__tests__/cmds/changelogs/index.test.ts @@ -30,11 +30,11 @@ describe('rdme changelogs', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(changelogs.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no path provided', () => { diff --git a/__tests__/cmds/changelogs/single.test.ts b/__tests__/cmds/changelogs/single.test.ts index 83f883f6c..032eedba0 100644 --- a/__tests__/cmds/changelogs/single.test.ts +++ b/__tests__/cmds/changelogs/single.test.ts @@ -30,11 +30,11 @@ describe('rdme changelogs (single)', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(changelogs.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no file path provided', () => { diff --git a/__tests__/cmds/custompages/index.test.ts b/__tests__/cmds/custompages/index.test.ts index abdb5dc1a..461f19e0d 100644 --- a/__tests__/cmds/custompages/index.test.ts +++ b/__tests__/cmds/custompages/index.test.ts @@ -30,11 +30,11 @@ describe('rdme custompages', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(custompages.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no path provided', () => { diff --git a/__tests__/cmds/custompages/single.test.ts b/__tests__/cmds/custompages/single.test.ts index 2456d70fa..15c0a7c02 100644 --- a/__tests__/cmds/custompages/single.test.ts +++ b/__tests__/cmds/custompages/single.test.ts @@ -30,11 +30,11 @@ describe('rdme custompages (single)', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(custompages.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no file path provided', () => { diff --git a/__tests__/cmds/docs/edit.test.ts b/__tests__/cmds/docs/edit.test.ts index 02bf91f46..ebd3364da 100644 --- a/__tests__/cmds/docs/edit.test.ts +++ b/__tests__/cmds/docs/edit.test.ts @@ -40,15 +40,15 @@ describe('rdme docs:edit', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(docsEdit.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.')); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should log deprecation notice', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(docsEdit.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.')); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; expect(getWarningCommandOutput()).toMatch('is now deprecated'); }); diff --git a/__tests__/cmds/docs/index.test.ts b/__tests__/cmds/docs/index.test.ts index 0e46e33ed..6002fa3d5 100644 --- a/__tests__/cmds/docs/index.test.ts +++ b/__tests__/cmds/docs/index.test.ts @@ -40,9 +40,9 @@ describe('rdme docs', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(docs.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.')); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no path provided', async () => { diff --git a/__tests__/cmds/docs/prune.test.ts b/__tests__/cmds/docs/prune.test.ts index 055f389a7..ccbcd1398 100644 --- a/__tests__/cmds/docs/prune.test.ts +++ b/__tests__/cmds/docs/prune.test.ts @@ -28,11 +28,11 @@ describe('rdme docs:prune', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(docsPrune.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no folder provided', () => { diff --git a/__tests__/cmds/docs/single.test.ts b/__tests__/cmds/docs/single.test.ts index 8e37368db..e030eb854 100644 --- a/__tests__/cmds/docs/single.test.ts +++ b/__tests__/cmds/docs/single.test.ts @@ -33,9 +33,9 @@ describe('rdme docs (single)', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(docs.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.')); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no file path provided', async () => { diff --git a/__tests__/cmds/openapi/index.test.ts b/__tests__/cmds/openapi/index.test.ts index 86eb35779..7d6cc1e8b 100644 --- a/__tests__/cmds/openapi/index.test.ts +++ b/__tests__/cmds/openapi/index.test.ts @@ -405,11 +405,11 @@ describe('rdme openapi', () => { describe('CI spec selection', () => { beforeEach(() => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; }); afterEach(() => { - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error out if multiple possible spec matches were found', () => { @@ -737,11 +737,11 @@ describe('rdme openapi', () => { describe('CI version handling', () => { beforeEach(() => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; }); afterEach(() => { - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should omit version header in CI environment', async () => { @@ -842,11 +842,11 @@ describe('rdme openapi', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(openapi.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if `--create` and `--update` flags are passed simultaneously', () => { diff --git a/__tests__/cmds/openapi/validate.test.ts b/__tests__/cmds/openapi/validate.test.ts index dfa768296..4d31631b4 100644 --- a/__tests__/cmds/openapi/validate.test.ts +++ b/__tests__/cmds/openapi/validate.test.ts @@ -115,18 +115,18 @@ describe('rdme openapi:validate', () => { describe('CI tests', () => { beforeEach(() => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; }); afterEach(() => { - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should successfully validate prompt and not run GHA onboarding', async () => { - process.env.TEST_CREATEGHA = 'true'; + process.env.TEST_RDME_CREATEGHA = 'true'; const spec = '__tests__/__fixtures__/petstore-simple-weird-version.json'; await expect(validate.run({ spec })).resolves.toBe(chalk.green(`${spec} is a valid OpenAPI API definition!`)); - delete process.env.TEST_CREATEGHA; + delete process.env.TEST_RDME_CREATEGHA; }); it('should fail if user attempts to pass `--github` flag in CI environment', () => { diff --git a/__tests__/cmds/versions/create.test.ts b/__tests__/cmds/versions/create.test.ts index 765c5336e..879c03a26 100644 --- a/__tests__/cmds/versions/create.test.ts +++ b/__tests__/cmds/versions/create.test.ts @@ -23,11 +23,11 @@ describe('rdme versions:create', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(createVersion.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should error if no version provided', () => { diff --git a/__tests__/cmds/versions/delete.test.ts b/__tests__/cmds/versions/delete.test.ts index d27443335..0817d866a 100644 --- a/__tests__/cmds/versions/delete.test.ts +++ b/__tests__/cmds/versions/delete.test.ts @@ -23,11 +23,11 @@ describe('rdme versions:delete', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(deleteVersion.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should delete a specific version', async () => { diff --git a/__tests__/cmds/versions/index.test.ts b/__tests__/cmds/versions/index.test.ts index 3c64ea5ff..c5893c071 100644 --- a/__tests__/cmds/versions/index.test.ts +++ b/__tests__/cmds/versions/index.test.ts @@ -45,9 +45,9 @@ describe('rdme versions', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(versions.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.')); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should make a request to get a list of existing versions', async () => { diff --git a/__tests__/cmds/versions/update.test.ts b/__tests__/cmds/versions/update.test.ts index a45c7d0b7..0848e9504 100644 --- a/__tests__/cmds/versions/update.test.ts +++ b/__tests__/cmds/versions/update.test.ts @@ -23,11 +23,11 @@ describe('rdme versions:update', () => { }); it('should error in CI if no API key provided', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(updateVersion.run({})).rejects.toStrictEqual( new Error('No project API key provided. Please use `--key`.') ); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; }); it('should update a specific version object', async () => { diff --git a/__tests__/helpers/get-gha-setup.ts b/__tests__/helpers/get-gha-setup.ts index 031ac459a..e880d0fc9 100644 --- a/__tests__/helpers/get-gha-setup.ts +++ b/__tests__/helpers/get-gha-setup.ts @@ -33,7 +33,7 @@ export function before(writeFileSyncCb) { // no need to construct everything global.Date = jest.fn(() => DATE_TO_USE); - process.env.TEST_CREATEGHA = 'true'; + process.env.TEST_RDME_CREATEGHA = 'true'; const spy = jest.spyOn(getPkgVersion, 'getPkgVersion'); spy.mockReturnValue(Promise.resolve('7.8.9')); @@ -44,7 +44,7 @@ export function before(writeFileSyncCb) { */ export function after() { configstore.clear(); - delete process.env.TEST_CREATEGHA; + delete process.env.TEST_RDME_CREATEGHA; jest.clearAllMocks(); process.chdir(testWorkingDir); } diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index c6ac6fd32..c2ce54132 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -92,24 +92,9 @@ describe('cli', () => { await expect(cli([])).resolves.toContain('OpenAPI / Swagger'); }); - it('should add stored apiKey to opts', async () => { - expect.assertions(1); - const key = '123456'; - const version = '1.0.0'; - conf.set('apiKey', key); - - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); - - await expect(cli(['docs', `--version=${version}`])).rejects.toStrictEqual( - new Error('No path provided. Usage `rdme docs [options]`.') - ); - - conf.clear(); - versionMock.done(); - }); - - describe('logged-in user notifications', () => { + describe('stored API key', () => { let consoleInfoSpy; + const key = '123456'; const getCommandOutput = () => { return [consoleInfoSpy.mock.calls.join('\n\n')].filter(Boolean).join('\n\n'); }; @@ -117,7 +102,7 @@ describe('cli', () => { beforeEach(() => { conf.set('email', 'owlbert@readme.io'); conf.set('project', 'owlbert'); - conf.set('apiKey', '123456'); + conf.set('apiKey', key); consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation(); }); @@ -126,6 +111,23 @@ describe('cli', () => { conf.clear(); }); + it('should add stored apiKey to opts', async () => { + expect.assertions(1); + const version = '1.0.0'; + + const versionMock = getAPIMock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); + + await expect(cli(['docs', `--version=${version}`])).rejects.toStrictEqual( + new Error('No path provided. Usage `rdme docs [options]`.') + ); + + conf.clear(); + versionMock.done(); + }); + it('should inform a logged in user which project is being updated', async () => { await expect(cli(['openapi', '--create', '--update'])).rejects.toThrow( 'The `--create` and `--update` options cannot be used simultaneously. Please use one or the other!' diff --git a/__tests__/lib/createGHA.test.ts b/__tests__/lib/createGHA.test.ts index 86e15434d..be018d6d9 100644 --- a/__tests__/lib/createGHA.test.ts +++ b/__tests__/lib/createGHA.test.ts @@ -175,11 +175,19 @@ describe('#createGHA', () => { }); it('should not run if in a CI environment', async () => { - process.env.TEST_CI = 'true'; + process.env.TEST_RDME_CI = 'true'; await expect(createGHA('success!', cmd, command.args, opts)).resolves.toBe('success!'); // asserts that git commands aren't run in CI expect(git.checkIsRepo).not.toHaveBeenCalled(); - delete process.env.TEST_CI; + delete process.env.TEST_RDME_CI; + }); + + it('should not run if in an npm lifecycle', async () => { + process.env.TEST_RDME_NPM_SCRIPT = 'true'; + await expect(createGHA('success!', cmd, command.args, opts)).resolves.toBe('success!'); + // asserts that git commands aren't run in CI + expect(git.checkIsRepo).not.toHaveBeenCalled(); + delete process.env.TEST_RDME_NPM_SCRIPT; }); it('should not run if repo only contains non-GitHub remotes', () => { diff --git a/__tests__/setup.js b/__tests__/setup.js index 029058c1c..c46a875ec 100644 --- a/__tests__/setup.js +++ b/__tests__/setup.js @@ -2,3 +2,7 @@ // colorization here for all tests. // https://github.com/chalk/supports-color/issues/106 process.env.FORCE_COLOR = 0; + +// Sets our test NODE_ENV to a custom value in case of false positives +// if someone is using this tool in a testing environment +process.env.NODE_ENV = 'rdme-test'; diff --git a/config/test.json b/config/rdme-test.json similarity index 100% rename from config/test.json rename to config/rdme-test.json diff --git a/package-lock.json b/package-lock.json index e2037f9ae..3aae87a5b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -65,6 +65,7 @@ "jest": "^29.0.3", "jest-extended": "^3.0.0", "js-yaml": "^4.1.0", + "keyv": "^4.5.0", "nock": "^13.2.7", "prettier": "^2.7.1", "ts-jest": "^29.0.0", @@ -1789,11 +1790,6 @@ "integrity": "sha512-FhpRzf927MNQdRZP0J5DLIdTXhjLYzeUTmLAu69mnVksLH9CJY3IuSeEgbKUki7GQZm0WqDkGzyxju2EZGD2wA==", "dev": true }, - "node_modules/@types/json-buffer": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/@types/json-buffer/-/json-buffer-3.0.0.tgz", - "integrity": "sha512-3YP80IxxFJB4b5tYC2SUPwkg0XQLiu0nWvhRgEatgjf+29IcWO9X1k8xRv5DGssJ/lCrjYTjQPcobJr2yWIVuQ==" - }, "node_modules/@types/json-schema": { "version": "7.0.11", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.11.tgz", @@ -3047,23 +3043,6 @@ "node": ">= 12.0.0" } }, - "node_modules/compress-brotli": { - "version": "1.3.8", - "resolved": "https://registry.npmjs.org/compress-brotli/-/compress-brotli-1.3.8.tgz", - "integrity": "sha512-lVcQsjhxhIXsuupfy9fmZUFtAIdBmXA7EGY6GBdgZ++qkM9zG4YFT8iU7FoBxzryNDMOpD1HIFHUSX4D87oqhQ==", - "dependencies": { - "@types/json-buffer": "~3.0.0", - "json-buffer": "~3.0.1" - }, - "engines": { - "node": ">= 12" - } - }, - "node_modules/compress-brotli/node_modules/json-buffer": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", - "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==" - }, "node_modules/compute-gcd": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/compute-gcd/-/compute-gcd-1.2.1.tgz", @@ -5300,20 +5279,6 @@ "url": "https://github.com/sindresorhus/got?sponsor=1" } }, - "node_modules/hercule/node_modules/json-buffer": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", - "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==" - }, - "node_modules/hercule/node_modules/keyv": { - "version": "4.3.3", - "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.3.3.tgz", - "integrity": "sha512-AcysI17RvakTh8ir03+a3zJr5r0ovnAH/XTXei/4HIv3bL2K/jzvgivLK9UuI/JbU1aJjM3NSAnVvVVd3n+4DQ==", - "dependencies": { - "compress-brotli": "^1.3.8", - "json-buffer": "3.0.1" - } - }, "node_modules/hercule/node_modules/lowercase-keys": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-2.0.0.tgz", @@ -7083,6 +7048,11 @@ "node": ">=4" } }, + "node_modules/json-buffer": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", + "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==" + }, "node_modules/json-parse-even-better-errors": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz", @@ -7198,6 +7168,14 @@ "integrity": "sha512-txPHx6nVLhv8PHGXIlAk0nYoh894SpAqGPXNvbg2hh8spvHXIah3+vT87DLoa59nKgC6scD3u3xAuRIgiMqbfQ==", "dev": true }, + "node_modules/keyv": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.0.tgz", + "integrity": "sha512-2YvuMsA+jnFGtBareKqgANOEKe1mk3HKiXu2fRmAfyxG0MJAywNhi5ttWA3PMjl4NmpyjZNbFifR2vNjW1znfA==", + "dependencies": { + "json-buffer": "3.0.1" + } + }, "node_modules/kind-of": { "version": "6.0.3", "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz", @@ -9737,19 +9715,6 @@ "url": "https://github.com/sindresorhus/got?sponsor=1" } }, - "node_modules/update-notifier-cjs/node_modules/json-buffer": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", - "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==" - }, - "node_modules/update-notifier-cjs/node_modules/keyv": { - "version": "4.5.0", - "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.0.tgz", - "integrity": "sha512-2YvuMsA+jnFGtBareKqgANOEKe1mk3HKiXu2fRmAfyxG0MJAywNhi5ttWA3PMjl4NmpyjZNbFifR2vNjW1znfA==", - "dependencies": { - "json-buffer": "3.0.1" - } - }, "node_modules/update-notifier-cjs/node_modules/lowercase-keys": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-2.0.0.tgz", @@ -11476,11 +11441,6 @@ "integrity": "sha512-FhpRzf927MNQdRZP0J5DLIdTXhjLYzeUTmLAu69mnVksLH9CJY3IuSeEgbKUki7GQZm0WqDkGzyxju2EZGD2wA==", "dev": true }, - "@types/json-buffer": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/@types/json-buffer/-/json-buffer-3.0.0.tgz", - "integrity": "sha512-3YP80IxxFJB4b5tYC2SUPwkg0XQLiu0nWvhRgEatgjf+29IcWO9X1k8xRv5DGssJ/lCrjYTjQPcobJr2yWIVuQ==" - }, "@types/json-schema": { "version": "7.0.11", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.11.tgz", @@ -12368,22 +12328,6 @@ "integrity": "sha512-B52sN2VNghyq5ofvUsqZjmk6YkihBX5vMSChmSK9v4ShjKf3Vk5Xcmgpw4o+iIgtrnM/u5FiMpz9VKb8lpBveA==", "dev": true }, - "compress-brotli": { - "version": "1.3.8", - "resolved": "https://registry.npmjs.org/compress-brotli/-/compress-brotli-1.3.8.tgz", - "integrity": "sha512-lVcQsjhxhIXsuupfy9fmZUFtAIdBmXA7EGY6GBdgZ++qkM9zG4YFT8iU7FoBxzryNDMOpD1HIFHUSX4D87oqhQ==", - "requires": { - "@types/json-buffer": "~3.0.0", - "json-buffer": "~3.0.1" - }, - "dependencies": { - "json-buffer": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", - "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==" - } - } - }, "compute-gcd": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/compute-gcd/-/compute-gcd-1.2.1.tgz", @@ -14035,20 +13979,6 @@ "responselike": "^2.0.0" } }, - "json-buffer": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", - "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==" - }, - "keyv": { - "version": "4.3.3", - "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.3.3.tgz", - "integrity": "sha512-AcysI17RvakTh8ir03+a3zJr5r0ovnAH/XTXei/4HIv3bL2K/jzvgivLK9UuI/JbU1aJjM3NSAnVvVVd3n+4DQ==", - "requires": { - "compress-brotli": "^1.3.8", - "json-buffer": "3.0.1" - } - }, "lowercase-keys": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-2.0.0.tgz", @@ -15323,6 +15253,11 @@ "integrity": "sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==", "dev": true }, + "json-buffer": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", + "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==" + }, "json-parse-even-better-errors": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz", @@ -15415,6 +15350,14 @@ "integrity": "sha512-txPHx6nVLhv8PHGXIlAk0nYoh894SpAqGPXNvbg2hh8spvHXIah3+vT87DLoa59nKgC6scD3u3xAuRIgiMqbfQ==", "dev": true }, + "keyv": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.0.tgz", + "integrity": "sha512-2YvuMsA+jnFGtBareKqgANOEKe1mk3HKiXu2fRmAfyxG0MJAywNhi5ttWA3PMjl4NmpyjZNbFifR2vNjW1znfA==", + "requires": { + "json-buffer": "3.0.1" + } + }, "kind-of": { "version": "6.0.3", "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz", @@ -17297,19 +17240,6 @@ "responselike": "^2.0.0" } }, - "json-buffer": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", - "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==" - }, - "keyv": { - "version": "4.5.0", - "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.0.tgz", - "integrity": "sha512-2YvuMsA+jnFGtBareKqgANOEKe1mk3HKiXu2fRmAfyxG0MJAywNhi5ttWA3PMjl4NmpyjZNbFifR2vNjW1znfA==", - "requires": { - "json-buffer": "3.0.1" - } - }, "lowercase-keys": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-2.0.0.tgz", diff --git a/package.json b/package.json index 0484ae129..0289bf22e 100644 --- a/package.json +++ b/package.json @@ -87,6 +87,7 @@ "jest": "^29.0.3", "jest-extended": "^3.0.0", "js-yaml": "^4.1.0", + "keyv": "^4.5.0", "nock": "^13.2.7", "prettier": "^2.7.1", "ts-jest": "^29.0.0", @@ -99,12 +100,13 @@ "bump": "npm version -m 'build: %s release'", "debug": "ts-node src/cli.ts", "lint": "eslint . bin/rdme bin/set-version-output --ext .js,.ts", - "lint-docs": "npx alex@10 . && npm run prettier", + "lint-docs": "npx alex@10 . && npm run prettier:docs", "postversion": "git tag $npm_package_version", "prebuild": "rm -rf dist/", "prepack": "npm run build", "pretest": "npm run lint && npm run lint-docs", "prettier": "prettier --list-different \"./**/**.{md,js,ts}\"", + "prettier:docs": "prettier --list-different \"./**/**.md\"", "prettier:write": "prettier --list-different --write \"./**/**.{md,js,ts}\"", "release": "npx conventional-changelog-cli -i CHANGELOG.md -s", "test": "jest --coverage", diff --git a/src/lib/createGHA/index.ts b/src/lib/createGHA/index.ts index 48c850974..17d147af6 100644 --- a/src/lib/createGHA/index.ts +++ b/src/lib/createGHA/index.ts @@ -15,7 +15,7 @@ import { transcludeString } from 'hercule/promises'; import { checkFilePath, cleanFileName } from '../checkFile'; import configstore from '../configstore'; import { getPkgVersion } from '../getPkgVersion'; -import isCI from '../isCI'; +import isCI, { isNpmScript, isTest } from '../isCI'; import { debug, info } from '../logger'; import promptTerminal from '../promptWrapper'; @@ -167,7 +167,7 @@ export default async function createGHA( // if in a CI environment, // don't even bother running the git commands - if (!opts.github && isCI()) { + if (!opts.github && (isCI() || isNpmScript())) { return msg; } @@ -188,7 +188,7 @@ export default async function createGHA( // is a repo, but only contains non-GitHub remotes (isRepo && containsNonGitHubRemote && !containsGitHubRemote) || // not testing this function - (process.env.NODE_ENV === 'test' && !process.env.TEST_CREATEGHA) + (isTest() && !process.env.TEST_RDME_CREATEGHA) ) { debug('not running GHA onboarding workflow, exiting'); // We return the original command message and pretend this command flow never happened. diff --git a/src/lib/isCI.ts b/src/lib/isCI.ts index df2621c63..0c1e06776 100644 --- a/src/lib/isCI.ts +++ b/src/lib/isCI.ts @@ -9,13 +9,36 @@ export function isGHA() { return ciDetect() === 'github-actions'; } +/** + * Small env check to determine if we're running our testbed + */ +export function isTest() { + return process.env.NODE_ENV === 'rdme-test'; +} + +/** + * Small env check to determine if the current command is being run + * via an npm (or yarn) script. + * + * The reason we have this weird conditional logic is because we run our tests via + * an npm script and we don't want false positives when running tests. + * + * @see {@link https://docs.npmjs.com/cli/v8/using-npm/scripts#current-lifecycle-event} + * @see {@link https://docs.npmjs.com/cli/v6/using-npm/scripts#current-lifecycle-event} + * @see {@link https://yarnpkg.com/advanced/lifecycle-scripts} + */ +export function isNpmScript() { + /* istanbul ignore next */ + return (!!process.env.npm_lifecycle_event && !isTest()) || !!process.env.TEST_RDME_NPM_SCRIPT; +} + /** * Small check to ensure we're in a safe CI environment. * - * The reason we have this `env` variable is because we run our tests in a CI environment - * and we don't want false positives when running tests on logic only intended for CI. + * The reason we have this weird conditional logic is because we run our tests in + * a CI environment and we don't want false positives when running tests. */ export default function isCI() { /* istanbul ignore next */ - return (ciDetect() && process.env.NODE_ENV !== 'test') || process.env.TEST_CI; + return (ciDetect() && !isTest()) || !!process.env.TEST_RDME_CI; } diff --git a/src/lib/logger.ts b/src/lib/logger.ts index f3d1b9135..26caa8433 100644 --- a/src/lib/logger.ts +++ b/src/lib/logger.ts @@ -6,7 +6,7 @@ import chalk from 'chalk'; import config from 'config'; import debugModule from 'debug'; -import { isGHA } from './isCI'; +import { isGHA, isTest } from './isCI'; const debugPackage = debugModule(config.get('cli')); @@ -15,7 +15,7 @@ const debugPackage = debugModule(config.get('cli')); */ function debug(input: string) { /* istanbul ignore next */ - if (isGHA() && process.env.NODE_ENV !== 'test') core.debug(`rdme: ${input}`); + if (isGHA() && !isTest()) core.debug(`rdme: ${input}`); return debugPackage(input); } @@ -24,7 +24,7 @@ function debug(input: string) { */ function warn(input: string) { /* istanbul ignore next */ - if (isGHA() && process.env.NODE_ENV !== 'test') return core.warning(input); + if (isGHA() && !isTest()) return core.warning(input); // eslint-disable-next-line no-console return console.warn(chalk.yellow(`⚠️ Warning! ${input}`)); } @@ -36,7 +36,7 @@ function warn(input: string) { */ function info(input: string, includeEmojiPrefix = true) { /* istanbul ignore next */ - if (isGHA() && process.env.NODE_ENV !== 'test') return core.notice(input); + if (isGHA() && !isTest()) return core.notice(input); /* istanbul ignore next */ if (!includeEmojiPrefix) return console.info(input); // eslint-disable-line no-console // eslint-disable-next-line no-console @@ -45,7 +45,7 @@ function info(input: string, includeEmojiPrefix = true) { function oraOptions() { // Disables spinner in tests so it doesn't pollute test output - const opts: Writable = { isSilent: process.env.NODE_ENV === 'test' }; + const opts: Writable = { isSilent: isTest() }; // Cleans up ora output so it prints nicely alongside debug logs /* istanbul ignore next */ diff --git a/src/lib/promptWrapper.ts b/src/lib/promptWrapper.ts index 644bea791..540160ff3 100644 --- a/src/lib/promptWrapper.ts +++ b/src/lib/promptWrapper.ts @@ -1,6 +1,7 @@ -import ciDetect from '@npmcli/ci-detect'; import prompts from 'prompts'; +import isCI from './isCI'; + /** * The `prompts` library doesn't always interpret CTRL+C and release the terminal back to the user * so we need handle this ourselves. This function is just a simple overload of the main `prompts` @@ -28,7 +29,7 @@ export default async function promptTerminal( * prompt is not being run in a CI environment. */ function onRender() { - if (ciDetect() && process.env.NODE_ENV !== 'test') { + if (isCI()) { process.stdout.write('\n'); process.stdout.write( 'Yikes! Looks like we were about to prompt you for something in a CI environment. Are you missing an argument?'