From d07e7b834831230b53860d0919a68edc2d36193d Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Sat, 8 Jan 2022 21:36:43 +0100 Subject: build: Eslint fixes for multi-module project MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kristóf Marussy --- scripts/build.js | 6 +++-- scripts/update-electron-vendors.js | 14 +++++----- scripts/watch.js | 52 +++++++++++++++++++++++++------------- 3 files changed, 46 insertions(+), 26 deletions(-) (limited to 'scripts') diff --git a/scripts/build.js b/scripts/build.js index ef2b998..1236a6c 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -1,8 +1,9 @@ -import { build as esbuildBuild } from 'esbuild'; import { join } from 'path'; + +import { build as esbuildBuild } from 'esbuild'; import { build as viteBuild } from 'vite'; -import { fileURLToDirname } from '../config/utils.js'; +import fileURLToDirname from '../config/fileURLToDirname.js'; const thisDir = fileURLToDirname(import.meta.url); @@ -12,6 +13,7 @@ const thisDir = fileURLToDirname(import.meta.url); */ async function buildPackageEsbuild(packageName) { /** @type {{ default: import('esbuild').BuildOptions }} */ + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- Read untyped config file. const { default: config } = await import(`../packages/${packageName}/esbuild.config.js`); return esbuildBuild(config); } diff --git a/scripts/update-electron-vendors.js b/scripts/update-electron-vendors.js index 70d3afc..91cdb61 100644 --- a/scripts/update-electron-vendors.js +++ b/scripts/update-electron-vendors.js @@ -1,9 +1,10 @@ import { execSync } from 'child_process'; -import electronPath from 'electron'; import { writeFile } from 'fs/promises'; import { join } from 'path'; -import { fileURLToDirname } from '../config/utils.js'; +import electronPath from 'electron'; + +import fileURLToDirname from '../config/fileURLToDirname.js'; const thisDir = fileURLToDirname(import.meta.url); @@ -15,11 +16,12 @@ const thisDir = fileURLToDirname(import.meta.url); * @returns {NodeJS.ProcessVersions} */ function getVendors() { - const output = execSync(`${electronPath} -p "JSON.stringify(process.versions)"`, { - env: { 'ELECTRON_RUN_AS_NODE': '1' }, + const output = execSync(`${electronPath.toString()} -p "JSON.stringify(process.versions)"`, { + env: { ELECTRON_RUN_AS_NODE: '1' }, encoding: 'utf-8', }); + // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- Read untyped output. return JSON.parse(output); } @@ -39,10 +41,10 @@ function updateVendors() { return Promise.all([ writeFile( join(thisDir, '../.electron-vendors.cache.json'), - JSON.stringify({ + `${JSON.stringify({ chrome: chromeMajorVersion, node: nodeMajorVersion, - }, null, 2) + '\n', + }, null, 2)}\n`, ), writeFile(browserslistrcPath, `Chrome ${chromeMajorVersion}\n`, 'utf8'), 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 @@ -import { build as esbuildBuild } from 'esbuild'; import { spawn } from 'child_process'; +import { join } from 'path'; + import { watch } from 'chokidar'; import electronPath from 'electron'; -import { join } from 'path'; +import { build as esbuildBuild } from 'esbuild'; import { createServer } from 'vite'; -import { fileURLToDirname } from '../config/utils.js'; +import fileURLToDirname from '../config/fileURLToDirname.js'; /** @type {string} */ const thisDir = fileURLToDirname(import.meta.url); @@ -35,6 +36,7 @@ const stderrIgnorePatterns = [ */ async function setupEsbuildWatcher(packageName, extraPaths, callback) { /** @type {{ default: import('esbuild').BuildOptions }} */ + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- Read untyped config file. const { default: config } = await import(`../packages/${packageName}/esbuild.config.js`); config.logLevel = 'info'; config.incremental = true; @@ -45,7 +47,7 @@ async function setupEsbuildWatcher(packageName, extraPaths, callback) { ...(extraPaths || []), ]; const watcher = watch(paths, { - ignored: /(^|[\/\\])\.|__(tests|mocks)__|\.(spec|test)\.[jt]sx?$/, + ignored: /(^|[/\\])\.|__(tests|mocks)__|\.(spec|test)\.[jt]sx?$/, ignoreInitial: true, persistent: true, }); @@ -59,13 +61,26 @@ async function setupEsbuildWatcher(packageName, extraPaths, callback) { callback(); } }).catch((err) => { - const errCount = err.errors.length; + if (typeof err === 'object' && 'errors' in err) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- We just checked. + const { errors } = err; + if (Array.isArray(errors)) { + const errCount = errors.length; + console.error( + '\ud83d\udd25', + errCount, + errCount > 1 ? 'errors' : 'error', + 'while rebuilding package', + packageName, + ); + return; + } + } console.error( '\ud83d\udd25', - errCount, - errCount > 1 ? 'errors' : 'error', - 'while rebuilding package', + 'error while rebuilding package', packageName, + err, ); }); }); @@ -121,13 +136,14 @@ function setupServicePackageWatcher(packageName, sendEvent) { */ function setupMainPackageWatcher(viteDevServer) { // Write a value to an environment variable to pass it to the main process. - const protocol = `http${viteDevServer.config.server.https ? 's' : ''}:`; - const host = viteDevServer.config.server.host || 'localhost'; - const port = viteDevServer.config.server.port; - const path = '/'; - process.env.VITE_DEV_SERVER_URL = `${protocol}//${host}:${port}${path}`; - - /** @type {import('child_process').ChildProcessByStdio | null} */ + const { config: { server: { port, https, host } } } = viteDevServer; + const protocol = `http${https ? 's' : ''}:`; + const hostOrDefault = typeof host === 'string' ? host : 'localhost'; + const portOrDefault = port || 3000; + process.env.VITE_DEV_SERVER_URL = `${protocol}//${hostOrDefault}:${portOrDefault}/`; + + /** @type {import('child_process').ChildProcessByStdio + | null} */ let spawnProcess = null; return setupEsbuildWatcher( @@ -146,8 +162,8 @@ function setupMainPackageWatcher(viteDevServer) { stdio: ['inherit', 'inherit', 'pipe'], }); - spawnProcess.stderr.on('data', (data) => { - const stderrString = data.toString('utf-8').trimRight(); + spawnProcess.stderr.on('data', (/** @type {Buffer} */ data) => { + const stderrString = data.toString('utf-8').trimEnd(); if (!stderrIgnorePatterns.some((r) => r.test(stderrString))) { console.error(stderrString); } @@ -193,7 +209,7 @@ async function setupDevEnvironment() { } console.log('\ud83c\udf80 Sophie is starting up'); - return setupMainPackageWatcher(viteDevServer); + await setupMainPackageWatcher(viteDevServer); } setupDevEnvironment().catch((err) => { -- cgit v1.2.3-54-g00ecf