githublogen 0.1.0 → 0.1.2

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/dist/cli.cjs CHANGED
@@ -1,47 +1,78 @@
1
1
  #!/usr/bin/env node
2
2
  'use strict';
3
3
 
4
- const fs = require('node:fs/promises');
4
+ const fs = require('node:fs');
5
5
  const kolorist = require('kolorist');
6
+ const execa = require('execa');
6
7
  const cac = require('cac');
7
8
  const index = require('./index.cjs');
8
9
  require('ohmyfetch');
9
10
  require('convert-gitmoji');
11
+ require('node:module');
12
+ require('node:url');
13
+ require('node:assert');
14
+ require('node:process');
15
+ require('node:path');
16
+ require('node:v8');
17
+ require('node:util');
10
18
 
11
19
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
12
20
 
13
- const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
14
21
  const cac__default = /*#__PURE__*/_interopDefaultCompat(cac);
15
22
 
16
- const version = "0.1.0";
23
+ const version = "0.1.2";
17
24
 
18
25
  const cli = cac__default("githublogen");
19
26
  cli.version(version).option("-t, --token <path>", "GitHub Token").option("--from <ref>", "From tag").option("--to <ref>", "To tag").option("--github <path>", "GitHub Repository, e.g. soybeanjs/githublogen").option("--name <name>", "Name of the release").option("--contributors", "Show contributors section").option("--prerelease", "Mark release as prerelease").option("-d, --draft", "Mark release as draft").option("--output <path>", "Output to file instead of sending to GitHub").option("--capitalize", "Should capitalize for each comment message").option("--emoji", "Use emojis in section titles", { default: true }).option("--group", "Nest commit messages under their scopes").option("--dry", "Dry run").help();
