|
| 1 | +var path = require("path"); |
| 2 | +var rootDir = path.join(__dirname, "../.."); |
| 3 | + |
| 4 | +var gitExecutableLocation = require( |
| 5 | + path.join(rootDir, "./utils/gitExecutableLocation") |
| 6 | +); |
| 7 | +var submoduleStatus = require("./getStatus"); |
| 8 | + |
| 9 | +var exec = require(path.join(rootDir, "./utils/execPromise")); |
| 10 | + |
| 11 | +module.exports = function submodules() { |
| 12 | + return gitExecutableLocation() |
| 13 | + .catch(function() { |
| 14 | + console.log("ERROR - Compilation of NodeGit requires git CLI to be " + |
| 15 | + "installed and on the path"); |
| 16 | + |
| 17 | + throw new Error("git CLI is not installed or not on the path"); |
| 18 | + }) |
| 19 | + .then(function() { |
| 20 | + return submoduleStatus(); |
| 21 | + }) |
| 22 | + .then(function(statuses) { |
| 23 | + function printSubmodule(submoduleName) { |
| 24 | + console.log("\t" + submoduleName); |
| 25 | + } |
| 26 | + |
| 27 | + var dirtySubmodules = statuses |
| 28 | + .filter(function(status) { |
| 29 | + return status.workDirDirty && !status.needsInitialization; |
| 30 | + }) |
| 31 | + .map(function(dirtySubmodule) { |
| 32 | + return dirtySubmodule.name; |
| 33 | + }); |
| 34 | + |
| 35 | + if (dirtySubmodules.length) { |
| 36 | + console.log( |
| 37 | + "ERROR - The following submodules have uncommited changes:" |
| 38 | + ); |
| 39 | + dirtySubmodules.forEach(printSubmodule); |
| 40 | + console.log( |
| 41 | + "\nThey must either be committed or discarded before we build" |
| 42 | + ); |
| 43 | + |
| 44 | + throw new Error("Dirty Submodules: " + dirtySubmodules.join(" ")); |
| 45 | + } |
| 46 | + |
| 47 | + var outOfSyncSubmodules = statuses |
| 48 | + .filter(function(status) { |
| 49 | + return status.onNewCommit && !status.needsInitialization; |
| 50 | + }) |
| 51 | + .map(function(outOfSyncSubmodule) { |
| 52 | + return outOfSyncSubmodule.name; |
| 53 | + }); |
| 54 | + |
| 55 | + if (outOfSyncSubmodules.length) { |
| 56 | + console.log( |
| 57 | + "WARNING - The following submodules are pointing to an new commit:" |
| 58 | + ); |
| 59 | + outOfSyncSubmodules.forEach(printSubmodule); |
| 60 | + console.log("\nThey will not be updated."); |
| 61 | + } |
| 62 | + |
| 63 | + return Promise.all(statuses |
| 64 | + .filter(function(status) { |
| 65 | + return !status.onNewCommit; |
| 66 | + }) |
| 67 | + .map(function(submoduleToUpdate) { |
| 68 | + return exec( |
| 69 | + "git submodule update --init --recursive " + submoduleToUpdate.name |
| 70 | + ); |
| 71 | + }) |
| 72 | + ); |
| 73 | + }); |
| 74 | +}; |
0 commit comments