Skip to content

Commit

Permalink
feat: convert to ESM (breaking change) (#856)
Browse files Browse the repository at this point in the history
* chore: drop module opts in test config

* feat: the epic renaming

* chore(deps): bump chalk to v5

* chore: lint

* chore: migrate a few more files that fell through the cracks

* fix: asserted JSON import

this probably won't work with node 14 at all ugh

* chore: rename config

* chore: rename a bunch of bin files

most of them are now .cjs with the exception of bin/rdme.js

* chore: lint

* chore: try this with jest

can we move off jest? all my homies hate jest

* chore: more jest mess around

* fix: one more thing in an attempt to fix the build

* fix: wait is this really all it took

* chore: update npmignore

* chore: more extension fixes

* ci: knip fix

* refactor: enable node protocol

deno needs this

* chore: lint

* chore: fix up some more extensions

* revert: rename file again

* fix: another thing that fell through the cracks

* chore: one more fix

* chore: bring in new eslint rule, fix up some more issues

* chore: bump oas and friends to get tests working

* chore: ignore TS in a few places... why though

* chore: more TS weirdness

* chore: rebuild lockfile

very tired of this mess

* refactor: remove moduleResolution

* chore: remove @ts-expect-error flags

* revert: broken oas-normalize state

* chore(deps): bump oas and oas-normalize

* chore(deps): bump oas again

* chore: disable test

* ci: stop running tests against node 14/16

* test: get test working again

* refactor: hide node warnings

taken from here: https://www.stefanjudis.com/snippets/how-to-import-json-files-in-es-modules-node-js/#option-1%3A-read-and-parse-json-files-yourself

this is the best way i could think of to avoid the experimental import assertions

* chore: bad merges

* revert: bring back node warnings lol

trying to see if this plays nicely with rollup

* chore: fix assertion

* refactor: use new eslint config, get rid of cjs files

* test: fix outdated config usage

* chore: tiny formatting fix

* chore: refine eslint extensions

* docs: make sure it references bin/rdme.js

* fix: attempt to get docker build working

* fix: use node 18

* chore: try bumping node-fetch

* fix: set rollup config flag

i guess node-fetch@3 wants to split the bundle so we need to set this now

* test: fix some fetch tests

* chore: lint

* test: more test fixes

* fix: swap out form-data with formdata-node

usage grabbed from step 9 on this page: https://github.com/octet-stream/form-data/blob/4d632678558e6b9210bb50f1fc023f31751b0eba/readme.md?plain=1#L228

* chore(deps): does it work to bump the rest of the deps?

* Revert "chore(deps): does it work to bump the rest of the deps?"

This reverts commit 48c77ba.

* chore(temp): see if colors are supported in GHA

* Revert "chore(temp): see if colors are supported in GHA"

This reverts commit f6939c5.

* chore: add dists to ignore files, clean up

* chore: add debug to pkg

* chore(deps): bump the deps that i can

* chore: update dependabot file yay

* chore: fix up ignore files again

* chore: comment

* chore: one of these days i'll be content with my ignore file formatting

* chore(deps): bump headers-polyfill

* refactor: hide rollup output in dist

* revert: remove unused debug flag

* chore: swap out headers-polyfill type for node-fetch

* chore: lint

* fix: type fixes
  • Loading branch information
kanadgupta authored Sep 14, 2023
1 parent 0007f4f commit 84b8571
Show file tree
Hide file tree
Showing 115 changed files with 4,195 additions and 2,050 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
coverage/
dist/
!.alexrc.js
exe/
14 changes: 9 additions & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"extends": ["@readme/eslint-config", "@readme/eslint-config/typescript"],
"extends": ["@readme/eslint-config", "@readme/eslint-config/typescript", "@readme/eslint-config/esm"],
"root": true,
"parserOptions": {
"ecmaVersion": 2020
},
"overrides": [
{
"files": ["bin/*.js"],
Expand All @@ -12,6 +9,13 @@
}
}
],
"env": {
"es2021": true,
"node": true
},
"parserOptions": {
"sourceType": "module"
},
"rules": {
"@typescript-eslint/ban-types": [
"error",
Expand All @@ -37,7 +41,7 @@
* console.log() calls, hurting our ability to write resilient tests and easily debug issues.
*
* We should be returning Promise-wrapped values in our main command functions
* so we can write robust tests and take advantage of `bin/rdme`,
* so we can write robust tests and take advantage of `bin/rdme.js`,
* 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)
Expand Down
16 changes: 2 additions & 14 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,7 @@ updates:
prefix: chore(deps)
prefix-development: chore(deps-dev)
ignore:
# All of these packages are now ESM-only and can't be used here without a rewrite.
- dependency-name: chalk
versions:
- '>= 5'
- dependency-name: configstore
versions:
- '>= 6'
- dependency-name: node-fetch
versions:
- '>= 3'
- dependency-name: open
versions:
- '>= 9'
# See https://github.com/sindresorhus/ora/issues/229
- dependency-name: ora
versions:
- '>= 6'
- '>= 7'
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 16
- 18
- 20

Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
coverage/
dist/
.env
exe/
node_modules/
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ __tests__/
.github/
.husky/
.prettier*
bin/*.js
bin/**
coverage/
packages/
vitest.*
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ __tests__/__fixtures__/invalid-json/yikes.json
CHANGELOG.md
coverage/
dist/
exe/
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

## Running Shell Commands Locally 🐚

To run test commands from within the repository, run the build and then run your commands from the root of the repository and use `./bin/rdme` instead of `rdme` so it properly points to the command executable, like so:
To run test commands from within the repository, run the build and then run your commands from the root of the repository and use `./bin/rdme.js` instead of `rdme` so it properly points to the command executable, like so:

```sh
npm run build
./bin/rdme openapi:validate __tests__/__fixtures__/ref-oas/petstore.json
./bin/rdme.js openapi:validate __tests__/__fixtures__/ref-oas/petstore.json
```

If you need to debug commands quicker and re-building TS everytime is becoming cumbersome, you can use the debug command, like so:
Expand Down Expand Up @@ -70,7 +70,7 @@ act -j simple

### Usage of `console`

As you'll learn in our commands logic (see [`bin/rdme`](bin/rdme) and the [`src/cmds`](src/cmds) directory), we wrap our command outputs in resolved/rejected [`Promise` objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) and use [`bin/rdme`](bin/rdme) file to log the results to the console and return the correct status code. This is so we can write more resilient tests, ensure that the proper exit codes are being returned, and make debugging easier.
As you'll learn in our commands logic (see [`bin/rdme.js`](bin/rdme.js) and the [`src/cmds`](src/cmds) directory), we wrap our command outputs in resolved/rejected [`Promise` objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) and use [`bin/rdme.js`](bin/rdme.js) file to log the results to the console and return the correct status code. This is so we can write more resilient tests, ensure that the proper exit codes are being returned, and make debugging easier.

When writing command logic, avoid using `console` statements (and correspondingly, avoid mocking `console` statements in tests) when possible.

Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM node:16-alpine as builder
FROM node:18-alpine as builder

COPY . /rdme

RUN cd /rdme && npm ci && npm run build && npx pkg@5 . --target host --out-path exe
RUN cd /rdme && npm ci && npm run build:exe

FROM alpine:3.14

Expand Down
4 changes: 2 additions & 2 deletions __tests__/bin.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exec } from 'child_process';
import { exec } from 'node:child_process';

import { describe, it, expect } from 'vitest';

Expand All @@ -7,7 +7,7 @@ describe('bin', () => {
expect.assertions(1);

await new Promise(resolve => {
exec(`node ${__dirname}/../bin/rdme`, (error, stdout) => {
exec(`node ${__dirname}/../bin/rdme.js`, (error, stdout) => {
expect(stdout).toContain('a utility for interacting with ReadMe');
resolve(true);
});
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/categories/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import nock from 'nock';
import prompts from 'prompts';
import { describe, beforeAll, afterEach, it, expect, vi } from 'vitest';

import CategoriesCreateCommand from '../../../src/cmds/categories/create';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock';
import CategoriesCreateCommand from '../../../src/cmds/categories/create.js';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js';

const categoriesCreate = new CategoriesCreateCommand();

Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/categories/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import nock from 'nock';
import prompts from 'prompts';
import { describe, beforeAll, afterEach, it, expect, vi } from 'vitest';

import CategoriesCommand from '../../../src/cmds/categories';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock';
import CategoriesCommand from '../../../src/cmds/categories/index.js';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js';

const categories = new CategoriesCommand();

Expand Down
12 changes: 6 additions & 6 deletions __tests__/cmds/changelogs/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';

import chalk from 'chalk';
import frontMatter from 'gray-matter';
import nock from 'nock';
import prompts from 'prompts';
import { describe, beforeAll, afterAll, beforeEach, it, expect, vi } from 'vitest';

import ChangelogsCommand from '../../../src/cmds/changelogs';
import APIError from '../../../src/lib/apiError';
import getAPIMock from '../../helpers/get-api-mock';
import hashFileContents from '../../helpers/hash-file-contents';
import ChangelogsCommand from '../../../src/cmds/changelogs.js';
import APIError from '../../../src/lib/apiError.js';
import getAPIMock from '../../helpers/get-api-mock.js';
import hashFileContents from '../../helpers/hash-file-contents.js';

const changelogs = new ChangelogsCommand();

Expand Down
12 changes: 6 additions & 6 deletions __tests__/cmds/changelogs/single.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';

import chalk from 'chalk';
import frontMatter from 'gray-matter';
import nock from 'nock';
import prompts from 'prompts';
import { describe, beforeAll, afterAll, beforeEach, it, expect, vi } from 'vitest';

import ChangelogsCommand from '../../../src/cmds/changelogs';
import APIError from '../../../src/lib/apiError';
import getAPIMock from '../../helpers/get-api-mock';
import hashFileContents from '../../helpers/hash-file-contents';
import ChangelogsCommand from '../../../src/cmds/changelogs.js';
import APIError from '../../../src/lib/apiError.js';
import getAPIMock from '../../helpers/get-api-mock.js';
import hashFileContents from '../../helpers/hash-file-contents.js';

const changelogs = new ChangelogsCommand();

Expand Down
12 changes: 6 additions & 6 deletions __tests__/cmds/custompages/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';

import chalk from 'chalk';
import frontMatter from 'gray-matter';
import nock from 'nock';
import prompts from 'prompts';
import { describe, beforeAll, afterAll, beforeEach, it, expect, vi } from 'vitest';

import CustomPagesCommand from '../../../src/cmds/custompages';
import APIError from '../../../src/lib/apiError';
import getAPIMock from '../../helpers/get-api-mock';
import hashFileContents from '../../helpers/hash-file-contents';
import CustomPagesCommand from '../../../src/cmds/custompages.js';
import APIError from '../../../src/lib/apiError.js';
import getAPIMock from '../../helpers/get-api-mock.js';
import hashFileContents from '../../helpers/hash-file-contents.js';

const custompages = new CustomPagesCommand();

Expand Down
12 changes: 6 additions & 6 deletions __tests__/cmds/custompages/single.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';

import chalk from 'chalk';
import frontMatter from 'gray-matter';
import nock from 'nock';
import prompts from 'prompts';
import { describe, beforeAll, afterAll, beforeEach, it, expect, vi } from 'vitest';

import CustomPagesCommand from '../../../src/cmds/custompages';
import APIError from '../../../src/lib/apiError';
import getAPIMock from '../../helpers/get-api-mock';
import hashFileContents from '../../helpers/hash-file-contents';
import CustomPagesCommand from '../../../src/cmds/custompages.js';
import APIError from '../../../src/lib/apiError.js';
import getAPIMock from '../../helpers/get-api-mock.js';
import hashFileContents from '../../helpers/hash-file-contents.js';

const custompages = new CustomPagesCommand();

Expand Down
8 changes: 4 additions & 4 deletions __tests__/cmds/docs/edit.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fs from 'fs';
import fs from 'node:fs';

import nock from 'nock';
import prompts from 'prompts';
import { describe, beforeAll, afterAll, beforeEach, afterEach, it, expect, vi } from 'vitest';

import DocsEditCommand from '../../../src/cmds/docs/edit';
import APIError from '../../../src/lib/apiError';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock';
import DocsEditCommand from '../../../src/cmds/docs/edit.js';
import APIError from '../../../src/lib/apiError.js';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js';

const docsEdit = new DocsEditCommand();

Expand Down
20 changes: 10 additions & 10 deletions __tests__/cmds/docs/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/* eslint-disable no-console */
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';

import chalk from 'chalk';
import frontMatter from 'gray-matter';
import nock from 'nock';
import prompts from 'prompts';
import { describe, beforeAll, afterAll, beforeEach, afterEach, it, expect, vi } from 'vitest';

import DocsCommand from '../../../src/cmds/docs';
import GuidesCommand from '../../../src/cmds/guides';
import APIError from '../../../src/lib/apiError';
import configstore from '../../../src/lib/configstore';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock';
import { after, before } from '../../helpers/get-gha-setup';
import hashFileContents from '../../helpers/hash-file-contents';
import { after as afterGHAEnv, before as beforeGHAEnv } from '../../helpers/setup-gha-env';
import DocsCommand from '../../../src/cmds/docs/index.js';
import GuidesCommand from '../../../src/cmds/guides/index.js';
import APIError from '../../../src/lib/apiError.js';
import configstore from '../../../src/lib/configstore.js';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js';
import { after, before } from '../../helpers/get-gha-setup.js';
import hashFileContents from '../../helpers/hash-file-contents.js';
import { after as afterGHAEnv, before as beforeGHAEnv } from '../../helpers/setup-gha-env.js';

const docs = new DocsCommand();
const guides = new GuidesCommand();
Expand Down
6 changes: 3 additions & 3 deletions __tests__/cmds/docs/prune.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import nock from 'nock';
import prompts from 'prompts';
import { describe, beforeAll, afterAll, it, expect, vi } from 'vitest';

import DocsPruneCommand from '../../../src/cmds/docs/prune';
import GuidesPruneCommand from '../../../src/cmds/guides/prune';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock';
import DocsPruneCommand from '../../../src/cmds/docs/prune.js';
import GuidesPruneCommand from '../../../src/cmds/guides/prune.js';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js';

const docsPrune = new DocsPruneCommand();
const guidesPrune = new GuidesPruneCommand();
Expand Down
14 changes: 7 additions & 7 deletions __tests__/cmds/docs/single.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';

import chalk from 'chalk';
import frontMatter from 'gray-matter';
import nock from 'nock';
import prompts from 'prompts';
import { describe, beforeAll, afterAll, beforeEach, afterEach, it, expect, vi } from 'vitest';

import DocsCommand from '../../../src/cmds/docs';
import APIError from '../../../src/lib/apiError';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock';
import hashFileContents from '../../helpers/hash-file-contents';
import { after as afterGHAEnv, before as beforeGHAEnv } from '../../helpers/setup-gha-env';
import DocsCommand from '../../../src/cmds/docs/index.js';
import APIError from '../../../src/lib/apiError.js';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js';
import hashFileContents from '../../helpers/hash-file-contents.js';
import { after as afterGHAEnv, before as beforeGHAEnv } from '../../helpers/setup-gha-env.js';

const docs = new DocsCommand();

Expand Down
8 changes: 4 additions & 4 deletions __tests__/cmds/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import nock from 'nock';
import prompts from 'prompts';
import { describe, beforeAll, afterAll, afterEach, it, expect } from 'vitest';

import Command from '../../src/cmds/login';
import APIError from '../../src/lib/apiError';
import configStore from '../../src/lib/configstore';
import getAPIMock from '../helpers/get-api-mock';
import Command from '../../src/cmds/login.js';
import APIError from '../../src/lib/apiError.js';
import configStore from '../../src/lib/configstore.js';
import getAPIMock from '../helpers/get-api-mock.js';

const cmd = new Command();

Expand Down
6 changes: 3 additions & 3 deletions __tests__/cmds/logout.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, afterEach, it, expect } from 'vitest';

