-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,7 +160,7 @@ const prepareImageOS1NetworkConfig = function ( | |
* | ||
* @returns {Promise<void>} | ||
*/ | ||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Repeats what we did in |
||
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 | ||
|
There was a problem hiding this comment.
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 ofdefinitionForImage()
, which we then read inreaddirAsync(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), sincereaddirAsync(CONNECTIONS_FOLDER);
reads easier thanreaddirAsync(connectionsFolderDefinition.path);
The change in
src/utils.ts
is an easier case that also demonstrates the same idea applied here.