wirejs-scripts 3.0.100 → 3.0.102

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.
Files changed (2) hide show
  1. package/bin.js +71 -15
  2. package/package.json +2 -2
package/bin.js CHANGED
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import process from 'process';
4
+ import { execSync, spawn } from 'child_process';
4
5
  import http from 'http';
5
- import fs from 'fs';
6
+ import fs, { watch } from 'fs';
6
7
  import path from 'path';
7
- import { Worker } from 'worker_threads';
8
8
 
9
9
  import webpack from 'webpack';
10
10
  import webpackConfigure from './configs/webpack.config.js';
@@ -19,7 +19,8 @@ import { prebuildApi } from 'wirejs-resources/internal';
19
19
 
20
20
  const CWD = process.cwd();
21
21
  const getWebpackConfig = () => webpackConfigure(process.env, process.argv);
22
- const [_nodeBinPath, _scriptPath, action] = process.argv;
22
+ const [_nodeBinPath, _scriptPath, action, ...actionArgs] = process.argv;
23
+ const [subAction] = actionArgs;
23
24
  const processes = [];
24
25
 
25
26
  const logger = {
@@ -132,7 +133,7 @@ async function handleApiResponse(req, res) {
132
133
  if (cookie.maxAge) cookieOptions.push(`Max-Age=${cookie.maxAge}`);
133
134
  if (cookie.httpOnly) cookieOptions.push('HttpOnly');
134
135
  if (cookie.secure) cookieOptions.push('Secure');
135
-
136
+
136
137
  logger.info('setting cookie', cookie.name, cookie.value, cookieOptions);
137
138
  res.appendHeader(
138
139
  'Set-Cookie',
@@ -196,7 +197,7 @@ async function tryStaticPath(req, res) {
196
197
  * @returns
197
198
  */
198
199
  function byLength(a, b) {
199
- return a.length - b.length;
200
+ return a.length - b.length;
200
201
  }
201
202
 
202
203
  /**
@@ -205,9 +206,9 @@ function byLength(a, b) {
205
206
  * @returns
206
207
  */
207
208
  function globMatch(pattern, text) {
208
- const parts = pattern.split('%');
209
- const regex = new RegExp(parts.join('.+'));
210
- return regex.test(text);
209
+ const parts = pattern.split('%');
210
+ const regex = new RegExp(parts.join('.+'));
211
+ return regex.test(text);
211
212
  }
212
213
 
213
214
  function toJSPath(path) {
@@ -236,7 +237,7 @@ function routeSSR(context, forceExt) {
236
237
  const allHandlers = fs.readdirSync(SSR_ROOT, { recursive: true })
237
238
  .filter(p => p.endsWith('.js'))
238
239
  .map(p => `/${p}`)
239
- ;
240
+ ;
240
241
  const matchingHandlers = allHandlers.filter(h => globMatch(h, asJSPath));
241
242
  const match = matchingHandlers.sort(byLength).pop();
242
243
 
@@ -308,13 +309,13 @@ async function trySSRPath(req, res) {
308
309
  globalThis.pendingDehydrations.shift()(doc);
309
310
  hydrationsFound++;
310
311
  }
311
-
312
+
312
313
  if (hydrationsFound) {
313
314
  const script = doc.parentNode.createElement('script');
314
315
  script.src = asJSPath;
315
316
  doc.parentNode.body.appendChild(script);
316
317
  }
317
-
318
+
318
319
  res.setHeader('Content-type', 'text/html; charset=utf-8');
319
320
  res.end([
320
321
  doctype ? `<!doctype ${doctype}>\n` : '',
@@ -384,7 +385,6 @@ async function compile(watch = false) {
384
385
  const stats = await new Promise(async (resolve, reject) => {
385
386
  let compiler;
386
387
  if (watch) {
387
-
388
388
  const apiDir = path.join(CWD, 'api');
389
389
  const prebuildApiWatch = await esbuild.context({
390
390
  entryPoints: [path.join(CWD, 'api', '**', '*.ts')],
@@ -402,7 +402,7 @@ async function compile(watch = false) {
402
402
  });
403
403
  await prebuildApiWatch.rebuild();
404
404
  prebuildApiWatch.watch();
405
-
405
+
406
406
  const prebuild = await esbuild.context({
407
407
  entryPoints: [`${CWD}/src/**/*.ts`],
408
408
  outdir: `${CWD}/pre-dist`,
@@ -420,14 +420,14 @@ async function compile(watch = false) {
420
420
  }, () => {
421
421
  console.log();
422
422
  console.log('Compiled: http://localhost:3000/');
423
- }).run(() => {});
423
+ }).run(() => { });
424
424
 
425
425
  logger.log('Starting server...');
426
426
  const server = http.createServer(handleRequest);
427
427
  server.listen(3000).on('listening', () => {
428
428
  console.log('Started listening on http://localhost:3000/')
429
429
  });
430
-
430
+
431
431
  } else {
432
432
  logger.log('prebundling JS');
433
433
  await esbuild.build({
@@ -462,6 +462,62 @@ async function compile(watch = false) {
462
462
  }
463
463
 
464
464
  const engine = {
465
+ async ['ws-run-parallel']() {
466
+ const scriptName = subAction;
467
+ const workspaces = JSON.parse(fs.readFileSync("./package.json")).workspaces;
468
+ for (const workspaceDir of workspaces) {
469
+ const pkg = JSON.parse(fs.readFileSync(
470
+ path.join(workspaceDir, 'package.json'),
471
+ 'utf8'
472
+ ));
473
+ if (pkg.scripts && pkg.scripts[scriptName]) {
474
+ processes.push(spawn(
475
+ 'npm',
476
+ ['run', scriptName, '--workspace', pkg.name],
477
+ { stdio: 'inherit' }
478
+ ));
479
+ }
480
+ }
481
+ await new Promise(resolve => {
482
+ function exitGracefully() {
483
+ logger.log('Exiting gracefully ...');
484
+ processes.forEach(p => p.kill());
485
+ resolve();
486
+ }
487
+ process.on('SIGINT', exitGracefully);
488
+ process.on('SIGTERM', exitGracefully);
489
+ });
490
+ },
491
+
492
+ async ['watch']() {
493
+ const [directory, cmd, ...cmdArgs] = actionArgs;
494
+ const runWatchCommand = () => {
495
+ try {
496
+ execSync([cmd, ...cmdArgs].join(' '));
497
+ } catch (error) {
498
+ console.error(error);
499
+ }
500
+ };
501
+ await new Promise(resolve => {
502
+ runWatchCommand();
503
+ let debounce = undefined;
504
+ const watcher = fs.watch(directory, { recursive: true }, () => {
505
+ if (debounce) clearTimeout(debounce);
506
+ debounce = setTimeout(() => {
507
+ debounce = undefined;
508
+ runWatchCommand();
509
+ }, 250);
510
+ });
511
+ function exitGracefully() {
512
+ logger.log('Exiting gracefully ...');
513
+ watcher.close();
514
+ resolve();
515
+ }
516
+ process.on('SIGINT', exitGracefully);
517
+ process.on('SIGTERM', exitGracefully);
518
+ });
519
+ },
520
+
465
521
  async build({ watch = false } = {}) {
466
522
  logger.log('build starting');
467
523
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wirejs-scripts",
3
- "version": "3.0.100",
3
+ "version": "3.0.102",
4
4
  "description": "Basic build and start commands for wirejs apps",
5
5
  "type": "module",
6
6
  "bin": {
@@ -35,6 +35,6 @@
35
35
  "style-loader": "^2.0.0",
36
36
  "webpack": "^5.97.1",
37
37
  "wirejs-dom": "^1.0.41",
38
- "wirejs-resources": "^0.1.102"
38
+ "wirejs-resources": "^0.1.104"
39
39
  }
40
40
  }