forked from fluffynuts/gulp-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-submodules.js
85 lines (82 loc) · 3.32 KB
/
git-submodules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var spawn = requireModule('spawn');
var gulp = require('gulp');
var log = requireModule('log');
var fs = require('fs');
var q = require('q');
var readFile = q.denodeify(fs.readFile);
var subModulesFile = '.gitmodules';
gulp.task('git-submodules', function() {
return spawn('git', ['submodule', 'update', '--init', '--recursive']);
});
gulp.task('git-submodules-as-externals', function() {
var deferred = q.defer();
if (!fs.existsSync(subModulesFile)) {
log.notice('no submodules file found');
deferred.resolve();
} else {
log.notice('performing submodule init/update...');
spawn('git', ['submodule', 'update', '--init', '--recursive']).then(function() {
log.info('getting list of local submodules...');
return readFile(subModulesFile);
}).then(function(buffer) {
log.info('grokking paths of local submodules...');
var fileContents = buffer.toString();
var lines = fileContents.split('\n');
var submodulePaths = lines.reduce(function(acc, cur) {
var parts = cur.split(' = ').map(function(item) {
return item.trim();
});
if (parts.length > 1 && parts[0] === 'path') {
acc.push(parts[1]);
}
return acc;
}, []);
return submodulePaths;
}).then(function(modulePaths) {
var mkdir = function(path) {
var parts = path.split('/');
var current = '';
parts.forEach(function(part) {
current += current ? '/' : '';
current += part;
if (!fs.existsSync(current)) {
fs.mkdirSync(current);
}
});
};
modulePaths.forEach(function(path) {
mkdir(path);
});
return modulePaths;
}).then(function(modulePaths) {
log.info('making sure local submodules are up to date...');
var innerDeferred = q.defer();
var spawnOptions = {
stdio: [process.stdin, process.stdout, process.stderr, 'pipe']
};
var finalPromise = modulePaths.reduce(function(acc, cur) {
var workingFolder = [process.cwd(), cur].join('/');
log.info('working with submodule at: ' + cur);
spawnOptions.cwd = workingFolder;
return acc.then(function() {
log.debug(' - fetch changes');
return spawn('git', ['fetch'], spawnOptions).then(function() {
log.debug(' - switch to master');
return spawn('git', ['checkout', 'master'], spawnOptions);
}).then(function() {
log.debug(' - fast-forward to HEAD');
return spawn('git', ['rebase'], spawnOptions);
});
});
}, innerDeferred.promise);
innerDeferred.resolve();
return finalPromise;
}).then(function() {
log.info('git submodule magick complete');
deferred.resolve();
}).catch(function(err) {
deferred.reject(err);
});
}
return deferred.promise;
});