Created
October 1, 2019 05:49
-
-
Save jpgls/e53919aafd90b8e14e01c233e6a8adb3 to your computer and use it in GitHub Desktop.
Listr - dynamically populate nested list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const Listr = require('listr'); | |
const config = require('./config'); | |
const { | |
connect, | |
getSaveableMeasures, | |
convertToMeasureValues, | |
saveMeasures, | |
updateMeasureState | |
} = require('.'); | |
const pool = connect(config); | |
const tasks = new Listr([{ | |
title: 'Find unsaved measures', | |
task: async ctx => { | |
ctx.unsavedMeasures = await getSaveableMeasures(pool); | |
} | |
}, { | |
title: 'Saving measures', | |
task: ctx => { | |
const savingProcedures = ctx.unsavedMeasures.map(({year, code, results}) => ({ | |
title: code, | |
task: async() => { | |
const measures = convertToMeasureValues(results, 4242, year); | |
await saveMeasures(measures, pool); | |
await updateMeasureState(code, 'Caricata', pool); | |
} | |
})); | |
return new Listr(savingProcedures); | |
} | |
}, { | |
title: 'All measures saved', | |
task: () => pool.close() | |
}]); | |
tasks.run().catch(err => { | |
console.error(err); | |
}); | |
// originally found at https://github.com/SamVerschueren/listr/issues/50 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment