-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
66 lines (56 loc) · 2.02 KB
/
index.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
const puppeteer = require("puppeteer");
exports.status = {
SUCCESS: 0,
CREDENTIALS_NOT_FOUND: 1,
CREDENTIALS_INVALID: 2,
WORKSPACE_NOT_FOUND: 3,
WORKSPACE_INACCESSIBLE: 4
};
exports.wake = async (user, workspace) => {
const email = process.env.C9_EMAIL;
const password = process.env.C9_PASSWORD;
if(!email || !password) {
return this.status.CREDENTIALS_NOT_FOUND;
}
user = user.toLowerCase();
workspace = workspace.toLowerCase();
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Will redirect to the login page
await page.goto(`https://ide.c9.io/${user}/${workspace}`);
await page.waitForSelector("#signinForm");
await page.type("#inpUsernameEmail", email);
await page.type("#inpPassword", password);
await page.click("#btnSignIn");
try {
await page.waitForNavigation({ waitUntil: "networkidle0", timeout: 5000 });
}
catch(e) {
// Login failed and no navigation took place, will be handled in `hasInvalidCredentials`
}
const hasInvalidCredentials = await page.$(".errorSignin");
if(hasInvalidCredentials) {
return this.status.CREDENTIALS_INVALID;
}
const isNonexistentWorkspace = await page.$(".error404");
if(isNonexistentWorkspace) {
return this.status.WORKSPACE_NOT_FOUND;
}
try {
// Searches for "Unable" in the "Unable to access your workspace" modal heading
await page.$eval(".bk-window h3", node => node.innerText.includes("Unable"));
return this.status.WORKSPACE_INACCESSIBLE;
}
catch(e) {
// Workspace is accessible
}
// If the workspace is in hiberation, wait until the restore overlay is gone.
try {
await page.waitForSelector("#c9_ide_restore", { hidden: true, timeout: 60000 });
}
catch(e) {
// The restore overlay is still visible, so it's most likely a large project. We'll assume it's ready at this point.
}
await browser.close();
return this.status.SUCCESS;
};