20
27
  cli.command("").action(async (args) => {
21
- args.token = args.token || process.env.GITHUB_TOKEN;
22
28
  try {
23
29
  console.log();
24
30
  console.log(kolorist.dim(`${kolorist.bold("github")}logen `) + kolorist.dim(`v${version}`));
25
- const { config, md, commits } = await index.generate(args);
31
+ const cwd = process.cwd();
32
+ const { config, md, changelog, commits } = await index.generate(cwd, args);
33
+ const markdown = md.replace(/&nbsp;/g, "");
26
34
  console.log(kolorist.cyan(config.from) + kolorist.dim(" -> ") + kolorist.blue(config.to) + kolorist.dim(` (${commits.length} commits)`));
27
35
  console.log(kolorist.dim("--------------"));
28
36
  console.log();
29
- console.log(md.replace(/&nbsp;/g, ""));
37
+ console.log(markdown);
30
38
  console.log();
31
39
  console.log(kolorist.dim("--------------"));
32
40
  if (config.dry) {
33
41
  console.log(kolorist.yellow("Dry run. Release skipped."));
34
42
  return;
35
43
  }
36
- if (!config.token) {
37
- console.error(kolorist.red("No GitHub token found, specify it via GITHUB_TOKEN env. Release skipped."));
38
- process.exitCode = 1;
39
- return;
40
- }
41
44
  if (typeof config.output === "string") {
42
- await fs__default.writeFile(config.output, md, "utf-8");
43
- console.log(kolorist.yellow(`Saved to ${config.output}`));
44
- return;
45
+ let changelogMD;
46
+ const changelogPrefix = "# Changelog";
47
+ if (fs.existsSync(config.output)) {
48
+ console.info(`Updating ${config.output}`);
49
+ changelogMD = await fs.promises.readFile(config.output, "utf8");
50
+ if (!changelogMD.startsWith(changelogPrefix)) {
51
+ changelogMD = `${changelogPrefix}
52
+
53
+ ${changelogMD}`;
54
+ }
55
+ } else {
56
+ console.info(`Creating ${config.output}`);
57
+ changelogMD = `${changelogPrefix}
58
+
59
+ `;
60
+ }
61
+ const lastEntry = changelogMD.match(/^###?\s+.*$/m);
62
+ if (lastEntry) {
63
+ changelogMD = `${changelogMD.slice(0, lastEntry.index) + changelog}
64
+
65
+ ${changelogMD.slice(lastEntry.index)}`;
66
+ } else {
67
+ changelogMD += `
68
+ ${changelog}
69
+
70
+ `;
71
+ }
72
+ await fs.promises.writeFile(config.output, changelogMD);
73
+ await execa.execa("git", ["add", "."]);
74
+ await execa.execa("git", ["commit", "-m", "docs(projects): CHANGELOG.md"], { cwd });
75
+ await execa.execa("git", ["push"], { cwd });
45
76
  }
46
77
  if (!await index.hasTagOnGitHub(config.to, config)) {
47
78
  console.error(kolorist.yellow(`Current ref "${kolorist.bold(config.to)}" is not available as tags on GitHub. Release skipped.`));
package/dist/cli.mjs CHANGED
@@ -1,40 +1,72 @@
1
1
  #!/usr/bin/env node
2
- import fs from 'node:fs/promises';
2
+ import { existsSync, promises } from 'node:fs';
3
3
  import { dim, bold, cyan, blue, yellow, red } from 'kolorist';
4
+ import { execa } from 'execa';
4
5
  import cac from 'cac';
5
6
  import { generate, hasTagOnGitHub, isRepoShallow, sendRelease } from './index.mjs';
6
7
  import 'ohmyfetch';
7
8
  import 'convert-gitmoji';
9
+ import 'node:module';
10
+ import 'node:url';
11
+ import 'node:assert';
12
+ import 'node:process';
13
+ import 'node:path';
14
+ import 'node:v8';
15
+ import 'node:util';
8
16
 
9
- const version = "0.1.0";
17
+ const version = "0.1.2";
10
18
 
11
19
  const cli = cac("githublogen");
12
20
  cli.version(version).option("-t, --token <path>", "GitHub Token").option("--from <ref>", "From tag").option("--to <ref>", "To tag").option("--github <path>", "GitHub Repository, e.g. soybeanjs/githublogen").option("--name <name>", "Name of the release").option("--contributors", "Show contributors section").option("--prerelease", "Mark release as prerelease").option("-d, --draft", "Mark release as draft").option("--output <path>", "Output to file instead of sending to GitHub").option("--capitalize", "Should capitalize for each comment message").option("--emoji", "Use emojis in section titles", { default: true }).option("--group", "Nest commit messages under their scopes").option("--dry", "Dry run").help();
13
21
  cli.command("").action(async (args) => {
14
- args.token = args.token || process.env.GITHUB_TOKEN;
15
22
  try {
16
23
  console.log();
17
24
  console.log(dim(`${bold("github")}logen `) + dim(`v${version}`));
18
- const { config, md, commits } = await generate(args);
25
+ const cwd = process.cwd();
26
+ const { config, md, changelog, commits } = await generate(cwd, args);
27
+ const markdown = md.replace(/&nbsp;/g, "");
19
28
  console.log(cyan(config.from) + dim(" -> ") + blue(config.to) + dim(` (${commits.length} commits)`));
20
29
  console.log(dim("--------------"));
21
30
  console.log();
22
- console.log(md.replace(/&nbsp;/g, ""));
31
+ console.log(markdown);
23
32
  console.log();
24
33
  console.log(dim("--------------"));
25
34
  if (config.dry) {
26
35
  console.log(yellow("Dry run. Release skipped."));
27
36
  return;
28
37
  }
29
- if (!config.token) {
30
- console.error(red("No GitHub token found, specify it via GITHUB_TOKEN env. Release skipped."));
31
- process.exitCode = 1;
32
- return;
33
- }
34
38
  if (typeof config.output === "string") {
35
- await fs.writeFile(config.output, md, "utf-8");
36
- console.log(yellow(`Saved to ${config.output}`));
37
- return;
39
+ let changelogMD;
40
+ const changelogPrefix = "# Changelog";
41
+ if (existsSync(config.output)) {
42
+ console.info(`Updating ${config.output}`);
43
+ changelogMD = await promises.readFile(config.output, "utf8");
44
+ if (!changelogMD.startsWith(changelogPrefix)) {
45
+ changelogMD = `${changelogPrefix}
46
+
47
+ ${changelogMD}`;
48
+ }
49
+ } else {
50
+ console.info(`Creating ${config.output}`);
51
+ changelogMD = `${changelogPrefix}
52
+
53
+ `;
54
+ }
55
+ const lastEntry = changelogMD.match(/^###?\s+.*$/m);
56
+ if (lastEntry) {
57
+ changelogMD = `${changelogMD.slice(0, lastEntry.index) + changelog}
58
+
59
+ ${changelogMD.slice(lastEntry.index)}`;
60
+ } else {
61
+ changelogMD += `
62
+ ${changelog}
63
+
64
+ `;
65
+ }
66
+ await promises.writeFile(config.output, changelogMD);
67
+ await execa("git", ["add", "."]);
68
+ await execa("git", ["commit", "-m", "docs(projects): CHANGELOG.md"], { cwd });
69
+ await execa("git", ["push"], { cwd });
38
70
  }
39
71
  if (!await hasTagOnGitHub(config.to, config)) {
40
72
  console.error(yellow(`Current ref "${bold(config.to)}" is not available as tags on GitHub. Release skipped.`));