Skip to content

Commit

Permalink
feat: export configs from package (#93)
Browse files Browse the repository at this point in the history
* refactor: move implicit starting point to src

* feat: export configs from package

* refactor: prettier requires the actual configuration

* refactor: export command from module

* refactor: simplify bundledConfigPaths

* refactor: add comment explaining special case
  • Loading branch information
varl authored Jul 15, 2019
1 parent 097a22c commit 55fd0f9
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { config } = require('./index.js')

module.exports = {
extends: ['./config/js/eslint.config.js'],
extends: [config.eslint],
}
2 changes: 1 addition & 1 deletion .huskyrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
hooks: {
'commit-msg': './bin/d2-style commit check',
'pre-commit':
'./bin/d2-style validate --lint-staged-config .lint-stagedrc.js',
'yarn test && ./bin/d2-style validate --lint-staged-config .lint-stagedrc.js',
},
}
1 change: 0 additions & 1 deletion .lint-stagedrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const stage = process.env.CLI_STYLE_STAGE === 'true'

module.exports = {
'*.{js,jsx,ts,tsx}': [
'yarn test',
`./bin/d2-style js ${fix ? 'apply' : 'check'} ${
fix && stage ? '--stage' : ''
}`,
Expand Down
6 changes: 5 additions & 1 deletion .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
module.exports = require('./config/js/prettier.config.js')
const { config } = require('./index.js')

module.exports = {
...require(config.prettier),
}
2 changes: 1 addition & 1 deletion bin/d2-style
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
const { makeEntryPoint } = require('@dhis2/cli-helpers-engine')
const command = require('..')
const command = require('../src')

makeEntryPoint(command)
4 changes: 3 additions & 1 deletion config/js/eslint.local.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { config } = require('@dhis2/cli-style')

module.exports = {
extends: ['./node_modules/@dhis2/cli-style/config/js/eslint.config.js'],
extends: [config.eslint],
}
4 changes: 3 additions & 1 deletion config/js/prettier.local.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { config } = require('@dhis2/cli-style')

module.exports = {
...require('@dhis2/cli-style/config/js/prettier.config.js'),
...require(config.prettier),
}
12 changes: 4 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const { namespace } = require('@dhis2/cli-helpers-engine')
const { bundledConfigPaths } = require('./src/groups.js')
const command = require('./src/index.js')

const command = namespace('style', {
desc: 'DHIS2 programmatic style for commit msgs/code',
aliases: 's',
builder: require('./src/cmds.js'),
})

module.exports = command
exports.config = bundledConfigPaths()
exports.command = command
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@dhis2/cli-style",
"version": "4.0.0",
"description": "The code and commit style for DHIS2.",
"main": "index.js",
"bin": {
"d2-style": "bin/d2-style"
},
Expand Down
41 changes: 41 additions & 0 deletions src/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,46 @@ const groupConfigs = selector => {
return [...result]
}

/**
* Returns an object which contains the bundled configuration file for
* each tool in cli-style
*/
const bundledConfigPaths = () => {
const config = {}

for (const selector of groups) {
const groupName = selector[0]
const tools = selector[1]

for (const identifier of tools) {
const toolName = identifier[0]
const toolConfigs = identifier[1]
const sourceConfigPath = toolConfigs[0]

switch (toolName) {
/* Some tools have two configs, e.g. `*.config.js` and `*.local.js`.
* Usually we want the local configuration (see the
* groups array) since that is what we install to the
* local repo, but in this case we need the internal
* configuration file path, so we need to override it
* here.
*/
case 'prettier':
config.prettier = PRETTIER_CONFIG
break
case 'eslint':
config.eslint = ESLINT_CONFIG
break
default:
config[toolName] = sourceConfigPath
break
}
}
}

return config
}

module.exports = {
groups,
projects,
Expand All @@ -247,4 +287,5 @@ module.exports = {
resolveProjectToGroups,
printGroups,
groupConfigs,
bundledConfigPaths,
}
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { namespace } = require('@dhis2/cli-helpers-engine')

const command = namespace('style', {
desc: 'DHIS2 programmatic style for commit msgs/code',
aliases: 's',
builder: require('./cmds.js'),
})

module.exports = command
30 changes: 30 additions & 0 deletions tests/group-resolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,22 @@ const {
resolveProjectToGroups,
groupConfigs,
expandGroupAll,
bundledConfigPaths,
} = require('../src/groups.js')

const {
BROWSERSLIST_CONFIG,
COMMITLINT_CONFIG,
HUSKY_CONFIG,
STALE_CONFIG,
DEPENDABOT_CONFIG,
EDITORCONFIG_CONFIG,
SEMANTIC_PR_CONFIG,
LINT_STAGED_CONFIG,
PRETTIER_CONFIG,
ESLINT_CONFIG,
} = require('../src/paths.js')

function findGroup(identifier) {
const res = []
for (const group of groups) {
Expand Down Expand Up @@ -76,3 +90,19 @@ test('valid groups can be expanded', t => {
'a combo group should expand to the comboed groups'
)
})

test('convert to tool:config object', t => {
t.plan(9)

const obj = bundledConfigPaths()

t.equals(obj.eslint, ESLINT_CONFIG)
t.equals(obj.husky, HUSKY_CONFIG)
t.equals(obj.prettier, PRETTIER_CONFIG)
t.equals(obj['lint-staged'], LINT_STAGED_CONFIG)
t.equals(obj.dependabot, DEPENDABOT_CONFIG)
t.equals(obj['probot-stale'], STALE_CONFIG)
t.equals(obj['probot-semantic-pull-requests'], SEMANTIC_PR_CONFIG)
t.equals(obj.editorconfig, EDITORCONFIG_CONFIG)
t.equals(obj.browserslist, BROWSERSLIST_CONFIG)
})
10 changes: 10 additions & 0 deletions tests/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const test = require('tape')

const { command, config } = require('../index.js')

test('base exports are objects', t => {
t.plan(2)

t.ok(typeof command === 'object')
t.ok(typeof config === 'object')
})

0 comments on commit 55fd0f9

Please sign in to comment.