|
| 1 | +const inquirer = require('inquirer').default |
| 2 | +const { initializeStore } = require('../store') |
| 3 | +const { logger } = require('../utils') |
| 4 | +const debug = require('debug')('cli:checklists') |
| 5 | +const { stringToArray } = require('@ulisesgascon/string-to-array') |
| 6 | +const checks = require('../checks') |
| 7 | + |
| 8 | +async function listChecklist (knex, options = {}) { |
| 9 | + const { getAllChecklists } = initializeStore(knex) |
| 10 | + const checklists = await getAllChecklists(knex) |
| 11 | + checklists.forEach(checklist => { |
| 12 | + logger.info(`- [${checklist.author}][${checklist.code_name}] ${checklist.title}`) |
| 13 | + }) |
| 14 | + logger.info('------------------------------------') |
| 15 | + logger.info(`Available checklists: ${checklists.length}.`) |
| 16 | + return checklists |
| 17 | +} |
| 18 | + |
| 19 | +async function runChecklist (knex, options = {}) { |
| 20 | + const { getAllChecklists, getAllProjects, getAllChecksInChecklistById } = initializeStore(knex) |
| 21 | + const checklists = await getAllChecklists(knex) |
| 22 | + const projects = await getAllProjects(knex) |
| 23 | + const validCommandNames = checklists.map((item) => item.code_name) |
| 24 | + const validProjectNames = ['ALL'].concat(projects.map((item) => item.name)) |
| 25 | + |
| 26 | + if (Object.keys(options).length && !validCommandNames.includes(options.name)) { |
| 27 | + throw new Error('Invalid checklist name. Please enter a valid checklist name.') |
| 28 | + } |
| 29 | + |
| 30 | + if (options.projectScope && options.projectScope.length > 0) { |
| 31 | + const invalidProjects = stringToArray(options.projectScope[0]).filter((project) => !validProjectNames.includes(project)) |
| 32 | + if (invalidProjects.length > 0) { |
| 33 | + throw new Error(`Invalid project names: ${invalidProjects.join(', ')}`) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + if (validProjectNames.length === 1) { |
| 38 | + throw new Error('No projects in database. Please add projects to run checklists') |
| 39 | + } |
| 40 | + |
| 41 | + const answers = options.name |
| 42 | + ? options |
| 43 | + : await inquirer.prompt([ |
| 44 | + { |
| 45 | + type: 'list', |
| 46 | + name: 'name', |
| 47 | + message: 'What is the name (code_name) of the checklist?', |
| 48 | + choices: validCommandNames, |
| 49 | + when: () => !options.name |
| 50 | + }, |
| 51 | + { |
| 52 | + type: 'checkbox', |
| 53 | + name: 'projectScope', |
| 54 | + choices: validProjectNames, |
| 55 | + message: 'Choose the projects to run the checklist against', |
| 56 | + when: () => !options.name |
| 57 | + } |
| 58 | + ]) |
| 59 | + |
| 60 | + if (!answers.projectScope || answers.projectScope?.length === 0 || stringToArray(answers.projectScope[0]).includes('ALL')) { |
| 61 | + answers.projectScope = undefined |
| 62 | + logger.info('Running checklist against all projects') |
| 63 | + } else { |
| 64 | + const projectsSelected = stringToArray(answers.projectScope[0]) |
| 65 | + answers.projectScope = projectsSelected.map(p => projects.find(project => project.name === p)) |
| 66 | + logger.info('Running checklist against specific projects') |
| 67 | + } |
| 68 | + |
| 69 | + debug('Running checklist with code_name:', answers.name) |
| 70 | + |
| 71 | + const checklist = checklists.find(checklist => checklist.code_name === answers.name) |
| 72 | + const checksToRun = await getAllChecksInChecklistById(knex, checklist.id) |
| 73 | + |
| 74 | + for (const check of checksToRun) { |
| 75 | + if (check.implementation_status !== 'completed') { |
| 76 | + logger.info(`Check (${check.code_name}) is not implemented yet. skipping...`) |
| 77 | + continue |
| 78 | + } |
| 79 | + logger.info(`Running check (${check.code_name})`) |
| 80 | + await checks[check.code_name](knex, { projects: answers.projectScope }) |
| 81 | + } |
| 82 | + |
| 83 | + logger.info(`Checklist (${answers.name}) ran successfully`) |
| 84 | +} |
| 85 | + |
| 86 | +module.exports = { |
| 87 | + listChecklist, |
| 88 | + runChecklist |
| 89 | +} |
0 commit comments