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) { for (const jsFilename of commandCategory.files) {
category.commands.push(...importOclifCommands(jsFilename)); category.commands.push(...(await importOclifCommands(jsFilename)));
} }
result.categories.push(category); 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 // TODO: Currently oclif commands with no `usage` overridden will cause
// an error when parsed. This should be improved so that `usage` does not have // an error when parsed. This should be improved so that `usage` does not have
// to be overridden if not necessary. // to be overridden if not necessary.
@ -86,7 +88,8 @@ function importOclifCommands(jsFilename: string): OclifCommand[] {
const command: OclifCommand = const command: OclifCommand =
jsFilename === 'help' jsFilename === 'help'
? (new FakeHelpCommand() as unknown as OclifCommand) ? (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]; return [command];
} }

View File

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

View File

@ -17,6 +17,8 @@
import { spawn } from 'child_process'; import { spawn } from 'child_process';
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs';
import { diffTrimmedLines } from 'diff';
export const ROOT = path.join(__dirname, '..'); export const ROOT = path.join(__dirname, '..');
@ -64,7 +66,6 @@ export class StdOutTap {
* https://www.npmjs.com/package/diff * https://www.npmjs.com/package/diff
*/ */
export function diffLines(str1: string, str2: string): string { export function diffLines(str1: string, str2: string): string {
const { diffTrimmedLines } = require('diff');
const diffObjs = diffTrimmedLines(str1, str2); const diffObjs = diffTrimmedLines(str1, str2);
const prefix = (chunk: string, char: string) => const prefix = (chunk: string, char: string) =>
chunk chunk
@ -84,7 +85,10 @@ export function diffLines(str1: string, str2: string): string {
} }
export function loadPackageJson() { 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": { "node_modules/@types/node": {
"version": "20.16.3", "version": "20.16.4",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.16.3.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.16.4.tgz",
"integrity": "sha512-/wdGiWRkMOm53gAsSyFMXFZHbVg7C6CbkrzHNpaHoYfsUWPg7m6ZRKtvQjgvQ9i8WT540a3ydRlRQbxjY30XxQ==", "integrity": "sha512-ioyQ1zK9aGEomJ45zz8S8IdzElyxhvP1RVWnPrXDf6wFaUb+kk1tEcVVJkF7RPGM0VWI7cp5U57oCPIn5iN1qg==",
"dependencies": { "dependencies": {
"undici-types": "~6.19.2" "undici-types": "~6.19.2"
} }
@ -5681,9 +5681,9 @@
} }
}, },
"node_modules/balena-sdk/node_modules/@types/node": { "node_modules/balena-sdk/node_modules/@types/node": {
"version": "18.19.48", "version": "18.19.49",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.48.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.49.tgz",
"integrity": "sha512-7WevbG4ekUcRQSZzOwxWgi5dZmTak7FaxXDoW7xVxPBmKx1rTzfmRLkeCgJzcbBnOV2dkhAPc8cCeT6agocpjg==", "integrity": "sha512-ALCeIR6n0nQ7j0FUF1ycOhrp6+XutJWqEu/vtdEqXFUQwkBfgUA5cEg3ZNmjWGF/ZYA/FcF9QMkL55Ar0O6UrA==",
"dependencies": { "dependencies": {
"undici-types": "~5.26.4" "undici-types": "~5.26.4"
} }
@ -14324,9 +14324,9 @@
} }
}, },
"node_modules/patch-package/node_modules/yaml": { "node_modules/patch-package/node_modules/yaml": {
"version": "2.5.0", "version": "2.5.1",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.5.0.tgz", "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.5.1.tgz",
"integrity": "sha512-2wWLbGbYDiSqqIKoPjar3MPgB94ErzCtrNE1FdqGuaO0pi2JGjmE8aW8TDZwzU7vuxcGRdL/4gPQwQ7hD5AMSw==", "integrity": "sha512-bLQOjaX/ADgQ20isPJRvF0iRUHIxVhYvr53Of7wGcWlO2jvtUlH5m87DsmulFVxRpNLOnI4tB6p/oh8D7kpn9Q==",
"bin": { "bin": {
"yaml": "bin.mjs" "yaml": "bin.mjs"
}, },

View File

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

View File

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

View File

@ -16,7 +16,7 @@
*/ */
import * as chai from 'chai'; 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 ejs from 'ejs';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';

View File

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

View File

@ -259,7 +259,7 @@ describe('balena build', function () {
const fsModPath = 'fs'; const fsModPath = 'fs';
const fsMod = await import(fsModPath); const fsMod = await import(fsModPath);
const qemuModPath = '../../build/utils/qemu'; const qemuModPath = '../../build/utils/qemu';
const qemuMod = require(qemuModPath); const qemuMod = await import(qemuModPath);
const qemuBinPath = await qemuMod.getQemuPath(arch); const qemuBinPath = await qemuMod.getQemuPath(arch);
try { try {
// patch fs.access and fs.stat to pretend that a copy of the Qemu binary // 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 { BalenaAPIMock } from '../../nock/balena-api-mock';
import { runCommand } from '../../helpers'; import { runCommand } from '../../helpers';
import { randomBytes } from 'node:crypto';
describe('balena envs', function () { describe('balena envs', function () {
const appName = 'test'; const appName = 'test';
@ -32,7 +33,7 @@ describe('balena envs', function () {
api.expectGetWhoAmI({ optional: true, persist: true }); api.expectGetWhoAmI({ optional: true, persist: true });
api.expectGetMixpanel({ optional: true }); api.expectGetMixpanel({ optional: true });
// Random device UUID used to frustrate _.memoize() in utils/cloud.ts // 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); shortUUID = fullUUID.substring(0, 7);
}); });

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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