From 4cea6b650ec1c911cf2fc12a4a1dc410c0e7d075 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Mon, 30 Dec 2024 14:40:28 +0200 Subject: [PATCH 1/2] Avoid unnecessary destructuring Change-type: patch --- src/utils.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 2b66773..5f3c70e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -154,24 +154,21 @@ export async function getImageOsVersion( try { manifest ??= (await getImageManifest(image)) ?? undefined; - const definition = { - ...convertFilePathDefinition( - definitionForImage( - image, - manifest?.configuration?.config ?? { - partition: (await getBootPartition(image)) ?? 1, - }, - ), + const definition = convertFilePathDefinition( + definitionForImage( + image, + manifest?.configuration?.config ?? { + partition: (await getBootPartition(image)) ?? 1, + }, ), - path: '/os-release', - }; + ); const osReleaseString = await imagefs.interact( definition.image, definition.partition, function (_fs) { const readFileAsync = util.promisify(_fs.readFile); - return readFileAsync(definition.path, { encoding: 'utf8' }); + return readFileAsync('/os-release', { encoding: 'utf8' }); }, ); From eb1105cd738998bae9c3dd604a8d25f71eb38e19 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Mon, 30 Dec 2024 15:26:54 +0200 Subject: [PATCH 2/2] Convert some parts to async await and simplify Change-type: patch --- src/network.ts | 145 +++++++++++++++++++++---------------------------- 1 file changed, 63 insertions(+), 82 deletions(-) diff --git a/src/network.ts b/src/network.ts index 405a784..2262436 100644 --- a/src/network.ts +++ b/src/network.ts @@ -160,7 +160,7 @@ const prepareImageOS1NetworkConfig = function ( * * @returns {Promise} */ -const prepareImageOS2WifiConfig = function ( +const prepareImageOS2WifiConfig = async function ( target: string, manifest: DeviceTypeJsonWithConfiguration, ) { @@ -171,96 +171,77 @@ const prepareImageOS2WifiConfig = function ( * * if the `resin-sample.ignore` exists, it's copied to `resin-wifi` * * otherwise, the new file is created from a hardcoded template */ - const connectionsFolderDefinition = utils.definitionForImage( + const inputDefinition = utils.definitionForImage( target, - getConfigPathDefinition(manifest, CONNECTIONS_FOLDER), + utils.convertFilePathDefinition(manifest.configuration.config), ); - return imagefs - .interact( - connectionsFolderDefinition.image, - connectionsFolderDefinition.partition, - function (_fs) { - const readdirAsync = util.promisify(_fs.readdir); - return readdirAsync(connectionsFolderDefinition.path); - }, - ) - .then(function (files) { - // The required file already exists - if (_.includes(files, 'resin-wifi')) { - return; - } - - // Fresh image, new format, according to https://github.com/resin-os/meta-resin/pull/770/files - if (_.includes(files, 'resin-sample.ignore')) { - const inputDefinition = utils.definitionForImage( - target, - getConfigPathDefinition( - manifest, - `${CONNECTIONS_FOLDER}/resin-sample.ignore`, - ), - ); - const outputDefinition = utils.definitionForImage( - target, - getConfigPathDefinition(manifest, `${CONNECTIONS_FOLDER}/resin-wifi`), - ); - return imagefs.interact( - inputDefinition.image, - inputDefinition.partition, - function (_fs) { - const readFileAsync = util.promisify(_fs.readFile); - const writeFileAsync = util.promisify(_fs.writeFile); - return readFileAsync(inputDefinition.path, { - encoding: 'utf8', - }).then((contents) => - writeFileAsync(outputDefinition.path, contents), - ); + const files = await imagefs.interact( + inputDefinition.image, + inputDefinition.partition, + function (_fs) { + const readdirAsync = util.promisify(_fs.readdir); + return readdirAsync(CONNECTIONS_FOLDER); + }, + ); + + // The required file already exists + if (_.includes(files, 'resin-wifi')) { + return; + } + + // Fresh image, new format, according to https://github.com/resin-os/meta-resin/pull/770/files + if (_.includes(files, 'resin-sample.ignore')) { + await imagefs.interact( + inputDefinition.image, + inputDefinition.partition, + async function (_fs) { + const readFileAsync = util.promisify(_fs.readFile); + const writeFileAsync = util.promisify(_fs.writeFile); + const contents = await readFileAsync( + `${CONNECTIONS_FOLDER}/resin-sample.ignore`, + { + encoding: 'utf8', }, ); - } - - // Fresh image, old format - if (_.includes(files, 'resin-sample')) { - const inputDefinition = utils.definitionForImage( - target, - getConfigPathDefinition( - manifest, - `${CONNECTIONS_FOLDER}/resin-sample`, - ), - ); - const outputDefinition = utils.definitionForImage( - target, - getConfigPathDefinition(manifest, `${CONNECTIONS_FOLDER}/resin-wifi`), - ); - return imagefs.interact( - inputDefinition.image, - inputDefinition.partition, - function (_fs) { - const readFileAsync = util.promisify(_fs.readFile); - const writeFileAsync = util.promisify(_fs.writeFile); - return readFileAsync(inputDefinition.path, { - encoding: 'utf8', - }).then((contents) => - writeFileAsync(outputDefinition.path, contents), - ); + await writeFileAsync(`${CONNECTIONS_FOLDER}/resin-wifi`, contents); + }, + ); + return; + } + + // Fresh image, old format + if (_.includes(files, 'resin-sample')) { + await imagefs.interact( + inputDefinition.image, + inputDefinition.partition, + async function (_fs) { + const readFileAsync = util.promisify(_fs.readFile); + const writeFileAsync = util.promisify(_fs.writeFile); + const contents = await readFileAsync( + `${CONNECTIONS_FOLDER}/resin-sample`, + { + encoding: 'utf8', }, ); - } + await writeFileAsync(`${CONNECTIONS_FOLDER}/resin-wifi`, contents); + }, + ); + return; + } - // In case there's no file at all (shouldn't happen normally, but the file might have been removed) - const definition = utils.definitionForImage( - target, - getConfigPathDefinition(manifest, `${CONNECTIONS_FOLDER}/resin-wifi`), - ); - return imagefs.interact( - definition.image, - definition.partition, - function (_fs) { - const writeFileAsync = util.promisify(_fs.writeFile); - return writeFileAsync(definition.path, DEFAULT_CONNECTION_FILE); - }, + // In case there's no file at all (shouldn't happen normally, but the file might have been removed) + await imagefs.interact( + inputDefinition.image, + inputDefinition.partition, + async function (_fs) { + const writeFileAsync = util.promisify(_fs.writeFile); + await writeFileAsync( + `${CONNECTIONS_FOLDER}/resin-wifi`, + DEFAULT_CONNECTION_FILE, ); - }); + }, + ); }; // Taken from https://goo.gl/kr1kCt