Skip to content

Commit

Permalink
Promisify GitHub Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Sep 25, 2020
1 parent d5748de commit c04e366
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 92 deletions.
8 changes: 1 addition & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,9 @@
"ava/no-statement-after-end": "off",
"eqeqeq": "off",
"no-useless-call": "off",
"no-var": "off",
"prefer-rest-params": "off",
"prefer-spread": "off",
"promise/always-return": "off",
"promise/catch-or-return": "off",
"promise/no-callback-in-promise": "off",
"promise/prefer-await-to-then": "off",
"unicorn/explicit-length-check": "off",
"unicorn/prevent-abbreviations": "off"
"unicorn/explicit-length-check": "off"
}
}
}
59 changes: 27 additions & 32 deletions src/github-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,29 @@ module.exports = (token) => {

return (...arguments_) =>
new Promise((resolve, reject) => {
q.push((callback) => {
q.push(async (callback) => {
let argumentsCallback = arguments_.pop();
if (typeof argumentsCallback !== 'function') {
arguments_.push(argumentsCallback);
argumentsCallback = null;
}

call.apply(null, arguments_).then(
(result) => {
callback();
if (argumentsCallback) {
argumentsCallback(null, result);
}

resolve(result);
},
(error) => {
callback();
if (argumentsCallback) {
argumentsCallback(error);
}

reject(error);
try {
const result = await call(...arguments_);
callback();
if (argumentsCallback) {
argumentsCallback(null, result);
}
);

resolve(result);
} catch (error) {
callback();
if (argumentsCallback) {
argumentsCallback(error);
}

reject(error);
}
});
q.start();
});
Expand All @@ -66,28 +64,25 @@ module.exports = (token) => {
throw new TypeError(`call should be a function: ${call}`);
}

return (...arguments_) => {
return async (...arguments_) => {
let argumentsCallback = arguments_.pop();
if (typeof argumentsCallback !== 'function') {
arguments_.push(argumentsCallback);
argumentsCallback = null;
}

return call.apply(null, arguments_).then(
(result) => {
result = transform(result);
if (argumentsCallback) {
argumentsCallback(null, result);
}
try {
const result = transform(await call(...arguments_));
if (argumentsCallback) {
argumentsCallback(null, result);
}

return result;
},
(error) => {
if (argumentsCallback) {
argumentsCallback(error);
}
return result;
} catch (error) {
if (argumentsCallback) {
argumentsCallback(error);
}
);
}
};
};

Expand Down
101 changes: 48 additions & 53 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const api = (token, options, callback) => {
});
};

api.get = (token, options, getCallback) => {
api.get = async (token, options, getCallback) => {
if (!getCallback) {
getCallback = options;
options = {};
Expand All @@ -32,66 +32,61 @@ api.get = (token, options, getCallback) => {
const github = githubFactory(token);

// Get all repositories
github.repos
.list({type: 'public'})
.then((repos) => {
// Keep only forks
let forks = repos.filter(({fork}) => fork);

// Keep only forks owned by a specified user
if (options.user) {
forks = forks.filter(({owner}) => owner.login === options.user);
}
try {
const repos = await github.repos.list({type: 'public'});
// Keep only forks
let forks = repos.filter(({fork}) => fork);

// Keep only forks owned by a specified user
if (options.user) {
forks = forks.filter(({owner}) => owner.login === options.user);
}

let countDoneForks = 0;
let countDoneForks = 0;
options.progress({
countInspected: 0,
totalToInspect: forks.length
});
const forkDone = (fork) => {
countDoneForks += 1;
options.progress({
countInspected: 0,
countInspected: countDoneForks,
lastInspected: fork.name,
totalToInspect: forks.length
});
const forkDone = (fork) => {
countDoneForks += 1;
options.progress({
countInspected: countDoneForks,
lastInspected: fork.name,
totalToInspect: forks.length
});
};

// Keep only useless forks
async.filter(
forks,
(fork, filterCallback) => {
shouldDeleteFork(github, fork, (error, result) => {
if (error) {
options.warnings(
`Failed to inspect ${fork.name}, skipping`,
error
);
result = false;
}

forkDone(fork);
filterCallback(null, result);
});
},
(error, forksToDelete) => {
};

// Keep only useless forks
async.filter(
forks,
(fork, filterCallback) => {
shouldDeleteFork(github, fork, (error, result) => {
if (error) {
return getCallback(error);
options.warnings(`Failed to inspect ${fork.name}, skipping`, error);
result = false;
}

// Map to our simple objects
const response = forksToDelete.map((fork) => ({
owner: fork.owner.login,
repo: fork.name,
url: fork.html_url
}));
getCallback(null, response);
forkDone(fork);
filterCallback(null, result);
});
},
(error, forksToDelete) => {
if (error) {
return getCallback(error);
}
);
})
.then(null, (error) => {
getCallback(error);
});

// Map to our simple objects
const response = forksToDelete.map((fork) => ({
owner: fork.owner.login,
repo: fork.name,
url: fork.html_url
}));
getCallback(null, response);
}
);
} catch (error) {
getCallback(error);
}
};

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

0 comments on commit c04e366

Please sign in to comment.