import Command from '../../src/cmds/logout';
import config from '../../src/lib/config';
import configStore from '../../src/lib/configstore';
import Command from '../../src/cmds/logout.js';
import config from '../../src/lib/config.js';
import configStore from '../../src/lib/configstore.js';

const cmd = new Command();

Expand Down
10 changes: 5 additions & 5 deletions __tests__/cmds/open.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Version } from '../../src/cmds/versions';
import type { Version } from '../../src/cmds/versions/index.js';

import chalk from 'chalk';
import { describe, afterEach, it, expect } from 'vitest';

import Command from '../../src/cmds/open';
import config from '../../src/lib/config';
import configStore from '../../src/lib/configstore';
import getAPIMock from '../helpers/get-api-mock';
import Command from '../../src/cmds/open.js';
import config from '../../src/lib/config.js';
import configStore from '../../src/lib/configstore.js';
import getAPIMock from '../helpers/get-api-mock.js';

const cmd = new Command();

Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/openapi/convert.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs';
import fs from 'node:fs';

import prompts from 'prompts';
import { describe, it, expect, vi } from 'vitest';

import OpenAPIConvertCommand from '../../../src/cmds/openapi/convert';
import OpenAPIConvertCommand from '../../../src/cmds/openapi/convert.js';

const convert = new OpenAPIConvertCommand();

Expand Down
Loading

0 comments on commit 84b8571

Please sign in to comment.