Skip to content

Commit

Permalink
Use the promises namespace of balena-image-fs
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
thgreasi committed Dec 31, 2024
1 parent a33a794 commit 06a68c7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
14 changes: 7 additions & 7 deletions src/commands/local/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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,
);
Expand Down
3 changes: 1 addition & 2 deletions src/commands/os/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
);
Expand Down
30 changes: 17 additions & 13 deletions tests/commands/os/configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 06a68c7

Please sign in to comment.