Skip to content

Commit

Permalink
lifecycleScripts/submodules/index.js: async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
samuela committed Oct 18, 2021
1 parent ee243f6 commit 971ba2d
Showing 1 changed file with 60 additions and 81 deletions.
141 changes: 60 additions & 81 deletions lifecycleScripts/submodules/index.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,61 @@
var path = require("path");
var rootDir = path.join(__dirname, "../..");

var gitExecutableLocation = require(
path.join(rootDir, "./utils/gitExecutableLocation")
);
var submoduleStatus = require("./getStatus");

var exec = require(path.join(rootDir, "./utils/execPromise"));

module.exports = function submodules() {
return gitExecutableLocation()
.catch(function() {
console.error("[nodegit] ERROR - Compilation of NodeGit requires git " +
"CLI to be installed and on the path");

throw new Error("git CLI is not installed or not on the path");
})
.then(function() {
console.log("[nodegit] Checking submodule status");
return submoduleStatus();
})
.then(function(statuses) {
function printSubmodule(submoduleName) {
console.log("\t" + submoduleName);
}

var dirtySubmodules = statuses
.filter(function(status) {
return status.workDirDirty && !status.needsInitialization;
})
.map(function(dirtySubmodule) {
return dirtySubmodule.name;
});

if (dirtySubmodules.length) {
console.error(
"[nodegit] ERROR - Some submodules have uncommited changes:"
);
dirtySubmodules.forEach(printSubmodule);
console.error(
"\nThey must either be committed or discarded before we build"
);

throw new Error("Dirty Submodules: " + dirtySubmodules.join(" "));
}

var outOfSyncSubmodules = statuses
.filter(function(status) {
return status.onNewCommit && !status.needsInitialization;
})
.map(function(outOfSyncSubmodule) {
return outOfSyncSubmodule.name;
});

if (outOfSyncSubmodules.length) {
console.warn(
"[nodegit] WARNING - Some submodules are pointing to an new commit:"
);
outOfSyncSubmodules.forEach(printSubmodule);
console.warn("\nThey will not be updated.");
}

return statuses
.filter(function(status) {
return !status.onNewCommit;
})
.reduce(function(chainPromise, submoduleToUpdate) {
return chainPromise
.then(function() {
console.log(
"[nodegit] Initializing submodule",
submoduleToUpdate.name
);
return exec(
"git submodule update --init --recursive " +
submoduleToUpdate.name
);
});
}, Promise.resolve());
});
const path = require("path");
const rootDir = path.join(__dirname, "../..");

const gitExecutableLocation = require(path.join(
rootDir,
"./utils/gitExecutableLocation"
));
const submoduleStatus = require("./getStatus");

const exec = require(path.join(rootDir, "./utils/execPromise"));

module.exports = async function submodules() {
try {
await gitExecutableLocation();
} catch (_) {
console.error(
"[nodegit] ERROR - Compilation of NodeGit requires git " +
"CLI to be installed and on the path"
);

throw new Error("git CLI is not installed or not on the path");
}

console.log("[nodegit] Checking submodule status");
const statuses = await submoduleStatus();

function printSubmodule(submoduleName) {
console.log("\t" + submoduleName);
}

const dirtySubmodules = statuses
.filter((status) => status.workDirDirty && !status.needsInitialization)
.map((dirtySubmodule) => dirtySubmodule.name);

if (dirtySubmodules.length) {
console.error("[nodegit] ERROR - Some submodules have uncommited changes:");
dirtySubmodules.forEach(printSubmodule);
console.error(
"\nThey must either be committed or discarded before we build"
);

throw new Error("Dirty Submodules: " + dirtySubmodules.join(" "));
}

const outOfSyncSubmodules = statuses
.filter((status) => status.onNewCommit && !status.needsInitialization)
.map((outOfSyncSubmodule) => outOfSyncSubmodule.name);

if (outOfSyncSubmodules.length) {
console.warn(
"[nodegit] WARNING - Some submodules are pointing to an new commit:"
);
outOfSyncSubmodules.forEach(printSubmodule);
console.warn("\nThey will not be updated.");
}

for (const status of statuses.filter((s) => !s.onNewCommit)) {
console.log("[nodegit] Initializing submodule", status.name);
await exec("git submodule update --init --recursive " + status.name);
}
};

0 comments on commit 971ba2d

Please sign in to comment.