Skip to content

Commit

Permalink
feat: add execa queued hook
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed May 20, 2020
1 parent 0a3a0a2 commit 042933e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 36 deletions.
9 changes: 6 additions & 3 deletions bin/runner.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
module.exports = flags => {
// Apply some hooks.
const hooks = require("../lib/hooks");

if (flags.watchspawn) {
hooks.watchspawn.hook();
hooks.spawn.hook();
}

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

// Imports.
Expand Down
59 changes: 27 additions & 32 deletions lib/hooks.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
/**
* @private
* @param {string} name Module name
* @return {boolean} Deletion result
*/
const uncache = name => delete require.cache[require.resolve(name)];

/**
* NOTE this workaround forces execa calls to be always sync
* Discussion: https://github.com/semantic-release/semantic-release/issues/193#issuecomment-462063871
* @private
* @type {{hook: hook, unhook: unhook}}
*/
const execasync = (() => {
const execa = (() => {
const ritm = require("require-in-the-middle");
const execa = require("execa");
let interceptor;
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 {
resolve(_execa.sync(...args));
} catch (e) {
reject(e);
}
});

result.stdout = new String("");
result.stderr = new String("");
result.stdout.pipe = () => {};
result.stderr.pipe = () => {};

return result;
}, _execa);

const getExecaSyncPromisified = () =>
Object.assign((...args) => {
const result = new Promise((resolve, reject) => {
try {
resolve(execa.sync(...args));
} catch (e) {
reject(e);
}
});

result.stdout = new String("");
result.stderr = new String("");
result.stdout.pipe = () => {};
result.stderr.pipe = () => {};
let interceptor;

return result;
}, execa);
const hook = type => {
const execaHooked = type === "queue" ? execaQueued : execaSynced;

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

const execaHooked = getExecaSyncPromisified();

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

Expand All @@ -67,7 +62,7 @@ const execasync = (() => {
* @private
* @type {{hook: hook, unhook: unhook}}
*/
const watchspawn = (() => {
const spawn = (() => {
const childProcess = require("child_process");
const oldSpawn = childProcess.spawn;

Expand Down Expand Up @@ -96,6 +91,6 @@ const watchspawn = (() => {
})();

module.exports = {
execasync,
watchspawn
execa,
spawn
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@
"blork": "^9.2.2",
"cosmiconfig": "^6.0.0",
"execa": "^4.0.0",
"lodash": "^4.17.15",
"get-stream": "^5.1.0",
"git-log-parser": "^1.2.0",
"lodash": "^4.17.15",
"meow": "^6.0.1",
"queuefy": "^1.0.0",
"require-in-the-middle": "^5.0.3",
"semantic-release": "^17.0.4",
"semver": "^7.1.3",
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6175,6 +6175,13 @@ query-string@^6.8.2:
split-on-first "^1.0.0"
strict-uri-encode "^2.0.0"

queuefy@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/queuefy/-/queuefy-1.0.0.tgz#3dd3630ec9a4511dde46b5f4b1f7ccf45f6f93cc"
integrity sha512-8rxJ7unA8zWDCK8YaCYwtn/iYWqALMzaJNHRiEhZS9YpSXEKdMJ8TpiQIZU+WIJqdwMcbu1igIzYUeBZ7oaw6w==
dependencies:
tslib "^2.0.0"

quick-lru@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-1.1.0.tgz#4360b17c61136ad38078397ff11416e186dcfbb8"
Expand Down Expand Up @@ -7524,6 +7531,11 @@ tslib@^1.9.0:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"
integrity sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==

tslib@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.0.0.tgz#18d13fc2dce04051e20f074cc8387fd8089ce4f3"
integrity sha512-lTqkx847PI7xEDYJntxZH89L2/aXInsyF2luSafe/+0fHOMjlBNXdH6th7f70qxLDhul7KZK0zC8V5ZIyHl0/g==

tunnel-agent@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.6.0.tgz#27a5dea06b36b04a0a9966774b290868f0fc40fd"
Expand Down

0 comments on commit 042933e

Please sign in to comment.