mnfst-run 1.0.0 → 1.0.1
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/package.json +2 -2
- package/serve.mjs +16 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mnfst-run",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Zero-dependency dev server for Manifest projects",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -27,4 +27,4 @@
|
|
|
27
27
|
"url": "git+https://github.com/andrewmatlock/Manifest.git",
|
|
28
28
|
"directory": "packages/run"
|
|
29
29
|
}
|
|
30
|
-
}
|
|
30
|
+
}
|
package/serve.mjs
CHANGED
|
@@ -16,9 +16,10 @@
|
|
|
16
16
|
* Live reload: CSS changes hot-swap the stylesheet without a page reload.
|
|
17
17
|
* All other file changes trigger a full browser refresh.
|
|
18
18
|
*/
|
|
19
|
-
import { createServer }
|
|
19
|
+
import { createServer } from 'http';
|
|
20
20
|
import { readFileSync, statSync, watch } from 'fs';
|
|
21
|
-
import { join, extname, resolve }
|
|
21
|
+
import { join, extname, resolve, basename, relative } from 'path';
|
|
22
|
+
import { exec } from 'child_process';
|
|
22
23
|
|
|
23
24
|
const MIME = {
|
|
24
25
|
'.html': 'text/html; charset=utf-8',
|
|
@@ -166,6 +167,16 @@ const server = createServer((req, res) => {
|
|
|
166
167
|
});
|
|
167
168
|
|
|
168
169
|
// --- Auto-port ---
|
|
170
|
+
// Human-readable label: the dir arg as given, or the cwd folder name if serving '.'
|
|
171
|
+
const label = dir === '.' ? basename(process.cwd()) : dir.replace(/\\/g, '/');
|
|
172
|
+
|
|
173
|
+
function openBrowser(url) {
|
|
174
|
+
const cmd = process.platform === 'win32' ? `start ${url}`
|
|
175
|
+
: process.platform === 'darwin' ? `open ${url}`
|
|
176
|
+
: `xdg-open ${url}`;
|
|
177
|
+
exec(cmd);
|
|
178
|
+
}
|
|
179
|
+
|
|
169
180
|
function tryListen(p, attempt = 0) {
|
|
170
181
|
if (attempt > 20) {
|
|
171
182
|
console.error('mnfst-run: could not find a free port after 20 attempts.');
|
|
@@ -176,9 +187,9 @@ function tryListen(p, attempt = 0) {
|
|
|
176
187
|
else throw err;
|
|
177
188
|
});
|
|
178
189
|
server.listen(p, () => {
|
|
179
|
-
|
|
180
|
-
console.log(`\n
|
|
181
|
-
|
|
190
|
+
const url = `http://localhost:${p}`;
|
|
191
|
+
console.log(`\n${label} running at ${url}\n`);
|
|
192
|
+
openBrowser(url);
|
|
182
193
|
});
|
|
183
194
|
}
|
|
184
195
|
|