@unvt/charites 2.1.2 → 2.1.4
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.
- package/dist/commands/serve.js +32 -21
- package/package.json +1 -1
- package/src/commands/serve.ts +39 -30
package/dist/commands/serve.js
CHANGED
|
@@ -38,6 +38,14 @@ export async function serve(source, options) {
|
|
|
38
38
|
}
|
|
39
39
|
const server = http.createServer(async (req, res) => {
|
|
40
40
|
const url = (req.url || '').replace(/\?.*/, '');
|
|
41
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
42
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS');
|
|
43
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
44
|
+
if (req.method === 'OPTIONS') {
|
|
45
|
+
res.statusCode = 204;
|
|
46
|
+
res.end();
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
41
49
|
if (typeof spriteOut !== 'undefined' &&
|
|
42
50
|
url.match(/^\/sprite(@2x)?\.(json|png)/)) {
|
|
43
51
|
res.statusCode = 200;
|
|
@@ -112,35 +120,38 @@ export async function serve(source, options) {
|
|
|
112
120
|
}
|
|
113
121
|
});
|
|
114
122
|
const wss = new WebSocketServer({ server });
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}));
|
|
123
|
-
}
|
|
124
|
-
else if (file?.toLowerCase().endsWith('.svg') &&
|
|
125
|
-
typeof spriteRefresher !== 'undefined') {
|
|
126
|
-
spriteRefresher().then(() => {
|
|
127
|
-
ws.send(JSON.stringify({
|
|
128
|
-
event: 'spriteUpdate',
|
|
129
|
-
}));
|
|
130
|
-
});
|
|
123
|
+
const sockets = new Set();
|
|
124
|
+
const watcher = watch(path.dirname(sourcePath), { recursive: true, filter: /\.yml$|\.svg$/i }, (event, file) => {
|
|
125
|
+
console.log(`${(event || '').toUpperCase()}: ${file}`);
|
|
126
|
+
try {
|
|
127
|
+
if (file?.toLowerCase().endsWith('.yml')) {
|
|
128
|
+
for (const client of sockets) {
|
|
129
|
+
client.send(JSON.stringify({ event: 'styleUpdate' }));
|
|
131
130
|
}
|
|
132
131
|
}
|
|
133
|
-
|
|
134
|
-
|
|
132
|
+
else if (file?.toLowerCase().endsWith('.svg') &&
|
|
133
|
+
typeof spriteRefresher !== 'undefined') {
|
|
134
|
+
spriteRefresher().then(() => {
|
|
135
|
+
for (const client of sockets) {
|
|
136
|
+
client.send(JSON.stringify({ event: 'spriteUpdate' }));
|
|
137
|
+
}
|
|
138
|
+
});
|
|
135
139
|
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
|
|
140
|
+
}
|
|
141
|
+
catch (_) {
|
|
142
|
+
// Nothing to do
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
wss.on('connection', (ws) => {
|
|
146
|
+
sockets.add(ws);
|
|
147
|
+
ws.on('close', () => {
|
|
148
|
+
sockets.delete(ws);
|
|
139
149
|
});
|
|
140
150
|
});
|
|
141
151
|
process.on('SIGINT', () => {
|
|
142
152
|
console.log('Cleaning up...');
|
|
143
153
|
server.close();
|
|
154
|
+
watcher.close();
|
|
144
155
|
if (typeof spriteOut !== 'undefined') {
|
|
145
156
|
fs.rmSync(spriteOut, { recursive: true });
|
|
146
157
|
spriteOut = undefined;
|
package/package.json
CHANGED
package/src/commands/serve.ts
CHANGED
|
@@ -5,6 +5,7 @@ import http from 'http'
|
|
|
5
5
|
import open from 'open'
|
|
6
6
|
// tweak to fix WebSocketServer is not a constructor
|
|
7
7
|
const ws = await import('ws')
|
|
8
|
+
import type { WebSocket as WSWebSocket } from 'ws'
|
|
8
9
|
const WebSocketServer = ws.default.WebSocketServer || ws.WebSocketServer
|
|
9
10
|
import watch from 'node-watch'
|
|
10
11
|
|
|
@@ -54,6 +55,14 @@ export async function serve(source: string, options: serveOptions) {
|
|
|
54
55
|
|
|
55
56
|
const server = http.createServer(async (req, res) => {
|
|
56
57
|
const url = (req.url || '').replace(/\?.*/, '')
|
|
58
|
+
res.setHeader('Access-Control-Allow-Origin', '*')
|
|
59
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS')
|
|
60
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type')
|
|
61
|
+
if (req.method === 'OPTIONS') {
|
|
62
|
+
res.statusCode = 204
|
|
63
|
+
res.end()
|
|
64
|
+
return
|
|
65
|
+
}
|
|
57
66
|
|
|
58
67
|
if (
|
|
59
68
|
typeof spriteOut !== 'undefined' &&
|
|
@@ -137,45 +146,45 @@ export async function serve(source: string, options: serveOptions) {
|
|
|
137
146
|
})
|
|
138
147
|
|
|
139
148
|
const wss = new WebSocketServer({ server })
|
|
149
|
+
const sockets = new Set<WSWebSocket>()
|
|
140
150
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
(event
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
JSON.stringify({
|
|
151
|
-
event: 'styleUpdate',
|
|
152
|
-
}),
|
|
153
|
-
)
|
|
154
|
-
} else if (
|
|
155
|
-
file?.toLowerCase().endsWith('.svg') &&
|
|
156
|
-
typeof spriteRefresher !== 'undefined'
|
|
157
|
-
) {
|
|
158
|
-
spriteRefresher().then(() => {
|
|
159
|
-
ws.send(
|
|
160
|
-
JSON.stringify({
|
|
161
|
-
event: 'spriteUpdate',
|
|
162
|
-
}),
|
|
163
|
-
)
|
|
164
|
-
})
|
|
151
|
+
const watcher = watch(
|
|
152
|
+
path.dirname(sourcePath),
|
|
153
|
+
{ recursive: true, filter: /\.yml$|\.svg$/i },
|
|
154
|
+
(event: string, file: string) => {
|
|
155
|
+
console.log(`${(event || '').toUpperCase()}: ${file}`)
|
|
156
|
+
try {
|
|
157
|
+
if (file?.toLowerCase().endsWith('.yml')) {
|
|
158
|
+
for (const client of sockets) {
|
|
159
|
+
client.send(JSON.stringify({ event: 'styleUpdate' }))
|
|
165
160
|
}
|
|
166
|
-
}
|
|
167
|
-
|
|
161
|
+
} else if (
|
|
162
|
+
file?.toLowerCase().endsWith('.svg') &&
|
|
163
|
+
typeof spriteRefresher !== 'undefined'
|
|
164
|
+
) {
|
|
165
|
+
spriteRefresher().then(() => {
|
|
166
|
+
for (const client of sockets) {
|
|
167
|
+
client.send(JSON.stringify({ event: 'spriteUpdate' }))
|
|
168
|
+
}
|
|
169
|
+
})
|
|
168
170
|
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
171
|
+
} catch (_) {
|
|
172
|
+
// Nothing to do
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
wss.on('connection', (ws: WSWebSocket) => {
|
|
178
|
+
sockets.add(ws)
|
|
179
|
+
ws.on('close', () => {
|
|
180
|
+
sockets.delete(ws)
|
|
173
181
|
})
|
|
174
182
|
})
|
|
175
183
|
|
|
176
184
|
process.on('SIGINT', () => {
|
|
177
185
|
console.log('Cleaning up...')
|
|
178
186
|
server.close()
|
|
187
|
+
watcher.close()
|
|
179
188
|
if (typeof spriteOut !== 'undefined') {
|
|
180
189
|
fs.rmSync(spriteOut, { recursive: true })
|
|
181
190
|
spriteOut = undefined
|