-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
96 lines (85 loc) · 2.88 KB
/
run.js
File metadata and controls
96 lines (85 loc) · 2.88 KB
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
const fs = require("fs");
const os = require("os");
const path = require("path");
const { exec, execSync, spawn } = require("child_process");
const PORT = 5050;
const PATH_LIMIT = 1024;
const SPAWN_SHELL_DELAY = 1000;
const REWRITE_NGROK_URL_DELAY = 2000;
const NGROK_PATH = process.argv[2];
const systemType = os.platform();
const windowsShellOptions = {
shell: process.env.ComSpec || "C:\\Windows\\system32\\cmd.exe",
};
function setNgrokPath() {
if (!NGROK_PATH) return;
const newPath = process.env.path + ";" + NGROK_PATH;
if (newPath.length > PATH_LIMIT) {
throw new Error(
"Your path variable is over 1024 characters long. You will have to shorten it or add ngrok manually to your path.",
);
} else {
console.log("path changed");
exec(`setx PATH "%PATH%;${NGROK_PATH}"`);
}
}
function newTerminal(command) {
const CWD = path.join(__dirname, "../");
switch (systemType) {
case "win32":
// empty double quotes is intentional because of how the "start" Windows
// command parameters work, setTimeout() used for Windows to prevent new
// shells from opening too fast and skipping some commands
setTimeout(() => {
spawn(`start "" /d ${CWD} ${command}`, [], windowsShellOptions);
}, SPAWN_SHELL_DELAY);
break;
case "darwin":
execSync(`
osascript -e 'tell application "Terminal" to activate' \
-e 'tell application "System Events" to keystroke "t" using {command down}' \
-e 'tell application "Terminal" to do script "cd ${CWD} && ${command}" in front window'
`);
break;
default:
console.log(
"This dev script currently only supports Windows/unix at the moment.",
);
}
}
// Make sure all instances of ngrok's process doesn't exist.
if (systemType === "win32") {
setNgrokPath();
spawn("Taskkill /IM ngrok.exe /F", [], windowsShellOptions);
} else if (systemType === "darwin") {
exec("killall ngrok");
}
newTerminal(`ngrok http ${PORT}`);
setTimeout(() => {
exec("curl http://127.0.0.1:4040/api/tunnels", (error, stdout) => {
const ngrok = JSON.parse(stdout);
const { tunnels } = ngrok;
const { public_url } = tunnels[0];
console.log(public_url);
fs.access("./packages/app/utils/", (error) => {
if (error) {
throw error;
} else {
const FILE_PATH = "./packages/app/utils/constants.js";
fs.readFile(FILE_PATH, "utf-8", function (error, data) {
if (error) throw error;
const overwrite = data.replace(
/\b(https:\/\/)\b.*\b(.ngrok.free.app)\b/,
public_url,
);
fs.writeFile(FILE_PATH, overwrite, "utf-8", function (error) {
if (error) throw error;
console.log("Successfully overwrite ngrok URL");
});
});
}
});
newTerminal("yarn server:dev");
newTerminal("yarn app:dev");
});
}, REWRITE_NGROK_URL_DELAY);