@itcase/config 1.6.48 → 1.6.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,18 +1,25 @@
1
+ import fs from 'fs'
1
2
  import path from 'path'
2
3
  import { pathToFileURL } from 'url'
3
4
 
4
- const COMMITLINT_CONFIG_FILE = 'commitlint.config.mjs'
5
+ const COMMITLINT_CONFIG_FILES = [
6
+ 'commitlint.config.mjs',
7
+ 'commitlint.config.js',
8
+ 'commitlint.config.cjs',
9
+ ]
10
+
5
11
  const SUB_SCOPE_MESSAGE = 'Select sub-scope (optional):'
6
12
  const SUBJECT_REQUIRED = 'Subject is required'
7
13
 
8
- function buildScope(scope, subScope) {
9
- if (!scope) return ''
10
- if (!subScope) return scope
11
- return `${scope} / ${subScope}`
12
- }
13
-
14
- function toChoices(values) {
15
- return values.map((value) => ({ name: value, value: value }))
14
+ function findCommitlintConfig() {
15
+ const cwd = process.cwd()
16
+ for (const name of COMMITLINT_CONFIG_FILES) {
17
+ const filePath = path.resolve(cwd, name)
18
+ if (fs.existsSync(filePath)) return filePath
19
+ }
20
+ throw new Error(
21
+ `Commitlint config not found. Tried: ${COMMITLINT_CONFIG_FILES.join(', ')}`,
22
+ )
16
23
  }
17
24
 
18
25
  function buildTypeChoices(types, typeEnum) {
@@ -26,98 +33,96 @@ function buildTypeChoices(types, typeEnum) {
26
33
  }
27
34
 
28
35
  async function loadConfig() {
29
- const [{ default: load }, { commitlintProject }] = await Promise.all([
30
- import('@commitlint/load'),
31
- import('./commitlintProject.js'),
32
- ])
33
-
36
+ const { default: load } = await import('@commitlint/load')
34
37
  const { prompt: promptOverrides, rules } = await load()
35
38
 
36
- const configModule = await import(
37
- pathToFileURL(path.resolve(process.cwd(), COMMITLINT_CONFIG_FILE)).href
38
- )
39
- const SCOPES = configModule.default?.SCOPES || {}
39
+ const configPath = findCommitlintConfig()
40
+ const configModule = await import(pathToFileURL(configPath).href)
41
+ const SCOPES = configModule.default?.SCOPES ?? {}
40
42
 
41
- return { commitlintProject, promptOverrides, rules, SCOPES }
43
+ return { promptOverrides, rules, SCOPES }
42
44
  }
43
45
 
44
- function prompter(inquirer, commit) {
45
- ;(async () => {
46
- try {
47
- const { commitlintProject, promptOverrides, rules, SCOPES } =
48
- await loadConfig()
49
-
50
- const q = commitlintProject.prompt.questions
51
- const over = promptOverrides?.questions
52
-
53
- const scopeEnum = rules['scope-enum']?.[2]
54
- const types =
55
- rules['type-enum']?.[2] ?? commitlintProject.rules['type-enum'][2]
56
- const typeEnum = over?.type?.enum ?? q.type.enum
57
- const typeChoices = buildTypeChoices(types, typeEnum)
58
-
59
- const isScopesArray = Array.isArray(SCOPES)
60
- const scopesFromConfig = isScopesArray ? SCOPES : Object.keys(SCOPES)
61
- const baseScopes = Array.isArray(scopeEnum)
62
- ? scopeEnum.filter((s) => !s.includes(' / '))
63
- : scopesFromConfig
64
- const scopeList = baseScopes.length > 0 ? baseScopes : scopesFromConfig
65
-
66
- const messages = {
67
- type: over?.type?.description ?? q.type.description,
68
- scope: over?.scope?.description ?? q.scope.description,
69
- subject: over?.subject?.description ?? q.subject.description,
70
- }
46
+ async function prompter(inquirer, commit) {
47
+ try {
48
+ const { promptOverrides, rules, SCOPES } = await loadConfig()
49
+
50
+ const questions = promptOverrides?.questions ?? {}
51
+ const scopeEnum = rules['scope-enum']?.[2]
52
+ const types = rules['type-enum']?.[2]
53
+ if (!types?.length) {
54
+ throw new Error(
55
+ 'Commitlint config must set rules["type-enum"][2] (list of types)',
56
+ )
57
+ }
58
+ const typeEnum = questions.type?.enum ?? {}
59
+ const typeChoices = buildTypeChoices(types, typeEnum)
60
+
61
+ const isScopesArray = Array.isArray(SCOPES)
62
+ const scopesFromConfig = isScopesArray ? SCOPES : Object.keys(SCOPES)
63
+ const baseScopes = Array.isArray(scopeEnum)
64
+ ? scopeEnum.filter((s) => !s.includes(' / '))
65
+ : scopesFromConfig
66
+ const scopeList = baseScopes.length > 0 ? baseScopes : scopesFromConfig
67
+
68
+ const messages = {
69
+ type: questions.type?.description ?? 'Select the type of change',
70
+ scope: questions.scope?.description ?? 'What is the scope of this change',
71
+ subject: questions.subject?.description ?? 'Write a short description',
72
+ }
71
73
 
72
- const answers = await inquirer.prompt([
73
- {
74
- type: 'list',
75
- name: 'type',
76
- message: messages.type,
77
- choices: typeChoices,
74
+ const answers = await inquirer.prompt([
75
+ {
76
+ type: 'list',
77
+ name: 'type',
78
+ message: messages.type,
79
+ choices: typeChoices,
80
+ },
81
+ {
82
+ type: 'list',
83
+ name: 'scope',
84
+ message: messages.scope,
85
+ choices: scopeList.map((value) => ({ name: value, value: value })),
86
+ },
87
+ {
88
+ type: 'list',
89
+ name: 'subScope',
90
+ message: SUB_SCOPE_MESSAGE,
91
+ choices: function (answers) {
92
+ const subs = isScopesArray ? [] : SCOPES[answers.scope] || []
93
+ return [
94
+ ...subs.map((value) => ({ name: value, value: value })),
95
+ new inquirer.Separator(),
96
+ { name: 'none', value: '' },
97
+ ]
78
98
  },
79
- {
80
- type: 'list',
81
- name: 'scope',
82
- message: messages.scope,
83
- choices: toChoices(scopeList),
99
+ when: function (answers) {
100
+ if (isScopesArray) return false
101
+ const subs = SCOPES[answers.scope]
102
+ return answers.scope && subs?.length > 0
84
103
  },
85
- {
86
- type: 'list',
87
- name: 'subScope',
88
- message: SUB_SCOPE_MESSAGE,
89
- choices: function (answers) {
90
- const subs = isScopesArray ? [] : SCOPES[answers.scope] || []
91
- return [
92
- ...toChoices(subs),
93
- new inquirer.Separator(),
94
- { name: 'none', value: '' },
95
- ]
96
- },
97
- when: function (answers) {
98
- if (isScopesArray) return false
99
- const subs = SCOPES[answers.scope]
100
- return answers.scope && subs?.length > 0
101
- },
102
- },
103
- {
104
- type: 'input',
105
- name: 'subject',
106
- message: messages.subject,
107
- validate: (input) => (input?.trim() ? true : SUBJECT_REQUIRED),
108
- },
109
- ])
110
-
111
- const scope = buildScope(answers.scope, answers.subScope)
112
- const header = scope
113
- ? `${answers.type}(${scope}): ${answers.subject.trim()}`
114
- : `${answers.type}: ${answers.subject.trim()}`
115
- commit(header)
116
- } catch (err) {
117
- console.error(err)
118
- process.exit(1)
119
- }
120
- })()
104
+ },
105
+ {
106
+ type: 'input',
107
+ name: 'subject',
108
+ message: messages.subject,
109
+ validate: (input) => (input?.trim() ? true : SUBJECT_REQUIRED),
110
+ },
111
+ ])
112
+
113
+ const scope = answers.scope
114
+ ? answers.subScope
115
+ ? `${answers.scope} / ${answers.subScope}`
116
+ : answers.scope
117
+ : ''
118
+ const header = scope
119
+ ? `${answers.type}(${scope}): ${answers.subject.trim()}`
120
+ : `${answers.type}: ${answers.subject.trim()}`
121
+ commit(header)
122
+ } catch (err) {
123
+ console.error(err)
124
+ process.exit(1)
125
+ }
121
126
  }
122
127
 
123
128
  export { prompter }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@itcase/config",
3
- "version": "1.6.48",
3
+ "version": "1.6.49",
4
4
  "author": "ITCase",
5
5
  "description": "ITCase Config",
6
6
  "engines": {
@@ -31,11 +31,11 @@
31
31
  "registry": "https://registry.npmjs.org/"
32
32
  },
33
33
  "dependencies": {
34
- "@lerna-lite/cli": "^4.11.0",
35
- "@lerna-lite/exec": "^4.11.1",
36
- "@lerna-lite/list": "^4.11.0",
37
- "@lerna-lite/run": "^4.11.1",
38
- "@lerna-lite/watch": "^4.11.0",
34
+ "@lerna-lite/cli": "^4.11.2",
35
+ "@lerna-lite/exec": "^4.11.2",
36
+ "@lerna-lite/list": "^4.11.2",
37
+ "@lerna-lite/run": "^4.11.2",
38
+ "@lerna-lite/watch": "^4.11.2",
39
39
  "semantic-release-lerna": "^2.17.0",
40
40
  "chokidar-cli": "^3.0.0",
41
41
  "@commitlint/cli": "^20.4.1",
@@ -95,12 +95,12 @@
95
95
  "rollup-plugin-dts": "^6.3.0",
96
96
  "rollup-plugin-peer-deps-external": "^2.2.4",
97
97
  "rollup-preserve-directives": "^1.1.3",
98
- "semantic-release": "^25.0.2",
98
+ "semantic-release": "^25.0.3",
99
99
  "postcss-urlrewrite": "^0.3.0"
100
100
  },
101
101
  "devDependencies": {
102
- "@itcase/lint": "^1.1.95",
103
- "eslint": "^9.39.2",
102
+ "@itcase/lint": "^1.1.96",
103
+ "eslint": "^10.0.0",
104
104
  "husky": "^9.1.7",
105
105
  "inquirer": "^13.2.2",
106
106
  "lint-staged": "^16.2.7",