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

feat: refactoring the codebase to be ESM #558

Closed
wants to merge 3 commits into from
Closed
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
21 changes: 20 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
*/
"class-methods-use-this": ["error", { "exceptMethods": ["run"] }],

/**
* Node doesn't ship ESM module resolution so we need to have file extensions on JS files that
* we we load.
*
* https://github.com/airbnb/javascript/issues/2030
*/
"import/extensions": ["error", "ignorePackages", {
"js": "always"
}],

/**
* This is a small rule to prevent us from using console.log() statements in our commands.
*
Expand All @@ -24,6 +34,15 @@
* 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"]
"no-console": ["warn"],

/**
* `__dirname` isn't defined within the scope of ES modules in Node!
*
* https://bobbyhadz.com/blog/javascript-dirname-is-not-defined-in-es-module-scope
*/
"no-underscore-dangle": ["error", { "allow": ["__dirname", "__filename"] }],

"no-restricted-syntax": "off"
}
}
9 changes: 0 additions & 9 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ updates:
- dependency-name: chalk
versions:
- '>= 5'
- dependency-name: configstore
versions:
- '>= 6'
- dependency-name: node-fetch
versions:
- '>= 3'
- dependency-name: ora
versions:
- '>= 6'
- dependency-name: update-notifier
versions:
- '>= 6'
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ jobs:
- name: Install deps
run: npm ci

- name: Lint code
run: npm run lint

- name: Lint docs
run: npm run lint-docs

- name: Run tests
run: npm test

Expand Down
6 changes: 3 additions & 3 deletions __tests__/bin.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { exec } = require('child_process');
const isSupportedNodeVersion = require('../src/lib/isSupportedNodeVersion');
const pkg = require('../package.json');
import { exec } from 'child_process';
import isSupportedNodeVersion from '../src/lib/isSupportedNodeVersion.js';
import pkg from '../src/lib/getPackage.js';

describe('bin', () => {
if (isSupportedNodeVersion(process.version)) {
Expand Down
8 changes: 4 additions & 4 deletions __tests__/cmds/categories.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const nock = require('nock');
import nock from 'nock';

const getApiNock = require('../get-api-nock');
import getApiNock from '../get-api-nock.js';

const CategoriesCommand = require('../../src/cmds/categories');
const CategoriesCreateCommand = require('../../src/cmds/categories/create');
import CategoriesCommand from '../../src/cmds/categories/index.js';
import CategoriesCreateCommand from '../../src/cmds/categories/create.js';

const categories = new CategoriesCommand();
const categoriesCreate = new CategoriesCreateCommand();
Expand Down
24 changes: 12 additions & 12 deletions __tests__/cmds/changelogs.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const nock = require('nock');
const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const frontMatter = require('gray-matter');

const APIError = require('../../src/lib/apiError');
const getApiNock = require('../get-api-nock');

const ChangelogsCommand = require('../../src/cmds/changelogs');
const SingleChangelogCommand = require('../../src/cmds/changelogs/single');
import nock from 'nock';
import chalk from 'chalk';
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
import frontMatter from 'gray-matter';

import APIError from '../../src/lib/apiError.js';
import getApiNock from '../get-api-nock.js';

import ChangelogsCommand from '../../src/cmds/changelogs/index.js';
import SingleChangelogCommand from '../../src/cmds/changelogs/single.js';

const changelogs = new ChangelogsCommand();
const changelogsSingle = new SingleChangelogCommand();
Expand Down
24 changes: 12 additions & 12 deletions __tests__/cmds/custompages.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const nock = require('nock');
const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const frontMatter = require('gray-matter');

const APIError = require('../../src/lib/apiError');
const getApiNock = require('../get-api-nock');

const CustomPagesCommand = require('../../src/cmds/custompages');
const SingleCustomPageCommand = require('../../src/cmds/custompages/single');
import nock from 'nock';
import chalk from 'chalk';
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
import frontMatter from 'gray-matter';

import APIError from '../../src/lib/apiError.js';
import getApiNock from '../get-api-nock.js';

import CustomPagesCommand from '../../src/cmds/custompages/index.js';
import SingleCustomPageCommand from '../../src/cmds/custompages/single.js';

const custompages = new CustomPagesCommand();
const customPagesSingle = new SingleCustomPageCommand();
Expand Down
26 changes: 13 additions & 13 deletions __tests__/cmds/docs.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* eslint-disable no-console */
const nock = require('nock');
const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const frontMatter = require('gray-matter');

const APIError = require('../../src/lib/apiError');
const getApiNock = require('../get-api-nock');

const DocsCommand = require('../../src/cmds/docs');
const DocsEditCommand = require('../../src/cmds/docs/edit');
const DocsSingleCommand = require('../../src/cmds/docs/single');
import nock from 'nock';
import chalk from 'chalk';
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
import frontMatter from 'gray-matter';

import APIError from '../../src/lib/apiError.js';
import getApiNock from '../get-api-nock.js';

import DocsCommand from '../../src/cmds/docs/index.js';
import DocsEditCommand from '../../src/cmds/docs/edit.js';
import DocsSingleCommand from '../../src/cmds/docs/single.js';

const docs = new DocsCommand();
const docsEdit = new DocsEditCommand();
Expand Down
10 changes: 5 additions & 5 deletions __tests__/cmds/login.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const nock = require('nock');
const configStore = require('../../src/lib/configstore');
const Command = require('../../src/cmds/login');
const APIError = require('../../src/lib/apiError');
const getApiNock = require('../get-api-nock');
import nock from 'nock';
import configStore from '../../src/lib/configstore.js';
import Command from '../../src/cmds/login.js';
import APIError from '../../src/lib/apiError.js';
import getApiNock from '../get-api-nock.js';

const cmd = new Command();

Expand Down
6 changes: 3 additions & 3 deletions __tests__/cmds/logout.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const config = require('config');
const configStore = require('../../src/lib/configstore');
const Command = require('../../src/cmds/logout');
import config from 'config';
import configStore from '../../src/lib/configstore.js';
import Command from '../../src/cmds/logout.js';

const cmd = new Command();

Expand Down
8 changes: 4 additions & 4 deletions __tests__/cmds/open.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const chalk = require('chalk');
const config = require('config');
const configStore = require('../../src/lib/configstore');
const Command = require('../../src/cmds/open');
import chalk from 'chalk';
import config from 'config';
import configStore from '../../src/lib/configstore.js';
import Command from '../../src/cmds/open.js';

const cmd = new Command();

Expand Down
18 changes: 9 additions & 9 deletions __tests__/cmds/openapi.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable no-console */
const nock = require('nock');
const chalk = require('chalk');
const config = require('config');
const fs = require('fs');
const promptHandler = require('../../src/lib/prompts');
const SwaggerCommand = require('../../src/cmds/swagger');
const OpenAPICommand = require('../../src/cmds/openapi');
const APIError = require('../../src/lib/apiError');
const getApiNock = require('../get-api-nock');
import nock from 'nock';
import chalk from 'chalk';
import config from 'config';
import fs from 'fs';
import * as promptHandler from '../../src/lib/prompts.js';
import SwaggerCommand from '../../src/cmds/swagger.js';
import OpenAPICommand from '../../src/cmds/openapi.js';
import APIError from '../../src/lib/apiError.js';
import getApiNock from '../get-api-nock.js';

const openapi = new OpenAPICommand();
const swagger = new SwaggerCommand();
Expand Down
6 changes: 3 additions & 3 deletions __tests__/cmds/validate.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
const fs = require('fs');
const chalk = require('chalk');
const Command = require('../../src/cmds/validate');
import fs from 'fs';
import chalk from 'chalk';
import Command from '../../src/cmds/validate.js';

const testWorkingDir = process.cwd();

Expand Down
18 changes: 9 additions & 9 deletions __tests__/cmds/versions.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const nock = require('nock');
const promptHandler = require('../../src/lib/prompts');
const APIError = require('../../src/lib/apiError');
const getApiNock = require('../get-api-nock');

const VersionsCommand = require('../../src/cmds/versions');
const CreateVersionCommand = require('../../src/cmds/versions/create');
const DeleteVersionCommand = require('../../src/cmds/versions/delete');
const UpdateVersionCommand = require('../../src/cmds/versions/update');
import nock from 'nock';
import * as promptHandler from '../../src/lib/prompts.js';
import APIError from '../../src/lib/apiError.js';
import getApiNock from '../get-api-nock.js';

import VersionsCommand from '../../src/cmds/versions/index.js';
import CreateVersionCommand from '../../src/cmds/versions/create.js';
import DeleteVersionCommand from '../../src/cmds/versions/delete.js';
import UpdateVersionCommand from '../../src/cmds/versions/update.js';

const key = 'API_KEY';
const version = '1.0.0';
Expand Down
6 changes: 3 additions & 3 deletions __tests__/cmds/whoami.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const config = require('config');
const configStore = require('../../src/lib/configstore');
const Command = require('../../src/cmds/whoami');
import config from 'config';
import configStore from '../../src/lib/configstore.js';
import Command from '../../src/cmds/whoami.js';

const cmd = new Command();

Expand Down
10 changes: 5 additions & 5 deletions __tests__/get-api-nock.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const config = require('config');
const nock = require('nock');
const { getUserAgent } = require('../src/lib/fetch');
import config from 'config';
import nock from 'nock';
import { getUserAgent } from '../src/lib/fetch.js';

module.exports = function (reqHeaders = {}) {
export default function getAPINock(reqHeaders = {}) {
return nock(config.get('host'), {
reqheaders: {
'User-Agent': getUserAgent(),
...reqHeaders,
},
});
};
}
14 changes: 7 additions & 7 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const nock = require('nock');
const cli = require('../src');
const { version } = require('../package.json');
const conf = require('../src/lib/configstore');
import nock from 'nock';
import cli from '../src/index.js';
import pkg from '../src/lib/getPackage.js';
import conf from '../src/lib/configstore.js';

describe('cli', () => {
it('command not found', async () => {
Expand All @@ -10,15 +10,15 @@ describe('cli', () => {

describe('--version', () => {
it('should return version from package.json', async () => {
await expect(cli(['--version'])).resolves.toBe(version);
await expect(cli(['--version'])).resolves.toBe(pkg.version);
});

it('should return version if the `-V` alias is supplied', async () => {
await expect(cli(['-V'])).resolves.toBe(version);
await expect(cli(['-V'])).resolves.toBe(pkg.version);
});

it('should return version from package.json for help command', async () => {
await expect(cli(['help', '--version'])).resolves.toBe(version);
await expect(cli(['help', '--version'])).resolves.toBe(pkg.version);
});

// This is necessary because we use --version for other commands like `docs`
Expand Down
2 changes: 1 addition & 1 deletion __tests__/lib/apiError.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const APIError = require('../../src/lib/apiError');
import APIError from '../../src/lib/apiError.js';

const response = {
error: 'VERSION_FORK_EMPTY',
Expand Down
20 changes: 13 additions & 7 deletions __tests__/lib/commands.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/* eslint-disable jest/no-conditional-expect */
const commands = require('../../src/lib/commands');
import * as commands from '../../src/lib/commands.js';

describe('utils', () => {
describe('#list', () => {
it('should have commands returned', () => {
expect(commands.list()).not.toHaveLength(0);
it('should have commands returned', async () => {
await expect(commands.list()).resolves.not.toHaveLength(0);
});

describe('commands', () => {
it('should be configured properly', () => {
commands.list().forEach(c => {
it('should be configured properly', async () => {
expect.hasAssertions();

const cmds = await commands.list();
cmds.forEach(c => {
const cmd = c.command;

expect(typeof cmd.command === 'string' && cmd.command.length !== 0).toBe(true);
Expand All @@ -31,8 +34,11 @@ describe('utils', () => {
});
});

it('should abide by our cli standards', () => {
commands.list().forEach(c => {
it('should abide by our cli standards', async () => {
expect.hasAssertions();

const cmds = await commands.list();
cmds.forEach(c => {
const cmd = c.command;

// Command descriptions should end with punctuation.
Expand Down
9 changes: 4 additions & 5 deletions __tests__/lib/fetch.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const config = require('config');
const fetch = require('../../src/lib/fetch');
const { cleanHeaders, handleRes } = require('../../src/lib/fetch');
const getApiNock = require('../get-api-nock');
const pkg = require('../../package.json');
import config from 'config';
import fetch, { cleanHeaders, handleRes } from '../../src/lib/fetch.js';
import getApiNock from '../get-api-nock.js';
import pkg from '../../src/lib/getPackage.js';

describe('#fetch()', () => {
describe('GitHub Actions environment', () => {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/lib/getNodeVersion.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const getNodeVersion = require('../../src/lib/getNodeVersion');
const pkg = require('../../package.json');
const semver = require('semver');
import getNodeVersion from '../../src/lib/getNodeVersion.js';
import pkg from '../../src/lib/getPackage.js';
import semver from 'semver';

describe('#getNodeVersion()', () => {
it('should extract version that matches range in package.json', () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/lib/isSupportedNodeVersion.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const isSupportedNodeVersion = require('../../src/lib/isSupportedNodeVersion');
import isSupportedNodeVersion from '../../src/lib/isSupportedNodeVersion.js';

describe('#isSupportedNodeVersion()', () => {
it('should return true for a supported version of node', () => {
Expand Down
Loading