@4s1/conventional-commit-creator 2.0.1 → 3.0.0-dev.0

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.
@@ -0,0 +1,7 @@
1
+ export class AppError extends Error {
2
+ constructor(msg) {
3
+ super(msg);
4
+ // Set the prototype explicitly.
5
+ Object.setPrototypeOf(this, AppError.prototype);
6
+ }
7
+ }
package/dist/config.js ADDED
@@ -0,0 +1,12 @@
1
+ import fs from 'node:fs';
2
+ import os from 'node:os';
3
+ import { AppError } from './app-error.js';
4
+ export function loadConfig() {
5
+ const path = `${os.homedir()}/.ccc.json`;
6
+ if (!fs.existsSync(path)) {
7
+ throw new AppError(`No configuration found at "${path}".`);
8
+ }
9
+ const content = fs.readFileSync(path, { encoding: 'utf-8' });
10
+ // ToDo: Exception handling
11
+ return JSON.parse(content);
12
+ }
package/dist/git.js CHANGED
@@ -21,15 +21,8 @@ export class Git {
21
21
  return false;
22
22
  }
23
23
  }
24
- async isAtWork() {
25
- try {
26
- const output = await this.execute('git remote -v');
27
- return output.includes('intra.bender:');
28
- }
29
- catch (err) {
30
- console.error(err);
31
- return false;
32
- }
24
+ async getGitRemoteOriginUrl() {
25
+ return this.execute('git remote get-url --push origin');
33
26
  }
34
27
  async commit(msg) {
35
28
  console.info('committing ...');
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
  import prompts from 'prompts';
3
3
  import { Git } from './git.js';
4
+ import { loadConfig } from './config.js';
5
+ import { AppError } from './app-error.js';
4
6
  // https://kapeli.com/cheat_sheets/Conventional_Commits.docset/Contents/Resources/Documents/index
5
7
  const questions = [
6
8
  {
@@ -50,6 +52,11 @@ const questions = [
50
52
  }
51
53
  },
52
54
  },
55
+ {
56
+ type: 'text',
57
+ name: 'body',
58
+ message: 'body?',
59
+ },
53
60
  {
54
61
  type: 'number',
55
62
  name: 'issue',
@@ -57,41 +64,118 @@ const questions = [
57
64
  },
58
65
  ];
59
66
  async function main() {
60
- const git = new Git();
61
- if (!(await git.isCurrentDirUnderGitControl())) {
62
- console.error('Current directory is not a git repository.');
63
- process.exit(0);
64
- }
65
- if (!(await git.haveStagedChanges())) {
66
- console.error('Nothing to commit');
67
- process.exit(0);
68
- }
69
- const data = (await prompts(questions, {
70
- onCancel: () => {
71
- console.error('Aborted');
72
- process.exit(1);
73
- },
74
- }));
75
- const isAtWork = await git.isAtWork();
76
- const msg = await createMsg(data, isAtWork);
77
- await git.commit(msg);
78
- console.info('done');
79
- }
80
- async function createMsg(data, isWork) {
81
- let msg = `${data.type.trim()}`;
82
- if (data.scope) {
83
- msg += `(${data.scope.trim()})`;
67
+ try {
68
+ const git = new Git();
69
+ if (!(await git.isCurrentDirUnderGitControl())) {
70
+ throw new AppError('Current directory is not a git repository.');
71
+ }
72
+ if (!(await git.haveStagedChanges())) {
73
+ throw new AppError('Nothing to commit');
74
+ }
75
+ const config = loadConfig();
76
+ const remoteOriginUrl = (await git.getGitRemoteOriginUrl()).trim();
77
+ const template = getMatchingTemplate(config, remoteOriginUrl);
78
+ console.info(`Using template "${template.name}"\n`);
79
+ const data = (await prompts(questions, {
80
+ onCancel: () => {
81
+ // ToDo: Better exception handling
82
+ console.error('Aborted');
83
+ process.exit(1);
84
+ },
85
+ }));
86
+ const msg = await createMsg(data, template);
87
+ console.info(`${'-'.repeat(42)}\n${msg}\n${'-'.repeat(42)}`);
88
+ await git.commit(msg);
89
+ console.info('done');
84
90
  }
85
- msg += `: ${data.description.trim()}`;
86
- if (data.issue) {
87
- // Hack to support Redmine ticket linking at work.
88
- if (!isWork) {
89
- msg += ` (#${data.issue})`;
91
+ catch (err) {
92
+ if (err instanceof AppError) {
93
+ console.error(err.message);
94
+ process.exit(0);
90
95
  }
91
96
  else {
92
- msg += ` [refs #${data.issue}]`;
97
+ console.error(err);
98
+ process.exit(1);
99
+ }
100
+ }
101
+ }
102
+ function getMatchingTemplate(config, remoteOriginUrl) {
103
+ // ToDo: rename var
104
+ let template2;
105
+ for (const template of config.templates) {
106
+ const regEx = new RegExp(template.regex);
107
+ if (regEx.test(remoteOriginUrl)) {
108
+ template2 = template;
93
109
  }
94
110
  }
111
+ if (!template2) {
112
+ throw new AppError(`No matching template found for "${remoteOriginUrl}"`);
113
+ }
114
+ return template2;
115
+ }
116
+ async function createMsg(data, template) {
117
+ const type = data.type ? data.type.trim() : '';
118
+ const scope = data.scope ? data.scope.trim() : '';
119
+ const body = data.body
120
+ ? data.body
121
+ .split('\\n')
122
+ .map((x) => x.trim())
123
+ .join('\n')
124
+ .trim()
125
+ : '';
126
+ const description = data.description ? data.description.trim() : '';
127
+ const issue = data.issue ? data.issue.toString() : '';
128
+ let msg = template.pattern;
129
+ if (type) {
130
+ const typeText = `${template.pre_type ?? ''}${type}${template.post_type ?? ''}`;
131
+ msg = msg.replace('{type}', typeText);
132
+ const type2Text = `${template.pre_type2 ?? ''}${type}${template.post_type2 ?? ''}`;
133
+ msg = msg.replace('{type2}', type2Text);
134
+ }
135
+ else {
136
+ msg = msg.replace('{type}', '');
137
+ msg = msg.replace('{type2}', '');
138
+ }
139
+ if (scope) {
140
+ const scopeText = `${template.pre_scope ?? ''}${scope}${template.post_scope ?? ''}`;
141
+ msg = msg.replace('{scope}', scopeText);
142
+ const scope2Text = `${template.pre_scope2 ?? ''}${scope}${template.post_scope2 ?? ''}`;
143
+ msg = msg.replace('{scope2}', scope2Text);
144
+ }
145
+ else {
146
+ msg = msg.replace('{scope}', '');
147
+ msg = msg.replace('{scope2}', '');
148
+ }
149
+ if (description) {
150
+ const descriptionText = `${template.pre_description ?? ''}${description}${template.post_description ?? ''}`;
151
+ msg = msg.replace('{description}', descriptionText);
152
+ const description2Text = `${template.pre_description2 ?? ''}${description}${template.post_description2 ?? ''}`;
153
+ msg = msg.replace('{description2}', description2Text);
154
+ }
155
+ else {
156
+ msg = msg.replace('{description}', '');
157
+ msg = msg.replace('{description2}', '');
158
+ }
159
+ if (body) {
160
+ const bodyText = `${template.pre_body ?? ''}${body}${template.post_body ?? ''}`;
161
+ msg = msg.replace('{body}', bodyText);
162
+ const body2Text = `${template.pre_body2 ?? ''}${body}${template.post_body2 ?? ''}`;
163
+ msg = msg.replace('{body2}', body2Text);
164
+ }
165
+ else {
166
+ msg = msg.replace('{body}', '');
167
+ msg = msg.replace('{body2}', '');
168
+ }
169
+ if (issue) {
170
+ const issueText = `${template.pre_issue ?? ''}${issue}${template.post_issue ?? ''}`;
171
+ msg = msg.replace('{issue}', issueText);
172
+ const issue2Text = `${template.pre_issue2 ?? ''}${issue}${template.post_issue2 ?? ''}`;
173
+ msg = msg.replace('{issue2}', issue2Text);
174
+ }
175
+ else {
176
+ msg = msg.replace('{issue}', '');
177
+ msg = msg.replace('{issue2}', '');
178
+ }
95
179
  return msg;
96
180
  }
97
181
  main().catch(console.error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@4s1/conventional-commit-creator",
3
- "version": "2.0.1",
3
+ "version": "3.0.0-dev.0",
4
4
  "description": "Conventional Commit Creator",
5
5
  "keywords": [
6
6
  "conventional commit",
@@ -28,15 +28,15 @@
28
28
  ],
29
29
  "scripts": {
30
30
  "build": "rm -rf dist && tsc",
31
- "build:dev": "pnpm run build -- --project tsconfig.dev.json",
31
+ "build:dev": "pnpm run build --project tsconfig.dev.json",
32
32
  "lbt": "npm run lint && npm run build && npm run test",
33
33
  "lint": "eslint --ext .ts src/ && prettier --check .",
34
34
  "lint:fix": "eslint --ext .ts --fix src/ && prettier --write .",
35
35
  "start": "node dist/index.js",
36
36
  "start:dev": "ts-node src/index.ts",
37
37
  "test": "echo no tests",
38
- "test:cov": "pnpm run test -- --coverage",
39
- "test:watch": "pnpm run test -- --watch"
38
+ "test:cov": "pnpm run test --coverage",
39
+ "test:watch": "pnpm run test --watch"
40
40
  },
41
41
  "prettier": "@4s1/eslint-config",
42
42
  "dependencies": {
@@ -44,7 +44,7 @@
44
44
  },
45
45
  "devDependencies": {
46
46
  "@4s1/eslint-config": "6.5.0",
47
- "@4s1/ts-config": "4.1.1",
47
+ "@4s1/ts-config": "4.3.0",
48
48
  "@types/node": "18.15.11",
49
49
  "@types/prompts": "2.4.4",
50
50
  "eslint": "8.38.0",