/
mkrepo
executable file
·113 lines (84 loc) · 2.41 KB
/
mkrepo
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
#!/usr/bin/env node
// -*- mode: js -*-
// Copyright 2012 Joyent, Inc. All rights reserved.
var child_process = require('child_process');
var fs = require('fs');
var path = require('path');
///--- Globals
var DIRS = [
'deps',
'docs',
'docs/media',
'lib',
'smf',
'smf/manifests',
'test',
'tools',
'tools/mk'
];
var SUBMODULES = {
'javascriptlint': 'git://github.com/davepacheco/javascriptlint.git',
'jsstyle': 'git://github.com/davepacheco/jsstyle.git',
'node': 'git://github.com/joyent/node.git',
'restdown': 'git://github.com/trentm/restdown.git'
};
///--- Internal Functions
function usage(code, message) {
if (message)
console.error(message);
console.error('usage: %s [repo ...]', path.basename(process.argv[1]));
process.exit(code);
}
function ensureDirectoryNotExists(dir) {
try {
var stats = fs.statSync(dir);
usage(1, dir + ' already exists');
} catch (e) {
return false;
}
}
function cp(src, dest) {
fs.createReadStream(src).pipe(fs.createWriteStream(dest));
}
function exec(cmd, dir, cb) {
child_process.exec(cmd, {cwd: dir}, function (err, stdout, stderr) {
if (err)
process.exit(err.code || 1);
if (typeof (cb) === 'function')
return cb(null);
});
}
function mkdir(d) {
fs.mkdirSync(d, '0750');
}
function gitify(dir, repo) {
exec('git init', dir, function () {
exec('git remote add origin git@git.joyent.com:' + repo + '.git', dir);
Object.keys(SUBMODULES).forEach(function (k) {
// stub out the git submodule call
console.error('Cloning into deps/' + k + '...');
exec('git submodule add ' + SUBMODULES[k] + ' ./deps/' + k, dir);
});
});
}
///--- Mainline
if (process.argv.length < 3)
usage(1, 'repo required');
process.argv.slice(2).forEach(function (arg) {
var repo = path.resolve(arg);
ensureDirectoryNotExists(repo);
mkdir(repo);
DIRS.concat('.').forEach(function (d) {
var dir = repo + '/' + d;
if (d != '.')
mkdir(dir);
fs.readdirSync('./' + d).forEach(function (f) {
var src = './' + d + '/' + f;
var dest = dir + '/' + f;
if (fs.statSync(src).isFile() && !/^\..*/.test(f))
cp(src, dest);
});
});
cp('./.gitignore', repo + '/.gitignore');
gitify(repo, arg);
});