From 06a68c7cfdb8dd6d128a35b9efd769174c3618f2 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Tue, 31 Dec 2024 14:15:46 +0200 Subject: [PATCH] Use the promises namespace of balena-image-fs Change-type: patch --- src/commands/local/configure.ts | 14 +++++++------- src/commands/os/configure.ts | 3 +-- tests/commands/os/configure.spec.ts | 30 ++++++++++++++++------------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/commands/local/configure.ts b/src/commands/local/configure.ts index 1206edefca..691bc28dda 100644 --- a/src/commands/local/configure.ts +++ b/src/commands/local/configure.ts @@ -16,7 +16,6 @@ */ import { Args, Command } from '@oclif/core'; -import { promisify } from 'util'; import { stripIndent } from '../../utils/lazy'; export default class LocalConfigureCmd extends Command { @@ -237,7 +236,7 @@ export default class LocalConfigureCmd extends Command { const bootPartition = await getBootPartition(target); const files = await imagefs.interact(target, bootPartition, async (_fs) => { - return await promisify(_fs.readdir)(this.CONNECTIONS_FOLDER); + return await _fs.promises.readdir(this.CONNECTIONS_FOLDER); }); let connectionFileName; @@ -246,13 +245,14 @@ export default class LocalConfigureCmd extends Command { } else if (_.includes(files, 'resin-sample.ignore')) { // Fresh image, new mode, accoding to https://github.com/balena-os/meta-balena/pull/770/files await imagefs.interact(target, bootPartition, async (_fs) => { - const readFileAsync = promisify(_fs.readFile); - const writeFileAsync = promisify(_fs.writeFile); - const contents = await readFileAsync( + const contents = await _fs.promises.readFile( `${this.CONNECTIONS_FOLDER}/resin-sample.ignore`, { encoding: 'utf8' }, ); - await writeFileAsync(`${this.CONNECTIONS_FOLDER}/resin-wifi`, contents); + await _fs.promises.writeFile( + `${this.CONNECTIONS_FOLDER}/resin-wifi`, + contents, + ); }); } else if (_.includes(files, 'resin-sample')) { // Legacy mode, to be removed later @@ -266,7 +266,7 @@ export default class LocalConfigureCmd extends Command { } else { // In case there's no file at all (shouldn't happen normally, but the file might have been removed) await imagefs.interact(target, bootPartition, async (_fs) => { - await promisify(_fs.writeFile)( + await _fs.promises.writeFile( `${this.CONNECTIONS_FOLDER}/resin-wifi`, this.CONNECTION_FILE, ); diff --git a/src/commands/os/configure.ts b/src/commands/os/configure.ts index c3157c9a42..3d9640ce06 100644 --- a/src/commands/os/configure.ts +++ b/src/commands/os/configure.ts @@ -18,7 +18,6 @@ import { Flags, Args, Command } from '@oclif/core'; import type { Interfaces } from '@oclif/core'; import type * as BalenaSdk from 'balena-sdk'; -import { promisify } from 'util'; import * as _ from 'lodash'; import { ExpectedError } from '../../errors'; import * as cf from '../../utils/common-flags'; @@ -292,7 +291,7 @@ export default class OsConfigureCmd extends Command { for (const { name, content } of files) { await imagefs.interact(image, bootPartition, async (_fs) => { - await promisify(_fs.writeFile)( + await _fs.promises.writeFile( path.join(CONNECTIONS_FOLDER, name), content, ); diff --git a/tests/commands/os/configure.spec.ts b/tests/commands/os/configure.spec.ts index 3d356ba1ac..14be65180f 100644 --- a/tests/commands/os/configure.spec.ts +++ b/tests/commands/os/configure.spec.ts @@ -62,20 +62,22 @@ if (process.platform !== 'win32') { tmpNonMatchingDtJsonPartitionPath, 12, async (_fs) => { - const readFileAsync = promisify(_fs.readFile); - const writeFileAsync = promisify(_fs.writeFile); - const dtJson = JSON.parse( - await readFileAsync('/device-type.json', { encoding: 'utf8' }), + await _fs.promises.readFile('/device-type.json', { + encoding: 'utf8', + }), ); expect(dtJson).to.have.nested.property( 'configuration.config.partition', 12, ); dtJson.configuration.config.partition = 999; - await writeFileAsync('/device-type.json', JSON.stringify(dtJson)); + await _fs.promises.writeFile( + '/device-type.json', + JSON.stringify(dtJson), + ); - await writeFileAsync( + await _fs.promises.writeFile( '/os-release', stripIndent` ID="balena-os" @@ -133,16 +135,17 @@ if (process.platform !== 'win32') { tmpMatchingDtJsonPartitionPath, 12, async (_fs) => { - const readFileAsync = promisify(_fs.readFile); const dtJson = JSON.parse( - await readFileAsync('/device-type.json', { encoding: 'utf8' }), + await _fs.promises.readFile('/device-type.json', { + encoding: 'utf8', + }), ); // confirm that the device-type.json mentions the expected partition expect(dtJson).to.have.nested.property( 'configuration.config.partition', 12, ); - return await readFileAsync('/config.json'); + return await _fs.promises.readFile('/config.json'); }, ); expect(config).to.not.be.empty; @@ -180,16 +183,17 @@ if (process.platform !== 'win32') { tmpNonMatchingDtJsonPartitionPath, 12, async (_fs) => { - const readFileAsync = promisify(_fs.readFile); const dtJson = JSON.parse( - await readFileAsync('/device-type.json', { encoding: 'utf8' }), + await _fs.promises.readFile('/device-type.json', { + encoding: 'utf8', + }), ); // confirm that the device-type.json mentions the expected partition expect(dtJson).to.have.nested.property( 'configuration.config.partition', 999, ); - return await readFileAsync('/config.json'); + return await _fs.promises.readFile('/config.json'); }, ); expect(config).to.not.be.empty; @@ -240,7 +244,7 @@ if (process.platform !== 'win32') { // confirm the image contains a config.json... const config = await imagefs.interact(tmpDummyPath, 1, async (_fs) => { - return await promisify(_fs.readFile)('/config.json'); + return await _fs.promises.readFile('/config.json'); }); expect(config).to.not.be.empty;