Skip to content

Commit

Permalink
Merge pull request #120 from rkotze/migrate-mob-print
Browse files Browse the repository at this point in the history
Migrate mob print to TypeScript and use Git Mob Core
  • Loading branch information
rkotze committed Nov 19, 2023
2 parents 76a9aa9 + acbd4b1 commit 8d52194
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 70 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Follows [Semantic Versioning](https://semver.org/).
- Remove legacy git-add-coauthor API and replace it with git-mob-core `saveNewCoAuthors`
- Remove legacy git-suggest-coauthor API and replace it with git-mob-core `repoAuthorList`
- Migrated `git-suggest-coauthors` to TypeScript
- `git mob-print -i` uses git mob core to print out initials of selected co-authors
- Migrated mob print file to TypeScript

## git-mob-core next

Expand Down
11 changes: 0 additions & 11 deletions packages/git-mob-core/src/git-mob-api/git-authors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,6 @@ export function gitAuthors(readFilePromise, writeFilePromise, overwriteFilePromi
return coauthors[authorInitials];
},

coAuthorsInitials(authorJson, currentCoAuthors) {
const { coauthors } = authorJson;
return Object.keys(coauthors).reduce((currentCoAuthorsInitials, initials) => {
if (currentCoAuthors.includes(author(coauthors[initials]))) {
currentCoAuthorsInitials.push(initials);
}

return currentCoAuthorsInitials;
}, []);
},

toList(authors) {
const entries = Object.entries(authors.coauthors);
return entries.map(([key, { name, email }]) => new Author(key, name, email));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,3 @@ test('Throw error if initials of author are not found', () => {
})
);
});

test('find initials of co-authors', () => {
const authors = gitAuthors();
const coAuthorsInitials = authors.coAuthorsInitials(authorsJson, [
'Jane Doe <jane@findmypast.com>',
]);

expect(coAuthorsInitials).toEqual(['jd']);
});
1 change: 0 additions & 1 deletion packages/git-mob-core/src/test-helpers/author-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export function mockGitAuthors(keys: string[]) {
fileExists: jest.fn(() => true),
coAuthors: jest.fn(() => []),
author: jest.fn(() => ({})),
coAuthorsInitials: jest.fn(() => []),
toList: jest.fn(() => authors),
};
}
12 changes: 0 additions & 12 deletions packages/git-mob/src/git-authors/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { readFile as _readFile, appendFile, writeFile, existsSync } from 'node:fs';
import { promisify } from 'node:util';
import { pathToCoAuthors } from 'git-mob-core';
import { authorBaseFormat } from './author-base-format.js';

