-
Notifications
You must be signed in to change notification settings - Fork 590
/
codebox.js
executable file
·119 lines (104 loc) · 3.17 KB
/
codebox.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
115
116
117
118
119
#! /usr/bin/env node
var _ = require("lodash");
var path = require("path");
var program = require('commander');
var open = require("open");
var pkg = require("../package.json");
var codebox = require("../lib");
var gitconfig = require('../lib/utils/gitconfig');
function printError(err) {
console.log(err.stack || err.message || err);
process.exit(1);
}
program
.version(pkg.version)
.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ codebox run ./myfolder');
console.log('');
});
//// Run Codebox
////
program
.command('run [root]')
.description('run codebox')
.option('-t, --templates [list]', 'Configuration templates, separated by commas', "")
.option('-p, --port [port]', 'HTTP port', 3000)
.option('-o, --open', 'Open the IDE in your favorite browser')
.option('-e, --email [email address]', 'Email address to use as a default authentication')
.option('-u, --users [list users]', 'List of coma seperated users and password (formatted as "username:password")', function (val) {
return _.object(_.map((val || "").split(','), function(x) {
// x === 'username:password'
return x.split(':', 2);
}));
}, {})
.action(function(root, opts) {
// Generate configration
var options = {
root: path.resolve(process.cwd(), root || "./"),
port: opts.port,
auth: {
users: opts.users
}
};
codebox.start(options)
.then(function() {
if (program.email) return program.email;
// Path to user's .gitconfig file
var configPath = path.join(
process.env.HOME,
'.gitconfig'
);
// Codebox git repo: use to identify the user
return gitconfig(configPath)
.get("user")
.get("email")
.fail(function() {
return "";
});
})
.then(function(email) {
var token = opts.users[email] || Math.random().toString(36).substring(7);
var url = "http://localhost:"+options.port;
console.log("\nCodebox is running at", url);
if (program.open) open(url+"/?email="+email+"&token="+token);
})
.fail(printError);
});
//// Install packages
////
program
.command('install')
.description('pre-install packages')
.option('-r, --root [path]', 'Root folder to store packages')
.option('-p, --packages <items>', 'Comma separated list of packages to install', function (val) {
return _.chain(val.split(","))
.compact()
.map(function(pkgref) {
var parts = pkgref.split(":");
var name = _.first(parts);
var url = parts.slice(1).join(":");
if (!name || !url) throw "Packages need to be formatted as 'name:url'";
return [name,url];
})
.object()
.value()
}, [])
.action(function(opts) {
codebox.prepare({
packages: {
root: opts.root? path.resolve(process.cwd(), opts.root) : undefined,
install: opts.packages,
defaults: null
}
})
.then(function() {
process.exit(0);
})
.fail(printError);
});
program.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp();
}