Skip to content

Commit

Permalink
feat(tools): adds add/remove commands for tools
Browse files Browse the repository at this point in the history
This adds support for more tools:

- EditorConfig
- Prettier
- ls-lint
- GitHub
  - Stale
  - Dependabot
  - Semantic Pull Requests
  - Workflows
    - NodeJs
    - App
    - Lib

Now uses the cli-helpers-engine cache to download the workflows directly
from dhis2/workflows.
  • Loading branch information
varl committed May 20, 2021
1 parent 4e71d3e commit 20d2150
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 68 deletions.
38 changes: 38 additions & 0 deletions src/commands/actions/editorconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const log = require('@dhis2/cli-helpers-engine').reporter
const cfg = require('../../utils/config.js')

exports.command = 'editorconfig [type]'

exports.desc = 'Add the editorconfig configuration to the project.'

exports.builder = yargs =>
yargs
.positional('type', {
describe: 'Template to use for EditorConfig',
type: 'string',
default: 'base',
})
.option('overwrite', {
describe: 'Overwrite the existing configuration.',
type: 'boolean',
})
.example(
'$0 add editorconfig',
'Adds the standard configuration to .editorconfig'
)

exports.handler = argv => {
const { add, type, overwrite } = argv

log.info(`editorconfig > ${add ? 'add' : 'remove'}`)

if (add) {
cfg.add({
tool: 'editorconfig',
type: type ? type : 'base',
overwrite,
})
} else {
cfg.remove({ tool: 'editorconfig', type: type ? type : 'base' })
}
}
12 changes: 6 additions & 6 deletions src/commands/actions/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ const cfg = require('../../utils/config.js')

exports.command = 'eslint [type]'

exports.desc = ''
exports.desc = 'Add ESLint configuration to the project.'

exports.builder = yargs =>
yargs
.positional('type', {
describe: '',
describe: 'Configuration template for ESLint.',
type: 'string',
choices: Object.keys(cfg.templateConfigs.eslint),
default: 'base',
})
.option('overwrite', {
describe: '',
describe: 'Overwrite the existing configuration.',
type: 'boolean',
})
.example('$0', 'Adds the standard ESLint configuration to .eslintrc.js')

