@thisismanta/semantic-version 7.0.0 → 8.0.1
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.
- package/README.md +35 -25
- package/lib/auto-npm-version.js +17 -6
- package/package.json +7 -8
- package/lib/install-git-hooks.d.ts +0 -1
- package/lib/install-git-hooks.js +0 -68
package/README.md
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# `npx lint-commit-message <path>`
|
|
2
|
+
|
|
3
|
+
The `<path>` must point to a text file containing commit message that complies with the following pattern:
|
|
2
4
|
|
|
3
5
|
```
|
|
4
6
|
<type>[!]: <subject>
|
|
@@ -8,9 +10,18 @@ Where
|
|
|
8
10
|
- `!` indicates that the commit contains breaking changes.
|
|
9
11
|
- `<subject>` is the actual commit message where the first word must be written in lower cases.
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
> Usage example with [**lefthook**](https://www.npmjs.com/package/lefthook)
|
|
14
|
+
> ```yml
|
|
15
|
+
> # lefthook.yml
|
|
16
|
+
> commit-msg:
|
|
17
|
+
> commands:
|
|
18
|
+
> lint:
|
|
19
|
+
> run: npx lint-commit-message {1}
|
|
20
|
+
> ```
|
|
21
|
+
|
|
22
|
+
# `npx auto-npm-version`
|
|
12
23
|
|
|
13
|
-
|
|
24
|
+
This command is supposed to be run on CI, such as **GitHub Actions**. It will run `npm version <new-version>`, which `<new-version>` is automatically derived from your commit messages according to the table below and then it creates a new entry on [**GitHub releases**](https://docs.github.com/en/repositories/releasing-projects-on-github/about-releases).
|
|
14
25
|
|
|
15
26
|
|Commit message type|Post-commit command|
|
|
16
27
|
|---|---|
|
|
@@ -19,25 +30,24 @@ Additionally, you can run `npx auto-npm-version` on **GitHub Actions** to trigge
|
|
|
19
30
|
|`fix`|`npm version patch`|
|
|
20
31
|
|Others|Does not run `npm version`|
|
|
21
32
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
```
|
|
33
|
+
> Usage example with **GitHub Actions**
|
|
34
|
+
> ```yml
|
|
35
|
+
> on:
|
|
36
|
+
> push:
|
|
37
|
+
> branches: [master]
|
|
38
|
+
> jobs:
|
|
39
|
+
> release:
|
|
40
|
+
> runs-on: ubuntu-latest
|
|
41
|
+
> steps:
|
|
42
|
+
> - uses: actions/checkout@v4
|
|
43
|
+
> with:
|
|
44
|
+
> fetch-depth: 0 # Ensure Git tags are fetched
|
|
45
|
+
> - uses: actions/setup-node@v4
|
|
46
|
+
> with:
|
|
47
|
+
> node-version-file: 'package.json'
|
|
48
|
+
> cache: npm
|
|
49
|
+
> - run: npm ci # Install semantic-version as part of the dependencies
|
|
50
|
+
> - run: npx auto-npm-version
|
|
51
|
+
> env:
|
|
52
|
+
> GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Make it possible to create a new release using GitHub API
|
|
53
|
+
> ```
|
package/lib/auto-npm-version.js
CHANGED
|
@@ -83,8 +83,13 @@ async function main() {
|
|
|
83
83
|
console.log('Done with a new release at', releaseCreationRespond.data.html_url);
|
|
84
84
|
}
|
|
85
85
|
async function getLastVersion() {
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
const versionFromGit = await (0, run_1.run)('git describe --tags --abbrev=0').catch(() => null);
|
|
87
|
+
const versionFromPackageJSON = JSON.parse(await (0, run_1.run)('npm pkg get version'));
|
|
88
|
+
const versions = [versionFromGit, versionFromPackageJSON]
|
|
89
|
+
.map(version => semver_1.default.valid(version))
|
|
90
|
+
.filter((version) => !!version);
|
|
91
|
+
// Choose the higher version
|
|
92
|
+
return semver_1.default.sort(versions).pop();
|
|
88
93
|
}
|
|
89
94
|
async function getGitHistory(version) {
|
|
90
95
|
const tagFound = !!(await (0, run_1.run)(`git tag --list v${version}`));
|
|
@@ -105,10 +110,16 @@ function getCommits(gitLog) {
|
|
|
105
110
|
});
|
|
106
111
|
}
|
|
107
112
|
function getReleaseType(commits) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
113
|
+
if (commits.find(({ breaking }) => breaking)) {
|
|
114
|
+
return 'major';
|
|
115
|
+
}
|
|
116
|
+
if (commits.find(({ type }) => type === 'feat')) {
|
|
117
|
+
return 'minor';
|
|
118
|
+
}
|
|
119
|
+
if (commits.find(({ type }) => type === 'fix' || type === 'build')) {
|
|
120
|
+
return 'patch';
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
112
123
|
}
|
|
113
124
|
function getReleaseNote(commits) {
|
|
114
125
|
const groups = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thisismanta/semantic-version",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.1",
|
|
4
4
|
"author": "Anantachai Saothong <thisismanta@gmail.com>",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"repository": {
|
|
@@ -24,22 +24,21 @@
|
|
|
24
24
|
"test": "jest",
|
|
25
25
|
"build": "rm -rf lib && tsc",
|
|
26
26
|
"preversion": "npm run build",
|
|
27
|
-
"version": "npm publish --access public"
|
|
28
|
-
"postinstall": "node ./lib/install-git-hooks.js"
|
|
27
|
+
"version": "npm publish --access public"
|
|
29
28
|
},
|
|
30
29
|
"devDependencies": {
|
|
31
|
-
"@types/jest": "^29.5.
|
|
30
|
+
"@types/jest": "^29.5.12",
|
|
32
31
|
"@types/node": "^20.0.0",
|
|
33
|
-
"@types/semver": "^7.5.
|
|
32
|
+
"@types/semver": "^7.5.7",
|
|
34
33
|
"jest": "^29.7.0",
|
|
35
|
-
"lefthook": "^1.6.
|
|
36
|
-
"ts-jest": "^29.1.
|
|
34
|
+
"lefthook": "^1.6.1",
|
|
35
|
+
"ts-jest": "^29.1.2",
|
|
37
36
|
"ts-node": "^10.9.2",
|
|
38
37
|
"typescript": "^5.3.3"
|
|
39
38
|
},
|
|
40
39
|
"dependencies": {
|
|
41
40
|
"@actions/github": "^6.0.0",
|
|
42
|
-
"semver": "^7.
|
|
41
|
+
"semver": "^7.6.0"
|
|
43
42
|
},
|
|
44
43
|
"jest": {
|
|
45
44
|
"preset": "ts-jest",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/install-git-hooks.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
const fs = __importStar(require("fs/promises"));
|
|
27
|
-
const fp = __importStar(require("path"));
|
|
28
|
-
const run_1 = require("./run");
|
|
29
|
-
const debug_1 = require("./debug");
|
|
30
|
-
main();
|
|
31
|
-
const packageName = require('../package.json').name;
|
|
32
|
-
async function main() {
|
|
33
|
-
const rootDirectoryPath = await (0, run_1.run)('git rev-parse --show-toplevel');
|
|
34
|
-
(0, debug_1.debug)('rootDirectoryPath »', rootDirectoryPath);
|
|
35
|
-
if (!rootDirectoryPath) {
|
|
36
|
-
throw new Error('Could not find a Git directory.');
|
|
37
|
-
}
|
|
38
|
-
const packageJSON = JSON.parse(await fs.readFile(fp.join(rootDirectoryPath, 'package.json'), 'utf-8'));
|
|
39
|
-
if (packageJSON.name === packageName) {
|
|
40
|
-
console.warn('Skip installing Git hooks as it is supposed to be done on a consumer repository.');
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
const hookDirectoryPath = await (0, run_1.run)('git config --get core.hooksPath').catch(() => '') || '.git/hooks';
|
|
44
|
-
const hookFilePath = fp.join(rootDirectoryPath, hookDirectoryPath, 'commit-msg');
|
|
45
|
-
(0, debug_1.debug)('hookFilePath »', hookFilePath);
|
|
46
|
-
try {
|
|
47
|
-
await fs.access(hookFilePath, fs.constants.R_OK | fs.constants.W_OK);
|
|
48
|
-
(0, debug_1.debug)('Found', hookFilePath);
|
|
49
|
-
}
|
|
50
|
-
catch {
|
|
51
|
-
await fs.mkdir(fp.dirname(hookFilePath), { recursive: true });
|
|
52
|
-
await fs.writeFile(hookFilePath, '#!/bin/sh\n', 'utf-8');
|
|
53
|
-
(0, debug_1.debug)('Created', hookFilePath);
|
|
54
|
-
}
|
|
55
|
-
const hookFileText = await fs.readFile(hookFilePath, 'utf-8');
|
|
56
|
-
if (/(^|\s|\/)lint-commit-message(\s|$)/m.test(hookFileText)) {
|
|
57
|
-
console.log('Skipped adding commit-msg Git hook.');
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
await fs.appendFile(hookFilePath, [
|
|
61
|
-
'',
|
|
62
|
-
'dir="$(git rev-parse --show-toplevel)"',
|
|
63
|
-
'"$dir/node_modules/.bin/lint-commit-message" "$@"',
|
|
64
|
-
'',
|
|
65
|
-
].join('\n'), 'utf-8');
|
|
66
|
-
console.log('Done adding commit-msg Git hook.');
|
|
67
|
-
}
|
|
68
|
-
}
|