Reduce use of CJS require() on automation files

Change-type: patch
This commit is contained in:
Otavio Jacobi 2024-09-04 14:47:18 -03:00
parent 6efd24489f
commit facc66e9f9
3 changed files with 21 additions and 18 deletions

View File

@ -39,7 +39,7 @@ export async function renderMarkdown(): Promise<string> {
};
for (const jsFilename of commandCategory.files) {
category.commands.push(...importOclifCommands(jsFilename));
category.commands.push(...(await importOclifCommands(jsFilename)));
}
result.categories.push(category);
}
@ -78,7 +78,9 @@ class FakeHelpCommand {
};
}
function importOclifCommands(jsFilename: string): OclifCommand[] {
async function importOclifCommands(
jsFilename: string,
): Promise<OclifCommand[]> {
// TODO: Currently oclif commands with no `usage` overridden will cause
// an error when parsed. This should be improved so that `usage` does not have
// to be overridden if not necessary.
@ -86,7 +88,8 @@ function importOclifCommands(jsFilename: string): OclifCommand[] {
const command: OclifCommand =
jsFilename === 'help'
? (new FakeHelpCommand() as unknown as OclifCommand)
: (require(path.join(process.cwd(), jsFilename)).default as OclifCommand);
: ((await import(path.join(process.cwd(), jsFilename)))
.default as OclifCommand);
return [command];
}

View File

@ -17,19 +17,15 @@
import * as _ from 'lodash';
import * as semver from 'semver';
import { Octokit } from '@octokit/rest';
import { throttling } from '@octokit/plugin-throttling';
const { GITHUB_TOKEN } = process.env;
/** Return a cached Octokit instance, creating a new one as needed. */
const getOctokit = _.once(function () {
const Octokit = (
require('@octokit/rest') as typeof import('@octokit/rest')
).Octokit.plugin(
(
require('@octokit/plugin-throttling') as typeof import('@octokit/plugin-throttling')
).throttling,
);
return new Octokit({
const OctokitConstructor = Octokit.plugin(throttling);
return new OctokitConstructor({
auth: GITHUB_TOKEN,
throttle: {
onRateLimit: (retryAfter: number, options: any) => {
@ -65,16 +61,16 @@ const getOctokit = _.once(function () {
* 'pages' is the total number of pages, and 'ordinal' is the ordinal number
* (3rd, 4th, 5th...) of the first item in the current page.
*/
function getPageNumbers(
async function getPageNumbers(
response: any,
perPageDefault: number,
): { page: number; pages: number; ordinal: number } {
): Promise<{ page: number; pages: number; ordinal: number }> {
const res = { page: 1, pages: 1, ordinal: 1 };
if (!response.headers.link) {
return res;
}
const parse =
require('parse-link-header') as typeof import('parse-link-header');
const parse = await import('parse-link-header');
const parsed = parse(response.headers.link);
if (parsed == null) {
throw new Error(`Failed to parse link header: '${response.headers.link}'`);
@ -129,7 +125,7 @@ async function updateGitHubReleaseDescriptions(
page: thisPage,
pages: totalPages,
ordinal,
} = getPageNumbers(response, perPage);
} = await getPageNumbers(response, perPage);
let i = 0;
for (const cliRelease of response.data) {
const prefix = `[#${ordinal + i++} pg ${thisPage}/${totalPages}]`;

View File

@ -17,6 +17,8 @@
import { spawn } from 'child_process';
import * as path from 'path';
import * as fs from 'fs';
import { diffTrimmedLines } from 'diff';
export const ROOT = path.join(__dirname, '..');
@ -64,7 +66,6 @@ export class StdOutTap {
* https://www.npmjs.com/package/diff
*/
export function diffLines(str1: string, str2: string): string {
const { diffTrimmedLines } = require('diff');
const diffObjs = diffTrimmedLines(str1, str2);
const prefix = (chunk: string, char: string) =>
chunk
@ -84,7 +85,10 @@ export function diffLines(str1: string, str2: string): string {
}
export function loadPackageJson() {
return require(path.join(ROOT, 'package.json'));
const packageJsonPath = path.join(ROOT, 'package.json');
const packageJson = fs.readFileSync(packageJsonPath, 'utf8');
return JSON.parse(packageJson);
}
/**