-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
123 lines (103 loc) · 3.31 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/
import { handleErrorMessage, handleSuccessMessage } from '../../utils/index.js';
import { Changelog } from 'lerna-changelog';
import { Command } from 'commander';
import { Ora } from 'ora';
import { execa } from 'execa';
import { token as getToken } from '../../github/index.js';
interface ILernaChangelogArgs {
from?: string;
to?: string;
path?: string;
token?: string;
spinner?: Ora;
}
/**
* Execute the `lerna-changelog` command.
*
* @param {string} [args.from] GitHub ref (commit SHA, tag).
* @param {string} [args.to] GitHub ref (commit SHA, tag).
* @param {string} [args.path] Path to a git directory.
* @param {string} [args.token] GitHub personal access token.
* @param {Ora} [args.spinner] Terminal spinner.
*
* @returns {Promise<string>} The markdown result of the changelog command.
*/
export const execute = async (args: ILernaChangelogArgs = {}): Promise<string | undefined> => {
let retVal: string | undefined;
try {
process.env.GITHUB_AUTH = args.token || (await getToken(args.spinner));
const revParseArgs = ['rev-parse', '--show-toplevel'];
const describeArgs = ['describe', '--abbrev=0', '--tags'];
if (args.path) {
revParseArgs.unshift('-C', args.path);
describeArgs.unshift('-C', args.path);
}
const fromPath = (await import('lerna-changelog/lib/configuration.js')).fromPath;
const rootPath = await execa('git', revParseArgs);
const config = fromPath(rootPath.stdout);
const changelog = new Changelog(config);
let tagTo = args.to;
let tagFrom = args.from;
if (!tagTo) {
const describe = await execa('git', describeArgs);
tagTo = describe.stdout.toString();
}
if (!tagFrom) {
describeArgs.push(`${tagTo}^`);
const describe = await execa('git', describeArgs);
tagFrom = describe.stdout.toString();
}
process.chdir(rootPath.stdout);
retVal = await changelog.createMarkdown({
tagFrom,
tagTo
});
} catch (error) {
handleErrorMessage(
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
(error as Error).message ? (error as Error).message : error,
'lerna-changelog',
args.spinner
);
throw error;
}
return retVal;
};
export default (spinner: Ora): Command => {
const command = new Command('lerna-changelog');
return command
.description('output generated changelog markdown for the repo')
.arguments('[path]')
.option('-f, --from <from-tag>', 'GitHub tag or commit SHA')
.option('-t, --to <to-tag>', 'GitHub tag or commit SHA')
.option('-a, --token <token>', 'GitHub access token')
.action(async path => {
try {
spinner.start();
const options = command.opts();
const markdown = await execute({
path,
from: options.from,
to: options.to,
token: options.token,
spinner
});
if (markdown) {
handleSuccessMessage(markdown, spinner);
} else {
throw new Error();
}
} catch {
spinner.fail('Unable to generate changelog');
process.exitCode = 1;
} finally {
spinner.stop();
}
});
};