-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new and improved d2-style (#378)
"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
Showing
71 changed files
with
874 additions
and
815 deletions.
There are no files selected for viewing
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
_ |
This file contains 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
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" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
./bin/d2-style check |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
yarn test |
This file contains 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
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.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains 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
Empty file.
This file contains 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
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}', | ||
}, | ||
} |
0
config/js/eslint-react.config.js → config/eslint-react.config.js
100644 → 100755
File renamed without changes.
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
0
config/js/prettier.config.js → config/prettier.config.js
100644 → 100755
File renamed without changes.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains 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
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.
This file contains 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
This file contains 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
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') |
This file contains 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
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 }) | ||
} | ||
} |
This file contains 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
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 }) | ||
} | ||
} |
This file contains 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
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"' | ||
) |
This file contains 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
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.' | ||
) |
This file contains 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
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.' | ||
) |
Oops, something went wrong.