Skip to content

Commit

Permalink
build(release): add git functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Feb 8, 2024
1 parent 5bff270 commit 357141b
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 7 deletions.
6 changes: 4 additions & 2 deletions apps/delivery-options/release.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ module.exports = {
addGitHubActionsOutputPlugin(),
addReleaseNotesGeneratorPlugin(),
addChangelogPlugin(),
'@myparcel-do/semantic-release-plugin',
addGitHubPlugin(),
addGitPlugin(),
/*
* Includes npm and git functionality
*/
'@myparcel-do/semantic-release-plugin',
],
};
22 changes: 22 additions & 0 deletions libs/semantic-release-plugin/src/prepare/gitCommit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {executeWithErrorHandling, hasErrors, throwIfHasErrors} from '../utils';
import {type ContextWithNextRelease} from '../types';

export const gitCommit = async (context: ContextWithNextRelease): Promise<void> => {
const {env, cwd, nextRelease} = context;

await executeWithErrorHandling(
'git',
['commit', '-am', `chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}`],
{
cwd,
env,
stdio: 'inherit',
},
);

if (!hasErrors()) {
await executeWithErrorHandling('git', ['push', '--tags'], {env, cwd});
}

throwIfHasErrors();
};
8 changes: 8 additions & 0 deletions libs/semantic-release-plugin/src/prepare/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {type PrepareCmd} from '../types';
import {updatePackageJsonVersions} from './updatePackageJsonVersions';
import {gitCommit} from './gitCommit';

export const prepare: PrepareCmd = async (_, context) => {
await updatePackageJsonVersions(context);
await gitCommit(context);
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {getPackageJson, execute, addError, throwIfHasErrors} from './utils';
import {type PrepareCmd} from './types';
import {getPackageJson, execute, addError, throwIfHasErrors} from '../utils';
import {type ContextWithNextRelease} from '../types';

export const prepare: PrepareCmd = async (_, context) => {
export const updatePackageJsonVersions = async (context: ContextWithNextRelease): Promise<void> => {
const {cwd, env, logger, nextRelease} = context;
const pkg = await getPackageJson(context);

Expand Down
1 change: 1 addition & 0 deletions libs/semantic-release-plugin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ContextWithNextRelease extends Context {
nextRelease: {
version: string;
channel: string;
notes: string;
};
}

Expand Down
14 changes: 14 additions & 0 deletions libs/semantic-release-plugin/src/utils/executeWithErrorHandling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {type Options} from 'execa';
import {execute} from './execute';
import {addError} from './errorHandling';

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const executeWithErrorHandling = async (command: string, args: string[], options?: Options) => {
const result = await execute(command, args, options);

if (result.stderr) {
addError(new Error(`Command "${command} ${args.join(' ')}" failed: ${result.stderr}`));
}

return result;
};
1 change: 1 addition & 0 deletions libs/semantic-release-plugin/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './errorHandling';
export * from './execute';
export * from './executeWithErrorHandling';
export * from './getPackageJson';
10 changes: 8 additions & 2 deletions libs/semantic-release-plugin/src/verifyConditions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import {addError, throwIfHasErrors} from './utils';
import {addError, throwIfHasErrors, execute} from './utils';
import {type VerifyConditionsCmd} from './types';

export const verifyConditions: VerifyConditionsCmd = (_, {env}) => {
export const verifyConditions: VerifyConditionsCmd = async (_, {env}) => {
if (!env.NPM_TOKEN) {
addError(new Error('NPM_TOKEN environment variable is required'));
}

const result = await execute('git', ['push', 'origin', '--dry-run'], {env, stdio: 'ignore'});

if (result.stderr) {
addError(new Error(result.stderr));
}

throwIfHasErrors();
};

0 comments on commit 357141b

Please sign in to comment.