Skip to content

Commit

Permalink
feat: new and improved d2-style (#378)
Browse files Browse the repository at this point in the history
"d2-style" no longer tries to obfuscate what tools are used under the hood, and
leans into being a friendly helper to nudge developers to comply with the dhis2
code style and guidelines.

New features:

-   "d2-style <check/apply>" can now run all configured linters and formatters
in a single command, making it easier to use.

-   "d2-style <add/remove>" is used to add configuration files for individual
tools, e.g. eslint, prettier, etc.

-   "d2-style add" supports templates for e.g. react using:

		d2-style add eslint react

-   "d2-style install" reads the project configuration from .d2/style.config.js
and sets up the project in accordance to that.

BREAKING CHANGE: The verb (check/apply) is now moved to the top-level.  E.g.
"d2-style js check" becomes "d2-style check js". This is to allow all checkers
to run with a single command: "d2-style check"

BREAKING CHANGE: "d2-style install" is no longer used to set up linters.  As of
husky@5 the tool is vastly simplified and much faster. Hooks can be installed
manually with "d2-style install", but is also run as a "post-install" script
that we control for consistency. Configuration is added to the project with the
"d2-style add" command.
  • Loading branch information
varl authored Apr 7, 2021
1 parent e30d0fe commit f8279e5
Show file tree
Hide file tree
Showing 71 changed files with 874 additions and 815 deletions.
Empty file modified .browserslistrc
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions .d2/hooks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
4 changes: 4 additions & 0 deletions .d2/hooks/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

./bin/d2-style check commit "$1"
4 changes: 4 additions & 0 deletions .d2/hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

./bin/d2-style check
4 changes: 4 additions & 0 deletions .d2/hooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn test
7 changes: 7 additions & 0 deletions .d2/style.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
hooks: {
'commit-msg': ['./bin/d2-style check commit "$1"'],
'pre-commit': ['./bin/d2-style check'],
'pre-push': ['yarn test'],
},
}
Empty file modified .editorconfig
100644 → 100755
Empty file.
Empty file modified .eslintrc.js
100644 → 100755
Empty file.
Empty file modified .github/dependabot.yml
100644 → 100755
Empty file.
Empty file modified .github/semantic.yml
100644 → 100755
Empty file.
Empty file modified .github/stale.yml
100644 → 100755
Empty file.
Empty file modified .github/workflows/codeql-analysis.yml
100644 → 100755
Empty file.
Empty file modified .github/workflows/comment-and-close.yml
100644 → 100755
Empty file.
Empty file modified .github/workflows/dhis2-verify-node.yml
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .prettierrc.js
100644 → 100755
Empty file.
23 changes: 23 additions & 0 deletions CHANGELOG.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

# [8.0.0-alpha.2](https://github.com/dhis2/cli-style/compare/v8.0.0-alpha.1...v8.0.0-alpha.2) (2020-12-16)

# [8.0.0-alpha.3](https://github.com/dhis2/cli-style/compare/v8.0.0-alpha.2...v8.0.0-alpha.3) (2020-12-16)

# [8.0.0-alpha.2](https://github.com/dhis2/cli-style/compare/v8.0.0-alpha.1...v8.0.0-alpha.2) (2020-12-16)

### Features

