githublogen 0.3.8 → 0.3.9-beta.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "githublogen",
3
3
  "type": "module",
4
- "version": "0.3.8",
4
+ "version": "0.3.9-beta.1",
5
5
  "packageManager": "pnpm@8.12.0",
6
6
  "author": {
7
7
  "name": "Soybean",
@@ -29,31 +29,29 @@
29
29
  },
30
30
  "sideEffects": false,
31
31
  "bin": {
32
- "githublogen": "./dist/cli.mjs"
32
+ "githublogen": "./dist/cli.js"
33
33
  },
34
34
  "files": [
35
35
  "dist"
36
36
  ],
37
37
  "engines": {
38
- "node": ">=12.0.0"
38
+ "node": ">=14"
39
39
  },
40
40
  "dependencies": {
41
- "c12": "1.5.1",
41
+ "c12": "1.6.1",
42
42
  "cac": "6.7.14",
43
43
  "execa": "8.0.1",
44
44
  "kolorist": "1.8.0",
45
45
  "ofetch": "1.3.3",
46
- "@soybeanjs/changelog": "0.3.8"
46
+ "@soybeanjs/changelog": "0.3.9-beta.1"
47
47
  },
48
48
  "devDependencies": {
49
- "@soybeanjs/cli": "0.8.8",
50
- "@types/node": "20.10.4",
51
- "typescript": "5.3.3",
52
- "unbuild": "2.0.0"
49
+ "@soybeanjs/cli": "1.0.1",
50
+ "@types/node": "20.11.0",
51
+ "typescript": "5.3.3"
53
52
  },
54
53
  "scripts": {
55
- "build": "unbuild",
56
- "stub": "pnpm unbuild --stub",
54
+ "build": "tsup",
57
55
  "typecheck": "tsc --noEmit --skipLibCheck"
58
56
  }
59
57
  }
