changelog-tool 0.5.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
package/changelog.md CHANGED
@@ -1,6 +1,22 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
+ 0.7.0 (2023-02-14)
5
+ ------------------
6
+
7
+ * The "release" command now automatically commits and and creates a git tag,
8
+ much like `npm version`
9
+
10
+
11
+ 0.6.0 (2023-02-14)
12
+ ------------------
13
+
14
+ * The release command now automatically calls "npm version" if a package.json
15
+ was found in the project directory
16
+ * Bug fix: the --major and --minor arguments were ignored when using "add" to
17
+ create a new version log
18
+
19
+
4
20
  0.5.0 (2023-02-12)
5
21
  ------------------
6
22
 
@@ -8,8 +24,6 @@ Changelog
8
24
  `--minor` arguments.
9
25
  * The `add` command now uses the -m argument instead of a positional for the
10
26
  message.
11
- * bla
12
- * bla
13
27
 
14
28
 
15
29
  0.4.1 (2023-02-12)
package/changelog.mjs CHANGED
@@ -38,13 +38,13 @@ export class Changelog {
38
38
  * Adds a new version to the log. Version string is automatically increased
39
39
  * from the previous one
40
40
  *
41
- * @params {'patch'|'minor'|'major'} changeType
41
+ * @param {'patch'|'minor'|'major'} changeType
42
42
  * @returns {VersionLog}
43
43
  */
44
44
  newVersion(changeType = 'patch') {
45
45
 
46
46
  const lastVersion = this.versions[0].version;
47
- const newVersion = calculateNextVersion(lastVersion);
47
+ const newVersion = calculateNextVersion(lastVersion, changeType);
48
48
  const versionLog = new VersionLog(newVersion);
49
49
 
50
50
  return this.add(versionLog);
package/cli.mjs CHANGED
@@ -3,9 +3,10 @@
3
3
  import { parseArgs } from 'node:util';
4
4
  import * as fs from 'node:fs/promises';
5
5
  import * as url from 'node:url';
6
- import { readPackageVersion, exists, calculateNextVersion } from './util.mjs';
6
+ import { readPackageVersion, exists, calculateNextVersion, isGit, isGitClean, runCommand } from './util.mjs';
7
7
  import { Changelog, VersionLog, LogItem } from './changelog.mjs';
8
8
  import { parseFile } from './parse.mjs';
9
+ import { execSync } from 'node:child_process';
9
10
 
10
11
  const filename = 'changelog.md';
11
12
 
@@ -78,7 +79,7 @@ async function main() {
78
79
  changeType = 'major';
79
80
  }
80
81
  if (!values.message) {
81
- throw new Error('The "-m" or "-message" argument is required');
82
+ throw new Error('The "-m" or "--message" argument is required');
82
83
  }
83
84
  await add({
84
85
  message: values.message,
@@ -235,9 +236,28 @@ async function release() {
235
236
  }
236
237
  lastVersion.date = new Date().toISOString().substr(0,10);
237
238
  console.log(`Releasing ${lastVersion.version}`);
239
+
240
+ const useGit = await isGit();
241
+
242
+ if (useGit) {
243
+ if (!await isGitClean()) {
244
+ throw new Error('Current git working directory is not clean. Please commit your changes first');
245
+ }
246
+ }
247
+
238
248
  await fs.writeFile(filename, changelog.toString());
239
249
  console.log(`${changelog.versions.length} changelogs saved to ${filename}`);
240
250
 
251
+ if (await exists('package.json')) {
252
+ runCommand(
253
+ `npm version "${lastVersion.version}" --no-git-tag-version`
254
+ );
255
+ }
256
+ if (useGit) {
257
+ runCommand(`git add --all`);
258
+ runCommand(`git tag v${lastVersion.version}`);
259
+ }
260
+
241
261
  }
242
262
 
243
263
  /**
package/package.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "changelog-tool",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "A CLI tool for manipulating changelogs",
5
+ "type": "module",
5
6
  "main": "index.mjs",
6
7
  "scripts": {
7
- "test": "node --test"
8
+ "test": "node --test",
9
+ "watch": "tsc --watch"
8
10
  },
9
11
  "keywords": [
10
12
  "changelog",
package/readme.md CHANGED
@@ -26,7 +26,7 @@ Installation
26
26
  ------------
27
27
 
28
28
  ```sh
29
- npm install changelog-tool --save-dev
29
+ npm install changelog-tool --global
30
30
  ```
31
31
 
32
32
  CLI
@@ -36,33 +36,61 @@ To tool can be used programmatically and with the CLI. The CLI has the
36
36
  following commands:
37
37
 
38
38
  ```
39
- npx changelog init - Create a new, empty npx changelog.
40
- npx changelog add -m [message] - Adds a new line to the npx changelog.
41
- npx changelog release - Marks the current npx changelog as released.
42
- npx changelog show - Show the last npx changelog.
43
- npx changelog show [version] - Show the npx changelog of a specific version.
44
- npx changelog list - List all versions in the npx changelog.
45
- npx changelog format - Reformats the npx changelog in the standard format.
39
+ changelog init - Create a new, empty npx changelog.
40
+ changelog add -m [message] - Adds a new line to the npx changelog.
41
+ changelog release - Marks the current npx changelog as released.
42
+ changelog show - Show the last npx changelog.
43
+ changelog show [version] - Show the npx changelog of a specific version.
44
+ changelog list - List all versions in the npx changelog.
45
+ changelog format - Reformats the npx changelog in the standard format.
46
46
  ```
47
47
 
48
- ### Invoking add
48
+ ### 'add' command
49
49
 
50
- Easiest is to just run:
50
+ The add comment lets you add a new message at the bottom of the last unreleased
51
+ version.
52
+
53
+ To use it, just run:
51
54
 
52
55
  ```
53
- npx changelog add -m "Bug fix"
56
+ changelog add -m "Bug fix"
54
57
  ```
55
58
 
56
- This will automatically add a line to the latest unreleased version. If there
57
- is no unreleased version, it will create a new patch version.
59
+ If there is no unreleased version, it will create a new section and increase
60
+ the version number.
58
61
 
59
- If the change should cause a minor or major version bump, you can specify the
60
- these options too:
62
+ If the current change should result in a new major or minor version number, you
63
+ can use the following arguments.
61
64
 
62
65
  ```
63
- npx changelog add --minor -m "New feature"
64
- npx changelog add --major -m "Backwards compatibility break"
66
+ changelog add --minor -m "New feature"
67
+ changelog add --major -m "Backwards compatibility break"
65
68
  ```
66
69
 
67
70
  These settings will automatically adjust the version string of the most recent
68
71
  unreleased version.
72
+
73
+ ### 'release' command
74
+
75
+ The release command will look for a recent unreleased version in the changelog
76
+ (where the date is marked `????-??-??`) and change it to the current date:
77
+
78
+ ```
79
+ changelog release
80
+ ```
81
+
82
+ If the tool detects a `package.json` file in the current directory, it will
83
+ also call:
84
+
85
+ ```
86
+ npm version [version] --no-git-tag-version
87
+ ```
88
+
89
+ This command adjust the `version` field in `package.json` to match the latest
90
+ changelog version.
91
+
92
+ If the tool detects if this is a git directory, it will also:
93
+
94
+ * Ensure that the working directory is clean.
95
+ * Commit the changes.
96
+ * Create a tag with `git tag v[version]`.
package/test/parse.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ // @ts-check
1
2
  import { test } from 'node:test';
2
3
  import { parse } from '../parse.mjs';
3
4
  import * as assert from 'node:assert';
package/tsconfig.json CHANGED
@@ -7,7 +7,6 @@
7
7
  "checkJs": true,
8
8
 
9
9
  "moduleResolution": "node",
10
- "resolveJsonModule": true,
11
10
 
12
11
  "noEmit": true,
13
12
  "strict": true,
package/util.mjs CHANGED
@@ -1,5 +1,7 @@
1
1
  // @ts-check
2
+ import { execSync } from 'node:child_process';
2
3
  import * as fs from 'node:fs/promises';
4
+ import * as path from 'node:path';
3
5
 
4
6
  /**
5
7
  * Checks if a file exists
@@ -108,3 +110,44 @@ export function calculateNextVersion(prevVersion, changeType = 'patch') {
108
110
  return parts.join('.');
109
111
 
110
112
  }
113
+
114
+ /**
115
+ * Returns true if we're in a git-powered directory
116
+ *
117
+ * @returns {Promise<boolean>}
118
+ */
119
+ export async function isGit() {
120
+
121
+ let currentPath = process.cwd();
122
+ while(currentPath!=='/') {
123
+ if (await exists(path.join(currentPath,'.git'))) {
124
+ return true;
125
+ }
126
+ currentPath = path.dirname(currentPath);
127
+ }
128
+ return false;
129
+
130
+ }
131
+
132
+ /**
133
+ * @param {string} command
134
+ * @returns {string}
135
+ */
136
+ export function runCommand(command) {
137
+
138
+ process.stderr.write(command + '\n');
139
+ return execSync(command).toString('utf-8');
140
+
141
+ }
142
+
143
+ /**
144
+ * Returns true if the current working directory is clean.
145
+ *
146
+ * @returns {boolean}
147
+ */
148
+ export function isGitClean() {
149
+
150
+ const result = execSync('git status --porcelain=v1').toString('utf-8');
151
+ return result.trim().length === 0;
152
+
153
+ }