* opt-in git hooks ([#333](https://github.com/dhis2/cli-style/issues/333)) ([a3bc415](https://github.com/dhis2/cli-style/commit/a3bc415d3971756e2ef53ab59dbbc4fb48046b07))
Expand All @@ -29,6 +33,25 @@ remove hooks that reference husky.sh in .git/hooks.

* **eslint:** add import order rule ([#334](https://github.com/dhis2/cli-style/issues/334)) ([c7b9991](https://github.com/dhis2/cli-style/commit/c7b99917e39b2ed16165d0a115f0e0c3ebe775e1))

# [8.0.0-alpha.2](https://github.com/dhis2/cli-style/compare/v8.0.0-alpha.1...v8.0.0-alpha.2) (2020-12-16)

### Features

* opt-in git hooks ([#333](https://github.com/dhis2/cli-style/issues/333)) ([a3bc415](https://github.com/dhis2/cli-style/commit/a3bc415d3971756e2ef53ab59dbbc4fb48046b07))

# [8.0.0-alpha.1](https://github.com/dhis2/cli-style/compare/v7.2.3...v8.0.0-alpha.1) (2020-12-16)


### Code Refactoring

* remove husky ([752b944](https://github.com/dhis2/cli-style/commit/752b94409a3a9735d324d05aefdd296f0d6bfb44))


### BREAKING CHANGES

* Husky has been removed from cli-style. You will need to
remove hooks that reference husky.sh in .git/hooks.

## [7.2.3](https://github.com/dhis2/cli-style/compare/v7.2.2...v7.2.3) (2020-12-16)


Expand Down
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified config/commitlint.config.js
100644 → 100755
Empty file.
18 changes: 18 additions & 0 deletions config/d2-style.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
hooks: {
'commit-msg': ['yarn d2-style commit check "$1"'],
'pre-commit': ['yarn d2-style check'],
'pre-push': ['yarn test'],
},
tools: {
eslint: ['react'],
prettier: [],
github: ['dependabot', 'semantic-pr', 'stale'],
editorconfig: [],
commitlint: [],
},
patterns: {
js: '**/*.{js,jsx,ts,tsx}',
text: '**/*.{md,json,yml,html}',
},
}
File renamed without changes.
3 changes: 0 additions & 3 deletions config/js/eslint.config.js → config/eslint.config.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ module.exports = {
// unignore implicit rules about what types of files can be linted
ignorePatterns: ['!.*'],

plugins: ['prettier'],

env: {
browser: true,
node: true,
Expand Down Expand Up @@ -42,7 +40,6 @@ module.exports = {
},
],
'no-mixed-spaces-and-tabs': ['error'],
'prettier/prettier': ['error'],
'import/order': [
'error',
{
Expand Down
2 changes: 0 additions & 2 deletions config/git/hooks/commit-msg

This file was deleted.

5 changes: 0 additions & 5 deletions config/js/browserslist.config.rc

This file was deleted.

File renamed without changes.
Empty file modified docs/_sidebar.md
100644 → 100755
Empty file.
Empty file modified docs/faq.md
100644 → 100755
Empty file.
Empty file modified docs/getting-started.md
100644 → 100755
Empty file.
Empty file modified docs/ignore-files.md
100644 → 100755
Empty file.
Empty file modified docs/migrate-guide.md
100644 → 100755
Empty file.
Empty file modified docs/overrides.md
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const command = require('./src/index.js')
const { bundledConfigPaths } = require('./src/utils/groups.js')
const { packageConfigs } = require('./src/utils/config.js')

exports.config = bundledConfigPaths()
exports.config = packageConfigs
exports.command = command
Empty file modified netlify.toml
100644 → 100755
Empty file.
16 changes: 6 additions & 10 deletions package.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@
},
"scripts": {
"test": "tape tests/**/*.js",
"lint:js": "./bin/d2-style js check",
"lint:text": "./bin/d2-style text check",
"lint:staged": "yarn lint:js --staged && yarn lint:text --staged",
"lint": "yarn lint:js && yarn lint:text",
"format:js": "./bin/d2-style js apply",
"format:text": "./bin/d2-style text apply",
"format:staged": "yarn format:js --staged && yarn format:text --staged",
"format": "yarn format:js && yarn format:text",
"lint:staged": "yarn lint --staged",
"lint": "./bin/d2-style check",
"format:staged": "yarn format --staged",
"format": "./bin/d2-style apply",
"build:docs": "d2-utils-docsite build ./docs -o ./dist --jsdoc src/ --jsdocOutputFile api.md",
"start": "d2-utils-docsite serve ./docs -o ./dist --jsdoc src/ --jsdocOutputFile api.md"
},
Expand All @@ -37,11 +33,11 @@
"eslint": "^7.18.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-react": "^7.22.0",
"fast-glob": "^3.2.5",
"find-up": "^5.0.0",
"fs-extra": "^9.0.1",
"fs-extra": "^9.1.0",
"husky": "^5.2.0",
"perfy": "^1.1.5",
"prettier": "^2.2.1",
"semver": "^7.3.4",
Expand Down
15 changes: 12 additions & 3 deletions src/commands.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
module.exports = yargs => {
yargs.commandDir('commands')
}
const { packageConfigs, styleConfig } = require('./utils/config.js')

module.exports = yargs =>
yargs
.config({
config: {
...require(packageConfigs.d2Style),
...styleConfig,
},
files: [],
})
.commandDir('commands')
35 changes: 35 additions & 0 deletions src/commands/actions/eslint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const log = require('@dhis2/cli-helpers-engine').reporter
const cfg = require('../../utils/config.js')

exports.command = 'eslint [type]'

exports.desc = ''

exports.builder = yargs =>
yargs
.positional('type', {
describe: '',
type: 'string',
})
.option('overwrite', {
describe: '',
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,
overwrite,
})
} else {
cfg.remove({ tool: 'eslint', type })
}
}
49 changes: 49 additions & 0 deletions src/commands/actions/git-hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const path = require('path')
const log = require('@dhis2/cli-helpers-engine').reporter
const { husky } = require('../../tools/husky.js')
const { remove } = require('../../utils/config.js')
const { CONSUMING_ROOT, PROJECT_HOOKS_DIR } = require('../../utils/paths.js')

const defaultCommand = type =>
`echo "To customize this hook, edit ${type} in ${path.relative(
CONSUMING_ROOT,
PROJECT_HOOKS_DIR
)}"\nexit 1`

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

exports.desc = ''

exports.builder = yargs =>
yargs
.positional('type', {
describe: '',
type: 'string',
})
.positional('command', {
describe: '',
type: 'string',
})
.option('overwrite', {
describe: '',
type: 'boolean',
})

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

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

if (add) {
const cmd = overwrite ? 'set' : 'add'

husky({
command: cmd,
hookType: type,
hookCmd: command ? command : defaultCommand(type),
})
} else {
const hookFile = path.join(PROJECT_HOOKS_DIR, type)
remove({ path: hookFile })
}
}
22 changes: 22 additions & 0 deletions src/commands/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
exports.command = 'add'

exports.describe = 'Add a configuration file for a tool to the project.'

exports.builder = yargs =>
yargs
.commandDir('actions')
.config({
add: true,
})
.example(
'$0 add eslint',
'Adds the standard ESLint configuration to the project.'
)
.example(
'$0 add eslint react',
'Adds React specific ESLint configuration to the project.'
)
.example(
'$0 add git-hook pre-push "yarn test"',
'Adds a pre-push git hook that runs the command "yarn test"'
)
31 changes: 31 additions & 0 deletions src/commands/apply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { jscmd } = require('./check.js')

exports.command = 'apply'

exports.describe =
'Apply code format and fix all lint issues that can be resolved automatically.'

exports.builder = yargs =>
jscmd(yargs)
.command(require('./types/javascript.js'))
.command(require('./types/structured-text.js'))
.option('staged', {
describe: 'Only check staged files in Git',
type: 'boolean',
default: false,
})
.config({
apply: true,
})
.example(
'$0 apply',
'Finds and applies code format and lint fixes to all supported, or configured, file formats.'
)
.example(
'$0 apply js',
'Applies code format and lint fixes to JavaScript files.'
)
.example(
'$0 apply text --staged',
'Only match files that are staged in Git.'
)
66 changes: 66 additions & 0 deletions src/commands/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const log = require('@dhis2/cli-helpers-engine').reporter
const { callback, exit } = require('../utils/run.js')
const { handler: jsHandler } = require('./types/javascript.js')
const { handler: textHandler } = require('./types/structured-text.js')

const statusCode = callback()

exports.jscmd = yargs =>
yargs.command(
'$0 [files..]',
'default',
yargs =>
yargs.positional('files', {
describe: '',
type: 'string',
}),
argv => {
if (!argv.config.patterns) {
log.warn(
'No patterns defined, please check the configuration file'
)
process.exit(1)
}

if (argv.config.patterns.js) {
jsHandler(argv, statusCode)
log.print('')
}

if (argv.config.patterns.text) {
textHandler(argv, statusCode)
}

exit(statusCode())
}
)

exports.command = 'check'

exports.describe = 'Automatically run the appropriate checks on files'

exports.builder = yargs =>
this.jscmd(yargs)
.command(require('./types/javascript.js'))
.command(require('./types/structured-text.js'))
.command(require('./types/commit.js'))
.option('staged', {
describe: 'Only check staged files in Git',
type: 'boolean',
default: false,
})
.config({
apply: false,
})
.example(
'$0 check',
'Finds and checks code for formatting and lint issues, for all supported, or configured, file formats.'
)
.example(
'$0 check js',
'Checks code format and lint issues to JavaScript files.'
)
.example(
'$0 check text --staged',
'Only match files that are staged in Git.'
)
Loading

0 comments on commit f8279e5

Please sign in to comment.