Skip to content

Commit c04e366

Browse files
committed
Promisify GitHub Factory
1 parent d5748de commit c04e366

File tree

3 files changed

+76
-92
lines changed

3 files changed

+76
-92
lines changed

package.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,9 @@
5555
"ava/no-statement-after-end": "off",
5656
"eqeqeq": "off",
5757
"no-useless-call": "off",
58-
"no-var": "off",
5958
"prefer-rest-params": "off",
6059
"prefer-spread": "off",
61-
"promise/always-return": "off",
62-
"promise/catch-or-return": "off",
63-
"promise/no-callback-in-promise": "off",
64-
"promise/prefer-await-to-then": "off",
65-
"unicorn/explicit-length-check": "off",
66-
"unicorn/prevent-abbreviations": "off"
60+
"unicorn/explicit-length-check": "off"
6761
}
6862
}
6963
}

src/github-factory.js

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,29 @@ module.exports = (token) => {
3131

3232
return (...arguments_) =>
3333
new Promise((resolve, reject) => {
34-
q.push((callback) => {
34+
q.push(async (callback) => {
3535
let argumentsCallback = arguments_.pop();
3636
if (typeof argumentsCallback !== 'function') {
3737
arguments_.push(argumentsCallback);
3838
argumentsCallback = null;
3939
}
4040

41-
call.apply(null, arguments_).then(
42-
(result) => {
43-
callback();
44-
if (argumentsCallback) {
45-
argumentsCallback(null, result);
46-
}
47-
48-
resolve(result);
49-
},
50-
(error) => {
51-
callback();
52-
if (argumentsCallback) {
53-
argumentsCallback(error);
54-
}
55-
56-
reject(error);
41+
try {
42+
const result = await call(...arguments_);
43+
callback();
44+
if (argumentsCallback) {
45+
argumentsCallback(null, result);
5746
}
58-
);
47+
48+
resolve(result);
49+
} catch (error) {
50+
callback();
51+
if (argumentsCallback) {
52+
argumentsCallback(error);
53+
}
54+
55+
reject(error);
56+
}
5957
});
6058
q.start();
6159
});
@@ -66,28 +64,25 @@ module.exports = (token) => {
6664
throw new TypeError(`call should be a function: ${call}`);
6765
}
6866

69-
return (...arguments_) => {
67+
return async (...arguments_) => {
7068
let argumentsCallback = arguments_.pop();
7169
if (typeof argumentsCallback !== 'function') {
7270
arguments_.push(argumentsCallback);
7371
argumentsCallback = null;
7472
}
7573

76-
return call.apply(null, arguments_).then(
77-
(result) => {
78-
result = transform(result);
79-
if (argumentsCallback) {
80-
argumentsCallback(null, result);
81-
}
74+
try {
75+
const result = transform(await call(...arguments_));
76+
if (argumentsCallback) {
77+
argumentsCallback(null, result);
78+
}
8279

83-
return result;
84-
},
85-
(error) => {
86-
if (argumentsCallback) {
87-
argumentsCallback(error);
88-
}
80+
return result;
81+
} catch (error) {
82+
if (argumentsCallback) {
83+
argumentsCallback(error);
8984
}
90-
);
85+
}
9186
};
9287
};
9388

src/index.js

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const api = (token, options, callback) => {
2020
});
2121
};
2222

23-
api.get = (token, options, getCallback) => {
23+
api.get = async (token, options, getCallback) => {
2424
if (!getCallback) {
2525
getCallback = options;
2626
options = {};
@@ -32,66 +32,61 @@ api.get = (token, options, getCallback) => {
3232
const github = githubFactory(token);
3333

3434
// Get all repositories
35-
github.repos
36-
.list({type: 'public'})
37-
.then((repos) => {
38-
// Keep only forks
39-
let forks = repos.filter(({fork}) => fork);
40-
41-
// Keep only forks owned by a specified user
42-
if (options.user) {
43-
forks = forks.filter(({owner}) => owner.login === options.user);
44-
}
35+
try {
36+
const repos = await github.repos.list({type: 'public'});
37+
// Keep only forks
38+
let forks = repos.filter(({fork}) => fork);
39+
40+
// Keep only forks owned by a specified user
41+
if (options.user) {
42+
forks = forks.filter(({owner}) => owner.login === options.user);
43+
}
4544

46-
let countDoneForks = 0;
45+
let countDoneForks = 0;
46+
options.progress({
47+
countInspected: 0,
48+
totalToInspect: forks.length
49+
});
50+
const forkDone = (fork) => {
51+
countDoneForks += 1;
4752
options.progress({
48-
countInspected: 0,
53+
countInspected: countDoneForks,
54+
lastInspected: fork.name,
4955
totalToInspect: forks.length
5056
});
51-
const forkDone = (fork) => {
52-
countDoneForks += 1;
53-
options.progress({
54-
countInspected: countDoneForks,
55-
lastInspected: fork.name,
56-
totalToInspect: forks.length
57-
});
58-
};
59-
60-
// Keep only useless forks
61-
async.filter(
62-
forks,
63-
(fork, filterCallback) => {
64-
shouldDeleteFork(github, fork, (error, result) => {
65-
if (error) {
66-
options.warnings(
67-
`Failed to inspect ${fork.name}, skipping`,
68-
error
69-
);
70-
result = false;
71-
}
72-
73-
forkDone(fork);
74-
filterCallback(null, result);
75-
});
76-
},
77-
(error, forksToDelete) => {
57+
};
58+
59+
// Keep only useless forks
60+
async.filter(
61+
forks,
62+
(fork, filterCallback) => {
63+
shouldDeleteFork(github, fork, (error, result) => {
7864
if (error) {
79-
return getCallback(error);
65+
options.warnings(`Failed to inspect ${fork.name}, skipping`, error);
66+
result = false;
8067
}
8168

82-
// Map to our simple objects
83-
const response = forksToDelete.map((fork) => ({
84-
owner: fork.owner.login,
85-
repo: fork.name,
86-
url: fork.html_url
87-
}));
88-
getCallback(null, response);
69+
forkDone(fork);
70+
filterCallback(null, result);
71+
});
72+
},
73+
(error, forksToDelete) => {
74+
if (error) {
75+
return getCallback(error);
8976
}
90-
);
91-
})
92-
.then(null, (error) => {
93-
getCallback(error);
94-
});
77+
78+
// Map to our simple objects
79+
const response = forksToDelete.map((fork) => ({
80+
owner: fork.owner.login,
81+
repo: fork.name,
82+
url: fork.html_url
83+
}));
84+
getCallback(null, response);
85+
}
86+
);
87+
} catch (error) {
88+
getCallback(error);
89+
}
9590
};
9691

9792
api.remove = (token, repos, removeCallback) => {

0 commit comments

Comments
 (0)