Skip to content

Commit

Permalink
feat(new): add commit messages
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-reis committed Jan 4, 2021
1 parent d3fbcf2 commit 1b27972
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 26 deletions.
12 changes: 5 additions & 7 deletions new/collect/add-git-status.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import simpleGit, { SimpleGit } from 'simple-git';
import { sortSemver } from '../helpers/sort-semver';
import { Module } from '../types';
import { Env, Module } from '../types';

/**
* add git information to env by using simple-git package
*/
export const addGitStatus: Module = async (env) => {
export const addGitStatus: Module = async (env: Env) => {
const git: SimpleGit = simpleGit();
const {
current: currentBranch,
files: uncommittedFiles,
} = await git.status();
const { current: currentBranch, files } = await git.status();

const uncommittedFiles = files.map((f) => f.path);
const currentHash = (await git.log()).latest?.hash;

const allTags = (await git.tags()).all.reverse();
Expand All @@ -37,5 +35,5 @@ export const addGitStatus: Module = async (env) => {
allTags,
tagsInTree,
currentHash,
};
} as Env;
};
4 changes: 2 additions & 2 deletions new/collect/add-last-release-hash.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { yellow } from 'chalk';
import { logger } from '../helpers/logger';
import { Module } from '../types';
import { Env, Module } from '../types';

const { log } = logger('prev hash');

Expand All @@ -18,7 +18,7 @@ export const findLastProductionTag = (tags: string[]) => {
/**
* Determin the git commit hash from where the last release has happened
*/
export const addLastReleaseHash: Module = async (env) => {
export const addLastReleaseHash: Module = async (env: Env) => {
// we usually write the last hash into the rlsr.json file
let lastReleaseHash: string = env.status?.lastReleaseHash ?? '';

Expand Down
28 changes: 28 additions & 0 deletions new/collect/add-raw-commit-messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { pick } from 'lodash/fp';
import { yellow } from 'chalk';
import simpleGit, { SimpleGit } from 'simple-git';
import { logger } from '../helpers/logger';
import { Module, Env } from '../types';

const { log } = logger('git messages');

/**
* Get all commit messages between the last hash and now
*/
export const addRawCommitMessages: Module = async (env: Env) => {
const git: SimpleGit = simpleGit();

let data = await git.log({
from: `${env.lastReleaseHash}^1`,
to: env.currentHash,
});

// parsing and enriching
const commitMessages = data.all.map(
pick(['hash', 'date', 'message', 'body'])
);

log(`${yellow(commitMessages.length)} overall affected commits`);

return { ...env, commitMessages } as Env;
};
2 changes: 1 addition & 1 deletion new/collect/check-git-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export enum exitCodes {
/**
* checks if user is on the right branch and has everything committed
*/
export const checkGitStatus: Module = async (env) => {
export const checkGitStatus: Module = async (env: Env) => {
// exit if there are still files to commit (unless in verify mode)
if (!isVerify(env) && (env.uncommittedFiles?.length ?? 0) > 0) {
error(
Expand Down
4 changes: 2 additions & 2 deletions new/collect/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Config, Module } from '../types';
import { Config, Env, Module } from '../types';
import { cosmiconfigSync } from 'cosmiconfig';
import { setDebug } from '../helpers/logger';

Expand All @@ -21,7 +21,7 @@ export const defaultConfig: Config = {
* Reads the current configuration with cosmic conf
* `.rlsrrc`, package.json > rlsr and other formats allowed
*/
export const config: Module = (env) => {
export const config: Module = (env: Env) => {
const cosmic = cosmiconfigSync('rlsr').search();

const config: Config = {
Expand Down
13 changes: 5 additions & 8 deletions new/collect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { addGitStatus } from './add-git-status';
import { checkGitStatus } from './check-git-status';
import { readStatusFile } from './read-status-file';
import { addLastReleaseHash } from './add-last-release-hash';
import { addRawCommitMessages } from './add-raw-commit-messages';

export const collect = composeAsync(
log('PHASE 1: collecting data'),
Expand All @@ -26,9 +27,6 @@ export const collect = composeAsync(
// add the top level package.json to the environment for later use
mainPackage,

// add the rlsr.json to the environment if available. If not, it creates one
// rlsrFile

// prints some useful status messages on which more it is running in
startReport,

Expand All @@ -43,13 +41,12 @@ export const collect = composeAsync(
addLastReleaseHash,

// retrieve all commit messages since the last hash
// getParsedCommitMessages,
addRawCommitMessages,
//parseCommitMessages,
//addFilesToCommitMessages,

//read all package jsons and add them to the env for later use
// getAllPackageJsons,
// addAllPackageJsons,

// read commit messages since last release or since tag or since beginning
// read all package.jsons
//
wait(1000)
);
4 changes: 2 additions & 2 deletions new/collect/main-package.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Module } from '../types';
import { Env, Module } from '../types';
import { CoreProperties as PackageJson } from '@schemastore/package';
import { join } from 'path';

/**
* Reads the top level package.json
*/
export const mainPackage: Module = (env) => {
export const mainPackage: Module = (env: Env) => {
const pkg: PackageJson = require(join(env.appRoot, 'package.json'));

return { ...env, pkg };
Expand Down
4 changes: 2 additions & 2 deletions new/collect/read-status-file.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Module, Status } from '../types';
import { Env, Module, Status } from '../types';
import { join } from 'path';

/**
* Reads the top level rlsr.json and adds the data to env
*/
export const readStatusFile: Module = async (env) => {
export const readStatusFile: Module = async (env: Env) => {
let status: Status;
try {
status = require(join(env.appRoot, 'rlsr.json'));
Expand Down
4 changes: 2 additions & 2 deletions new/collect/start-report.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { bold, yellow } from 'chalk';
import { logger } from '../helpers/logger';
import { Module } from '../types';
import { Env, Module } from '../types';

const { log, debug } = logger('report');

/**
* Prints initial status information
*/
export const startReport: Module = (env) => {
export const startReport: Module = (env: Env) => {
log(`Running in stage ${bold(yellow(env.stage))}`);
env.verify && log(`verifying status only!`);
!env.verify && env.dryrun && log(`dryrun only!`);
Expand Down
9 changes: 9 additions & 0 deletions new/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ export type Status = {
versions: Record<string, string>;
};

export type Message = {
hash: string;
date: string;
message: string;
body: string;
};

export type Env = {
/** The stage as demanded by the command line - canary, beta or production */
stage: Stage;
Expand Down Expand Up @@ -77,6 +84,8 @@ export type Env = {
lastReleaseHash?: string;
/** previous rlsr.json available? */
hasStatusFile?: boolean;
/** affected messages */
commitMessages?: Message[];
};

export type Module = (env: Env) => Promise<Env> | Env;

0 comments on commit 1b27972

Please sign in to comment.