Skip to content

Commit c781cb8

Browse files
authored
Add init command (#77)
This command generates an empty changelog. It doesn't list any changes or releases. Closes #14
1 parent 5966857 commit c781cb8

File tree

2 files changed

+62
-17
lines changed

2 files changed

+62
-17
lines changed

src/cli.ts

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { hideBin } from 'yargs/helpers';
1010

1111
import { updateChangelog } from './update-changelog';
1212
import { generateDiff } from './generate-diff';
13+
import { createEmptyChangelog } from './init';
1314

1415
import { unreleased, Version } from './constants';
1516

@@ -137,6 +138,16 @@ async function validate({
137138
}
138139
}
139140

141+
interface InitOptions {
142+
changelogPath: string;
143+
repoUrl: string;
144+
}
145+
146+
async function init({ changelogPath, repoUrl }: InitOptions) {
147+
const changelogContent = await createEmptyChangelog({ repoUrl });
148+
await saveChangelog(changelogPath, changelogContent);
149+
}
150+
140151
const rootDescription = `The root project directory. This determines where we \
141152
look for changes since the last release (defaults to the entire repository at \
142153
the current working directory), and where the changelog path is resolved from \
@@ -149,12 +160,6 @@ function configureCommonCommandOptions(_yargs: Argv) {
149160
description: 'The changelog file path',
150161
type: 'string',
151162
})
152-
.option('currentVersion', {
153-
default: npmPackageVersion,
154-
description:
155-
'The current version of the project that the changelog belongs to.',
156-
type: 'string',
157-
})
158163
.option('repo', {
159164
default: githubRepositoryUrl,
160165
description: `The GitHub repository URL`,
@@ -178,6 +183,12 @@ async function main() {
178183
description: `Add new changes to the current release header, rather than to the '${unreleased}' section.`,
179184
type: 'boolean',
180185
})
186+
.option('currentVersion', {
187+
default: npmPackageVersion,
188+
description:
189+
'The current version of the project that the changelog belongs to.',
190+
type: 'string',
191+
})
181192
.epilog(updateEpilog),
182193
)
183194
.command(
@@ -190,8 +201,17 @@ async function main() {
190201
description: `Verify that the current version has a release header in the changelog`,
191202
type: 'boolean',
192203
})
204+
.option('currentVersion', {
205+
default: npmPackageVersion,
206+
description:
207+
'The current version of the project that the changelog belongs to.',
208+
type: 'string',
209+
})
193210
.epilog(validateEpilog),
194211
)
212+
.command('init', 'Initialize a new empty changelog', (_yargs) => {
213+
configureCommonCommandOptions(_yargs);
214+
})
195215
.strict()
196216
.demandCommand()
197217
.help('help')
@@ -255,33 +275,45 @@ async function main() {
255275
changelogPath = path.resolve(projectRootDirectory, changelogFilename);
256276
}
257277

258-
try {
259-
// eslint-disable-next-line no-bitwise
260-
await fs.access(changelogPath, fsConstants.F_OK | fsConstants.W_OK);
261-
} catch (error) {
262-
if (error.code === 'ENOENT') {
263-
exitWithError(`File does not exist: '${changelogPath}'`);
264-
} else {
265-
exitWithError(`File is not writable: '${changelogPath}'`);
278+
if (!argv._) {
279+
throw new Error('No command provided');
280+
}
281+
const command = argv._[0];
282+
283+
if (command !== 'init') {
284+
try {
285+
// eslint-disable-next-line no-bitwise
286+
await fs.access(changelogPath, fsConstants.F_OK | fsConstants.W_OK);
287+
} catch (error) {
288+
if (error.code === 'ENOENT') {
289+
exitWithError(`File does not exist: '${changelogPath}'`);
290+
} else {
291+
exitWithError(`File is not writable: '${changelogPath}'`);
292+
}
293+
return;
266294
}
267-
return;
268295
}
269296

270-
if (argv._ && argv._[0] === 'update') {
297+
if (command === 'update') {
271298
await update({
272299
changelogPath,
273300
currentVersion,
274301
isReleaseCandidate,
275302
repoUrl,
276303
projectRootDirectory,
277304
});
278-
} else if (argv._ && argv._[0] === 'validate') {
305+
} else if (command === 'validate') {
279306
await validate({
280307
changelogPath,
281308
currentVersion,
282309
isReleaseCandidate,
283310
repoUrl,
284311
});
312+
} else if (command === 'init') {
313+
await init({
314+
changelogPath,
315+
repoUrl,
316+
});
285317
}
286318
}
287319

src/init.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Changelog from './changelog';
2+
3+
/**
4+
* Creates a new empty changelog.
5+
*
6+
* @param options
7+
* @param options.repoUrl - The GitHub repository URL for the current project.
8+
* @returns The initial changelog text.
9+
*/
10+
export function createEmptyChangelog({ repoUrl }: { repoUrl: string }) {
11+
const changelog = new Changelog({ repoUrl });
12+
return changelog.toString();
13+
}

0 commit comments

Comments
 (0)