Merge pull request #2823 from balena-io/reduce-require-usage

Reduce usage of not necessary CJS require()
This commit is contained in:
Otávio Jacobi 2024-09-05 09:33:07 -03:00 committed by GitHub
commit 233ee990f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 58 additions and 58 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);
}
/**

18
npm-shrinkwrap.json generated
View File

@ -3894,9 +3894,9 @@
}
},
"node_modules/@types/node": {
"version": "20.16.3",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.16.3.tgz",
"integrity": "sha512-/wdGiWRkMOm53gAsSyFMXFZHbVg7C6CbkrzHNpaHoYfsUWPg7m6ZRKtvQjgvQ9i8WT540a3ydRlRQbxjY30XxQ==",
"version": "20.16.4",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.16.4.tgz",
"integrity": "sha512-ioyQ1zK9aGEomJ45zz8S8IdzElyxhvP1RVWnPrXDf6wFaUb+kk1tEcVVJkF7RPGM0VWI7cp5U57oCPIn5iN1qg==",
"dependencies": {
"undici-types": "~6.19.2"
}
@ -5681,9 +5681,9 @@
}
},
"node_modules/balena-sdk/node_modules/@types/node": {
"version": "18.19.48",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.48.tgz",
"integrity": "sha512-7WevbG4ekUcRQSZzOwxWgi5dZmTak7FaxXDoW7xVxPBmKx1rTzfmRLkeCgJzcbBnOV2dkhAPc8cCeT6agocpjg==",
"version": "18.19.49",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.49.tgz",
"integrity": "sha512-ALCeIR6n0nQ7j0FUF1ycOhrp6+XutJWqEu/vtdEqXFUQwkBfgUA5cEg3ZNmjWGF/ZYA/FcF9QMkL55Ar0O6UrA==",
"dependencies": {
"undici-types": "~5.26.4"
}
@ -14324,9 +14324,9 @@
}
},
"node_modules/patch-package/node_modules/yaml": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.5.0.tgz",
"integrity": "sha512-2wWLbGbYDiSqqIKoPjar3MPgB94ErzCtrNE1FdqGuaO0pi2JGjmE8aW8TDZwzU7vuxcGRdL/4gPQwQ7hD5AMSw==",
"version": "2.5.1",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.5.1.tgz",
"integrity": "sha512-bLQOjaX/ADgQ20isPJRvF0iRUHIxVhYvr53Of7wGcWlO2jvtUlH5m87DsmulFVxRpNLOnI4tB6p/oh8D7kpn9Q==",
"bin": {
"yaml": "bin.mjs"
},

View File

@ -20,7 +20,7 @@ import Command from '../../command';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';
import type * as BalenaSdk from 'balena-sdk';
import jsyaml = require('js-yaml');
import * as yaml from 'js-yaml';
import { tryAsInteger } from '../../utils/validation';
import { jsonInfo } from '../../utils/messages';
@ -82,7 +82,7 @@ export default class ReleaseCmd extends Command {
$select: 'composition',
});
console.log(jsyaml.dump(release.composition));
console.log(yaml.dump(release.composition));
}
async showReleaseInfo(

View File

@ -19,7 +19,7 @@ import type { BalenaSDK } from 'balena-sdk';
import type { TransposeOptions } from '@balena/compose/dist/emulate';
import type * as Dockerode from 'dockerode';
import { promises as fs } from 'fs';
import jsyaml = require('js-yaml');
import * as yaml from 'js-yaml';
import * as _ from 'lodash';
import * as path from 'path';
import type {
@ -180,7 +180,6 @@ async function mergeDevComposeOverlay(
interface ComposeObj {
services?: object;
}
const yaml = await import('js-yaml');
const loadObj = (inputStr: string): ComposeObj =>
(yaml.load(inputStr) || {}) as ComposeObj;
try {
@ -659,7 +658,7 @@ async function loadBuildMetatada(
if (metadataPath.endsWith('json')) {
buildMetadata = JSON.parse(rawString);
} else {
buildMetadata = require('js-yaml').load(rawString);
buildMetadata = yaml.load(rawString) as MultiBuild.ParsedBalenaYml;
}
} catch (err) {
throw new ExpectedError(
@ -944,7 +943,7 @@ async function parseRegistrySecrets(
const multiBuild = await import('@balena/compose/dist/multibuild');
const registrySecrets =
new multiBuild.RegistrySecretValidator().validateRegistrySecrets(
isYaml ? require('js-yaml').load(raw) : JSON.parse(raw),
isYaml ? yaml.load(raw) : JSON.parse(raw),
);
multiBuild.addCanonicalDockerHubEntry(registrySecrets);
return registrySecrets;
@ -1494,7 +1493,7 @@ async function getContractContent(
let asJson;
try {
asJson = jsyaml.load(fileContentAsString);
asJson = yaml.load(fileContentAsString);
} catch (err) {
throw new ExpectedError(
`Error parsing file "${filePath}":\n ${err.message}`,

View File

@ -16,7 +16,7 @@
*/
import * as chai from 'chai';
import chaiAsPromised = require('chai-as-promised');
import * as chaiAsPromised from 'chai-as-promised';
import * as ejs from 'ejs';
import * as fs from 'fs';
import * as path from 'path';

