@hubspot/ui-extensions-dev-server 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,190 +0,0 @@
1
- const { spawn } = require('child_process');
2
- const fs = require('fs');
3
- const path = require('path');
4
- const assert = require('assert');
5
- const http = require('http');
6
- const WebSocket = require('ws');
7
- const {
8
- WEBSOCKET_MESSAGE_VERSION,
9
- EXTENSIONS_MESSAGE_VERSION,
10
- } = require('../constants');
11
-
12
- const testResults = {
13
- buildTestPassed: false,
14
- expressStaticTestPassed: false,
15
- extensionsEndpointPassed: false,
16
- webSocketTestPassed: false,
17
- };
18
-
19
- const port = 5172;
20
- const host = 'hslocal.net';
21
-
22
- function testDevServer(logger, devServerProcess) {
23
- logger.warn('\nDev Server Tests started 🤞');
24
-
25
- // We need to use spawn here because it will put the process into the background,
26
- // which is required because dev mode is a blocking process and we want to test that
27
- // the express server and websocket server are starting properly
28
- devServerProcess = spawn('hs-ui-extensions-dev-server', [
29
- 'dev',
30
- '--extension',
31
- 'PhoneLines.tsx',
32
- '--port',
33
- `${port}`,
34
- ]);
35
-
36
- devServerProcess.stdout.on('data', buffer => {
37
- const data = buffer.toString().toLowerCase();
38
- if (data.includes('built in')) {
39
- testBuild(testResults, logger);
40
- }
41
- if (
42
- data.includes('listening') &&
43
- data.includes(`${host}:${port}/extensions`)
44
- ) {
45
- setTimeout(() => {
46
- testExpressServer(testResults, logger);
47
- testWebSocketServer(testResults, logger);
48
- }, 1000);
49
- }
50
- });
51
-
52
- // If the dev server writes to stderr, log the error and throw a new error
53
- devServerProcess.stderr.on('data', buffer => {
54
- const data = buffer.toString();
55
- logger.error(data.toString());
56
- throw new Error(data);
57
- });
58
-
59
- // When the process closes make sure we met all the success conditions
60
- devServerProcess.on('close', () => {
61
- if (metConditions()) {
62
- logger.info('Dev Server Tests passed 🚀');
63
- } else {
64
- console.log(testResults);
65
- logger.error('Tests failed 😭');
66
- }
67
- });
68
-
69
- const interval = setInterval(callback, 1000);
70
- let count = 0;
71
- function callback() {
72
- count += 1;
73
- if (metConditions() || count === 5) {
74
- devServerProcess.kill();
75
- clearInterval(interval);
76
- }
77
- }
78
- }
79
-
80
- function metConditions() {
81
- const {
82
- buildTestPassed,
83
- expressStaticTestPassed,
84
- extensionsEndpointPassed,
85
- webSocketTestPassed,
86
- } = testResults;
87
- return (
88
- buildTestPassed &&
89
- expressStaticTestPassed &&
90
- extensionsEndpointPassed &&
91
- webSocketTestPassed
92
- );
93
- }
94
-
95
- // Test that the files were built in the proper location and spot
96
- // check the contents of the files.
97
- function testBuild(results, logger) {
98
- // // Make sure the files are getting generated in the dist dir
99
- const distDir = path.join(process.cwd(), 'dist');
100
- const filesInOutputDir = fs.readdirSync(distDir);
101
- assert.deepStrictEqual(filesInOutputDir, ['PhoneLines.js', 'manifest.json']);
102
- const fileContents = fs
103
- .readFileSync(path.join(distDir, filesInOutputDir[0]))
104
- .toString();
105
- const stringsToSpotCheck = [
106
- '.createRemoteReactComponent',
107
- '.createElement',
108
- 'hubspot.extend',
109
- 'React',
110
- 'RemoteUI',
111
- ];
112
- stringsToSpotCheck.forEach(stringToCheck => {
113
- assert(
114
- fileContents.includes(stringToCheck),
115
- `File ${filesInOutputDir[0]} contents should contain: "${stringToCheck}"`
116
- );
117
- });
118
- logger.info('- Build succeeded 🚀');
119
- results.buildTestPassed = true;
120
- }
121
-
122
- // Test that the express server is running on the expected port
123
- // and that it is serving the files as expected.
124
- function testExpressServer(results, logger) {
125
- http.get(
126
- {
127
- host,
128
- port,
129
- path: '/PhoneLines.js',
130
- },
131
- response => {
132
- if (response.statusCode !== 200) {
133
- throw Error('Error with express server');
134
- }
135
- logger.info('- Express server connected and serving files 🚀');
136
- results.expressStaticTestPassed = true;
137
- }
138
- );
139
- http.get(
140
- {
141
- host,
142
- port,
143
- path: '/extensions',
144
- },
145
- response => {
146
- let body = '';
147
- response.on('data', chunk => {
148
- body += chunk.toString();
149
- });
150
- response.on('end', () => {
151
- assert(response.statusCode === 200);
152
- body = JSON.parse(body);
153
- assert(body.extensions);
154
- assert.equal(body.extensions.length, 1);
155
- const extension = body.extensions.pop();
156
- assert(extension.manifest);
157
- assert.equal(extension.appName, 'Example App React UI');
158
- assert.equal(extension.title, 'Phone Lines');
159
- assert.equal(
160
- extension.callback,
161
- `http://${host}:${port}/PhoneLines.js`
162
- );
163
- assert.equal(body.websocket, `ws://localhost:${port}`);
164
- assert.strictEquals(body.version, EXTENSIONS_MESSAGE_VERSION);
165
- logger.info('- Express serving extension data 🚀');
166
- results.extensionsEndpointPassed = true;
167
- });
168
- }
169
- );
170
- }
171
-
172
- // Test the the web socket server is running on the expected port and
173
- // that we are able to receive messages from it.
174
- function testWebSocketServer(results, logger) {
175
- const ws = new WebSocket(`ws://localhost:${port}`);
176
- ws.on('message', messageBuffer => {
177
- const message = JSON.parse(messageBuffer.toString());
178
- assert(message.event === 'start');
179
- assert.strictEqual(message.appName, 'Example App React UI');
180
- assert.strictEqual(message.title, 'Phone Lines');
181
- assert.strictEqual(message.version, WEBSOCKET_MESSAGE_VERSION);
182
- logger.info('- WebSocket server connected and sending messages 🚀');
183
- results.webSocketTestPassed = true;
184
- ws.close(); // The test passed, close the connection
185
- });
186
- }
187
-
188
- module.exports = {
189
- testDevServer,
190
- };
package/utils.js DELETED
@@ -1,10 +0,0 @@
1
- const path = require('path');
2
-
3
- function getUrlSafeFileName(filePath) {
4
- const { name } = path.parse(filePath);
5
- return encodeURIComponent(`${name}.js`);
6
- }
7
-
8
- module.exports = {
9
- getUrlSafeFileName,
10
- };
File without changes
File without changes