Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert some parts to async await and simplify #49

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 63 additions & 82 deletions src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const prepareImageOS1NetworkConfig = function (
*
* @returns {Promise<void>}
*/
const prepareImageOS2WifiConfig = function (
const prepareImageOS2WifiConfig = async function (
target: string,
manifest: DeviceTypeJsonWithConfiguration,
) {
Expand All @@ -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),
Copy link
Contributor Author

@thgreasi thgreasi Dec 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All that this was doing was setting path=CONNECTIONS_FOLDER to the result of definitionForImage(), which we then read in readdirAsync(connectionsFolderDefinition.path);.
So the reasoning of this refactoring (which I repeat all of the place) was to avoid the extra destructing and make things simpler by directly referencing the CONNECTIONS_FOLDER in the readdirAsync (9 lines lower), since readdirAsync(CONNECTIONS_FOLDER); reads easier than readdirAsync(connectionsFolderDefinition.path);

The change in src/utils.ts is an easier case that also demonstrates the same idea applied here.

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(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repeats what we did in const connectionsFolderDefinition = utils.definitionForImage() above, and just sets a different path. Since we no longer access the .path via this result, we can run definitionForImage() just once and re-use it in all branches w/ just different inline paths ${CONNECTIONS_FOLDER}/resin-sample.ignore in this case

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);
joshbwlng marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
19 changes: 8 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
},
);

Expand Down
Loading