Skip to content

Commit

Permalink
WIP module node16 for ESM
Browse files Browse the repository at this point in the history
change-type: patch
  • Loading branch information
otaviojacobi committed Jul 16, 2024
1 parent 003d537 commit 5e5b3a2
Show file tree
Hide file tree
Showing 100 changed files with 290 additions and 264 deletions.
12 changes: 6 additions & 6 deletions automation/build-bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
import type { JsonVersions } from '../lib/commands/version/index';

import { run as oclifRun } from '@oclif/core';
import * as archiver from 'archiver';
import * as Bluebird from 'bluebird';
import archiver from 'archiver';
import Bluebird from 'bluebird';
import { exec, execFile } from 'child_process';
import * as filehound from 'filehound';
import type { Stats } from 'fs';
import * as fs from 'fs-extra';
import * as klaw from 'klaw';
import klaw from 'klaw';
import * as path from 'path';
import * as rimraf from 'rimraf';
import rimraf from 'rimraf';
import * as semver from 'semver';
import { promisify } from 'util';
import { notarize } from '@electron/notarize';
Expand Down Expand Up @@ -87,7 +87,7 @@ export const finalReleaseAssets: { [platform: string]: string[] } = {
* Throw an error if the diff is not empty.
*/
async function diffPkgOutput(pkgOut: string) {
const { monochrome } = await import('../tests/helpers');
const { monochrome } = await import('../tests/helpers.js');
const relSavedPath = path.join(
'tests',
'test-data',
Expand Down Expand Up @@ -263,7 +263,7 @@ async function testPkg() {
'version',
'-j',
]);
const { filterCliOutputForTests } = await import('../tests/helpers');
const { filterCliOutputForTests } = await import('../tests/helpers.js');
const filtered = filterCliOutputForTests({
err: stderr.split(/\r?\n/),
out: stdout.split(/\r?\n/),
Expand Down
2 changes: 1 addition & 1 deletion automation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function loadPackageJson() {
* @returns The program's full path, e.g. 'C:\WINDOWS\System32\OpenSSH\ssh.EXE'
*/
export async function which(program: string): Promise<string> {
const whichMod = await import('which');
const { default: whichMod } = await import('which');
let programPath: string;
try {
programPath = await whichMod(program);
Expand Down
23 changes: 12 additions & 11 deletions lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { run as mainRun, settings } from '@oclif/core';
* @see https://docs.sentry.io/error-reporting/quickstart/?platform=node
*/
export const setupSentry = onceAsync(async () => {
const config = await import('./config');
const config = await import('./config.js');
const Sentry = await import('@sentry/node');
Sentry.init({
autoSessionTracking: false,
Expand All @@ -51,7 +51,7 @@ export const setupSentry = onceAsync(async () => {
async function checkNodeVersion() {
const validNodeVersions = packageJSON.engines.node;
if (!(await import('semver')).satisfies(process.version, validNodeVersions)) {
const { getNodeEngineVersionWarn } = await import('./utils/messages');
const { getNodeEngineVersionWarn } = await import('./utils/messages.js');
console.warn(getNodeEngineVersionWarn(process.version, validNodeVersions));
}
}
Expand Down Expand Up @@ -89,13 +89,13 @@ async function init() {
const settings = new CliSettings();

// Proxy setup should be done early on, before loading balena-sdk
await (await import('./utils/proxy')).setupGlobalHttpProxy(settings);
await (await import('./utils/proxy.js')).setupGlobalHttpProxy(settings);

setupBalenaSdkSharedOptions(settings);

// check for CLI updates once a day
if (!process.env.BALENARC_OFFLINE_MODE) {
(await import('./utils/update')).notify();
(await import('./utils/update.js')).notify();
}
}

Expand All @@ -106,7 +106,7 @@ async function oclifRun(command: string[], options: AppOptions) {
if (unsupportedFlag || process.env.BALENARC_UNSUPPORTED) {
deprecationPromise = Promise.resolve();
} else {
const { DeprecationChecker } = await import('./deprecation');
const { DeprecationChecker } = await import('./deprecation.js');
const deprecationChecker = new DeprecationChecker(packageJSON.version);
// warnAndAbortIfDeprecated uses previously cached data only
await deprecationChecker.warnAndAbortIfDeprecated();
Expand Down Expand Up @@ -137,7 +137,8 @@ async function oclifRun(command: string[], options: AppOptions) {
}
}
if (shouldFlush) {
await import('@oclif/core/flush');
const { default: flush } = await import('@oclif/core/flush.js');
await flush();
}
// TODO: figure out why we need to call fast-boot stop() here, in
// addition to calling it in the main `run()` function in this file.
Expand All @@ -148,11 +149,11 @@ async function oclifRun(command: string[], options: AppOptions) {
// the try/catch block above, execution does not get past the
// Promise.all() call below, but I don't understand why.
if (isEEXIT) {
(await import('./fast-boot')).stop();
(await import('./fast-boot.js')).stop();
}
})(!options.noFlush);

const { trackPromise } = await import('./hooks/prerun/track');
const { trackPromise } = await import('./hooks/prerun/track.js');

await Promise.all([trackPromise, deprecationPromise, runPromise]);
}
Expand All @@ -161,7 +162,7 @@ async function oclifRun(command: string[], options: AppOptions) {
export async function run(cliArgs = process.argv, options: AppOptions) {
try {
const { setOfflineModeEnvVars, normalizeEnvVars, pkgExec } = await import(
'./utils/bootstrap'
'./utils/bootstrap.js'
);
setOfflineModeEnvVars();
normalizeEnvVars();
Expand All @@ -180,10 +181,10 @@ export async function run(cliArgs = process.argv, options: AppOptions) {
const args = await preparseArgs(cliArgs);
await oclifRun(args, options);
} catch (err) {
await (await import('./errors')).handleError(err);
await (await import('./errors.js')).handleError(err);
} finally {
try {
(await import('./fast-boot')).stop();
(await import('./fast-boot.js')).stop();
} catch (e) {
if (process.env.DEBUG) {
console.error(`[debug] Stopping fast-boot: ${e}`);
Expand Down
4 changes: 2 additions & 2 deletions lib/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { LoginServer } from './server';
* console.log("My session token is: #{sessionToken}")
*/
export async function login({ host = '127.0.0.1', port = 0 }) {
const utils = await import('./utils');
const utils = await import('./utils.js');

const loginServer = new LoginServer();
const {
Expand All @@ -55,7 +55,7 @@ export async function login({ host = '127.0.0.1', port = 0 }) {
const loginUrl = await utils.getDashboardLoginURL(callbackUrl);

console.info(`Opening web browser for URL:\n${loginUrl}`);
const open = await import('open');
const { default: open } = await import('open');
await open(loginUrl, { wait: false });

const balena = getBalenaSdk();
Expand Down
2 changes: 1 addition & 1 deletion lib/auth/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

import * as bodyParser from 'body-parser';
import { EventEmitter } from 'events';
import * as express from 'express';
import express from 'express';
import type { Socket } from 'net';
import * as path from 'path';

Expand Down
10 changes: 6 additions & 4 deletions lib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default abstract class BalenaCommand extends Command {
* - other code needs to execute before check
*/
protected static async checkElevatedPrivileges() {
const isElevated = await (await import('is-elevated'))();
const isElevated = await (await import('is-elevated')).default();
if (!isElevated) {
throw new InsufficientPrivilegesError(
'You need root/admin privileges to run this command',
Expand All @@ -94,7 +94,7 @@ export default abstract class BalenaCommand extends Command {
* @throws {NotLoggedInError}
*/
public static async checkLoggedIn() {
await (await import('./utils/patterns')).checkLoggedIn();
await (await import('./utils/patterns.js')).checkLoggedIn();
}

/**
Expand Down Expand Up @@ -139,14 +139,16 @@ export default abstract class BalenaCommand extends Command {
* values from stdin based in configuration, minimising command implementation.
*/
protected async getStdin() {
this.stdin = await (await import('get-stdin'))();
const { default: getStdin } = await import('get-stdin');
this.stdin = await getStdin();
}

/**
* Get a logger instance.
*/
protected static async getLogger() {
return (await import('./utils/logger')).getLogger();
const { default: logger } = await import('./utils/logger.js');
return logger.getLogger();
}

protected async init() {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/api-keys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class ApiKeysCmd extends Command {
public async run() {
const { flags: options } = await this.parse(ApiKeysCmd);

const { getApplication } = await import('../../utils/sdk');
const { getApplication } = await import('../../utils/sdk.js');
const actorId = options.fleet
? (
await getApplication(getBalenaSdk(), options.fleet, {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/app/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default class AppCreateCmd extends Command {
const { args: params, flags: options } = await this.parse(AppCreateCmd);

await (
await import('../../utils/application-create')
await import('../../utils/application-create.js')
).applicationCreateBase('app', options, params);
}
}
2 changes: 1 addition & 1 deletion lib/commands/block/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default class BlockCreateCmd extends Command {
const { args: params, flags: options } = await this.parse(BlockCreateCmd);

await (
await import('../../utils/application-create')
await import('../../utils/application-create.js')
).applicationCreateBase('block', options, params);
}
}
23 changes: 16 additions & 7 deletions lib/commands/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type { ComposeCliFlags, ComposeOpts } from '../../utils/compose-types';
import { buildProject, composeCliFlags } from '../../utils/compose_ts';
import type { BuildOpts, DockerCliFlags } from '../../utils/docker';
import { dockerCliFlags } from '../../utils/docker';
import type Dockerode from 'dockerode';

// TODO: For this special one we can't use Interfaces.InferredFlags/InferredArgs
// because of the 'registry-secrets' type which is defined in the actual code
Expand Down Expand Up @@ -157,14 +158,16 @@ ${dockerignoreHelp}
(opts.fleet == null && (opts.arch == null || opts.deviceType == null)) ||
(opts.fleet != null && (opts.arch != null || opts.deviceType != null))
) {
const { ExpectedError } = await import('../../errors');
const { ExpectedError } = await import('../../errors.js');
throw new ExpectedError(
'You must specify either a fleet (-f), or the device type (-d) and optionally the architecture (-A)',
);
}

// Validate project directory
const { validateProjectDirectory } = await import('../../utils/compose_ts');
const { validateProjectDirectory } = await import(
'../../utils/compose_ts.js'
);
const { dockerfilePath, registrySecrets } = await validateProjectDirectory(
sdk,
{
Expand Down Expand Up @@ -197,7 +200,7 @@ ${dockerignoreHelp}
)) as PineTypedResult<DeviceType, typeof deviceTypeOpts>
).is_of__cpu_architecture[0].slug;
} catch (err) {
const { ExpectedError } = await import('../../errors');
const { ExpectedError } = await import('../../errors.js');
if (err instanceof sdk.errors.BalenaInvalidDeviceType) {
let message = err.message;
if (!(await sdk.auth.isLoggedIn())) {
Expand All @@ -214,16 +217,22 @@ ${dockerignoreHelp}

protected async getAppAndResolveArch(opts: FlagsDef) {
if (opts.fleet) {
const { getAppWithArch } = await import('../../utils/helpers');
const { getAppWithArch } = await import('../../utils/helpers.js');
const app = await getAppWithArch(opts.fleet);
opts.arch = app.arch;
opts.deviceType = app.is_for__device_type[0].slug;
return app;
}
}

protected async prepareBuild(options: FlagsDef) {
const { getDocker, generateBuildOpts } = await import('../../utils/docker');
protected async prepareBuild(options: FlagsDef): Promise<{
docker: Dockerode;
buildOpts: BuildOpts;
composeOpts: ComposeOpts;
}> {
const { getDocker, generateBuildOpts } = await import(
'../../utils/docker.js'
);
const [docker, buildOpts, composeOpts] = await Promise.all([
getDocker(options),
generateBuildOpts(options),
Expand Down Expand Up @@ -261,7 +270,7 @@ ${dockerignoreHelp}
buildOpts: BuildOpts;
},
) {
const { loadProject } = await import('../../utils/compose_ts');
const { loadProject } = await import('../../utils/compose_ts.js');

const project = await loadProject(
logger,
Expand Down
18 changes: 9 additions & 9 deletions lib/commands/config/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export default class ConfigGenerateCmd extends Command {
public static authenticated = true;

public async getApplication(balena: BalenaSDK, fleet: string) {
const { getApplication } = await import('../../utils/sdk');
const { getApplication } = await import('../../utils/sdk.js');
return await getApplication(balena, fleet, {
$select: 'slug',
$expand: {
Expand All @@ -152,7 +152,7 @@ export default class ConfigGenerateCmd extends Command {
$expand: { is_of__device_type: { $select: 'slug' } },
});
if (!rawDevice.belongs_to__application) {
const { ExpectedError } = await import('../../errors');
const { ExpectedError } = await import('../../errors.js');
throw new ExpectedError(stripIndent`
Device ${options.device} does not appear to belong to an accessible fleet.
Try with a different device, or use '--fleet' instead of '--device'.`);
Expand All @@ -171,14 +171,14 @@ export default class ConfigGenerateCmd extends Command {

// Check compatibility if application and deviceType provided
if (options.fleet && options.deviceType) {
const helpers = await import('../../utils/helpers');
const helpers = await import('../../utils/helpers.js');
if (
!(await helpers.areDeviceTypesCompatible(
resourceDeviceType,
deviceType,
))
) {
const { ExpectedError } = await import('../../errors');
const { ExpectedError } = await import('../../errors.js');
throw new ExpectedError(
`Device type ${options.deviceType} is incompatible with fleet ${options.fleet}`,
);
Expand All @@ -189,7 +189,7 @@ export default class ConfigGenerateCmd extends Command {
await balena.models.config.getDeviceTypeManifestBySlug(deviceType);

const { validateSecureBootOptionAndWarn } = await import(
'../../utils/config'
'../../utils/config.js'
);
await validateSecureBootOptionAndWarn(
options.secureBoot,
Expand All @@ -211,7 +211,7 @@ export default class ConfigGenerateCmd extends Command {

// Generate config
const { generateDeviceConfig, generateApplicationConfig } = await import(
'../../utils/config'
'../../utils/config.js'
);

let config;
Expand Down Expand Up @@ -250,7 +250,7 @@ export default class ConfigGenerateCmd extends Command {
protected async validateOptions(
options: Interfaces.InferredFlags<typeof ConfigGenerateCmd.flags>,
) {
const { ExpectedError } = await import('../../errors');
const { ExpectedError } = await import('../../errors.js');

if (options.device == null && options.fleet == null) {
throw new ExpectedError(this.missingDeviceOrAppMessage);
Expand All @@ -259,9 +259,9 @@ export default class ConfigGenerateCmd extends Command {
if (!options.fleet && options.deviceType) {
throw new ExpectedError(this.deviceTypeNotAllowedMessage);
}
const { normalizeOsVersion } = await import('../../utils/normalization');
const { normalizeOsVersion } = await import('../../utils/normalization.js');
options.version = normalizeOsVersion(options.version);
const { validateDevOptionAndWarn } = await import('../../utils/config');
const { validateDevOptionAndWarn } = await import('../../utils/config.js');
await validateDevOptionAndWarn(options.dev, options.version);
}
}
2 changes: 1 addition & 1 deletion lib/commands/config/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class ConfigInjectCmd extends Command {
public async run() {
const { args: params, flags: options } = await this.parse(ConfigInjectCmd);

const { safeUmount } = await import('../../utils/umount');
const { safeUmount } = await import('../../utils/umount.js');

const drive =
options.drive || (await getVisuals().drive('Select the device/OS drive'));
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/config/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class ConfigReadCmd extends Command {
public async run() {
const { flags: options } = await this.parse(ConfigReadCmd);

const { safeUmount } = await import('../../utils/umount');
const { safeUmount } = await import('../../utils/umount.js');

const drive =
options.drive || (await getVisuals().drive('Select the device drive'));
Expand Down
Loading

0 comments on commit 5e5b3a2

Please sign in to comment.