@thisismanta/semantic-version 8.0.1 → 9.1.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.
- package/bin/auto-npm-version +1 -1
- package/bin/lint-commit-message +1 -1
- package/lib/auto-npm-version.d.ts +8 -0
- package/lib/auto-npm-version.js +66 -46
- package/lib/index.d.ts +1 -1
- package/lib/index.js +3 -7
- package/lib/lint-commit-message.d.ts +1 -1
- package/lib/lint-commit-message.js +8 -12
- package/lib/run.js +7 -7
- package/package.json +7 -14
- package/lib/debug.d.ts +0 -1
- package/lib/debug.js +0 -9
package/bin/auto-npm-version
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
require('../lib/auto-npm-version')
|
|
2
|
+
require('../lib/auto-npm-version').default()
|
package/bin/lint-commit-message
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
require('../lib/lint-commit-message')
|
|
2
|
+
require('../lib/lint-commit-message').default(process.argv.at(2))
|
package/lib/auto-npm-version.js
CHANGED
|
@@ -26,86 +26,106 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
|
|
29
|
+
exports.default = main;
|
|
30
|
+
exports.getReleaseType = getReleaseType;
|
|
31
|
+
const valid_1 = __importDefault(require("semver/functions/valid"));
|
|
30
32
|
const github = __importStar(require("@actions/github"));
|
|
31
33
|
const index_1 = require("./index");
|
|
32
34
|
const run_1 = require("./run");
|
|
33
|
-
const debug_1 = require("./debug");
|
|
34
|
-
main();
|
|
35
35
|
async function main() {
|
|
36
|
-
(0, debug_1.debug)('process.env.GITHUB_TOKEN »', process.env.GITHUB_TOKEN);
|
|
37
|
-
if (!process.env.GITHUB_TOKEN) {
|
|
38
|
-
throw new Error('Expected "GITHUB_TOKEN" env to be provided.');
|
|
39
|
-
}
|
|
40
|
-
// Set up Git commit author for further use in "npm version" and "git push" command
|
|
41
36
|
// See https://github.com/actions/checkout#push-a-commit-using-the-built-in-token
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
37
|
+
const existingGitUserName = await (0, run_1.run)(`git config user.name`).catch(() => '');
|
|
38
|
+
if (!existingGitUserName) {
|
|
39
|
+
console.log('Setting Git commit author for further use in `npm version` and `git push` command...');
|
|
40
|
+
const name = github.context.payload.pusher?.name || github.context.actor;
|
|
41
|
+
console.log(' user.name =', name);
|
|
42
|
+
await (0, run_1.run)(`git config user.name ${name}`);
|
|
43
|
+
const email = github.context.payload.pusher?.email || 'github-actions@github.com';
|
|
44
|
+
console.log(' user.email =', name);
|
|
45
|
+
await (0, run_1.run)(`git config user.email ${email}`);
|
|
45
46
|
}
|
|
47
|
+
console.log('Getting the remote repository...');
|
|
46
48
|
const remote = await (0, run_1.run)(`git remote`) || 'origin';
|
|
47
|
-
|
|
49
|
+
console.log(' remote =', remote);
|
|
50
|
+
if (!process.env.GITHUB_TOKEN) {
|
|
51
|
+
throw new Error('Expected "GITHUB_TOKEN" to be set in the environment variables.');
|
|
52
|
+
}
|
|
48
53
|
// See https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-github-actions-settings-for-a-repository#configuring-the-default-github_token-permissions
|
|
54
|
+
console.log('Checking if the current setup has the permission to push to the remote repository...');
|
|
49
55
|
await (0, run_1.run)(`git push --dry-run ${remote}`);
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
56
|
+
console.log(' OK');
|
|
57
|
+
console.log('Getting the current package version...');
|
|
58
|
+
const currentVersion = await getCurrentPackageVersion();
|
|
59
|
+
console.log(' version =', currentVersion ?? JSON.stringify(currentVersion));
|
|
60
|
+
if (!currentVersion) {
|
|
53
61
|
throw new Error('Expected "version" field to exist in package.json.');
|
|
54
62
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
63
|
+
console.log('Getting the commit history since the current version...');
|
|
64
|
+
const commits = await getGitHistory(currentVersion);
|
|
65
|
+
if (commits.length === 0) {
|
|
66
|
+
console.log(' Found 0 commits.');
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
for (const commit of commits) {
|
|
70
|
+
console.log(` - ${commit.type}${commit.breaking ? '!' : ''}: ${commit.subject} (${commit.hash.substring(0, 7)})`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
console.log('Determining the upcoming release type...');
|
|
58
74
|
const releaseType = getReleaseType(commits);
|
|
59
|
-
|
|
75
|
+
console.log(' ' + JSON.stringify(releaseType));
|
|
60
76
|
if (!releaseType) {
|
|
61
|
-
console.log('
|
|
77
|
+
console.log('');
|
|
78
|
+
console.log('Done without a new version.');
|
|
62
79
|
return;
|
|
63
80
|
}
|
|
64
|
-
|
|
81
|
+
console.log('Running `npm version` command and its pre-post scripts...');
|
|
82
|
+
await (0, run_1.run)(`npm version ${releaseType} --git-tag-version --no-commit-hooks`);
|
|
83
|
+
console.log(' OK');
|
|
84
|
+
console.log('Pushing the new version to the remote repository...');
|
|
65
85
|
await (0, run_1.run)(`git push --follow-tags ${remote}`);
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
86
|
+
console.log(' OK');
|
|
87
|
+
console.log('Verifying the new version...');
|
|
88
|
+
const latestVersion = await getCurrentPackageVersion();
|
|
89
|
+
console.log(' version =', latestVersion ?? JSON.stringify(latestVersion));
|
|
90
|
+
console.log('Creating a release note on GitHub...');
|
|
72
91
|
const releaseNote = getReleaseNote(commits);
|
|
73
|
-
(0, debug_1.debug)('releaseNote »', releaseNote);
|
|
74
|
-
const octokit = github.getOctokit(process.env.GITHUB_TOKEN);
|
|
75
92
|
// See https://octokit.github.io/rest.js/v19#repos-create-release
|
|
93
|
+
const octokit = github.getOctokit(process.env.GITHUB_TOKEN);
|
|
76
94
|
const releaseCreationRespond = await octokit.rest.repos.createRelease({
|
|
77
95
|
...github.context.repo,
|
|
78
|
-
tag_name: 'v' +
|
|
96
|
+
tag_name: 'v' + latestVersion,
|
|
79
97
|
body: releaseNote,
|
|
80
98
|
make_latest: 'legacy',
|
|
81
99
|
});
|
|
82
|
-
|
|
83
|
-
console.log('
|
|
100
|
+
console.log(' ' + releaseCreationRespond.data.html_url);
|
|
101
|
+
console.log('');
|
|
102
|
+
console.log('Done with the new version.');
|
|
84
103
|
}
|
|
85
|
-
async function
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
104
|
+
async function getCurrentPackageVersion() {
|
|
105
|
+
const version = JSON.parse(await (0, run_1.run)('npm pkg get version'));
|
|
106
|
+
if (typeof version === 'string' && (0, valid_1.default)(version)) {
|
|
107
|
+
return version;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
throw new Error('Expected a valid version field in package.json.');
|
|
111
|
+
}
|
|
93
112
|
}
|
|
94
113
|
async function getGitHistory(version) {
|
|
95
|
-
const
|
|
96
|
-
|
|
114
|
+
const tag = (await (0, run_1.run)(`git tag --list v${version}`) ||
|
|
115
|
+
await (0, run_1.run)('git describe --tags --abbrev=0'));
|
|
116
|
+
return getCommits(await (0, run_1.run)(`git --no-pager log ${tag ? tag + '..HEAD' : ''} --format=%H%s`));
|
|
97
117
|
}
|
|
98
|
-
function getCommits(
|
|
99
|
-
return
|
|
118
|
+
function getCommits(gitLogs) {
|
|
119
|
+
return gitLogs
|
|
100
120
|
.split('\n')
|
|
101
121
|
.filter(line => line.length > 0)
|
|
102
122
|
.map(line => ({
|
|
103
123
|
hash: line.substring(0, 40),
|
|
104
124
|
message: line.substring(40),
|
|
105
125
|
}))
|
|
106
|
-
.filter(({ message }) =>
|
|
126
|
+
.filter(({ message }) => (0, valid_1.default)(message) === null)
|
|
107
127
|
.map(({ hash, message }) => {
|
|
108
|
-
const { type, breaking, subject } = (0, index_1.checkConventionalMessage)(message
|
|
128
|
+
const { type, breaking, subject } = (0, index_1.checkConventionalMessage)(message);
|
|
109
129
|
return { hash, type, breaking, subject };
|
|
110
130
|
});
|
|
111
131
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare const allowedTypes: readonly ["feat", "fix", "build", "chore"];
|
|
2
2
|
export type SemanticType = typeof allowedTypes[number];
|
|
3
|
-
export declare function checkConventionalMessage(message: string
|
|
3
|
+
export declare function checkConventionalMessage(message: string): {
|
|
4
4
|
type: "feat" | "fix" | "build" | "chore" | undefined;
|
|
5
5
|
breaking: boolean;
|
|
6
6
|
subject: string;
|
package/lib/index.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.allowedTypes = void 0;
|
|
4
|
+
exports.checkConventionalMessage = checkConventionalMessage;
|
|
4
5
|
const titlePattern = /^(?<type>\w+)(?<scope>\(.*?\))?(?<breaking>\!)?:(?<subject>.+)/;
|
|
5
6
|
exports.allowedTypes = ['feat', 'fix', 'build', 'chore'];
|
|
6
|
-
function checkConventionalMessage(message
|
|
7
|
+
function checkConventionalMessage(message) {
|
|
7
8
|
const pattern = (message.match(titlePattern)?.groups || {});
|
|
8
9
|
const { type, scope, breaking, subject } = pattern;
|
|
9
|
-
debug('type »', type);
|
|
10
|
-
debug('scope »', scope);
|
|
11
|
-
debug('breaking »', breaking);
|
|
12
|
-
debug('subject »', subject?.trim());
|
|
13
10
|
const errors = [
|
|
14
11
|
!type &&
|
|
15
12
|
'The pull request title must match the pattern of "<type>[!]: <subject>" which is a reduced set of https://www.conventionalcommits.org/en/v1.0.0/',
|
|
@@ -37,4 +34,3 @@ function checkConventionalMessage(message, { debug }) {
|
|
|
37
34
|
errors
|
|
38
35
|
};
|
|
39
36
|
}
|
|
40
|
-
exports.checkConventionalMessage = checkConventionalMessage;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export default function main(messageFilePath: string): Promise<void>;
|
|
@@ -23,20 +23,16 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.default = main;
|
|
26
27
|
const fs = __importStar(require("fs/promises"));
|
|
27
28
|
const index_1 = require("./index");
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
async function main() {
|
|
31
|
-
const [messageFilePath] = process.argv.slice(2);
|
|
32
|
-
(0, debug_1.debug)('messageFilePath »', messageFilePath);
|
|
29
|
+
async function main(messageFilePath) {
|
|
30
|
+
console.log('Verifying the commit message...');
|
|
33
31
|
const message = (await fs.readFile(messageFilePath, 'utf-8')).trim();
|
|
34
|
-
|
|
35
|
-
const { errors } = (0, index_1.checkConventionalMessage)(message
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
console.error(error);
|
|
39
|
-
}
|
|
40
|
-
process.exit(1);
|
|
32
|
+
console.log(' input =', message);
|
|
33
|
+
const { errors } = (0, index_1.checkConventionalMessage)(message);
|
|
34
|
+
for (const error of errors) {
|
|
35
|
+
console.error(' error =', error);
|
|
41
36
|
}
|
|
37
|
+
process.exitCode = errors.length;
|
|
42
38
|
}
|
package/lib/run.js
CHANGED
|
@@ -23,22 +23,22 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.run =
|
|
26
|
+
exports.run = run;
|
|
27
27
|
const cp = __importStar(require("child_process"));
|
|
28
|
-
const debug_1 = require("./debug");
|
|
29
28
|
function run(command) {
|
|
30
|
-
(0, debug_1.debug)(command);
|
|
31
29
|
return new Promise((resolve, reject) => {
|
|
32
30
|
cp.exec(command, (error, stdout, stderr) => {
|
|
33
31
|
if (error) {
|
|
32
|
+
// See https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#grouping-log-lines
|
|
33
|
+
console.log('::group::' + command);
|
|
34
|
+
console.log(stdout);
|
|
35
|
+
console.error(stderr);
|
|
36
|
+
console.log('::endgroup::');
|
|
34
37
|
reject(error);
|
|
35
38
|
}
|
|
36
39
|
else {
|
|
37
|
-
|
|
38
|
-
(0, debug_1.debug)(output);
|
|
39
|
-
resolve(output);
|
|
40
|
+
resolve((stdout + stderr).trim());
|
|
40
41
|
}
|
|
41
42
|
});
|
|
42
43
|
});
|
|
43
44
|
}
|
|
44
|
-
exports.run = run;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thisismanta/semantic-version",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.1.0",
|
|
4
4
|
"author": "Anantachai Saothong <thisismanta@gmail.com>",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"repository": {
|
|
@@ -21,27 +21,20 @@
|
|
|
21
21
|
"./bin"
|
|
22
22
|
],
|
|
23
23
|
"scripts": {
|
|
24
|
-
"test": "
|
|
24
|
+
"test": "vitest --no-watch",
|
|
25
25
|
"build": "rm -rf lib && tsc",
|
|
26
26
|
"preversion": "npm run build",
|
|
27
27
|
"version": "npm publish --access public"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@types/jest": "^29.5.12",
|
|
31
30
|
"@types/node": "^20.0.0",
|
|
32
|
-
"@types/semver": "^7.5.
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"ts-node": "^10.9.2",
|
|
37
|
-
"typescript": "^5.3.3"
|
|
31
|
+
"@types/semver": "^7.5.8",
|
|
32
|
+
"lefthook": "^1.7.14",
|
|
33
|
+
"typescript": "^5.5.4",
|
|
34
|
+
"vitest": "^2.0.5"
|
|
38
35
|
},
|
|
39
36
|
"dependencies": {
|
|
40
37
|
"@actions/github": "^6.0.0",
|
|
41
|
-
"semver": "^7.6.
|
|
42
|
-
},
|
|
43
|
-
"jest": {
|
|
44
|
-
"preset": "ts-jest",
|
|
45
|
-
"resetMocks": true
|
|
38
|
+
"semver": "^7.6.3"
|
|
46
39
|
}
|
|
47
40
|
}
|
package/lib/debug.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function debug(...args: Array<any>): void;
|
package/lib/debug.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.debug = void 0;
|
|
4
|
-
function debug(...args) {
|
|
5
|
-
if (process.env.DEBUG || process.env.RUNNER_DEBUG /* For GitHub Actions */) {
|
|
6
|
-
console.log(...args);
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
exports.debug = debug;
|