-
Notifications
You must be signed in to change notification settings - Fork 2
/
c9.ide.janitorconfig.js
143 lines (126 loc) · 4.95 KB
/
c9.ide.janitorconfig.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
define(function(require, exports, module) {
main.consumes = ["settings", "Plugin", "fs", "run.gui"];
main.provides = ["c9.ide.janitorconfig"];
return main;
function main(options, imports, register) {
var settings = imports.settings;
var Plugin = imports.Plugin;
var fs = imports.fs;
var runGui = imports["run.gui"];
/***** Initialization *****/
var plugin = new Plugin("janitorconfig", main.consumes);
var emit = plugin.getEmitter();
function readJanitorManifest() {
const filePaths = [
"~/janitor.json",
"~/.janitor.json",
"~/.janitor/janitor.json",
"/janitor.json",
"/.janitor.json",
"/.janitor/janitor.json",
];
let readFile = function (path) {
return new Promise((resolve, reject) => {
fs.readFile(path, function(err, data) {
if (err) {
resolve(undefined);
return;
}
console.info("[c9.ide.janitorconfig]", path, "found");
resolve(data);
});
});
};
let promises = Promise.all(filePaths.map(path => readFile(path)));
return promises.then(files => {
files = files.filter(v => !!v);
if (files.length > 1) {
console.log(files);
throw new Error("[c9.ide.janitorconfig] More than one janitor.json file detected.")
} else if (files.length === 1) {
return JSON.parse(files[0]);
} else {
throw new Error("[c9.ide.janitorconfig] janitor.json file not found")
}
});
}
function load() {
readJanitorManifest().then(manifest => {
loadSettings(manifest.scripts);
loadPreview(manifest.ports);
}).catch(err => {
console.error("[c9.ide.janitorconfig] Failed to parse or load janitor.json", err)
});
}
function loadSettings(scripts) {
let c9runners = {};
let i = 0;
for (let name in scripts) {
const script = scripts[name];
let fullName = name;
if (script.cmd) {
const cmd = script.cmd;
fullName += ' (' + cmd + ')';
c9runners[fullName] = {
"command": cmd,
"cwd": script.cwd || "/",
"name": fullName,
"runner": "Shell command",
"toolbar": true,
};
} else if (typeof script === "string") {
const cmd = script;
fullName += ' (' + cmd + ')';
c9runners[fullName] = {
"command": cmd,
"cwd": "/",
"name": fullName,
"runner": "Shell command",
"toolbar": true,
};
} else {
console.error("[c9.ide.janitorconfig]", "Failed to parse script", script);
}
if (i === 0) {
c9runners[fullName].default = true;
}
i++;
}
settings.setJson("project/run/configs", c9runners);
console.info("[c9.ide.janitorconfig] Finished loading janitor.json scripts.");
runGui.on("unload", function() {
runGui.load();
});
runGui.unload();
}
function loadPreview(ports) {
for (let port in ports) {
if (ports[port].preview) {
let container;
if (window.location.search.indexOf("container=") !== -1) {
container = window.location.search.split("container=")[1].split("&")[0];
} else {
container = window.location.pathname.split("/")[1];
}
let url = "https://" + window.location.host +
"?container=" + container +
"&port=" + port;
settings.set("project/preview/@url", url);
return;
}
}
}
/***** Lifecycle *****/
plugin.on("load", function() {
load();
});
plugin.on("unload", function() {
});
/***** Register and define API *****/
plugin.freezePublicAPI({
});
register(null, {
"c9.ide.janitorconfig": plugin
});
}
});