@thisismanta/semantic-version 1.0.5 → 2.0.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/lib/auto-npm-version.js +102 -15
- package/lib/debug.js +1 -5
- package/lib/index.d.ts +1 -1
- package/lib/index.js +5 -1
- package/lib/index.test.js +14 -0
- package/lib/install-git-hooks.js +0 -1
- package/package.json +9 -7
package/lib/auto-npm-version.js
CHANGED
|
@@ -1,36 +1,123 @@
|
|
|
1
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
2
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
const semver_1 = __importDefault(require("semver"));
|
|
3
30
|
const execa_1 = require("execa");
|
|
31
|
+
const github = __importStar(require("@actions/github"));
|
|
4
32
|
const index_1 = require("./index");
|
|
5
33
|
const debug_1 = require("./debug");
|
|
6
34
|
main();
|
|
7
35
|
async function main() {
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
36
|
+
const lastVersion = semver_1.default.valid(await run('git describe --tags --abbrev=0') ||
|
|
37
|
+
await run('npm pkg get version'));
|
|
38
|
+
(0, debug_1.debug)('lastVersion »', JSON.stringify(lastVersion));
|
|
39
|
+
if (!lastVersion) {
|
|
40
|
+
throw new Error('Expect to have a last version on Git tag or package.json `version` field.');
|
|
41
|
+
}
|
|
42
|
+
const commits = (await run(`git log v${lastVersion}..HEAD --format="%H %s"`))
|
|
43
|
+
.trim()
|
|
44
|
+
.split('\n')
|
|
45
|
+
.map(line => {
|
|
46
|
+
const hash = line.substring(0, line.indexOf(' '));
|
|
47
|
+
const message = line.substring(line.indexOf(' ') + 1);
|
|
48
|
+
const { type, breaking, subject } = (0, index_1.checkConventionalMessage)(message, { debug: debug_1.debug });
|
|
49
|
+
return {
|
|
50
|
+
hash,
|
|
51
|
+
type,
|
|
52
|
+
breaking,
|
|
53
|
+
subject,
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
console.log(`Found ${commits.length} commits since ${lastVersion}`);
|
|
57
|
+
(0, debug_1.debug)('commits »', JSON.stringify(commits, null, 2));
|
|
58
|
+
const releaseType = commits.reduce((releaseType, { type, breaking }) => {
|
|
59
|
+
if (releaseType === 'major' || breaking) {
|
|
13
60
|
return 'major';
|
|
14
61
|
}
|
|
15
|
-
|
|
16
|
-
return
|
|
62
|
+
if (releaseType === 'minor' || type === 'feat') {
|
|
63
|
+
return releaseType;
|
|
17
64
|
}
|
|
18
|
-
|
|
65
|
+
if (type === 'fix' || type === 'refactor') {
|
|
19
66
|
return 'patch';
|
|
20
67
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
})();
|
|
68
|
+
return releaseType;
|
|
69
|
+
}, null);
|
|
24
70
|
(0, debug_1.debug)('releaseType »', JSON.stringify(releaseType));
|
|
25
71
|
if (!releaseType) {
|
|
26
|
-
console.log('
|
|
72
|
+
console.log('Exited without releasing a new version');
|
|
27
73
|
return;
|
|
28
74
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
75
|
+
const nextVersion = await run(`npm version --json --no-commit-hooks ${releaseType}`);
|
|
76
|
+
(0, debug_1.debug)('nextVersion »', nextVersion);
|
|
77
|
+
console.log(`Created version ${releaseType}`);
|
|
78
|
+
await run(`git push --follow-tags origin master`);
|
|
79
|
+
console.log(`Pushed Git tags`);
|
|
80
|
+
if (semver_1.default.valid(nextVersion) && process.env.GITHUB_TOKEN) {
|
|
81
|
+
const commitGroups = {
|
|
82
|
+
'BREAKING CHANGES': [],
|
|
83
|
+
'Features': [],
|
|
84
|
+
'Bug Fixes': [],
|
|
85
|
+
'Others': [],
|
|
86
|
+
};
|
|
87
|
+
for (const commit of commits) {
|
|
88
|
+
if (commit.breaking) {
|
|
89
|
+
commitGroups['BREAKING CHANGES'].push(commit);
|
|
90
|
+
}
|
|
91
|
+
else if (commit.type === 'feat') {
|
|
92
|
+
commitGroups['Features'].push(commit);
|
|
93
|
+
}
|
|
94
|
+
else if (commit.type === 'bug') {
|
|
95
|
+
commitGroups['Bug Fixes'].push(commit);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
commitGroups['Others'].push(commit);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const releaseNote = Object.entries(commitGroups)
|
|
102
|
+
.filter(([title, commits]) => commits.length > 0)
|
|
103
|
+
.map(([title, commits]) => `### ${title}\n\n` +
|
|
104
|
+
commits.map(({ subject, hash }) => `- ${subject} (${hash})`).join('\n'))
|
|
105
|
+
.join('\n\n');
|
|
106
|
+
(0, debug_1.debug)('releaseNote »', releaseNote);
|
|
107
|
+
const octokit = github.getOctokit(process.env.GITHUB_TOKEN);
|
|
108
|
+
// See https://octokit.github.io/rest.js/v19#repos-create-release
|
|
109
|
+
const octokitRespond = await octokit.rest.repos.createRelease({
|
|
110
|
+
...github.context.repo,
|
|
111
|
+
tag_name: nextVersion,
|
|
112
|
+
body: releaseNote,
|
|
113
|
+
});
|
|
114
|
+
(0, debug_1.debug)('octokitRespond »', JSON.stringify(octokitRespond, null, 2));
|
|
115
|
+
console.log('Created a new release on GitHub');
|
|
116
|
+
}
|
|
32
117
|
}
|
|
33
118
|
async function run(command) {
|
|
119
|
+
(0, debug_1.debug)(command);
|
|
34
120
|
const { stdout } = await (0, execa_1.execaCommand)(command);
|
|
121
|
+
(0, debug_1.debug)(stdout);
|
|
35
122
|
return stdout;
|
|
36
123
|
}
|
package/lib/debug.js
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.debug = void 0;
|
|
7
|
-
const yn_1 = __importDefault(require("yn"));
|
|
8
4
|
function debug(...args) {
|
|
9
|
-
if (
|
|
5
|
+
if (process.env.DEBUG) {
|
|
10
6
|
console.log(...args);
|
|
11
7
|
}
|
|
12
8
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -2,6 +2,6 @@ export declare const allowedTypes: string[];
|
|
|
2
2
|
export declare function checkConventionalMessage(message: string, { debug }: Pick<Console, 'debug'>): {
|
|
3
3
|
type: string | undefined;
|
|
4
4
|
breaking: boolean;
|
|
5
|
-
subject: string
|
|
5
|
+
subject: string;
|
|
6
6
|
errors: string[];
|
|
7
7
|
};
|
package/lib/index.js
CHANGED
|
@@ -25,11 +25,15 @@ function checkConventionalMessage(message, { debug }) {
|
|
|
25
25
|
'A single space must be after ":" symbol.',
|
|
26
26
|
typeof subject === 'string' && /^[a-z]/.test(subject.trim()) === false &&
|
|
27
27
|
'The subject must start with a lower case latin alphabet.',
|
|
28
|
+
typeof subject === 'string' && /[\s\.]+$/.test(subject) && /\.{3}$/.test(subject.trim()) === false &&
|
|
29
|
+
'The subject must not end with a period or a space.',
|
|
28
30
|
].filter((error) => typeof error === 'string');
|
|
29
31
|
return {
|
|
30
32
|
type,
|
|
31
33
|
breaking: !!breaking,
|
|
32
|
-
subject: typeof subject === 'string'
|
|
34
|
+
subject: typeof subject === 'string'
|
|
35
|
+
? subject.trim().replace(/[\s\.]+$/, '') + (/\.{3}$/.test(subject.trim()) ? '...' : '')
|
|
36
|
+
: message,
|
|
33
37
|
errors
|
|
34
38
|
};
|
|
35
39
|
}
|
package/lib/index.test.js
CHANGED
|
@@ -68,3 +68,17 @@ it('returns the error, given the first word in non-lower-case for the subject',
|
|
|
68
68
|
]
|
|
69
69
|
});
|
|
70
70
|
});
|
|
71
|
+
it('returns the error, given a period after the subject', () => {
|
|
72
|
+
expect((0, index_1.checkConventionalMessage)('chore: xxx.', { debug })).toMatchObject({
|
|
73
|
+
subject: 'xxx',
|
|
74
|
+
errors: [
|
|
75
|
+
'The subject must not end with a period or a space.'
|
|
76
|
+
]
|
|
77
|
+
});
|
|
78
|
+
expect((0, index_1.checkConventionalMessage)('chore: xxx ...', { debug })).toEqual({
|
|
79
|
+
type: 'chore',
|
|
80
|
+
breaking: false,
|
|
81
|
+
subject: 'xxx...',
|
|
82
|
+
errors: []
|
|
83
|
+
});
|
|
84
|
+
});
|
package/lib/install-git-hooks.js
CHANGED
|
@@ -48,7 +48,6 @@ async function main() {
|
|
|
48
48
|
(0, debug_1.debug)('huskyDirectoryPath »', huskyDirectoryPath);
|
|
49
49
|
await fs.access(huskyDirectoryPath);
|
|
50
50
|
await upsert(fp.join(huskyDirectoryPath, 'commit-msg'), 'npx lint-commit-message ${1}');
|
|
51
|
-
await upsert(fp.join(huskyDirectoryPath, 'post-commit'), 'npx auto-npm-version');
|
|
52
51
|
console.log('Done adding Git hooks.');
|
|
53
52
|
}
|
|
54
53
|
async function findGitDirectoryPath(path) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thisismanta/semantic-version",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-1",
|
|
4
4
|
"author": "Anantachai Saothong <thisismanta@gmail.com>",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"engines": {
|
|
@@ -18,23 +18,25 @@
|
|
|
18
18
|
],
|
|
19
19
|
"scripts": {
|
|
20
20
|
"test": "jest",
|
|
21
|
-
"build": "tsc",
|
|
22
|
-
"preversion": "npm run test && npm run build
|
|
21
|
+
"build": "rm -rf lib && tsc",
|
|
22
|
+
"preversion": "npm run test && npm run build",
|
|
23
23
|
"version": "npm publish --access public",
|
|
24
|
-
"postversion": "git push --tags origin master",
|
|
24
|
+
"postversion": "git push --follow-tags origin master",
|
|
25
25
|
"postinstall": "node ./lib/install-git-hooks.js"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@types/jest": "^29.4.0",
|
|
29
29
|
"@types/node": "^16.0.0",
|
|
30
|
+
"@types/semver": "^7.3.13",
|
|
30
31
|
"jest": "^29.4.3",
|
|
31
32
|
"ts-jest": "^29.0.5",
|
|
32
|
-
"typescript": "^4.9.5"
|
|
33
|
-
"yn": "^5.0.0"
|
|
33
|
+
"typescript": "^4.9.5"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
+
"@actions/github": "^5.1.1",
|
|
36
37
|
"execa": "npm:@esm2cjs/execa@^6.1.1-cjs.1",
|
|
37
|
-
"husky": "^8.0.3"
|
|
38
|
+
"husky": "^8.0.3",
|
|
39
|
+
"semver": "^7.3.8"
|
|
38
40
|
},
|
|
39
41
|
"jest": {
|
|
40
42
|
"preset": "ts-jest",
|