-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlakespace.js
executable file
·229 lines (179 loc) · 5.04 KB
/
lakespace.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env node
// #############################################################################
// lakespace
//
// given a workspace.toml toml file, open chrome/firefox windows, ide, terminals.
//
// #############################################################################
'use strict';
const path = require("path");
const argv = require('minimist')(process.argv.slice(2));
const cp = require('child_process');
const sleep = require('sleep');
var toml = require('toml');
var validUrl = require('valid-url');
var fs = require('fs');
// defaults
var configuration = "lakespace.toml";
var browser = 'firefox';
var editor = 'atom';
var project_root = '.';
var desktop = "0";
// when 1, only show commands but do not execute.
var dryrun = 0;
// track full paths to prevent circular references
var config_file_instances = [];
// array for commands
var cmdStack = [];
var serial_cmd = function() {
Object.keys(cmdStack).forEach(function(key) {
var val = cmdStack[key];
if (dryrun == 1) {
console.log(`${val}`);
} else {
// need to make this work on different platforms.
var child = cp.spawn(val, { shell: '/bin/bash', stdio: 'ignore', detached: true});
child.unref();
sleep.sleep(1);
}
});
}
var ide_branch = function(data) {
var offset = "0,0";
var resize = "-1,-1";
Object.keys(data).forEach(function(key) {
var val = data[key];
if (key == 'offset') {
offset = val;
}
if (key == 'resize') {
resize = val;
}
if (key == 'editor') {
editor = val;
}
if (key == 'project_root') {
project_root = val;
}
});
cmdStack.push("wmctrl -r :ACTIVE: -e 0," + offset + "," + resize);
}
var terminal_branch = function(data) {
var working_directory = "~";
var terminal_command = "gnome-terminal --window";
var tabcount = 0;
var offset = "0,0";
var resize = "-1,-1";
Object.keys(data).forEach(function(key) {
var val = data[key];
if (key == 'offset') {
offset = val;
}
if (key == 'resize') {
resize = val;
}
if (key == 'working_directory') {
working_directory = val;
}
if (key.startsWith('command_')) {
if (tabcount == 0) {
terminal_command = terminal_command + " --working-directory=" + working_directory + " -e \"bash -c '" + val + "; bash'\"";
tabcount++;
} else {
terminal_command = terminal_command + " --tab --working-directory=" + working_directory + " -e \"bash -c '" + val + "; bash'\"";
}
}
});
cmdStack.push(terminal_command);
cmdStack.push("wmctrl -r :ACTIVE: -e 0," + offset + "," + resize);
}
var browser_branch = function(data) {
var offset = "0,0";
var resize = "-1,-1";
var browser_command = "";
Object.keys(data).forEach(function(key) {
var val = data[key];
if (key == 'offset') {
offset = val;
}
if (key == 'resize') {
resize = val;
}
if (key == 'browser') {
browser = val;
}
if (key == 'tabs') {
// at least on ubuntu, launching firefox with multiple
// sites/tabs requires they be enclosed in quotes, as
// some urls will break otherwise.
cmdStack.push(browser + ' "' + val.join('" "') + '"');
}
if (key == 'editor') {
cmdStack.push(val + ' . ');
}
});
cmdStack.push("wmctrl -r :ACTIVE: -e 0," + offset + "," + resize);
}
var lakespace_branch = function(data) {
Object.keys(data).forEach(function(key) {
var val = data[key];
if (key == 'desktop') {
desktop = val;
// pre-empt switch to desktop as normal behavior;
cmdStack.push(`wmctrl -s ${desktop}`);
}
if (key.startsWith('include_')) {
parse_file(val);
}
});
}
var tree = function (data) {
Object.keys(data).forEach(function(key) {
var val = data[key];
// could be a new section group, flag it
if (typeof val == 'object') {
// so we can have multiple groups, split on _
var index = key.split('_')[0];
switch(index) { // key
case 'lakespace':
lakespace_branch(val);
break;
case 'ide':
ide_branch(val);
cmdStack.push(editor + ' ' + project_root);
break;
case 'terminal':
terminal_branch(val);
default:
browser_branch(val);
}
} // else { root property }
});
}
var parse_file = function (config_file) {
if (config_file_instances.includes(config_file)) {
console.log(`Refusing to parse ${config_file} more than once.`)
return;
}
config_file_instances.push(config_file);
var str = fs.readFileSync(path.resolve(process.cwd(), config_file), 'utf8')
var parsed = toml.parse(str);
tree(parsed);
}
var main = function() {
// before we do anything, let's ensure we have a supported
// platform
if (process.platform !== 'linux') {
console.log(`Platform '${process.platform}' is not supported.`);
process.exit(0);
}
if (argv["_"][0] !== undefined) {
configuration = argv["_"][0];
}
if (argv["dryrun"]) {
dryrun = 1;
}
parse_file(configuration);
serial_cmd(cmdStack);
}
main();