Skip to content

Commit f056300

Browse files
committed
Allow a list of users
1 parent 2714b91 commit f056300

File tree

4 files changed

+48
-20
lines changed

4 files changed

+48
-20
lines changed

index.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
#!/usr/bin/env node
22
const program = require('commander');
33
const { merge } = require('rxjs/observable/merge');
4-
const { switchMap, partition } = require('rxjs/operators');
4+
const { from } = require('rxjs/observable/from');
5+
const {
6+
tap,
7+
switchMap,
8+
reduce,
9+
mergeMap,
10+
partition
11+
} = require('rxjs/operators');
512

613
const { VERSION } = require('./src/constants');
714
const commit = require('./src/commit');
@@ -17,32 +24,40 @@ program
1724
.option('-a, --all', 'commit all changed files')
1825
.option(
1926
'-f, --force',
20-
'ignore cached commit templates, look up user again on githup'
27+
'ignore cached commit templates, look up user again on github'
2128
)
22-
.arguments('<github-username>')
29+
.arguments('<github-usernames...>')
2330
.description(
2431
'Searches Github for the user and auto generates a co-authored tag for your commit message'
2532
)
2633
.parse(process.argv);
2734

28-
const username = program.args[0];
35+
const usernames = program.args;
2936
const args = [program.all && '-a'].filter(v => v);
3037
const cwd = process.cwd();
3138

32-
if (!username) {
39+
if (!usernames.length > 0) {
3340
program.help();
3441
process.exit(1);
3542
}
3643

37-
const [filepath$, noFilepath$] = getCommitTemplateFor(username).pipe(
44+
const [filepath$, noFilepath$] = getCommitTemplateFor(usernames).pipe(
3845
partition(filepath => filepath && !program.force)
3946
);
4047

4148
merge(
4249
filepath$,
4350
noFilepath$.pipe(
4451
switchMap(filepath =>
45-
search(username).pipe(switchMap(createCommitTemplate))
52+
from(usernames).pipe(
53+
mergeMap(search),
54+
reduce(
55+
(users, user) => Object.assign(users, { [user.login]: user }),
56+
{}
57+
),
58+
tap(us => console.log({ us })),
59+
switchMap(createCommitTemplate)
60+
)
4661
)
4762
)
4863
)

src/create-commit-template.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,38 @@ const access = bindNodeCallback(fs.access);
1111
const mkdir = bindNodeCallback(fs.mkdir);
1212
const writeFile = bindNodeCallback(fs.writeFile);
1313

14-
function getTemplatePath(login) {
15-
return path.join(TEMPLATE_ROOT, `.${login}`);
14+
function getTemplatePath(logins) {
15+
return path.join(TEMPLATE_ROOT, `.${logins.join('-')}`);
1616
}
1717

18-
function coauthor({ name, email }) {
18+
function coauthor(users) {
1919
return dedent`
2020
# Commiting with commit-with 🤗
2121
22-
Co-Authored-By: ${name} <${email}>
22+
${users
23+
.map(({ name, email }) => `Co-Authored-By: ${name} <${email}>`)
24+
.join('\n')}
2325
2426
# ${Date.now()}-${VERSION}
2527
`;
2628
}
2729

2830
module.exports = {
29-
createCommitTemplate({ login, name, email }) {
31+
createCommitTemplate(users) {
32+
console.log({ users });
33+
3034
return access(TEMPLATE_ROOT).pipe(
3135
catchError(() => mkdir(TEMPLATE_ROOT)),
3236
switchMap(() => {
33-
const p = getTemplatePath(login);
37+
const logins = Object.keys(users);
38+
const p = getTemplatePath(logins);
3439

35-
return writeFile(p, coauthor({ name, email })).pipe(mapTo(p));
40+
return writeFile(p, coauthor(Object.values(users))).pipe(mapTo(p));
3641
})
3742
);
3843
},
39-
getCommitTemplateFor(login) {
40-
const p = getTemplatePath(login);
44+
getCommitTemplateFor(logins) {
45+
const p = getTemplatePath(logins);
4146

4247
return access(p).pipe(map(() => p), catchError(() => of(false)));
4348
}

src/helpers/get.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const url = require('url');
44
const { merge } = require('rxjs/observable/merge');
55
const { fromEvent } = require('rxjs/observable/fromEvent');
66
const { bindCallback } = require('rxjs/observable/bindCallback');
7-
const { map, switchMap, tap } = require('rxjs/operators');
7+
const { take, map, switchMap, tap } = require('rxjs/operators');
88

99
const bufferToJson = require('./buffer-to-json');
1010

@@ -33,7 +33,7 @@ module.exports = function get(uri) {
3333
merge(
3434
fromEvent(r, 'end').pipe(map(() => buffer), bufferToJson),
3535
fromEvent(r, 'error')
36-
)
36+
).pipe(take(1))
3737
)
3838
);
3939
};

src/search.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
const https = require('https');
2-
const { switchMap, zip, map } = require('rxjs/operators');
2+
const { tap, switchMap, zip, map } = require('rxjs/operators');
33

44
const { GITHUB_URL } = require('./constants');
55
const get = require('./helpers/get');
66
const getEmail = require('./get-email');
77

8+
function findMatchingUser(username, users) {
9+
const filteredUsers = users.filter(({ login }) => login === username);
10+
11+
return filteredUsers && filteredUsers.length > 0 ? filteredUsers[0] : {};
12+
}
13+
814
module.exports = function search(username) {
915
return get(
1016
`${GITHUB_URL}/search/users?q=${username}+${encodeURIComponent(
1117
'in:login'
1218
)}&type=Users`
1319
).pipe(
1420
switchMap(({ total_count, items }) =>
15-
getEmail(username).pipe(zip((total_count > 0 && [items[0]]) || [{}]))
21+
getEmail(username).pipe(
22+
zip((total_count > 0 && [findMatchingUser(username, items)]) || [{}])
23+
)
1624
),
1725
switchMap(
1826
([email, { url }]) =>

0 commit comments

Comments
 (0)