Skip to content

Commit

Permalink
Stop returning Bluebird promises
Browse files Browse the repository at this point in the history
Change-type: major
  • Loading branch information
thgreasi committed Dec 17, 2024
1 parent ae26ec4 commit cf99815
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 148 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
"dependencies": {
"balena-image-fs": "^7.0.6",
"balena-semver": "^2.2.0",
"bluebird": "^3.7.2",
"lodash": "^4.17.15",
"reconfix": "1.0.0-v0-1-0-fork-46760acff4d165f5238bfac5e464256ef1944476",
"resin-device-operations": "^2.0.0",
Expand Down
122 changes: 60 additions & 62 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ limitations under the License.
*/

import _ from 'lodash';
import Promise from 'bluebird';
import * as operations from 'resin-device-operations';
import * as balenaSemver from 'balena-semver';
import * as utils from './utils';
Expand Down Expand Up @@ -119,71 +118,66 @@ export interface InitializeEmitter {
* configuration.on 'end', ->
* console.log('Configuration finished')
*/
export function configure(
export async function configure(
image: string,
manifest: DeviceTypeJson,
config: Record<string, any>,
options: object = {},
): Promise<InitializeEmitter> {
return Promise.try(function () {
// We only know how to find /etc/os-release on specific types of OS image. In future, we'd like to be able
// to do this for any image, but for now we'll just treat others as unknowable (which means below we'll
// configure the network to work for _either_ OS version.
if (
manifest.yocto?.image === 'resin-image' &&
_.includes(['resinos-img', 'resin-sdcard'], manifest.yocto?.fstype)
) {
return utils.getImageOsVersion(image, manifest);
}
}).then(function (osVersion) {
if (manifest.configuration == null) {
throw new Error(
'Unsupported device type: Manifest missing configuration parameters',
);
}

const { configuration } = manifest;
// TS should be able to detect this on its own and we shoulnd't have to do it manually
const manifestWithConfiguration = manifest as typeof manifest & {
configuration: object;
};

const majorVersion = balenaSemver.major(osVersion);

const configPathDefinition = utils.convertFilePathDefinition(
configuration.config,
// We only know how to find /etc/os-release on specific types of OS image. In future, we'd like to be able
// to do this for any image, but for now we'll just treat others as unknowable (which means below we'll
// configure the network to work for _either_ OS version.
let osVersion: string | null = null;
if (
manifest.yocto?.image === 'resin-image' &&
_.includes(['resinos-img', 'resin-sdcard'], manifest.yocto?.fstype)
) {
osVersion = await utils.getImageOsVersion(image, manifest);
}

if (manifest.configuration == null) {
throw new Error(
'Unsupported device type: Manifest missing configuration parameters',
);
return utils
.writeConfigJSON(image, config, configPathDefinition)
.then(function () {
// Configure for OS2 if it is OS2, or if we're just not sure
if (majorVersion == null || majorVersion === 2) {
return network.configureOS2Network(
image,
manifestWithConfiguration,
options,
);
}
})
.then(function () {
// Configure for OS1 if it is OS1, or if we're just not sure
if (majorVersion == null || majorVersion === 1) {
return network.configureOS1Network(
image,
manifestWithConfiguration,
options,
);
}
})
.then(() =>
operations.execute(
image,
// @ts-expect-error TODO: Check whether this should be `manifest.initialization.operations` ?
configuration.operations,
options,
),
);
});
}

const { configuration } = manifest;
// TS should be able to detect this on its own and we shoulnd't have to do it manually
const manifestWithConfiguration = manifest as typeof manifest & {
configuration: object;
};

const majorVersion = balenaSemver.major(osVersion);

const configPathDefinition = utils.convertFilePathDefinition(
configuration.config,
);
await utils.writeConfigJSON(image, config, configPathDefinition);

// Configure for OS2 if it is OS2, or if we're just not sure
if (majorVersion == null || majorVersion === 2) {
await network.configureOS2Network(
image,
manifestWithConfiguration,
options,
);
}

// Configure for OS1 if it is OS1, or if we're just not sure
if (majorVersion == null || majorVersion === 1) {
await network.configureOS1Network(
image,
manifestWithConfiguration,
options,
);
}

return await operations.execute(
image,
// @ts-expect-error TODO: Check whether this should be `manifest.initialization.operations` ?
configuration.operations,
options,
);
}

/**
Expand Down Expand Up @@ -216,7 +210,7 @@ export function configure(
* configuration.on 'end', ->
* console.log('Configuration finished')
*/
export function initialize(
export async function initialize(
image: string,
manifest: DeviceTypeJson,
options: object,
Expand All @@ -226,5 +220,9 @@ export function initialize(
'Unsupported device type: Manifest missing initialization parameters',
);
}
return operations.execute(image, manifest.initialization.operations, options);
return await operations.execute(
image,
manifest.initialization.operations,
options,
);
}
44 changes: 19 additions & 25 deletions src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ limitations under the License.
*/

import _ from 'lodash';
import type * as fsPromise from 'fs/promises';
import path from 'path';
import * as util from 'node:util';
import * as imagefs from 'balena-image-fs';
import reconfix from 'reconfix';
import Promise from 'bluebird';
import * as utils from './utils';
import type { DeviceTypeJsonWithConfiguration } from './device-type-json';

Expand Down Expand Up @@ -122,16 +121,17 @@ const prepareImageOS1NetworkConfig = function (
configFilePath.image,
configFilePath.partition,
function (_fs) {
const readFileAsync = Promise.promisify(
_fs.readFile,
) as typeof fsPromise.readFile;
const writeFileAsync = Promise.promisify(_fs.writeFile);
return (
readFileAsync(configFilePath.path, {
encoding: 'utf8',
}) as Promise<string>
)
.catch(fileNotFoundError, () => '{}')
const readFileAsync = util.promisify(_fs.readFile);
const writeFileAsync = util.promisify(_fs.writeFile);
return readFileAsync(configFilePath.path, {
encoding: 'utf8',
})
.catch((e) => {
if (fileNotFoundError(e)) {
return '{}';
}
throw e;
})
.then(JSON.parse)
.then(function (contents) {
if (contents.files == null) {
Expand Down Expand Up @@ -181,10 +181,8 @@ const prepareImageOS2WifiConfig = function (
connectionsFolderDefinition.image,
connectionsFolderDefinition.partition,
function (_fs) {
const readdirAsync = Promise.promisify(_fs.readdir);
return readdirAsync(connectionsFolderDefinition.path) as Promise<
string[]
>;
const readdirAsync = util.promisify(_fs.readdir);
return readdirAsync(connectionsFolderDefinition.path);
},
)
.then(function (files) {
Expand All @@ -210,10 +208,8 @@ const prepareImageOS2WifiConfig = function (
inputDefinition.image,
inputDefinition.partition,
function (_fs) {
const readFileAsync = Promise.promisify(
_fs.readFile,
) as typeof fsPromise.readFile;
const writeFileAsync = Promise.promisify(_fs.writeFile);
const readFileAsync = util.promisify(_fs.readFile);
const writeFileAsync = util.promisify(_fs.writeFile);
return readFileAsync(inputDefinition.path, {
encoding: 'utf8',
}).then((contents) =>
Expand All @@ -240,10 +236,8 @@ const prepareImageOS2WifiConfig = function (
inputDefinition.image,
inputDefinition.partition,
function (_fs) {
const readFileAsync = Promise.promisify(
_fs.readFile,
) as typeof fsPromise.readFile;
const writeFileAsync = Promise.promisify(_fs.writeFile);
const readFileAsync = util.promisify(_fs.readFile);
const writeFileAsync = util.promisify(_fs.writeFile);
return readFileAsync(inputDefinition.path, {
encoding: 'utf8',
}).then((contents) =>
Expand All @@ -262,7 +256,7 @@ const prepareImageOS2WifiConfig = function (
definition.image,
definition.partition,
function (_fs) {
const writeFileAsync = Promise.promisify(_fs.writeFile);
const writeFileAsync = util.promisify(_fs.writeFile);
return writeFileAsync(definition.path, DEFAULT_CONNECTION_FILE);
},
);
Expand Down
18 changes: 7 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import Promise from 'bluebird';
import _ from 'lodash';
import type * as fsPromise from 'fs/promises';
import path from 'path';
import * as util from 'node:util';
import * as imagefs from 'balena-image-fs';
import type {
DeviceTypeConfigurationConfig,
Expand All @@ -43,15 +42,14 @@ export function getImageManifest(
// we encounter any errors along the way.
return Promise.resolve(
imagefs.interact(image, 1, function (_fs) {
const readFileAsync = Promise.promisify(_fs.readFile);
// @ts-expect-error TODO: Change to use node's util.promisify to fix the typings
const readFileAsync = util.promisify(_fs.readFile);
return readFileAsync('/device-type.json', {
encoding: 'utf8',
}) as Promise<string>;
});
}),
)
.then(JSON.parse)
.catchReturn(null);
.catch(() => null);
}

/**
Expand Down Expand Up @@ -157,9 +155,7 @@ export function getImageOsVersion(

return Promise.resolve(
imagefs.interact(definition.image, definition.partition, function (_fs) {
const readFileAsync = Promise.promisify(
_fs.readFile,
) as typeof fsPromise.readFile;
const readFileAsync = util.promisify(_fs.readFile);
return readFileAsync(definition.path, { encoding: 'utf8' });
}),
)
Expand Down Expand Up @@ -190,7 +186,7 @@ export function getImageOsVersion(
return parsedOsRelease.VERSION || null;
}
})
.catchReturn(null);
.catch(() => null);
}

/**
Expand Down Expand Up @@ -226,7 +222,7 @@ export const writeConfigJSON = function (
definitionWithImage.image,
definitionWithImage.partition,
function (_fs) {
const writeFileAsync = Promise.promisify(_fs.writeFile);
const writeFileAsync = util.promisify(_fs.writeFile);
return writeFileAsync(definitionWithImage.path, serializedConfig);
},
);
Expand Down
Loading

0 comments on commit cf99815

Please sign in to comment.