This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.ts
175 lines (152 loc) · 5 KB
/
server.ts
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
import express = require('express');
import path = require('path');
import compression = require('compression');
import fs = require('fs');
import childProcess = require('child_process');
import colors = require('colors/safe');
import { timer } from 'rxjs';
//
import { ProcessRunner } from './process-runner';
import { AngularCliProcessStatus } from './models/angular-cli-process-status.enum';
import { AngularProjectChecker } from './angular-project-checker';
import { printLogo } from './logo-printer.helper';
import { NgWizLogger } from './ngWizLogger';
import { CommandStatusResponse } from './models/command-status-response.interface';
const app = express();
const STATIC_FILES_LOCATION = path.join(__dirname, '../../..', '/dist/ngWiz');
const PORT = 3000;
const logger = new NgWizLogger('debug');
let isOpenBrowser;
process.argv.forEach((val, index, array) => {
if (val === '-o') {
isOpenBrowser = true;
}
});
const processRunner = new ProcessRunner();
app.use(compression());
app.use(express.static(STATIC_FILES_LOCATION));
app.use(express.json()); // to support JSON-encoded bodies
app.use(express.urlencoded()); // to support URL-encoded bodies
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/', (req, res) => {
res.sendFile(`${STATIC_FILES_LOCATION}/index.html`);
});
app.get('/isAngularProject', (req, res) => {
const angularProjectChecker = new AngularProjectChecker();
angularProjectChecker.check()
.then(
projectFolder => res.send(true),
nonProjectFolder => res.send(false)
);
});
app.get('/stopServing', (req, res) => {
const id = req.query.id;
if (processRunner.runningProcesses[id]) {
const port = processRunner.runningProcesses[id].command.match(/\s([0-9]{4,5})\s?/g)[0].replace(/\s/g, '');
const killProcess = {
id: 'killer',
params: `for /f "tokens=5" %a in ('netstat -ano ^| find "${port}" ^| find "LISTENING"') do taskkill /f /pid %a`
};
const ngServeStopper = timer(0, 500).subscribe(() => {
if (processRunner.runningProcesses[id].status === AngularCliProcessStatus.done) {
processRunner.run(killProcess);
processRunner.runningProcesses['killer'] = null;
res.send({id: req.query.id});
processRunner.runningProcesses[id] = null;
ngServeStopper.unsubscribe();
}
});
} else {
res.sendStatus(404);
}
});
app.get('/projects', (req, res) => {
const projects: string[] = [];
const folderContent = fs.readdirSync(process.cwd());
folderContent.forEach(dirName => {
const dirPath = process.cwd() + path.sep + dirName;
try {
if (fs.statSync(dirPath).isDirectory()) {
const file = `${dirPath}${path.sep}angular.json`;
fs.accessSync(file);
projects.push(dirName);
}
} catch {
console.error('Failed attempt to add an angular project to available projects array');
}
});
res.send(projects);
});
app.get('/chooseProject', (req, res) => {
const projectName = req.query.name;
console.log('choosing project', projectName);
try {
process.chdir(projectName);
res.send();
} catch (error) {
res.sendStatus(511);
}
});
app.get('/keepAlive', (req, res) => {
res.send(true);
});
app.get('/leaveProject', (req, res) => {
try {
const projectName = process.cwd().split(path.sep).pop();
process.chdir('../');
console.log(`leaving project "${projectName}", current directory ${process.cwd()}`);
res.send();
} catch {
res.sendStatus(404);
}
});
app.get('/status', (req, res) => {
const id = req.query.id;
if (processRunner.runningProcesses[id]) {
const processStatus = processRunner.runningProcesses[id].status;
res.send(<CommandStatusResponse>{id: id, status: processStatus});
if (processStatus === AngularCliProcessStatus.done
&& !processRunner.runningProcesses[id].command.includes('ng serve ')) {
processRunner.runningProcesses[id] = null;
}
} else {
res.sendStatus(404);
}
});
app.post('/command', (req, res) => {
try {
const currentProcess = {
id: ProcessRunner.guid(),
params: req.body.command
}
processRunner.run(currentProcess);
res.send(currentProcess.id);
}
catch(error) {
console.log(error);
res.status(400).end();
}
});
//this function will help us in development using Postman to change the working directory
app.post('/DEVchangeDir', (req, res) => {
process.chdir(req.body.folder);
res.send(`Working directory chaged to: ${process.cwd()}`);
})
app.listen(PORT, () => {
console.clear();
printLogo();
// TODO: Add version/build number here
logger.log.debug(`Listening on ${colors.gray(`http://localhost:${PORT}`)}`);
if (isOpenBrowser) {
openBrowser(PORT);
}
});
function openBrowser(port) {
const url = `http://localhost:${port}`;
const start = process.platform === 'darwin'? 'open': process.platform === 'win32'? 'start': 'xdg-open';
childProcess.exec(`${start} ${url}`);
}