diff --git a/dev/scripts/lib/server.mjs b/dev/scripts/lib/server.mjs index b9a04bb..f8294b4 100644 --- a/dev/scripts/lib/server.mjs +++ b/dev/scripts/lib/server.mjs @@ -11,7 +11,7 @@ const __dirname = path.dirname(__filename); const root = path.resolve(__dirname, '../../..'); -const port = 5114; +const port = process.env.PORT ?? 8514; let server; export async function main(options) { @@ -21,29 +21,59 @@ export async function main(options) { `http://${process.env.HOST ?? 'localhost'}${req.url}`, ).pathname; debug(`Got request: ${req.method} '${reqUrl}'`); - const url = '.' + reqUrl; - const filePath = path.join(root, url); - const extension = path.extname(filePath).slice(1); - if (req.method === 'GET') { - if (reqUrl === '/') { - res.writeHead(302, { Location: '/demos/index.html' }); - res.end(); - return; - } + + const getFileStat = async (filePath) => { if (!fs.existsSync(filePath)) { - res.writeHead(404); - res.end(`Cannot ${req.method} ${reqUrl}: Not Found`); - return; + return 'NOT_FOUND'; } if (!(await fs.promises.lstat(filePath)).isFile()) { - res.writeHead(404); - res.end(`Cannot ${req.method} ${reqUrl}: Is not a file`); - return; + return 'NOT_A_FILE'; } + return 'EXISTS'; + }; + const sendFile = async (res, filePath) => { + const extension = path.extname(filePath).slice(1); const mimeType = MIMETypes[extension] || 'text/plain'; res.setHeader('Content-Type', mimeType); res.setHeader('X-Real-File-Path', filePath); fs.createReadStream(filePath).pipe(res); + }; + const resolveAndSendFile = async (reqUrl) => { + const filePath = path.join(root, reqUrl); + if (reqUrl === '/') { + res.writeHead(302, { Location: '/demos/index.html' }); + res.end(); + return; + } + const stat = await getFileStat(filePath); + if (stat !== 'EXISTS') { + // try to resolve the file as .js + if (path.extname(filePath) !== '.js') { + const jsFilePath = `${filePath}.js`; + const jsStat = await getFileStat(jsFilePath); + if (jsStat === 'EXISTS') { + sendFile(res, jsFilePath); + return; + } + } + + if (stat === 'NOT_FOUND') { + res.writeHead(404); + res.end(`Cannot ${req.method} ${reqUrl}: Not Found`); + return; + } + if (stat === 'NOT_A_FILE') { + res.writeHead(404); + res.end(`Cannot ${req.method} ${reqUrl}: Is not a file`); + return; + } + } else { + sendFile(res, filePath); + } + }; + + if (req.method === 'GET') { + resolveAndSendFile(reqUrl); return; } else { res.writeHead(405); diff --git a/dev/scripts/lib/watch.mjs b/dev/scripts/lib/watch.mjs index 0ce5357..451de24 100644 --- a/dev/scripts/lib/watch.mjs +++ b/dev/scripts/lib/watch.mjs @@ -9,6 +9,17 @@ const __dirname = path.dirname(__filename); const root = path.resolve(__dirname, '../../..'); +const ignoredPaths = [ + 'node_modules', + 'test', + 'dist', + 'coverage', + 'dev', + '.vscode', + '.github', + '.git', +]; + const watcherAbortController = new AbortController(); const taskStack = []; let taskProcessing = false; @@ -30,12 +41,9 @@ export async function main(options) { return; } const normalizedPathname = path.resolve(root, filename); - const isIgnored = - checkPath(path.join(root, '.__compile_cache__'), normalizedPathname) || - checkPath(path.join(root, 'node_modules'), normalizedPathname) || - checkPath(path.join(root, 'dist'), normalizedPathname) || - checkPath(path.join(root, 'dev'), normalizedPathname) || - checkPath(path.join(root, '.git'), normalizedPathname); + const isIgnored = ignoredPaths.some((ignoredPath) => + checkPath(path.join(root, ignoredPath), normalizedPathname), + ); if (isIgnored) { return; }