export function gitAuthors(readFilePromise, writeFilePromise, overwriteFilePromise) {
async function readFile(path) {
Expand Down Expand Up @@ -73,17 +72,6 @@ export function gitAuthors(readFilePromise, writeFilePromise, overwriteFilePromi
return coauthors[authorInitials];
},

coAuthorsInitials(authorJson, currentCoAuthors) {
const { coauthors } = authorJson;
return Object.keys(coauthors).reduce((currentCoAuthorsInitials, initials) => {
if (currentCoAuthors.includes(authorBaseFormat(coauthors[initials]))) {
currentCoAuthorsInitials.push(initials);
}

return currentCoAuthorsInitials;
}, []);
},

toList(authors) {
const entries = Object.entries(authors.coauthors);
return entries.map(authorParts => {
Expand Down
9 changes: 0 additions & 9 deletions packages/git-mob/src/git-authors/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,3 @@ test('create an organised string list of .git-coauthors', async t => {
];
t.deepEqual(expectAuthorList, authorList);
});

test('find initials of co-authors', t => {
const authors = gitAuthors();
const coAuthorsInitials = authors.coAuthorsInitials(authorsJson, [
'Jane Doe <jane@findmypast.com>',
]);

t.deepEqual(coAuthorsInitials, ['jd']);
});
45 changes: 45 additions & 0 deletions packages/git-mob/src/git-mob-print.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import test from 'ava';
import {
addAuthor,
addCoAuthor,
removeCoAuthors,
safelyRemoveGitConfigSection,
deleteGitMessageFile,
exec,
setCoauthorsFile,
deleteCoauthorsFile,
setup,
tearDown,
unsetCommitTemplate,
} from '../test-helpers/index.js';

const { before, after, afterEach } = test;

before('setup', () => {
setup();
setCoauthorsFile();
});

after.always('final cleanup', () => {
deleteCoauthorsFile();
deleteGitMessageFile();
tearDown();
});

afterEach.always('each cleanup', () => {
safelyRemoveGitConfigSection('user');
safelyRemoveGitConfigSection('git-mob');
safelyRemoveGitConfigSection('commit');
});

test('Print a list of selected author keys', t => {
addAuthor('John Doe', 'jdoe@example.com');
addCoAuthor('Jane Doe', 'jane@findmypast.com');
addCoAuthor('Elliot Alderson', 'ealderson@findmypast.com>');
const { stdout } = exec('git mob-print -i');

t.regex(stdout, /jd,ea/);

unsetCommitTemplate();
removeCoAuthors();
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import os from 'node:os';
import minimist from 'minimist';
import { getAllAuthors, getSelectedCoAuthors } from 'git-mob-core';
import { gitAuthors } from './git-authors/index.js';
import { runMobPrintHelp } from './helpers.js';
import { getCoAuthors } from './git-mob-commands.js';

const argv = minimist(process.argv.slice(2), {
alias: {
Expand All @@ -14,15 +12,14 @@ const argv = minimist(process.argv.slice(2), {

await execute(argv);

async function execute(args) {
async function execute(args: minimist.ParsedArgs) {
if (args.help) {
runMobPrintHelp();
process.exit(0);
}

if (args.initials) {
await printCoAuthorsInitials();
process.exit(0);
return printCoAuthorsInitials();
}

return printCoAuthors();
Expand All @@ -34,27 +31,24 @@ async function printCoAuthors() {
const selectedAuthors = getSelectedCoAuthors(allAuthors);
const coAuthors = selectedAuthors.map(author => author.format()).join(os.EOL);
console.log(os.EOL + os.EOL + coAuthors);
} catch (error) {
console.error(`Error: ${error.message}`);
} catch (error: unknown) {
const printError = error as Error;
console.error(`Error: ${printError.message}`);
process.exit(1);
}
}

async function printCoAuthorsInitials() {
try {
const instance = gitAuthors();
const authorList = await instance.read();
const currentCoAuthors = getCoAuthors();

const coAuthorsInitials = instance.coAuthorsInitials(
authorList,
currentCoAuthors
);
if (coAuthorsInitials.length > 0) {
console.log(coAuthorsInitials.join(','));
const allAuthors = await getAllAuthors();
const selectedAuthors = getSelectedCoAuthors(allAuthors);

if (selectedAuthors.length > 0) {
console.log(selectedAuthors.map(author => author.key).join(','));
}
} catch (error) {
console.error(`Error: ${error.message}`);
} catch (error: unknown) {
const initialsError = error as Error;
console.error(`Error: ${initialsError.message}`);
process.exit(1);
}
}
9 changes: 0 additions & 9 deletions packages/git-mob/test-helpers/env.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ const { spawnSync } = require('child_process');
const path = require('path');

const testHelperPath = path.join(process.cwd(), '/test-helpers');
// module.exports = {
// GITMOB_COAUTHORS_PATH: path.join(testHelperPath, '.git-coauthors'),
// GITMOB_MESSAGE_PATH: path.join(testHelperPath, '.gitmessage'),
// GITMOB_GLOBAL_MESSAGE_PATH: path.join(testHelperPath, '.gitglobalmessage'),
// NO_UPDATE_NOTIFIER: true,
// HOME: testHelperPath,
// GITMOB_TEST_ENV_FOLDER: './test-env',
// GITMOB_TEST_HELPER_FOLDER: './test-helpers',
// };

process.env.GITMOB_COAUTHORS_PATH = path.join(testHelperPath, '.git-coauthors');
process.env.GITMOB_MESSAGE_PATH = path.join(testHelperPath, '.gitmessage');
Expand Down

0 comments on commit 8d52194

Please sign in to comment.