Skip to content

Commit

Permalink
Reduce use of CJS require() on automation files
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
otaviojacobi committed Sep 4, 2024
1 parent 6efd244 commit facc66e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
9 changes: 6 additions & 3 deletions automation/capitanodoc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -78,15 +78,18 @@ 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.

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];
}
Expand Down
22 changes: 9 additions & 13 deletions automation/deploy-bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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}'`);
Expand Down Expand Up @@ -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}]`;
Expand Down
8 changes: 6 additions & 2 deletions automation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '..');

Expand Down Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit facc66e

Please sign in to comment.