-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add debug messages, separate synchronizer layer
closes #24
- Loading branch information
1 parent
cd0ee38
commit 6930cb6
Showing
9 changed files
with
391 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,33 @@ | ||
#!/usr/bin/env node | ||
|
||
const meow = require("meow"); | ||
const { toPairs, set } = require("lodash"); | ||
const runner = require("./runner"); | ||
const cli = meow( | ||
` | ||
Usage | ||
$ multi-semantic-release | ||
runner(process.argv); | ||
Options | ||
--sequential-init Avoid hypothetical concurrent initialization collisions. | ||
--debug Output debugging information. | ||
--help Help info. | ||
Examples | ||
$ multi-semantic-release | ||
`, | ||
{ | ||
flags: { | ||
sequentialInit: { | ||
type: "boolean", | ||
}, | ||
debug: { | ||
type: "boolean", | ||
}, | ||
}, | ||
} | ||
); | ||
|
||
const processFlags = (flags) => toPairs(flags).reduce((m, [k, v]) => set(m, k, v), {}); | ||
|
||
runner(processFlags(cli.flags)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const EventEmitter = require("promise-events"); | ||
const { identity } = require("lodash"); | ||
const debug = require("debug")("msr:synchronizer"); | ||
|
||
/** | ||
* Cross-packages synchronization context. | ||
* @typedef Synchronizer | ||
* @param {EventEmitter} ee Shared event emitter class. | ||
* @param {Function} todo Gets the list of packages which are still todo | ||
* @param {Function} once Memoized event subscriber. | ||
* @param {Function} emit Memoized event emitter. | ||
* @params {Function} waitFor Function returns a promise that waits until the package has target probe value. | ||
* @params {Function} waitForAll Function returns a promise that waits until all the packages have the same target probe value. | ||
*/ | ||
|
||
/** | ||
* Creates shared signal bus and its assets. | ||
* @param {Package[]} packages The multi-semantic-release context. | ||
* @returns {Synchronizer} Shared sync assets. | ||
*/ | ||
const getSynchronizer = (packages) => { | ||
const ee = new EventEmitter(); | ||
const getEventName = (probe, pkg) => `${probe}${pkg ? ":" + pkg.name : ""}`; | ||
|
||
// List of packages which are still todo (don't yet have a result). | ||
const todo = () => packages.filter((p) => p.result === undefined); | ||
|
||
// Emitter with memo: next subscribers will receive promises from the past if exists. | ||
const store = { | ||
evt: {}, | ||
subscr: {}, | ||
}; | ||
|
||
const emit = (probe, pkg) => { | ||
const name = getEventName(probe, pkg); | ||
debug("ready: %s", name); | ||
|
||
return store.evt[name] || (store.evt[name] = ee.emit(name)); | ||
}; | ||
|
||
const once = (probe, pkg) => { | ||
const name = getEventName(probe, pkg); | ||
return store.evt[name] || store.subscr[name] || (store.subscr[name] = ee.once(name)); | ||
}; | ||
|
||
const waitFor = (probe, pkg) => { | ||
const name = getEventName(probe, pkg); | ||
return pkg[name] || (pkg[name] = once(probe, pkg)); | ||
}; | ||
|
||
// Status sync point. | ||
const waitForAll = (probe, filter = identity) => { | ||
const promise = once(probe); | ||
|
||
if ( | ||
todo() | ||
.filter(filter) | ||
.every((p) => p.hasOwnProperty(probe)) | ||
) { | ||
debug("ready: %s", probe); | ||
emit(probe); | ||
} | ||
|
||
return promise; | ||
}; | ||
|
||
// Only the first lucky package passes the probe. | ||
const getLucky = (probe, pkg) => { | ||
if (getLucky[probe]) { | ||
return; | ||
} | ||
const name = getEventName(probe, pkg); | ||
debug("lucky: %s", name); | ||
|
||
getLucky[probe] = emit(probe, pkg); | ||
}; | ||
|
||
return { | ||
ee, | ||
emit, | ||
once, | ||
todo, | ||
waitFor, | ||
waitForAll, | ||
getLucky, | ||
}; | ||
}; | ||
|
||
module.exports = getSynchronizer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ | |
], | ||
"noCi": true | ||
} | ||
} | ||
} |
Oops, something went wrong.