Skip to content

Commit

Permalink
feat: apply queuefy to plugin methods instead of execa
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed May 20, 2020
1 parent 042933e commit 9ae7d0d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 16 deletions.
10 changes: 6 additions & 4 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ const cli = meow(
$ multi-semantic-release
Options
--execasync, Forces all execa calls to be synchronous
--execasync Forces all execa calls to be synchronous
--queuefy Makes plugins methods work like a single thread with a queue
--watchspawn Turns on logging for process.spawn
Examples
$ multi-semantic-release --execasync --watchspawn
$ multi-semantic-release --execaqueue
$ multi-semantic-release --queuefy
`,
{
flags: {
execasync: {
type: "boolean",
alias: "sync"
},
execaqueue: {
type: "boolean"
queuefy: {
type: "boolean",
alias: "queue"
},
watchspawn: {
type: "boolean"
Expand Down
6 changes: 3 additions & 3 deletions bin/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module.exports = flags => {
}

if (flags.execasync || flags.sync) {
hooks.execa.hook("sync");
} else if (flags.execaqueue) {
hooks.execa.hook("queue");
hooks.execa.hook();
} else if (flags.queuefy || flags.queue) {
hooks.queue.hook();
}

// Imports.
Expand Down
65 changes: 58 additions & 7 deletions lib/hooks.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
/**
* @private
* @type {{hook: hook, unhook: unhook}}
*/
const queue = (() => {
const ritm = require("require-in-the-middle");
const { each } = require("lodash");
const { queuefy } = require("queuefy");
const plugins = [ // eslint-disable-line
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/npm",
"@semantic-release/git",
"@semantic-release/github"
];
const _hooks = [];

const hook = () => {
each(plugins, pluginName => {
const plugin = require(pluginName);

each(plugin, (handler, ref) => {
plugin[ref] = queuefy(handler);
});

_hooks.push(ritm([pluginName], () => plugin));
uncache(pluginName);

console.log(`hooked plugin '${pluginName}'`);
});
};

const unhook = () => {
each(_hooks, _hook => _hook.unhook());
each(plugins, pluginName => {
uncache(pluginName);
console.log(`unhooked plugin '${pluginName}'`);
});
_hooks.lenght = 0;
};

return {
hook,
unhook
};
})();

/**
* NOTE this workaround forces execa calls to be always sync
* Discussion: https://github.com/semantic-release/semantic-release/issues/193#issuecomment-462063871
Expand All @@ -6,10 +53,8 @@
*/
const execa = (() => {
const ritm = require("require-in-the-middle");
const { queuefy } = require("queuefy");
const _execa = require("execa");
const uncache = name => delete require.cache[require.resolve(name)];
const execaQueued = Object.assign(queuefy(_execa), _execa);
const execaSynced = Object.assign((...args) => {
const result = new Promise((resolve, reject) => {
try {
Expand All @@ -29,14 +74,12 @@ const execa = (() => {

let interceptor;

const hook = type => {
const execaHooked = type === "queue" ? execaQueued : execaSynced;

const hook = () => {
if (interceptor) {
return;
}

interceptor = ritm(["execa"], () => execaHooked);
interceptor = ritm(["execa"], () => execaSynced);
uncache("execa");

console.log('"execa" hooked');
Expand Down Expand Up @@ -90,7 +133,15 @@ const spawn = (() => {
};
})();

/**
* @private
* @param {string} name Pkg name
* @return {boolean} Deletion result
*/
const uncache = name => delete require.cache[require.resolve(name)];

module.exports = {
execa,
spawn
spawn,
queue
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@qiwi/multi-semantic-release",
"author": "Dave Houlbrooke <dave@shax.com>",
"version": "2.4.3",
"version": "2.4.4-beta.5",
"license": "0BSD",
"engines": {
"node": ">=8.3",
Expand All @@ -26,7 +26,7 @@
"test:report": "yarn test && yarn codeclimate:push && yarn coveralls:push",
"codeclimate:push": "codeclimate-test-reporter < ./coverage/lcov.info",
"coveralls:push": "cat ./coverage/lcov.info | coveralls",
"publish": "semantic-release",
"_publish": "semantic-release",
"build": "echo 'There is no need for build && exit 0'",
"postupdate": "yarn && yarn build && yarn test"
},
Expand Down

0 comments on commit 9ae7d0d

Please sign in to comment.