Misc refactoring

This commit is contained in:
Radon Rosborough 2021-03-26 20:31:46 -07:00
parent 17d0167611
commit 76a2a2d1ee
1 changed files with 27 additions and 17 deletions

View File

@ -291,7 +291,14 @@ function getTransitiveDependencies({ artifacts, targets }) {
return _.sortBy([...found], (name) => Object.keys(artifacts).indexOf(name)); return _.sortBy([...found], (name) => Object.keys(artifacts).indexOf(name));
} }
async function executeDepGraph({ depgraph, manual, publish, yes, targets }) { async function executeDepGraph({
depgraph,
manual,
holdManual,
publish,
yes,
targets,
}) {
const artifacts = {}; const artifacts = {};
for (const artifact of depgraph.artifacts) { for (const artifact of depgraph.artifacts) {
artifacts[artifact.name] = artifact; artifacts[artifact.name] = artifact;
@ -334,30 +341,31 @@ async function executeDepGraph({ depgraph, manual, publish, yes, targets }) {
promises.local[target] = artifacts[target].getLocalHash(info); promises.local[target] = artifacts[target].getLocalHash(info);
promises.published[target] = artifacts[target].getPublishedHash(info); promises.published[target] = artifacts[target].getPublishedHash(info);
promises.desired[target] = (async () => { promises.desired[target] = (async () => {
if (manual) {
// Artifact has no dependencies in this case.
return await artifacts[target].getDesiredHash({});
}
const dependencyHashes = {}; const dependencyHashes = {};
for (const dependency of artifacts[target].dependencies) { for (const dependency of artifacts[target].dependencies) {
dependencyHashes[dependency] = await promises.desired[dependency]; dependencyHashes[dependency] = await promises.desired[dependency];
if (!dependencyHashes[dependency]) {
throw new Error(
`manual dependency must be built explicitly: dep ${target} --manual [--publish]`
);
}
} }
let hash = await artifacts[target].getDesiredHash(dependencyHashes); let hash = await artifacts[target].getDesiredHash(dependencyHashes);
if (hash !== null) { if (hash || manual) {
return hash; return hash;
} }
// If hash is missing, cast about in blind panic for another const promiseSets = [promises.published, promises.local];
// possible way to compute it. if (holdLocal) {
hash = await promises.published[target]; sets.reverse();
if (hash !== null) {
return hash;
} }
hash = await promises.local[target]; for (const promiseSet of promiseSets) {
if (hash !== null) { const hash = await promiseSet[target];
return hash; if (hash) {
return hash;
}
} }
throw new Error( throw new Error(
`artifact must be built manually: dep ${target} --manual [--publish]` `manual artifact must be built explicitly: dep ${target} --manual [--publish]`
); );
})(); })();
} }
@ -389,11 +397,12 @@ async function main() {
const program = new Command(); const program = new Command();
program.usage("<target>..."); program.usage("<target>...");
program.option("--list", "list available artifacts; ignore other arguments"); program.option("--list", "list available artifacts; ignore other arguments");
program.option("--manual", "operate manually on leaf artifacts"); program.option("--manual", "operate explicitly on manual artifacts");
program.option("--hold-manual", "prefer local versions of manual artifacts");
program.option("--publish", "publish artifacts to remote registries"); program.option("--publish", "publish artifacts to remote registries");
program.option("--yes", "execute plan without confirmation"); program.option("--yes", "execute plan without confirmation");
program.parse(process.argv); program.parse(process.argv);
const { list, manual, publish, yes } = program.opts(); const { list, manual, holdManual, publish, yes } = program.opts();
const depgraph = await getDepGraph(); const depgraph = await getDepGraph();
if (list) { if (list) {
for (const { name } of depgraph.artifacts) { for (const { name } of depgraph.artifacts) {
@ -409,6 +418,7 @@ async function main() {
await executeDepGraph({ await executeDepGraph({
depgraph, depgraph,
manual, manual,
holdManual,
publish, publish,
yes, yes,
targets: program.args, targets: program.args,