Skip to content

Commit 965bec4

Browse files
authored
Merge pull request #125 from rkotze/create-coauthor-file
Fix: Create global coauthor file
2 parents 68fdc54 + 784a317 commit 965bec4

File tree

8 files changed

+40
-24
lines changed

8 files changed

+40
-24
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
Follows [Semantic Versioning](https://semver.org/).
44

5+
## git-mob-core 0.9.1
6+
7+
### Fixes
8+
9+
- When creating a new `.git-coauthors` using the `createCoAuthorsFile` it is created only globally by providing internally the global path.
10+
511
## git-mob-core 0.9.0
612

713
### Added

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/git-mob-core/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/git-mob-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "git-mob-core",
3-
"version": "0.9.0",
3+
"version": "0.9.1",
44
"description": "Git Mob Core library to manage co-authoring",
55
"homepage": "https://github.com/rkotze/git-mob/blob/master/packages/git-mob-core/README.md",
66
"main": "./dist/index.js",

packages/git-mob-core/src/git-mob-api/git-authors/create-coauthors-file.spec.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { existsSync } from 'node:fs';
22
import { mockGitAuthors as mockGitAuthorsFn } from '../../test-helpers/author-mocks';
33
import { Author } from '../author';
44
import { createCoAuthorsFile } from './create-coauthors-file';
5-
import { gitAuthors } from '.';
5+
import { gitAuthors, globalGitCoAuthorsPath } from '.';
66

77
jest.mock('node:fs');
88
jest.mock('.');
@@ -23,12 +23,21 @@ test('Throw error if coauthor file exists', async () => {
2323
});
2424

2525
test('Save coauthor file in home directory', async () => {
26+
const defaultAuthor = {
27+
pa: {
28+
name: 'Placeholder Author',
29+
email: 'placeholder@author.org',
30+
},
31+
};
2632
mockExistsSync.mockReturnValueOnce(false);
2733
const mockGitAuthorsObject = mockGitAuthorsFn(['jo', 'hu']);
2834
mockGitAuthors.mockReturnValue(mockGitAuthorsObject);
2935

3036
await expect(createCoAuthorsFile()).resolves.toEqual(true);
31-
expect(mockGitAuthorsObject.overwrite).toHaveBeenCalled();
37+
expect(mockGitAuthorsObject.overwrite).toHaveBeenCalledWith(
38+
{ coauthors: defaultAuthor },
39+
globalGitCoAuthorsPath()
40+
);
3241
});
3342

3443
test('Save coauthor file with defined coauthor list', async () => {
@@ -49,7 +58,10 @@ test('Save coauthor file with defined coauthor list', async () => {
4958
createCoAuthorsFile([new Author('rk', 'rich kid', 'richkid@gmail.com')])
5059
).resolves.toEqual(true);
5160

52-
expect(mockGitAuthorsObject.overwrite).toHaveBeenCalledWith({
53-
coauthors: expectAuthor,
54-
});
61+
expect(mockGitAuthorsObject.overwrite).toHaveBeenCalledWith(
62+
{
63+
coauthors: expectAuthor,
64+
},
65+
globalGitCoAuthorsPath()
66+
);
5567
});

packages/git-mob-core/src/git-mob-api/git-authors/create-coauthors-file.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { gitAuthors, gitCoauthorsFileName, globalGitCoAuthorsPath } from './inde
44

55
const coAuthorSchema = {
66
coauthors: {
7-
hh: {
8-
name: 'Hulk Hogan',
9-
email: 'hulk_hogan22@hotmail.org',
7+
pa: {
8+
name: 'Placeholder Author',
9+
email: 'placeholder@author.org',
1010
},
1111
},
1212
};
1313

14-
export async function createCoAuthorsFile(authorList: Author[]): Promise<boolean> {
14+
export async function createCoAuthorsFile(authorList?: Author[]): Promise<boolean> {
1515
const authorOps = gitAuthors();
1616
const coAuthorFilePath: string = globalGitCoAuthorsPath();
1717
if (existsSync(coAuthorFilePath)) {
@@ -20,9 +20,9 @@ export async function createCoAuthorsFile(authorList: Author[]): Promise<boolean
2020

2121
if (authorList && authorList.length > 0) {
2222
const schema = authorOps.toObject(authorList);
23-
await authorOps.overwrite(schema);
23+
await authorOps.overwrite(schema, coAuthorFilePath);
2424
} else {
25-
await authorOps.overwrite(coAuthorSchema);
25+
await authorOps.overwrite(coAuthorSchema, coAuthorFilePath);
2626
}
2727

2828
return true;

packages/git-mob-core/src/git-mob-api/git-authors/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ export function gitAuthors(
1414
overwriteFilePromise?: () => Promise<void>
1515
) {
1616
return {
17-
read: async () => {
17+
read: async (path?: string) => {
1818
const readPromise = readFilePromise || promisify(fs.readFile);
1919
const authorJsonString = (await readPromise(
20-
await pathToCoAuthors()
20+
path || (await pathToCoAuthors())
2121
)) as string;
2222
return JSON.parse(authorJsonString) as CoAuthorSchema;
2323
},
2424

25-
overwrite: async (authorJson: CoAuthorSchema) => {
25+
overwrite: async (authorJson: CoAuthorSchema, path?: string) => {
2626
const overwritePromise = overwriteFilePromise || promisify(fs.writeFile);
2727
return overwritePromise(
28-
await pathToCoAuthors(),
28+
path || (await pathToCoAuthors()),
2929
JSON.stringify(authorJson, null, 2)
3030
);
3131
},

packages/git-mob-core/src/test-helpers/author-mocks.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ export function mockGitAuthors(keys: string[]) {
2424
return {
2525
read: jest.fn(async () => coAuthors),
2626
overwrite: jest.fn(async () => ''),
27-
coAuthors: jest.fn(() => []),
28-
author: jest.fn(() => ({})),
2927
toList: jest.fn(() => authors),
3028
toObject: jest.fn(() => ({ coauthors: {} })),
3129
};

0 commit comments

Comments
 (0)