View File

@ -1,15 +1,15 @@
import * as Bluebird from 'bluebird';
import { expect } from 'chai';
import rewire = require('rewire');
import * as sinon from 'sinon';
import * as url from 'url';
import { getBalenaSdk } from '../../build/utils/lazy';
import tokens from './tokens';
const utils = rewire('../../build/auth/utils');
const balena = getBalenaSdk();
describe('Utils:', function () {
describe('Utils:', async function () {
const rewire = await import('rewire');
const utils = rewire('../../build/auth/utils');
describe('.getDashboardLoginURL()', function () {
it('should eventually be a valid url', () =>
utils

View File

@ -259,7 +259,7 @@ describe('balena build', function () {
const fsModPath = 'fs';
const fsMod = await import(fsModPath);
const qemuModPath = '../../build/utils/qemu';
const qemuMod = require(qemuModPath);
const qemuMod = await import(qemuModPath);
const qemuBinPath = await qemuMod.getQemuPath(arch);
try {
// patch fs.access and fs.stat to pretend that a copy of the Qemu binary

View File

@ -20,6 +20,7 @@ import { stripIndent } from '../../../build/utils/lazy';
import { BalenaAPIMock } from '../../nock/balena-api-mock';
import { runCommand } from '../../helpers';
import { randomBytes } from 'node:crypto';
describe('balena envs', function () {
const appName = 'test';
@ -32,7 +33,7 @@ describe('balena envs', function () {
api.expectGetWhoAmI({ optional: true, persist: true });
api.expectGetMixpanel({ optional: true });
// Random device UUID used to frustrate _.memoize() in utils/cloud.ts
fullUUID = require('crypto').randomBytes(16).toString('hex');
fullUUID = randomBytes(16).toString('hex');
shortUUID = fullUUID.substring(0, 7);
});

View File

@ -16,7 +16,7 @@
*/
import { expect } from 'chai';
import mock = require('mock-require');
import * as mock from 'mock-require';
import type { Server } from 'net';
import { createServer } from 'net';

View File

@ -18,7 +18,7 @@
import * as settings from 'balena-settings-client';
import { getStorage } from 'balena-settings-storage';
import { expect } from 'chai';
import mock = require('mock-require');
import * as mock from 'mock-require';
import * as semver from 'semver';
import * as sinon from 'sinon';

View File

@ -20,12 +20,12 @@ import * as _ from 'lodash';
import { promises as fs } from 'fs';
import * as path from 'path';
import { PathUtils } from '@balena/compose/dist/multibuild';
import rewire = require('rewire');
import * as sinon from 'sinon';
import { Readable } from 'stream';
import * as tar from 'tar-stream';
import { streamToBuffer } from 'tar-utils';
import { URL } from 'url';
import { diff } from 'deep-object-diff';
import { makeImageName } from '../build/utils/compose_ts';
import { stripIndent } from '../build/utils/lazy';
@ -101,8 +101,6 @@ export async function inspectTarStream(
try {
expect($expected).to.deep.equal(found);
} catch (e) {
const { diff } =
require('deep-object-diff') as typeof import('deep-object-diff');
const diffStr = JSON.stringify(
diff($expected, found),
(_k, v) => (v === undefined ? 'undefined' : v),
@ -202,7 +200,7 @@ export async function testDockerBuildStream(o: {
}
}
resetDockerignoreCache();
await resetDockerignoreCache();
const { exitCode, out, err } = await runCommand(o.commandLine);
@ -254,7 +252,7 @@ export async function testPushBuildStream(o: {
inspectTarStream(buildRequestBody, o.expectedFiles, o.projectPath),
});
resetDockerignoreCache();
await resetDockerignoreCache();
const { out, err } = await runCommand(o.commandLine);
@ -262,7 +260,9 @@ export async function testPushBuildStream(o: {
expect(cleanOutput(out, true)).to.include.members(expectedResponseLines);
}
export function resetDockerignoreCache() {
export async function resetDockerignoreCache() {
const rewire = await import('rewire');
if (process.env.BALENA_CLI_TEST_TYPE !== 'source') {
return;
}

View File

@ -19,6 +19,8 @@ import * as _ from 'lodash';
import * as path from 'path';
import * as packageJSON from '../package.json';
import { getNodeEngineVersionWarn } from '../build/utils/messages';
import { warnify } from '../build/utils/messages';
const balenaExe = process.platform === 'win32' ? 'balena.exe' : 'balena';
const standalonePath = path.resolve(__dirname, '..', 'build-bin', balenaExe);
@ -41,7 +43,6 @@ function matchesNodeEngineVersionWarn(msg: string) {
.map((l) => l.trim())
.filter((l) => l);
const { getNodeEngineVersionWarn } = require('../build/utils/messages');
let nodeEngineWarn: string = getNodeEngineVersionWarn(
'x.y.z',
packageJSON.engines.node,
@ -179,8 +180,6 @@ async function runCommandInSubprocess(
const msg = `
Error (possibly expected) executing child CLI process "${standalonePath}"
${$error}`;
const { warnify } =
require('../build/utils/messages') as typeof import('../build/utils/messages');
console.error(warnify(msg, '[debug] '));
}
resolve();

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
import Bluebird = require('bluebird');
import * as Bluebird from 'bluebird';
import * as path from 'path';
import * as zlib from 'zlib';

View File

@ -17,6 +17,7 @@
import * as nock from 'nock';
import * as fs from 'fs';
import { interceptorServerPort } from './proxy-server';
export interface ScopeOpts {
optional?: boolean;
@ -170,8 +171,6 @@ export class NockMock {
}
protected handleUnexpectedRequest(req: any) {
const { interceptorServerPort } =
require('./proxy-server') as typeof import('./proxy-server');
const o = req.options || {};
const u = o.uri || {};
const method = req.method;

View File

@ -55,6 +55,7 @@
*/
import * as http from 'http';
import * as httpProxy from 'http-proxy';
const proxyServers: http.Server[] = [];
@ -81,8 +82,6 @@ export async function createProxyServerOnce(): Promise<[number, number]> {
}
async function createProxyServer(): Promise<[number, number]> {
const httpProxy = require('http-proxy') as typeof import('http-proxy');
const interceptorPort = await createInterceptorServer();
const proxy = httpProxy.createProxyServer();

View File

@ -132,8 +132,8 @@ describeSS('LivepushManager::setupFilesystemWatcher', function () {
await setupDockerignoreTestData({ cleanup: true });
});
this.beforeEach(() => {
resetDockerignoreCache();
this.beforeEach(async () => {
await resetDockerignoreCache();
});
describe('for project no-docker-compose/basic', function () {