exports.handler = argv => {
const { add, type, overwrite } = argv

log.info(`eslint > ${add ? 'add' : 'remove'}`)

if (add) {
const template = type ? type : 'base'
cfg.add({
tool: 'eslint',
template,
type,
overwrite,
})
} else {
Expand Down
20 changes: 15 additions & 5 deletions src/commands/actions/git-hook.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path')
const log = require('@dhis2/cli-helpers-engine').reporter
const { husky } = require('../../tools/husky.js')
const { husky, supportedHooks } = require('../../tools/husky.js')
const { remove } = require('../../utils/config.js')
const { CONSUMING_ROOT, PROJECT_HOOKS_DIR } = require('../../utils/paths.js')

Expand All @@ -12,22 +12,32 @@ const defaultCommand = type =>

exports.command = 'git-hook <type> [command]'

exports.desc = ''
exports.desc = 'Add Git Hooks to the project.'

exports.builder = yargs =>
yargs
.positional('type', {
describe: '',
describe: 'Git Hook to register a command against.',
type: 'string',
choices: supportedHooks,
})
.positional('command', {
describe: '',
describe:
'The command the hook will trigger. Remember it must be enclosed in quotes.',
type: 'string',
})
.option('overwrite', {
describe: '',
describe: 'Overwrite the existing configuration.',
type: 'boolean',
})
.example(
'$0 add git-hook pre-push "yarn test"',
'Adds a pre-push git hook that runs the command "yarn test".'
)
.example(
'$0 add git-hook pre-commit "yarn d2-style apply --staged"',
'Applies code style to staged files before committing.'
)

exports.handler = argv => {
const { add, type, command, overwrite } = argv
Expand Down
43 changes: 43 additions & 0 deletions src/commands/actions/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const log = require('@dhis2/cli-helpers-engine').reporter
const cfg = require('../../utils/config.js')

exports.command = 'github <type>'

exports.desc = 'Add configuration files for GitHub'

exports.builder = yargs =>
yargs
.positional('type', {
describe: 'Template to use for GitHub configuration',
type: 'string',
choices: Object.keys(cfg.templateConfigs.github),
})
.option('overwrite', {
describe: 'Overwrite the existing configuration.',
type: 'boolean',
})
.example(
'$0 add github workflow-node',
'Adds the GitHub Workflow template for NodeJS projects'
)
.example(
'$0 add github dependabot',
'Adds the configuration file for Dependabot with DHIS2 defaults'
)

exports.handler = argv => {
const { add, type, overwrite, getCache } = argv

log.info(`github > ${add ? 'add' : 'remove'}`)

if (add) {
cfg.add({
tool: 'github',
type,
cache: getCache(),
overwrite,
})
} else {
cfg.remove({ tool: 'github', type })
}
}
15 changes: 9 additions & 6 deletions src/commands/actions/ls-lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,31 @@ exports.desc = 'Lint file and directory names.'
exports.builder = yargs =>
yargs
.positional('type', {
describe: '',
describe: 'Configuration template to use for ls-lint',
type: 'string',
default: 'base',
})
.option('overwrite', {
describe: '',
describe: 'Overwrite the existing configuration.',
type: 'boolean',
})
.example('$0', 'Adds the standard configuration to .ls-lint.yml')
.example(
'$0 add ls-lint',
'Adds the standard configuration to .ls-lint.yml'
)

exports.handler = argv => {
const { add, type, overwrite } = argv

log.info(`ls-lint > ${add ? 'add' : 'remove'}`)

if (add) {
const template = type ? type : 'base'
cfg.add({
tool: 'lslint',
template,
type: type ? type : 'base',
overwrite,
})
} else {
cfg.remove({ tool: 'lslint', type })
cfg.remove({ tool: 'lslint', type: type ? type : 'base' })
}
}
38 changes: 38 additions & 0 deletions src/commands/actions/prettier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const log = require('@dhis2/cli-helpers-engine').reporter
const cfg = require('../../utils/config.js')

exports.command = 'prettier [type]'

exports.desc = 'Add the Prettier configuration to the project.'

exports.builder = yargs =>
yargs
.positional('type', {
describe: 'Configuration template to use for Prettier',
type: 'string',
default: 'base',
})
.option('overwrite', {
describe: 'Overwrite the existing configuration.',
type: 'boolean',
})
.example(
'$0 add prettier',
'Adds the standard configuration to .prettierrc.js'
)

exports.handler = argv => {
const { add, type, overwrite } = argv

log.info(`prettier > ${add ? 'add' : 'remove'}`)

if (add) {
cfg.add({
tool: 'prettier',
type: type ? type : 'base',
overwrite,
})
} else {
cfg.remove({ tool: 'prettier', type: type ? type : 'base' })
}
}
31 changes: 3 additions & 28 deletions src/tools/husky.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,22 @@
const { join } = require('path')
const { bin } = require('@dhis2/cli-helpers-engine').exec
const { exit } = require('@dhis2/cli-helpers-engine')
const { PACKAGE_ROOT, PROJECT_HOOKS_DIR } = require('../utils/paths.js')

// https://git-scm.com/docs/githooks
const supportedHooks = [
'applypatch-msg',
'pre-applypatch',
'post-applypatch',
// subset of https://git-scm.com/docs/githooks
exports.supportedHooks = [
'pre-commit',
'pre-merge-commit',
'prepare-commit-msg',
'commit-msg',
'post-commit',
'pre-rebase',
'post-checkout',
'post-merge',
'pre-push',
'pre-receive',
'update',
'proc-receive',
'post-receive',
'post-update',
'reference-transaction',
'push-to-checkout',
'pre-auto-gc',
'post-rewrite',
'sendemail-validate',
'fsmonitor-watchman',
'p4-changelist',
'p4-prepare-changelist',
'p4-post-changelist',
'p4-pre-submit',
'post-index-change',
]

exports.isSupportedHook = hook => supportedHooks.includes(hook)
exports.isSupportedHook = hook => this.supportedHooks.includes(hook)

exports.husky = ({ command, hookType, hookCmd, callback }) => {
if (hookType && !this.isSupportedHook(hookType)) {
exit(1, 'Unsupported hook')
}

const cmd = 'husky'
const cwd = PACKAGE_ROOT
const args = [
Expand Down
Loading

0 comments on commit 20d2150

Please sign in to comment.