aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/watch.js
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/watch.js')
-rw-r--r--scripts/watch.js52
1 files changed, 34 insertions, 18 deletions
diff --git a/scripts/watch.js b/scripts/watch.js
index a61d3c8..1345a0f 100644
--- a/scripts/watch.js
+++ b/scripts/watch.js
@@ -1,11 +1,12 @@
1import { build as esbuildBuild } from 'esbuild';
2import { spawn } from 'child_process'; 1import { spawn } from 'child_process';
2import { join } from 'path';
3
3import { watch } from 'chokidar'; 4import { watch } from 'chokidar';
4import electronPath from 'electron'; 5import electronPath from 'electron';
5import { join } from 'path'; 6import { build as esbuildBuild } from 'esbuild';
6import { createServer } from 'vite'; 7import { createServer } from 'vite';
7 8
8import { fileURLToDirname } from '../config/utils.js'; 9import fileURLToDirname from '../config/fileURLToDirname.js';
9 10
10/** @type {string} */ 11/** @type {string} */
11const thisDir = fileURLToDirname(import.meta.url); 12const thisDir = fileURLToDirname(import.meta.url);
@@ -35,6 +36,7 @@ const stderrIgnorePatterns = [
35 */ 36 */
36async function setupEsbuildWatcher(packageName, extraPaths, callback) { 37async function setupEsbuildWatcher(packageName, extraPaths, callback) {
37 /** @type {{ default: import('esbuild').BuildOptions }} */ 38 /** @type {{ default: import('esbuild').BuildOptions }} */
39 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- Read untyped config file.
38 const { default: config } = await import(`../packages/${packageName}/esbuild.config.js`); 40 const { default: config } = await import(`../packages/${packageName}/esbuild.config.js`);
39 config.logLevel = 'info'; 41 config.logLevel = 'info';
40 config.incremental = true; 42 config.incremental = true;
@@ -45,7 +47,7 @@ async function setupEsbuildWatcher(packageName, extraPaths, callback) {
45 ...(extraPaths || []), 47 ...(extraPaths || []),
46 ]; 48 ];
47 const watcher = watch(paths, { 49 const watcher = watch(paths, {
48 ignored: /(^|[\/\\])\.|__(tests|mocks)__|\.(spec|test)\.[jt]sx?$/, 50 ignored: /(^|[/\\])\.|__(tests|mocks)__|\.(spec|test)\.[jt]sx?$/,
49 ignoreInitial: true, 51 ignoreInitial: true,
50 persistent: true, 52 persistent: true,
51 }); 53 });
@@ -59,13 +61,26 @@ async function setupEsbuildWatcher(packageName, extraPaths, callback) {
59 callback(); 61 callback();
60 } 62 }
61 }).catch((err) => { 63 }).catch((err) => {
62 const errCount = err.errors.length; 64 if (typeof err === 'object' && 'errors' in err) {
65 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- We just checked.
66 const { errors } = err;
67 if (Array.isArray(errors)) {
68 const errCount = errors.length;
69 console.error(
70 '\ud83d\udd25',
71 errCount,
72 errCount > 1 ? 'errors' : 'error',
73 'while rebuilding package',
74 packageName,
75 );
76 return;
77 }
78 }
63 console.error( 79 console.error(
64 '\ud83d\udd25', 80 '\ud83d\udd25',
65 errCount, 81 'error while rebuilding package',
66 errCount > 1 ? 'errors' : 'error',
67 'while rebuilding package',
68 packageName, 82 packageName,
83 err,
69 ); 84 );
70 }); 85 });
71 }); 86 });
@@ -121,13 +136,14 @@ function setupServicePackageWatcher(packageName, sendEvent) {
121 */ 136 */
122function setupMainPackageWatcher(viteDevServer) { 137function setupMainPackageWatcher(viteDevServer) {
123 // Write a value to an environment variable to pass it to the main process. 138 // Write a value to an environment variable to pass it to the main process.
124 const protocol = `http${viteDevServer.config.server.https ? 's' : ''}:`; 139 const { config: { server: { port, https, host } } } = viteDevServer;
125 const host = viteDevServer.config.server.host || 'localhost'; 140 const protocol = `http${https ? 's' : ''}:`;
126 const port = viteDevServer.config.server.port; 141 const hostOrDefault = typeof host === 'string' ? host : 'localhost';
127 const path = '/'; 142 const portOrDefault = port || 3000;
128 process.env.VITE_DEV_SERVER_URL = `${protocol}//${host}:${port}${path}`; 143 process.env.VITE_DEV_SERVER_URL = `${protocol}//${hostOrDefault}:${portOrDefault}/`;
129 144
130 /** @type {import('child_process').ChildProcessByStdio<null, null, import('stream').Readable> | null} */ 145 /** @type {import('child_process').ChildProcessByStdio<null, null, import('stream').Readable>
146 | null} */
131 let spawnProcess = null; 147 let spawnProcess = null;
132 148
133 return setupEsbuildWatcher( 149 return setupEsbuildWatcher(
@@ -146,8 +162,8 @@ function setupMainPackageWatcher(viteDevServer) {
146 stdio: ['inherit', 'inherit', 'pipe'], 162 stdio: ['inherit', 'inherit', 'pipe'],
147 }); 163 });
148 164
149 spawnProcess.stderr.on('data', (data) => { 165 spawnProcess.stderr.on('data', (/** @type {Buffer} */ data) => {
150 const stderrString = data.toString('utf-8').trimRight(); 166 const stderrString = data.toString('utf-8').trimEnd();
151 if (!stderrIgnorePatterns.some((r) => r.test(stderrString))) { 167 if (!stderrIgnorePatterns.some((r) => r.test(stderrString))) {
152 console.error(stderrString); 168 console.error(stderrString);
153 } 169 }
@@ -193,7 +209,7 @@ async function setupDevEnvironment() {
193 } 209 }
194 210
195 console.log('\ud83c\udf80 Sophie is starting up'); 211 console.log('\ud83c\udf80 Sophie is starting up');
196 return setupMainPackageWatcher(viteDevServer); 212 await setupMainPackageWatcher(viteDevServer);
197} 213}
198 214
199setupDevEnvironment().catch((err) => { 215setupDevEnvironment().catch((err) => {