Skip to content
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ You can list all the implemented checks, use the following command:
node index.js check list
```

You can any implemented check at any time by running the following command:

```bash
node index.js check run [--name <name>]
```


## Debug mode

This project uses the [debug library](https://www.npmjs.com/package/debug), so you can always use the environmental variable `DEBUG=*` to print more detailed information of the execution.
Expand Down
10 changes: 10 additions & 0 deletions __tests__/cli/check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ describe('list - Non-Interactive Mode', () => {
await expect(availableChecksList).toEqual(relevantChecks)
})
})

describe('run - Interactive Mode', () => {
test.todo('Should run the check when a valid name is provided')
})
describe('run - Non-Interactive Mode', () => {
test.todo('Should throw an error when invalid name is provided')
test.todo('Should throw an error when no name is provided')
test.todo('Should throw an error when the check is not implemented')
test.todo('Should run the check when a valid name is provided')
})
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { Command } = require('commander')
const { getConfig } = require('./src/config')
const { projectCategories, dbSettings } = getConfig()
const { logger } = require('./src/utils')
const { runAddProjectCommand, runWorkflowCommand, listWorkflowCommand, listCheckCommand } = require('./src/cli')
const { runAddProjectCommand, runWorkflowCommand, listWorkflowCommand, listCheckCommand, runCheckCommand } = require('./src/cli')
const knex = require('knex')(dbSettings)

const program = new Command()
Expand Down Expand Up @@ -69,4 +69,19 @@ check
}
})

check
.command('run')
.description('Run a check')
.option('--name <name>', 'Name of the check')
.action(async (options) => {
try {
await runCheckCommand(knex, options)
} catch (error) {
logger.error('Error running check:', error.message)
process.exit(1)
} finally {
await knex.destroy()
}
})

program.parse(process.argv)
Empty file.
20 changes: 20 additions & 0 deletions src/checks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { readdirSync } = require('fs')
const { join } = require('path')
const debug = require('debug')('checks:index')

// This will load all the files in the complianceChecks directory and export them as an object. It works similar to require-all
debug('Loading compliance check files...')
const checksPath = join(__dirname, 'complianceChecks')
const files = readdirSync(checksPath)
const jsFiles = files.filter(file => file.endsWith('.js'))
const checks = {}
for (const file of jsFiles) {
debug(`Loading ${file}...`)
const [fileName] = file.split('.')
const fileContent = require(join(checksPath, file))
checks[fileName] = fileContent
}

debug('Checks files loaded')

module.exports = checks
32 changes: 31 additions & 1 deletion src/cli/checks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const inquirer = require('inquirer').default
const { initializeStore } = require('../store')
const { logger } = require('../utils')
const checks = require('../checks')
const debug = require('debug')('cli:checks')

async function listCheckCommand (knex, options = {}) {
const { getAllComplianceChecks } = initializeStore(knex)
Expand All @@ -13,6 +16,33 @@ async function listCheckCommand (knex, options = {}) {
return implementedChecks
}

async function runCheckCommand (knex, options = {}) {
const { getAllComplianceChecks } = initializeStore(knex)
const complianceChecks = await getAllComplianceChecks(knex)
const implementedChecks = complianceChecks.filter(check => check.implementation_status === 'completed')
const validCommandNames = implementedChecks.map((item) => item.code_name)

if (Object.keys(options).length && !validCommandNames.includes(options.name)) {
throw new Error('Invalid check name. Please enter a valid check name.')
}

const answers = options.name
? options
: await inquirer.prompt([
{
type: 'list',
name: 'name',
message: 'What is the name (code_name) of the check?',
choices: validCommandNames,
when: () => !options.name
}
])

debug('Running check with code_name:', answers.name)
checks[answers.name](knex)
}

module.exports = {
listCheckCommand
listCheckCommand,
runCheckCommand
}