Skip to content

Commit

Permalink
feat(semantic-release): Detect provider if set to auto
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Apr 30, 2020
1 parent 952d41e commit df9b216
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
5 changes: 3 additions & 2 deletions packages/spire-plugin-semantic-release/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ yarn add --dev spire-plugin-semantic-release
- `config` \<[string]\> Default [semantic-release] configuration. Defaults to
one of the configs in [`./config/`](./config/) based on the setting of
provider.
- `provider` \<['none'|'github'|'gitlab']\> Provider where the repository is
hosted. Has no effect if `config` is set. Defaults to `none`.
- `provider` \<['auto'|'none'|'github'|'gitlab']\> Provider where the
repository is hosted. Has no effect if `config` is set. `auto` will try to
detect the provider based on the git remote url. Defaults to `auto`.
- `allowCustomConfig` \<[boolean]\> Whether to allow user-provided config. If
this option is `false` and there's custom semantic-release config found it
will throw an error. Defaults to `true`.
Expand Down
43 changes: 27 additions & 16 deletions packages/spire-plugin-semantic-release/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,51 @@ const execa = require('execa');
const SpireError = require('spire/error');

function semanticRelease(
{ setState, getState, hasFile, hasPackageProp },
{ setState, getState, hasFile, hasPackageProp, getProvider },
{
command = 'release',
provider = 'none',
provider = 'auto',
config: defaultSemanticReleaseConfig,
allowCustomConfig = true,
changelogName = 'CHANGELOG.md',
gitAuthorName = undefined,
gitAuthorEmail = undefined,
}
) {
if (!defaultSemanticReleaseConfig) {
const configName = provider === 'none' ? 'default' : provider;
defaultSemanticReleaseConfig = `spire-plugin-semantic-release/config/${configName}`;
}
const hasCustomConfig = async () =>
(await hasFile('release.config.js')) ||
(await hasFile('.releaserc')) ||
(await hasPackageProp('release'));

return {
name: 'spire-plugin-semantic-release',
command,
description: 'run semantic-release',
async setup({ resolve }) {
const hasCustomConfig =
(await hasFile('release.config.js')) ||
(await hasFile('.releaserc')) ||
(await hasPackageProp('release'));
const semanticReleaseConfig =
allowCustomConfig && hasCustomConfig
? []
: ['--extends', resolve(defaultSemanticReleaseConfig)];
let semanticReleaseArgs = [];
let semanticReleaseConfigFile = defaultSemanticReleaseConfig;
const customConfig = hasCustomConfig();

if (!customConfig || !allowCustomConfig) {
if (!semanticReleaseConfigFile) {
let actualProvider = provider;
if (actualProvider === 'auto') {
actualProvider = getProvider();
}
semanticReleaseConfigFile = `spire-plugin-semantic-release/config/${
actualProvider === 'none' ? 'default' : actualProvider
}`;
}

semanticReleaseArgs = ['--extends', resolve(semanticReleaseConfigFile)];
}

setState({
semanticReleaseArgs: [...semanticReleaseConfig],
semanticReleaseArgs,
});

// Inform user about disallowed overrides
if (hasCustomConfig && !allowCustomConfig) {
if (customConfig && !allowCustomConfig) {
throw new SpireError(
`Custom semantic-release config is not allowed, using ${defaultSemanticReleaseConfig} instead`
);
Expand Down
20 changes: 20 additions & 0 deletions packages/spire/lib/create-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
readFile,
writeFile,
} = require('fs-extra');
const execa = require('execa');
const detectIndent = require('detect-indent');

function createCore({ cwd }, { setState, getState }) {
Expand Down Expand Up @@ -33,6 +34,24 @@ function createCore({ cwd }, { setState, getState }) {
const nextContents = { ...currentContents, [prop]: value };
await writeJSON(pkgPath, nextContents, { spaces });
}
async function getProvider() {
const remoteUrl = await execa(
'git',
['remote', 'get-url', '--push', 'origin'],
{
cwd,
}
);

if (remoteUrl.includes('github.com')) {
return 'github';
} else if (remoteUrl.includes('gitlab.com')) {
return 'gitlab';
}

return 'none';
}

return {
setState,
getState,
Expand All @@ -42,6 +61,7 @@ function createCore({ cwd }, { setState, getState }) {
getPackageProp,
hasPackageProp,
setPackageProp,
getProvider,
};
}

Expand Down

0 comments on commit df9b216

Please sign in to comment.