Skip to content

Commit

Permalink
feat: add capability to add extends configuration to release-it
Browse files Browse the repository at this point in the history
  • Loading branch information
juancarlosjr97 committed Dec 8, 2024
1 parent 09d7d1b commit bd9723e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
31 changes: 31 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ const getLocalConfig = ({ file, dir = process.cwd() }) => {
return result && _.isPlainObject(result.config) ? result.config : localConfig;
};

const fetchConfigurationFromGitHub = async pattern => {
const regex = /^github>([^/]+)\/([^#]+)(?::([^#]+))?(?:#(.+))?$/;
const match = pattern.match(regex);

if (!match) {
throw new Error('Invalid Remote Configuration from GitHub');
}

const [, owner, repo, file = '.release-it.json', tag] = match;
const branchOrTag = tag ? `refs/tags/${tag}` : 'HEAD';
const url = `https://raw.githubusercontent.com/${owner}/${repo}/${branchOrTag}/${file}`;

const response = await fetch(url);

if (!response.ok) {
throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
}

return response.json();
};

const getRemoteConfiguration = async configuration => {
return fetchConfigurationFromGitHub(configuration);
};

class Config {
constructor(config = {}) {
this.constructorConfig = config;
Expand Down Expand Up @@ -83,6 +108,10 @@ class Config {
);
}

mergeRemoteOptions(remoteConfiguration) {
return _.merge({}, this.options, remoteConfiguration);
}

getContext(path) {
const context = _.merge({}, this.options, this.contextOptions);
return path ? _.get(context, path) : context;
Expand Down Expand Up @@ -138,4 +167,6 @@ class Config {
}
}

export { getRemoteConfiguration };

export default Config;
8 changes: 7 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'lodash';
import { getPlugins } from './plugin/factory.js';
import Logger from './log.js';
import Config from './config.js';
import Config, { getRemoteConfiguration } from './config.js';
import Shell from './shell.js';
import Prompt from './prompt.js';
import Spinner from './spinner.js';
Expand All @@ -14,6 +14,12 @@ const runTasks = async (opts, di) => {
Object.assign(container, di);
container.config = container.config || new Config(opts);

if ('extends' in container.config.localConfig) {
const remoteConfiguration = await getRemoteConfiguration(container.config.localConfig.extends);

container.config.options = container.config.mergeRemoteOptions(remoteConfiguration);
}

const { config } = container;
const { isCI, isVerbose, verbosityLevel, isDryRun, isChangelog, isReleaseVersion } = config;

Expand Down

0 comments on commit bd9723e

Please sign in to comment.