Skip to content

feat: add --config option to allow custom config file path #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ _Having problems? Want to contribute? Join us on the [node-tooling community Sla
- [Release as a Pre-Release](#release-as-a-pre-release)
- [Release as a Target Type Imperatively (`npm version`-like)](#release-as-a-target-type-imperatively-npm-version-like)
- [Prevent Git Hooks](#prevent-git-hooks)
- [Custom Config Path](#custom-config-path)
- [Signing Commits and Tags](#signing-commits-and-tags)
- [Lifecycle Scripts](#lifecycle-scripts)
- [Skipping Lifecycle Steps](#skipping-lifecycle-steps)
Expand Down Expand Up @@ -312,6 +313,23 @@ npm run release -- --no-verify
commit-and-tag-version --no-verify
```

### Custom Config Path

Specify a custom path to the configuration file using the `--config` option

```sh
commit-and-tag-version --config ./path/to/.versionrc.js

# or using alias
commit-and-tag-version -c ./path/to/.versionrc.js
```

Supports all of the existing config extensions

```sh
commit-and-tag-version -c ./path/to/.versionrc[.js|.cjs|.json]
```

### Signing Commits and Tags

If you have your GPG key set up, add the `--sign` or `-s` flag to your `commit-and-tag-version` command.
Expand Down
9 changes: 8 additions & 1 deletion command.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ const yargs = require('yargs')
default: defaults.npmPublishHint,
describe: 'Customized publishing hint',
})
.option('config', {
type: 'string',
default: defaults.config,
alias: 'c',
describe: 'Path to a custom configuration file',
})
.check((argv) => {
if (typeof argv.scripts !== 'object' || Array.isArray(argv.scripts)) {
throw Error('scripts must be an object');
Expand All @@ -150,9 +156,10 @@ const yargs = require('yargs')
)
.pkgConf('standard-version')
.pkgConf('commit-and-tag-version')
.config(getConfiguration())
.wrap(97);

yargs.config(getConfiguration(yargs.argv));

Object.keys(spec.properties).forEach((propertyKey) => {
const property = spec.properties[propertyKey];
yargs.option(propertyKey, {
Expand Down
1 change: 1 addition & 0 deletions defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const defaults = {
gitTagFallback: true,
preset: require.resolve('conventional-changelog-conventionalcommits'),
npmPublishHint: undefined,
config: undefined,
};

/**
Expand Down
8 changes: 6 additions & 2 deletions lib/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ const CONFIGURATION_FILES = [
'.versionrc.js',
];

module.exports.getConfiguration = function () {
module.exports.getConfiguration = function (argv) {
let config = {};
const configPath = findUp.sync(CONFIGURATION_FILES);

// If the user has provided a configuration file via the `--config` argument, we use that.
const configurationFiles = argv.config ?? CONFIGURATION_FILES;

const configPath = findUp.sync(configurationFiles);
if (!configPath) {
return config;
}
Expand Down
24 changes: 24 additions & 0 deletions test/config-files.integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ describe('config files', function () {
expect(content).toContain(issueUrlFormat);
});

it('reads config from custom path', async function () {
const issueUrlFormat = 'http://www.foo.com/{{id}}';
const changelog = ({ preset }) => preset.issueUrlFormat;
mock({ bump: 'minor', changelog });
fs.mkdirSync('custom-folder');
fs.writeFileSync(
'custom-folder/.versionrc.json',
JSON.stringify({ issueUrlFormat }),
'utf-8',
);

// Override process.argv to simulate CLI arguments before `exec`.
// This ensures yargs parses the custom config argument.
const originalArgv = process.argv;
process.argv = ['node', 'script.js', '-c', 'custom-folder/.versionrc.json'];

await exec(['-c', 'custom-folder/.versionrc.json']);
const content = fs.readFileSync('CHANGELOG.md', 'utf-8');
expect(content).toContain(issueUrlFormat);

// Restore original process.argv
process.argv = originalArgv;
});

it('evaluates a config-function from .versionrc.js', async function () {
const issueUrlFormat = 'http://www.foo.com/{{id}}';
const src = `module.exports = function() { return ${JSON.stringify({
Expand Down