@unvt/charites 2.1.3 → 2.1.5
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 +46 -22
- package/package.json +1 -1
- package/src/commands/serve.ts +52 -31
package/dist/commands/serve.js
CHANGED
|
@@ -67,7 +67,28 @@ export async function serve(source, options) {
|
|
|
67
67
|
try {
|
|
68
68
|
style = parser(sourcePath);
|
|
69
69
|
if (typeof spriteOut !== 'undefined') {
|
|
70
|
-
|
|
70
|
+
const spriteUrl = `http://${req.headers.host || `localhost:${port}`}/sprite`;
|
|
71
|
+
if (typeof style.sprite === 'string') {
|
|
72
|
+
// update a single sprite URL
|
|
73
|
+
style.sprite = spriteUrl;
|
|
74
|
+
}
|
|
75
|
+
else if (Array.isArray(style.sprite)) {
|
|
76
|
+
// if sprite is an array, update default sprite URL.
|
|
77
|
+
// if default sprite is not found, add default url.
|
|
78
|
+
if (style.sprite.find((s) => s.id === 'default')) {
|
|
79
|
+
for (const s of style.sprite) {
|
|
80
|
+
if (s.id === 'default') {
|
|
81
|
+
s.url = spriteUrl;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
style.sprite.push({
|
|
87
|
+
id: 'default',
|
|
88
|
+
url: spriteUrl,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
71
92
|
}
|
|
72
93
|
validateStyle(style);
|
|
73
94
|
}
|
|
@@ -120,35 +141,38 @@ export async function serve(source, options) {
|
|
|
120
141
|
}
|
|
121
142
|
});
|
|
122
143
|
const wss = new WebSocketServer({ server });
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}));
|
|
131
|
-
}
|
|
132
|
-
else if (file?.toLowerCase().endsWith('.svg') &&
|
|
133
|
-
typeof spriteRefresher !== 'undefined') {
|
|
134
|
-
spriteRefresher().then(() => {
|
|
135
|
-
ws.send(JSON.stringify({
|
|
136
|
-
event: 'spriteUpdate',
|
|
137
|
-
}));
|
|
138
|
-
});
|
|
144
|
+
const sockets = new Set();
|
|
145
|
+
const watcher = watch(path.dirname(sourcePath), { recursive: true, filter: /\.yml$|\.svg$/i }, (event, file) => {
|
|
146
|
+
console.log(`${(event || '').toUpperCase()}: ${file}`);
|
|
147
|
+
try {
|
|
148
|
+
if (file?.toLowerCase().endsWith('.yml')) {
|
|
149
|
+
for (const client of sockets) {
|
|
150
|
+
client.send(JSON.stringify({ event: 'styleUpdate' }));
|
|
139
151
|
}
|
|
140
152
|
}
|
|
141
|
-
|
|
142
|
-
|
|
153
|
+
else if (file?.toLowerCase().endsWith('.svg') &&
|
|
154
|
+
typeof spriteRefresher !== 'undefined') {
|
|
155
|
+
spriteRefresher().then(() => {
|
|
156
|
+
for (const client of sockets) {
|
|
157
|
+
client.send(JSON.stringify({ event: 'spriteUpdate' }));
|
|
158
|
+
}
|
|
159
|
+
});
|
|
143
160
|
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
161
|
+
}
|
|
162
|
+
catch (_) {
|
|
163
|
+
// Nothing to do
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
wss.on('connection', (ws) => {
|
|
167
|
+
sockets.add(ws);
|
|
168
|
+
ws.on('close', () => {
|
|
169
|
+
sockets.delete(ws);
|
|
147
170
|
});
|
|
148
171
|
});
|
|
149
172
|
process.on('SIGINT', () => {
|
|
150
173
|
console.log('Cleaning up...');
|
|
151
174
|
server.close();
|
|
175
|
+
watcher.close();
|
|
152
176
|
if (typeof spriteOut !== 'undefined') {
|
|
153
177
|
fs.rmSync(spriteOut, { recursive: true });
|
|
154
178
|
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
|
|
|
@@ -86,9 +87,29 @@ export async function serve(source: string, options: serveOptions) {
|
|
|
86
87
|
try {
|
|
87
88
|
style = parser(sourcePath)
|
|
88
89
|
if (typeof spriteOut !== 'undefined') {
|
|
89
|
-
|
|
90
|
+
const spriteUrl = `http://${
|
|
90
91
|
req.headers.host || `localhost:${port}`
|
|
91
92
|
}/sprite`
|
|
93
|
+
|
|
94
|
+
if (typeof style.sprite === 'string') {
|
|
95
|
+
// update a single sprite URL
|
|
96
|
+
style.sprite = spriteUrl
|
|
97
|
+
} else if (Array.isArray(style.sprite)) {
|
|
98
|
+
// if sprite is an array, update default sprite URL.
|
|
99
|
+
// if default sprite is not found, add default url.
|
|
100
|
+
if (style.sprite.find((s) => s.id === 'default')) {
|
|
101
|
+
for (const s of style.sprite) {
|
|
102
|
+
if (s.id === 'default') {
|
|
103
|
+
s.url = spriteUrl
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
style.sprite.push({
|
|
108
|
+
id: 'default',
|
|
109
|
+
url: spriteUrl,
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
}
|
|
92
113
|
}
|
|
93
114
|
validateStyle(style)
|
|
94
115
|
} catch (error) {
|
|
@@ -145,45 +166,45 @@ export async function serve(source: string, options: serveOptions) {
|
|
|
145
166
|
})
|
|
146
167
|
|
|
147
168
|
const wss = new WebSocketServer({ server })
|
|
169
|
+
const sockets = new Set<WSWebSocket>()
|
|
148
170
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
(event
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
JSON.stringify({
|
|
159
|
-
event: 'styleUpdate',
|
|
160
|
-
}),
|
|
161
|
-
)
|
|
162
|
-
} else if (
|
|
163
|
-
file?.toLowerCase().endsWith('.svg') &&
|
|
164
|
-
typeof spriteRefresher !== 'undefined'
|
|
165
|
-
) {
|
|
166
|
-
spriteRefresher().then(() => {
|
|
167
|
-
ws.send(
|
|
168
|
-
JSON.stringify({
|
|
169
|
-
event: 'spriteUpdate',
|
|
170
|
-
}),
|
|
171
|
-
)
|
|
172
|
-
})
|
|
171
|
+
const watcher = watch(
|
|
172
|
+
path.dirname(sourcePath),
|
|
173
|
+
{ recursive: true, filter: /\.yml$|\.svg$/i },
|
|
174
|
+
(event: string, file: string) => {
|
|
175
|
+
console.log(`${(event || '').toUpperCase()}: ${file}`)
|
|
176
|
+
try {
|
|
177
|
+
if (file?.toLowerCase().endsWith('.yml')) {
|
|
178
|
+
for (const client of sockets) {
|
|
179
|
+
client.send(JSON.stringify({ event: 'styleUpdate' }))
|
|
173
180
|
}
|
|
174
|
-
}
|
|
175
|
-
|
|
181
|
+
} else if (
|
|
182
|
+
file?.toLowerCase().endsWith('.svg') &&
|
|
183
|
+
typeof spriteRefresher !== 'undefined'
|
|
184
|
+
) {
|
|
185
|
+
spriteRefresher().then(() => {
|
|
186
|
+
for (const client of sockets) {
|
|
187
|
+
client.send(JSON.stringify({ event: 'spriteUpdate' }))
|
|
188
|
+
}
|
|
189
|
+
})
|
|
176
190
|
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
191
|
+
} catch (_) {
|
|
192
|
+
// Nothing to do
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
wss.on('connection', (ws: WSWebSocket) => {
|
|
198
|
+
sockets.add(ws)
|
|
199
|
+
ws.on('close', () => {
|
|
200
|
+
sockets.delete(ws)
|
|
181
201
|
})
|
|
182
202
|
})
|
|
183
203
|
|
|
184
204
|
process.on('SIGINT', () => {
|
|
185
205
|
console.log('Cleaning up...')
|
|
186
206
|
server.close()
|
|
207
|
+
watcher.close()
|
|
187
208
|
if (typeof spriteOut !== 'undefined') {
|
|
188
209
|
fs.rmSync(spriteOut, { recursive: true })
|
|
189
210
|
spriteOut = undefined
|