Skip to content

Commit 680300c

Browse files
feat: add --config option to allow custom config file path
1 parent 400e3c1 commit 680300c

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ _Having problems? Want to contribute? Join us on the [node-tooling community Sla
3838
- [Release as a Pre-Release](#release-as-a-pre-release)
3939
- [Release as a Target Type Imperatively (`npm version`-like)](#release-as-a-target-type-imperatively-npm-version-like)
4040
- [Prevent Git Hooks](#prevent-git-hooks)
41+
- [Custom Config Path](#custom-config-path)
4142
- [Signing Commits and Tags](#signing-commits-and-tags)
4243
- [Lifecycle Scripts](#lifecycle-scripts)
4344
- [Skipping Lifecycle Steps](#skipping-lifecycle-steps)
@@ -312,6 +313,23 @@ npm run release -- --no-verify
312313
commit-and-tag-version --no-verify
313314
```
314315

316+
### Custom Config Path
317+
318+
Specify a custom path to the configuration file using the `--config` option
319+
320+
```sh
321+
commit-and-tag-version --config ./path/to/.versionrc.js
322+
323+
# or using alias
324+
commit-and-tag-version -c ./path/to/.versionrc.js
325+
```
326+
327+
Supports all of the existing config extensions
328+
329+
```sh
330+
commit-and-tag-version -c ./path/to/.versionrc[.js|.cjs|.json]
331+
```
332+
315333
### Signing Commits and Tags
316334

317335
If you have your GPG key set up, add the `--sign` or `-s` flag to your `commit-and-tag-version` command.

command.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ const yargs = require('yargs')
132132
default: defaults.npmPublishHint,
133133
describe: 'Customized publishing hint',
134134
})
135+
.option('config', {
136+
type: 'string',
137+
default: defaults.config,
138+
alias: 'c',
139+
describe: 'Path to a custom configuration file',
140+
})
135141
.check((argv) => {
136142
if (typeof argv.scripts !== 'object' || Array.isArray(argv.scripts)) {
137143
throw Error('scripts must be an object');
@@ -150,9 +156,10 @@ const yargs = require('yargs')
150156
)
151157
.pkgConf('standard-version')
152158
.pkgConf('commit-and-tag-version')
153-
.config(getConfiguration())
154159
.wrap(97);
155160

161+
yargs.config(getConfiguration(yargs.argv));
162+
156163
Object.keys(spec.properties).forEach((propertyKey) => {
157164
const property = spec.properties[propertyKey];
158165
yargs.option(propertyKey, {

defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const defaults = {
1717
gitTagFallback: true,
1818
preset: require.resolve('conventional-changelog-conventionalcommits'),
1919
npmPublishHint: undefined,
20+
config: undefined,
2021
};
2122

2223
/**

lib/configuration.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ const CONFIGURATION_FILES = [
99
'.versionrc.js',
1010
];
1111

12-
module.exports.getConfiguration = function () {
12+
module.exports.getConfiguration = function (argv) {
1313
let config = {};
14-
const configPath = findUp.sync(CONFIGURATION_FILES);
14+
15+
// If the user has provided a configuration file via the `--config` argument, we use that.
16+
const configurationFiles = argv.config ?? CONFIGURATION_FILES;
17+
18+
const configPath = findUp.sync(configurationFiles);
1519
if (!configPath) {
1620
return config;
1721
}

test/config-files.integration-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ describe('config files', function () {
8686
expect(content).toContain(issueUrlFormat);
8787
});
8888

89+
it('reads config from custom path', async function () {
90+
const issueUrlFormat = 'http://www.foo.com/{{id}}';
91+
const changelog = ({ preset }) => preset.issueUrlFormat;
92+
mock({ bump: 'minor', changelog });
93+
fs.mkdirSync('custom-folder');
94+
fs.writeFileSync(
95+
'custom-folder/.versionrc.json',
96+
JSON.stringify({ issueUrlFormat }),
97+
'utf-8',
98+
);
99+
100+
// Override process.argv to simulate CLI arguments before `exec`.
101+
// This ensures yargs parses the custom config argument.
102+
const originalArgv = process.argv;
103+
process.argv = ['node', 'script.js', '-c', 'custom-folder/.versionrc.json'];
104+
105+
await exec(['-c', 'custom-folder/.versionrc.json']);
106+
const content = fs.readFileSync('CHANGELOG.md', 'utf-8');
107+
expect(content).toContain(issueUrlFormat);
108+
109+
// Restore original process.argv
110+
process.argv = originalArgv;
111+
});
112+
89113
it('evaluates a config-function from .versionrc.js', async function () {
90114
const issueUrlFormat = 'http://www.foo.com/{{id}}';
91115
const src = `module.exports = function() { return ${JSON.stringify({

0 commit comments

Comments
 (0)