changelog-tool 0.6.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,13 @@
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
+
4
11
  0.6.0 (2023-02-14)
5
12
  ------------------
6
13
 
package/cli.mjs CHANGED
@@ -3,7 +3,7 @@
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
9
  import { execSync } from 'node:child_process';
@@ -236,13 +236,26 @@ async function release() {
236
236
  }
237
237
  lastVersion.date = new Date().toISOString().substr(0,10);
238
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
+
239
248
  await fs.writeFile(filename, changelog.toString());
240
249
  console.log(`${changelog.versions.length} changelogs saved to ${filename}`);
241
250
 
242
251
  if (await exists('package.json')) {
243
- const command = `npm version "${lastVersion.version}" --no-git-tag-version`;
244
- console.log(command);
245
- execSync(command);
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}`);
246
259
  }
247
260
 
248
261
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "changelog-tool",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "A CLI tool for manipulating changelogs",
5
5
  "type": "module",
6
6
  "main": "index.mjs",
package/readme.md CHANGED
@@ -88,3 +88,9 @@ npm version [version] --no-git-tag-version
88
88
 
89
89
  This command adjust the `version` field in `package.json` to match the latest
90
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/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
+ }