-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathindex.js
115 lines (103 loc) · 3.17 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
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
const path = require('path');
const fs = require('fs');
const portscanner = require('portscanner');
const httpServer = require('http-server');
const logger = require('./utils/logger');
const { SERVER_PORT } = require('./shared-constants');
const mediator = require('./mediator');
const sourceWatcher = require('./source-watcher');
const setup = (options, devOptions) => {
logger.fun(`>\n> Codecrumbs magic begins!\n>`);
logger.info(`> started with options: ${JSON.stringify(options)}`);
const {
projectNameAlias,
projectDir,
entryPoint,
webpackConfigPath,
tsConfigPath,
clientPort,
excludeDir,
ideCmd
} = options;
const PORT_IN_USE = 'open';
const HOST = '0.0.0.0';
validateProjectPath(projectDir, entryPoint);
Promise.all([
portscanner.checkPortStatus(clientPort, HOST).then(status => {
if (status !== PORT_IN_USE) {
/**
* socket+http server for data exchange between browser and source servers
*/
logger.log(`- starting: mediator server `);
mediator.run({ port: SERVER_PORT, clientPort });
}
}),
portscanner.checkPortStatus(clientPort, HOST).then(status => {
if (status !== PORT_IN_USE) {
/**
* http server to host "codecrumbs" client source for browser
*/
logger.log(`- starting: http server for client assets`);
httpServer
.createServer({
root: devOptions.httpServerRoot || path.resolve(__dirname, '../public/dist/local'),
cache: devOptions.isDev ? -1 : 3600
})
.listen(clientPort, HOST, () => {
logger.info(
`+ started: http server for client assets,`,
` codecrumbs client is served on http://localhost:${clientPort}`
);
});
}
})
])
.then(() => {
/**
* source server instance (one per source code project)
*/
logger.log(`- starting: source watcher`);
sourceWatcher.run(
{
mediatorEndPoint: `${HOST}:${SERVER_PORT}`,
namespace: `source-project-${Date.now()}`,
projectName: `project:${projectNameAlias || projectDir}`
},
{
projectDir: alignPlatformPath(projectDir),
entryPoint: alignPlatformPath(entryPoint),
webpackConfigPath: alignPlatformPath(webpackConfigPath),
tsConfigPath: alignPlatformPath(tsConfigPath),
excludeDir: (excludeDir ? excludeDir.split(',') : []).map(alignPlatformPath),
ideCmd
}
);
})
.catch(e => {
logger.error(`index server setup failed: ${logger.getText(e)}`);
process.exit(1);
});
};
const alignPlatformPath = (p = '') =>
p
.replace(/^(\.?\/)/, '')
.replace(/\/$/, '')
.replace(/\//g, path.sep);
const validateProjectPath = (pDir, ePoint) => {
const paths = [];
if (!fs.existsSync(alignPlatformPath(pDir))) {
paths.push(pDir);
}
if (!fs.existsSync(alignPlatformPath(ePoint))) {
paths.push(ePoint);
}
if (paths.length) {
logger.error(
`Not valid paths. Please enter valid path, next path does not exist: ${paths.join(', ')}`
);
process.exit(1);
}
};
module.exports = {
setup
};