package/dist/cli.mjs DELETED
@@ -1,593 +0,0 @@
1
- #!/usr/bin/env node
2
- import process from 'node:process';
3
- import cac from 'cac';
4
- import { cyan, green, red, yellow, dim, blue, bold } from 'kolorist';
5
- import { getChangelogMarkdown } from '@soybeanjs/changelog';
6
- import { ofetch } from 'ofetch';
7
-
8
- const LogLevels = {
9
- silent: Number.NEGATIVE_INFINITY,
10
- fatal: 0,
11
- error: 0,
12
- warn: 1,
13
- log: 2,
14
- info: 3,
15
- success: 3,
16
- fail: 3,
17
- ready: 3,
18
- start: 3,
19
- box: 3,
20
- debug: 4,
21
- trace: 5,
22
- verbose: Number.POSITIVE_INFINITY
23
- };
24
- const LogTypes = {
25
- // Silent
26
- silent: {
27
- level: -1
28
- },
29
- // Level 0
30
- fatal: {
31
- level: LogLevels.fatal
32
- },
33
- error: {
34
- level: LogLevels.error
35
- },
36
- // Level 1
37
- warn: {
38
- level: LogLevels.warn
39
- },
40
- // Level 2
41
- log: {
42
- level: LogLevels.log
43
- },
44
- // Level 3
45
- info: {
46
- level: LogLevels.info
47
- },
48
- success: {
49
- level: LogLevels.success
50
- },
51
- fail: {
52
- level: LogLevels.fail
53
- },
54
- ready: {
55
- level: LogLevels.info
56
- },
57
- start: {
58
- level: LogLevels.info
59
- },
60
- box: {
61
- level: LogLevels.info
62
- },
63
- // Level 4
64
- debug: {
65
- level: LogLevels.debug
66
- },
67
- // Level 5
68
- trace: {
69
- level: LogLevels.trace
70
- },
71
- // Verbose
72
- verbose: {
73
- level: LogLevels.verbose
74
- }
75
- };
76
-
77
- function isObject(value) {
78
- return value !== null && typeof value === "object";
79
- }
80
- function _defu(baseObject, defaults, namespace = ".", merger) {
81
- if (!isObject(defaults)) {
82
- return _defu(baseObject, {}, namespace, merger);
83
- }
84
- const object = Object.assign({}, defaults);
85
- for (const key in baseObject) {
86
- if (key === "__proto__" || key === "constructor") {
87
- continue;
88
- }
89
- const value = baseObject[key];
90
- if (value === null || value === void 0) {
91
- continue;
92
- }
93
- if (merger && merger(object, key, value, namespace)) {
94
- continue;
95
- }
96
- if (Array.isArray(value) && Array.isArray(object[key])) {
97
- object[key] = [...value, ...object[key]];
98
- } else if (isObject(value) && isObject(object[key])) {
99
- object[key] = _defu(
100
- value,
101
- object[key],
102
- (namespace ? `${namespace}.` : "") + key.toString(),
103
- merger
104
- );
105
- } else {
106
- object[key] = value;
107
- }
108
- }
109
- return object;
110
- }
111
- function createDefu(merger) {
112
- return (...arguments_) => (
113
- // eslint-disable-next-line unicorn/no-array-reduce
114
- arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
115
- );
116
- }
117
- const defu = createDefu();
118
-
119
- function isPlainObject(obj) {
120
- return Object.prototype.toString.call(obj) === "[object Object]";
121
- }
122
- function isLogObj(arg) {
123
- if (!isPlainObject(arg)) {
124
- return false;
125
- }
126
- if (!arg.message && !arg.args) {
127
- return false;
128
- }
129
- if (arg.stack) {
130
- return false;
131
- }
132
- return true;
133
- }
134
-
135
- let paused = false;
136
- const queue = [];
137
- class Consola {
138
- constructor(options = {}) {
139
- const types = options.types || LogTypes;
140
- this.options = defu(
141
- {
142
- ...options,
143
- defaults: { ...options.defaults },
144
- level: _normalizeLogLevel(options.level, types),
145
- reporters: [...options.reporters || []]
146
- },
147
- {
148
- types: LogTypes,
149
- throttle: 1e3,
150
- throttleMin: 5,
151
- formatOptions: {
152
- date: true,
153
- colors: false,
154
- compact: true
155
- }
156
- }
157
- );
158
- for (const type in types) {
159
- const defaults = {
160
- type,
161
- ...this.options.defaults,
162
- ...types[type]
163
- };
164
- this[type] = this._wrapLogFn(defaults);
165
- this[type].raw = this._wrapLogFn(
166
- defaults,
167
- true
168
- );
169
- }
170
- if (this.options.mockFn) {
171
- this.mockTypes();
172
- }
173
- this._lastLog = {};
174
- }
175
- get level() {
176
- return this.options.level;
177
- }
178
- set level(level) {
179
- this.options.level = _normalizeLogLevel(
180
- level,
181
- this.options.types,
182
- this.options.level
183
- );
184
- }
185
- prompt(message, opts) {
186
- if (!this.options.prompt) {
187
- throw new Error("prompt is not supported!");
188
- }
189
- return this.options.prompt(message, opts);
190
- }
191
- create(options) {
192
- const instance = new Consola({
193
- ...this.options,
194
- ...options
195
- });
196
- if (this._mockFn) {
197
- instance.mockTypes(this._mockFn);
198
- }
199
- return instance;
200
- }
201
- withDefaults(defaults) {
202
- return this.create({
203
- ...this.options,
204
- defaults: {
205
- ...this.options.defaults,
206
- ...defaults
207
- }
208
- });
209
- }
210
- withTag(tag) {
211
- return this.withDefaults({
212
- tag: this.options.defaults.tag ? this.options.defaults.tag + ":" + tag : tag
213
- });
214
- }
215
- addReporter(reporter) {
216
- this.options.reporters.push(reporter);
217
- return this;
218
- }
219
- removeReporter(reporter) {
220
- if (reporter) {
221
- const i = this.options.reporters.indexOf(reporter);
222
- if (i >= 0) {
223
- return this.options.reporters.splice(i, 1);
224
- }
225
- } else {
226
- this.options.reporters.splice(0);
227
- }
228
- return this;
229
- }
230
- setReporters(reporters) {
231
- this.options.reporters = Array.isArray(reporters) ? reporters : [reporters];
232
- return this;
233
- }
234
- wrapAll() {
235
- this.wrapConsole();
236
- this.wrapStd();
237
- }
238
- restoreAll() {
239
- this.restoreConsole();
240
- this.restoreStd();
241
- }
242
- wrapConsole() {
243
- for (const type in this.options.types) {
244
- if (!console["__" + type]) {
245
- console["__" + type] = console[type];
246
- }
247
- console[type] = this[type].raw;
248
- }
249
- }
250
- restoreConsole() {
251
- for (const type in this.options.types) {
252
- if (console["__" + type]) {
253
- console[type] = console["__" + type];
254
- delete console["__" + type];
255
- }
256
- }
257
- }
258
- wrapStd() {
259
- this._wrapStream(this.options.stdout, "log");
260
- this._wrapStream(this.options.stderr, "log");
261
- }
262
- _wrapStream(stream, type) {
263
- if (!stream) {
264
- return;
265
- }
266
- if (!stream.__write) {
267
- stream.__write = stream.write;
268
- }
269
- stream.write = (data) => {
270
- this[type].raw(String(data).trim());
271
- };
272
- }
273
- restoreStd() {
274
- this._restoreStream(this.options.stdout);
275
- this._restoreStream(this.options.stderr);
276
- }
277
- _restoreStream(stream) {
278
- if (!stream) {
279
- return;
280
- }
281
- if (stream.__write) {
282
- stream.write = stream.__write;
283
- delete stream.__write;
284
- }
285
- }
286
- pauseLogs() {
287
- paused = true;
288
- }
289
- resumeLogs() {
290
- paused = false;
291
- const _queue = queue.splice(0);
292
- for (const item of _queue) {
293
- item[0]._logFn(item[1], item[2]);
294
- }
295
- }
296
- mockTypes(mockFn) {
297
- const _mockFn = mockFn || this.options.mockFn;
298
- this._mockFn = _mockFn;
299
- if (typeof _mockFn !== "function") {
300
- return;
301
- }
302
- for (const type in this.options.types) {
303
- this[type] = _mockFn(type, this.options.types[type]) || this[type];
304
- this[type].raw = this[type];
305
- }
306
- }
307
- _wrapLogFn(defaults, isRaw) {
308
- return (...args) => {
309
- if (paused) {
310
- queue.push([this, defaults, args, isRaw]);
311
- return;
312
- }
313
- return this._logFn(defaults, args, isRaw);
314
- };
315
- }
316
- _logFn(defaults, args, isRaw) {
317
- if ((defaults.level || 0) > this.level) {
318
- return false;
319
- }
320
- const logObj = {
321
- date: /* @__PURE__ */ new Date(),
322
- args: [],
323
- ...defaults,
324
- level: _normalizeLogLevel(defaults.level, this.options.types)
325
- };
326
- if (!isRaw && args.length === 1 && isLogObj(args[0])) {
327
- Object.assign(logObj, args[0]);
328
- } else {
329
- logObj.args = [...args];
330
- }
331
- if (logObj.message) {
332
- logObj.args.unshift(logObj.message);
333
- delete logObj.message;
334
- }
335
- if (logObj.additional) {
336
- if (!Array.isArray(logObj.additional)) {
337
- logObj.additional = logObj.additional.split("\n");
338
- }
339
- logObj.args.push("\n" + logObj.additional.join("\n"));
340
- delete logObj.additional;
341
- }
342
- logObj.type = typeof logObj.type === "string" ? logObj.type.toLowerCase() : "log";
343
- logObj.tag = typeof logObj.tag === "string" ? logObj.tag : "";
344
- const resolveLog = (newLog = false) => {
345
- const repeated = (this._lastLog.count || 0) - this.options.throttleMin;
346
- if (this._lastLog.object && repeated > 0) {
347
- const args2 = [...this._lastLog.object.args];
348
- if (repeated > 1) {
349
- args2.push(`(repeated ${repeated} times)`);
350
- }
351
- this._log({ ...this._lastLog.object, args: args2 });
352
- this._lastLog.count = 1;
353
- }
354
- if (newLog) {
355
- this._lastLog.object = logObj;
356
- this._log(logObj);
357
- }
358
- };
359
- clearTimeout(this._lastLog.timeout);
360
- const diffTime = this._lastLog.time && logObj.date ? logObj.date.getTime() - this._lastLog.time.getTime() : 0;
361
- this._lastLog.time = logObj.date;
362
- if (diffTime < this.options.throttle) {
363
- try {
364
- const serializedLog = JSON.stringify([
365
- logObj.type,
366
- logObj.tag,
367
- logObj.args
368
- ]);
369
- const isSameLog = this._lastLog.serialized === serializedLog;
370
- this._lastLog.serialized = serializedLog;
371
- if (isSameLog) {
372
- this._lastLog.count = (this._lastLog.count || 0) + 1;
373
- if (this._lastLog.count > this.options.throttleMin) {
374
- this._lastLog.timeout = setTimeout(
375
- resolveLog,
376
- this.options.throttle
377
- );
378
- return;
379
- }
380
- }
381
- } catch {
382
- }
383
- }
384
- resolveLog(true);
385
- }
386
- _log(logObj) {
387
- for (const reporter of this.options.reporters) {
388
- reporter.log(logObj, {
389
- options: this.options
390
- });
391
- }
392
- }
393
- }
394
- function _normalizeLogLevel(input, types = {}, defaultLevel = 3) {
395
- if (input === void 0) {
396
- return defaultLevel;
397
- }
398
- if (typeof input === "number") {
399
- return input;
400
- }
401
- if (types[input] && types[input].level !== void 0) {
402
- return types[input].level;
403
- }
404
- return defaultLevel;
405
- }
406
- Consola.prototype.add = Consola.prototype.addReporter;
407
- Consola.prototype.remove = Consola.prototype.removeReporter;
408
- Consola.prototype.clear = Consola.prototype.removeReporter;
409
- Consola.prototype.withScope = Consola.prototype.withTag;
410
- Consola.prototype.mock = Consola.prototype.mockTypes;
411
- Consola.prototype.pause = Consola.prototype.pauseLogs;
412
- Consola.prototype.resume = Consola.prototype.resumeLogs;
413
- function createConsola$1(options = {}) {
414
- return new Consola(options);
415
- }
416
-
417
- class BrowserReporter {
418
- constructor(options) {
419
- this.options = { ...options };
420
- this.defaultColor = "#7f8c8d";
421
- this.levelColorMap = {
422
- 0: "#c0392b",
423
- // Red
424
- 1: "#f39c12",
425
- // Yellow
426
- 3: "#00BCD4"
427
- // Cyan
428
- };
429
- this.typeColorMap = {
430
- success: "#2ecc71"
431
- // Green
432
- };
433
- }
434
- _getLogFn(level) {
435
- if (level < 1) {
436
- return console.__error || console.error;
437
- }
438
- if (level === 1) {
439
- return console.__warn || console.warn;
440
- }
441
- return console.__log || console.log;
442
- }
443
- log(logObj) {
444
- const consoleLogFn = this._getLogFn(logObj.level);
445
- const type = logObj.type === "log" ? "" : logObj.type;
446
- const tag = logObj.tag || "";
447
- const color = this.typeColorMap[logObj.type] || this.levelColorMap[logObj.level] || this.defaultColor;
448
- const style = `
449
- background: ${color};
450
- border-radius: 0.5em;
451
- color: white;
452
- font-weight: bold;
453
- padding: 2px 0.5em;
454
- `;
455
- const badge = `%c${[tag, type].filter(Boolean).join(":")}`;
456
- if (typeof logObj.args[0] === "string") {
457
- consoleLogFn(
458
- `${badge}%c ${logObj.args[0]}`,
459
- style,
460
- // Empty string as style resets to default console style
461
- "",
462
- ...logObj.args.slice(1)
463
- );
464
- } else {
465
- consoleLogFn(badge, style, ...logObj.args);
466
- }
467
- }
468
- }
469
-
470
- function createConsola(options = {}) {
471
- const consola2 = createConsola$1({
472
- reporters: options.reporters || [new BrowserReporter({})],
473
- prompt(message, options2 = {}) {
474
- if (options2.type === "confirm") {
475
- return Promise.resolve(confirm(message));
476
- }
477
- return Promise.resolve(prompt(message));
478
- },
479
- ...options
480
- });
481
- return consola2;
482
- }
483
- const consola = createConsola();
484
-
485
- const version = "0.3.8";
486
-
487
- function getHeaders(githubToken) {
488
- return {
489
- accept: "application/vnd.github.v3+json",
490
- authorization: `token ${githubToken}`
491
- };
492
- }
493
- async function hasTagOnGitHub(tag, repo, githubToken) {
494
- try {
495
- await ofetch(`https://api.github.com/repos/${repo}/git/ref/tags/${tag}`, {
496
- headers: getHeaders(githubToken)
497
- });
498
- return true;
499
- } catch (e) {
500
- return false;
501
- }
502
- }
503
- async function sendRelease(options, content) {
504
- const headers = getHeaders(options.github.token);
505
- const github = options.github.repo;
506
- let url = `https://api.github.com/repos/${github}/releases`;
507
- let method = "POST";
508
- try {
509
- const exists = await ofetch(`https://api.github.com/repos/${github}/releases/tags/${options.to}`, {
510
- headers
511
- });
512
- if (exists.url) {
513
- url = exists.url;
514
- method = "PATCH";
515
- }
516
- } catch (e) {
517
- }
518
- const body = {
519
- body: content,
520
- draft: false,
521
- name: options.to,
522
- prerelease: options.prerelease,
523
- tag_name: options.to
524
- };
525
- const webUrl = `https://github.com/${github}/releases/new?title=${encodeURIComponent(
526
- String(body.name)
527
- )}&body=${encodeURIComponent(String(body.body))}&tag=${encodeURIComponent(String(options.to))}&prerelease=${options.prerelease}`;
528
- try {
529
- consola.log(cyan(method === "POST" ? "Creating release notes..." : "Updating release notes..."));
530
- const res = await ofetch(url, {
531
- method,
532
- body: JSON.stringify(body),
533
- headers
534
- });
535
- consola.log(green(`Released on ${res.html_url}`));
536
- } catch (e) {
537
- consola.error(red("Failed to create the release. Using the following link to create it manually:"));
538
- consola.error(yellow(webUrl));
539
- throw e;
540
- }
541
- }
542
- async function execCommand(cmd, args) {
543
- const { execa } = await import('execa');
544
- const res = await execa(cmd, args);
545
- return res.stdout.trim();
546
- }
547
- async function isRepoShallow() {
548
- return (await execCommand("git", ["rev-parse", "--is-shallow-repository"])).trim() === "true";
549
- }
550
-
551
- function setupCli() {
552
- const cli = cac("githublogen");
553
- cli.version(version).option("-t, --token <path>", "GitHub Token").help();
554
- cli.command("").action(async (args) => {
555
- try {
556
- const cwd = process.cwd();
557
- const { options, commits, markdown } = await getChangelogMarkdown(
558
- {
559
- cwd,
560
- ...args
561
- },
562
- false
563
- );
564
- consola.log(cyan(options.from) + dim(" -> ") + blue(options.to) + dim(` (${commits.length} commits)`));
565
- if (!await hasTagOnGitHub(options.to, options.github.repo, options.github.token)) {
566
- consola.error(yellow(`Current ref "${bold(options.to)}" is not available as tags on GitHub. Release skipped.`));
567
- if (process.exitCode) {
568
- process.exitCode = 1;
569
- }
570
- }
571
- if (!commits.length && await isRepoShallow()) {
572
- consola.error(
573
- yellow(
574
- "The repo seems to be clone shallowly, which make changelog failed to generate. You might want to specify `fetch-depth: 0` in your CI config."
575
- )
576
- );
577
- if (process.exitCode) {
578
- process.exitCode = 1;
579
- }
580
- return;
581
- }
582
- await sendRelease(options, markdown);
583
- } catch (e) {
584
- consola.error(red(String(e)));
585
- if (e?.stack) {
586
- consola.error(dim(e.stack?.split("\n").slice(1).join("\n")));
587
- }
588
- process.exit(1);
589
- }
590
- });
591
- cli.parse();
592
- }
593
- setupCli();