Skip to content

Commit 55fd0f9

Browse files
authored
feat: export configs from package (#93)
* 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
1 parent 097a22c commit 55fd0f9

File tree

13 files changed

+111
-15
lines changed

13 files changed

+111
-15
lines changed

.eslintrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { config } = require('./index.js')
2+
13
module.exports = {
2-
extends: ['./config/js/eslint.config.js'],
4+
extends: [config.eslint],
35
}

.huskyrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module.exports = {
22
hooks: {
33
'commit-msg': './bin/d2-style commit check',
44
'pre-commit':
5-
'./bin/d2-style validate --lint-staged-config .lint-stagedrc.js',
5+
'yarn test && ./bin/d2-style validate --lint-staged-config .lint-stagedrc.js',
66
},
77
}

.lint-stagedrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const stage = process.env.CLI_STYLE_STAGE === 'true'
33

44
module.exports = {
55
'*.{js,jsx,ts,tsx}': [
6-
'yarn test',
76
`./bin/d2-style js ${fix ? 'apply' : 'check'} ${
87
fix && stage ? '--stage' : ''
98
}`,

.prettierrc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
module.exports = require('./config/js/prettier.config.js')
1+
const { config } = require('./index.js')
2+
3+
module.exports = {
4+
...require(config.prettier),
5+
}

bin/d2-style

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
22
const { makeEntryPoint } = require('@dhis2/cli-helpers-engine')
3-
const command = require('..')
3+
const command = require('../src')
44

55
makeEntryPoint(command)

config/js/eslint.local.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { config } = require('@dhis2/cli-style')
2+
13
module.exports = {
2-
extends: ['./node_modules/@dhis2/cli-style/config/js/eslint.config.js'],
4+
extends: [config.eslint],
35
}

config/js/prettier.local.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { config } = require('@dhis2/cli-style')
2+
13
module.exports = {
2-
...require('@dhis2/cli-style/config/js/prettier.config.js'),
4+
...require(config.prettier),
35
}

index.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
const { namespace } = require('@dhis2/cli-helpers-engine')
1+
const { bundledConfigPaths } = require('./src/groups.js')
2+
const command = require('./src/index.js')
23

3-
const command = namespace('style', {
4-
desc: 'DHIS2 programmatic style for commit msgs/code',
5-
aliases: 's',
6-
builder: require('./src/cmds.js'),
7-
})
8-
9-
module.exports = command
4+
exports.config = bundledConfigPaths()
5+
exports.command = command

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "@dhis2/cli-style",
33
"version": "4.0.0",
44
"description": "The code and commit style for DHIS2.",
5+
"main": "index.js",
56
"bin": {
67
"d2-style": "bin/d2-style"
78
},

src/groups.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,46 @@ const groupConfigs = selector => {
238238
return [...result]
239239
}
240240

241+
/**
242+
* Returns an object which contains the bundled configuration file for
243+
* each tool in cli-style
244+
*/
245+
const bundledConfigPaths = () => {
246+
const config = {}
247+
248+
for (const selector of groups) {
249+
const groupName = selector[0]
250+
const tools = selector[1]
251+
252+
for (const identifier of tools) {
253+
const toolName = identifier[0]
254+
const toolConfigs = identifier[1]
255+
const sourceConfigPath = toolConfigs[0]
256+
257+
switch (toolName) {
258+
/* Some tools have two configs, e.g. `*.config.js` and `*.local.js`.
259+
* Usually we want the local configuration (see the
260+
* groups array) since that is what we install to the
261+
* local repo, but in this case we need the internal
262+
* configuration file path, so we need to override it
263+
* here.
264+
*/
265+
case 'prettier':
266+
config.prettier = PRETTIER_CONFIG
267+
break
268+
case 'eslint':
269+
config.eslint = ESLINT_CONFIG
270+
break
271+
default:
272+
config[toolName] = sourceConfigPath
273+
break
274+
}
275+
}
276+
}
277+
278+
return config
279+
}
280+
241281
module.exports = {
242282
groups,
243283
projects,
@@ -247,4 +287,5 @@ module.exports = {
247287
resolveProjectToGroups,
248288
printGroups,
249289
groupConfigs,
290+
bundledConfigPaths,
250291
}

0 commit comments

Comments
 (0)