-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
executable file
·115 lines (105 loc) · 2.84 KB
/
setup.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env node
var fs = require('fs')
, child = require('child_process')
, path = require('path')
, exec = child.exec
, execFile = child.execFile
, Batch = require('batch')
, program = require('commander')
, debug = require('debug')('runner')
, repoPath = path.join(__dirname, '../repos');
function clone(child, next) {
fs.exists(child.name, function (exists) {
if (exists) {
debug('Repo exists:', child.full);
return next();
}
debug('Cloning', child.full);
var ex = exec('git clone [email protected]:' + child.full + '.git', function () {
var ex = exec('cd ' + child.name + ' && component install --dev', next);
});
});
}
function link(repo, child, next) {
var toPath = path.join(repo.name, 'components', child.component);
next = next || function(){};
var link = function () {
debug('Linking', child.name, toPath);
fs.symlink('../../' + child.name, toPath, function(err) {
if (err) {
debug('Failed to link', child.name, toPath, err);
return next(err);
}
next();
});
};
fs.exists(toPath, function (exists) {
if (exists) {
rmrf(toPath, link);
} else {
// not linking, not needed
}
});
}
var rmrf = function(directories, callback) {
debug('Removing', directories);
var args = ['-rf', directories];
execFile('rm', args, {env:process.env}, function(err, stdout, stderr) {
callback.apply(this, arguments);
});
};
function getRepos(next) {
fs.readFile(repoPath, function (err, data) {
if (err) return next(err)
var lines = data.toString('utf8').split('\n');
next(null, lines.map(function(line){
line = line.trim();
if (!line) return;
var parts = line.split('/');
return {
name: parts[1],
component: parts.join('-'),
full: line,
user: parts[0]
};
}).filter(function(x){return !!x;}));
});
}
var cmds = {
clone: function (batch, err, repos) {
if (err) throw new Error(err);
repos.forEach(function(repo) {
batch.push(clone.bind(null, repo));
});
},
link: function (batch, err, repos) {
if (err) throw new Error(err);
repos.forEach(function(repo) {
repos.forEach(function(sub) {
if (repo.full === sub.full) return;
batch.push(link.bind(null, repo, sub));
});
});
},
rm: function (batch, err, repos) {
if (err) throw new Error(err);
repos.forEach(function(repo) {
batch.push(rmrf.bind(null, repo.name));
});
}
}
program
.version('0.0.1')
.parse(process.argv);
if (process.argv.length < 3 || !cmds[process.argv[2]]) {
console.log(Object.keys(cmds).join(' | '));
program.outputHelp();
} else {
var batch = new Batch;
getRepos(function (err, repos) {
cmds[process.argv[2]](batch, err, repos);
batch.end(function (err, res) {
debug('finished');
});
});
}