@mokup/cli 0.2.0 → 0.3.0

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/dist/index.cjs CHANGED
@@ -1,16 +1,23 @@
1
1
  'use strict';
2
2
 
3
3
  const node_fs = require('node:fs');
4
- const node_process = require('node:process');
5
- const pathe = require('pathe');
4
+ const process = require('node:process');
5
+ const pathe = require('@mokup/shared/pathe');
6
6
  const node_buffer = require('node:buffer');
7
7
  const node_module = require('node:module');
8
8
  const node_url = require('node:url');
9
- const esbuild = require('esbuild');
9
+ const esbuild = require('@mokup/shared/esbuild');
10
10
  const runtime = require('@mokup/runtime');
11
- const jsoncParser = require('jsonc-parser');
11
+ const jsoncParser = require('@mokup/shared/jsonc-parser');
12
+ const nodeServer = require('@hono/node-server');
13
+ const server = require('@mokup/server');
14
+ const commander = require('commander');
12
15
 
13
16
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
17
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
18
+
19
+ const process__default = /*#__PURE__*/_interopDefaultCompat(process);
20
+
14
21
  async function writeBundle(outDir, hasHandlers) {
15
22
  const lines = [
16
23
  "import manifest from './mokup.manifest.mjs'"
@@ -574,7 +581,7 @@ async function loadRules(file) {
574
581
  }
575
582
 
576
583
  async function buildManifest(options = {}) {
577
- const root = options.root ?? node_process.cwd();
584
+ const root = options.root ?? process.cwd();
578
585
  const outDir = pathe.resolve(root, options.outDir ?? ".mokup");
579
586
  const handlersDir = pathe.join(outDir, "mokup-handlers");
580
587
  const dirs = resolveDirs(options.dir, root);
@@ -724,4 +731,134 @@ async function buildManifest(options = {}) {
724
731
  };
725
732
  }
726
733
 
734
+ function collectValues(value, previous) {
735
+ return [...previous ?? [], value];
736
+ }
737
+ function collectRegex(value, previous) {
738
+ const next = previous ?? [];
739
+ next.push(new RegExp(value));
740
+ return next;
741
+ }
742
+ function toBuildOptions(options) {
743
+ const buildOptions = {
744
+ handlers: options.handlers !== false,
745
+ log: (message) => {
746
+ console.log(message);
747
+ }
748
+ };
749
+ if (options.dir && options.dir.length > 0) {
750
+ buildOptions.dir = options.dir;
751
+ }
752
+ if (options.out) {
753
+ buildOptions.outDir = options.out;
754
+ }
755
+ if (options.prefix) {
756
+ buildOptions.prefix = options.prefix;
757
+ }
758
+ if (options.include && options.include.length > 0) {
759
+ buildOptions.include = options.include;
760
+ }
761
+ if (options.exclude && options.exclude.length > 0) {
762
+ buildOptions.exclude = options.exclude;
763
+ }
764
+ return buildOptions;
765
+ }
766
+ function toServeOptions(options) {
767
+ const serveOptions = {
768
+ watch: options.watch !== false,
769
+ log: options.log !== false
770
+ };
771
+ if (options.dir && options.dir.length > 0) {
772
+ serveOptions.dir = options.dir;
773
+ }
774
+ if (options.prefix) {
775
+ serveOptions.prefix = options.prefix;
776
+ }
777
+ if (options.include && options.include.length > 0) {
778
+ serveOptions.include = options.include;
779
+ }
780
+ if (options.exclude && options.exclude.length > 0) {
781
+ serveOptions.exclude = options.exclude;
782
+ }
783
+ if (options.host) {
784
+ serveOptions.host = options.host;
785
+ }
786
+ if (typeof options.port === "string" && options.port.length > 0) {
787
+ const parsed = Number(options.port);
788
+ if (!Number.isFinite(parsed)) {
789
+ throw new TypeError(`Invalid port: ${options.port}`);
790
+ }
791
+ serveOptions.port = parsed;
792
+ }
793
+ if (typeof options.playground !== "undefined") {
794
+ serveOptions.playground = options.playground;
795
+ }
796
+ return serveOptions;
797
+ }
798
+ function createCli() {
799
+ const program = new commander.Command();
800
+ program.name("mokup").description("Mock utilities for file-based routes.").showHelpAfterError();
801
+ program.command("build").description("Generate .mokup build output").option("-d, --dir <dir>", "Mock directory (repeatable)", collectValues).option("-o, --out <dir>", "Output directory (default: .mokup)").option("--prefix <prefix>", "URL prefix").option("--include <pattern>", "Include regex (repeatable)", collectRegex).option("--exclude <pattern>", "Exclude regex (repeatable)", collectRegex).option("--no-handlers", "Skip function handler output").action(async (options) => {
802
+ const buildOptions = toBuildOptions(options);
803
+ await buildManifest(buildOptions);
804
+ });
805
+ program.command("serve").description("Start a Node.js mock server").option("-d, --dir <dir>", "Mock directory (repeatable)", collectValues).option("--prefix <prefix>", "URL prefix").option("--include <pattern>", "Include regex (repeatable)", collectRegex).option("--exclude <pattern>", "Exclude regex (repeatable)", collectRegex).option("--host <host>", "Hostname (default: localhost)").option("--port <port>", "Port (default: 8080)").option("--no-watch", "Disable file watching").option("--no-playground", "Disable Playground").option("--no-log", "Disable logging").action(async (options) => {
806
+ const serveOptions = toServeOptions(options);
807
+ const host = serveOptions.host ?? "localhost";
808
+ const port = serveOptions.port ?? 8080;
809
+ const playgroundEnabled = serveOptions.playground !== false;
810
+ const playgroundPath = "/_mokup";
811
+ const server$1 = await server.createFetchServer(serveOptions);
812
+ const nodeServer$1 = nodeServer.serve(
813
+ {
814
+ fetch: server$1.fetch,
815
+ hostname: host,
816
+ port
817
+ },
818
+ (info) => {
819
+ const resolvedHost = typeof info === "string" ? host : info?.address ?? host;
820
+ const resolvedPort = typeof info === "string" ? port : info?.port ?? port;
821
+ console.log(`Mock server ready at http://${resolvedHost}:${resolvedPort}`);
822
+ if (playgroundEnabled) {
823
+ console.log(`Playground at http://${resolvedHost}:${resolvedPort}${playgroundPath}`);
824
+ }
825
+ }
826
+ );
827
+ const shutdown = async () => {
828
+ try {
829
+ if (server$1.close) {
830
+ await server$1.close();
831
+ }
832
+ await new Promise((resolve, reject) => {
833
+ nodeServer$1.close((error) => {
834
+ if (error) {
835
+ reject(error);
836
+ return;
837
+ }
838
+ resolve();
839
+ });
840
+ });
841
+ } finally {
842
+ process__default.exit(0);
843
+ }
844
+ };
845
+ process__default.on("SIGINT", shutdown);
846
+ process__default.on("SIGTERM", shutdown);
847
+ });
848
+ program.command("help").description("Show help").action(() => {
849
+ program.help();
850
+ });
851
+ return program;
852
+ }
853
+ async function runCli(argv = process__default.argv) {
854
+ const program = createCli();
855
+ if (argv.length <= 2) {
856
+ program.help();
857
+ return;
858
+ }
859
+ await program.parseAsync(argv);
860
+ }
861
+
727
862
  exports.buildManifest = buildManifest;
863
+ exports.createCli = createCli;
864
+ exports.runCli = runCli;
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Manifest } from '@mokup/runtime';
2
+ import { Command } from 'commander';
2
3
 
3
4
  interface BuildOptions {
4
5
  dir?: string | string[];
@@ -16,5 +17,8 @@ declare function buildManifest(options?: BuildOptions): Promise<{
16
17
  manifestPath: string;
17
18
  }>;
18
19
 
19
- export { buildManifest };
20
+ declare function createCli(): Command;
21
+ declare function runCli(argv?: string[]): Promise<void>;
22
+
23
+ export { buildManifest, createCli, runCli };
20
24
  export type { BuildOptions };
package/dist/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Manifest } from '@mokup/runtime';
2
+ import { Command } from 'commander';
2
3
 
3
4
  interface BuildOptions {
4
5
  dir?: string | string[];
@@ -16,5 +17,8 @@ declare function buildManifest(options?: BuildOptions): Promise<{
16
17
  manifestPath: string;
17
18
  }>;
18
19
 
19
- export { buildManifest };
20
+ declare function createCli(): Command;
21
+ declare function runCli(argv?: string[]): Promise<void>;
22
+
23
+ export { buildManifest, createCli, runCli };
20
24
  export type { BuildOptions };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Manifest } from '@mokup/runtime';
2
+ import { Command } from 'commander';
2
3
 
3
4
  interface BuildOptions {
4
5
  dir?: string | string[];
@@ -16,5 +17,8 @@ declare function buildManifest(options?: BuildOptions): Promise<{
16
17
  manifestPath: string;
17
18
  }>;
18
19
 
19
- export { buildManifest };
20
+ declare function createCli(): Command;
21
+ declare function runCli(argv?: string[]): Promise<void>;
22
+
23
+ export { buildManifest, createCli, runCli };
20
24
  export type { BuildOptions };
package/dist/index.mjs CHANGED
@@ -1,12 +1,15 @@
1
1
  import { promises } from 'node:fs';
2
- import { cwd } from 'node:process';
3
- import { join, normalize, dirname, resolve, isAbsolute, basename, extname, relative } from 'pathe';
2
+ import process, { cwd } from 'node:process';
3
+ import { join, normalize, dirname, resolve, isAbsolute, basename, extname, relative } from '@mokup/shared/pathe';
4
4
  import { Buffer } from 'node:buffer';
5
5
  import { createRequire } from 'node:module';
6
6
  import { pathToFileURL } from 'node:url';
7
- import { build } from 'esbuild';
7
+ import { build } from '@mokup/shared/esbuild';
8
8
  import { parseRouteTemplate, compareRouteScore } from '@mokup/runtime';
9
- import { parse } from 'jsonc-parser';
9
+ import { parse } from '@mokup/shared/jsonc-parser';
10
+ import { serve } from '@hono/node-server';
11
+ import { createFetchServer } from '@mokup/server';
12
+ import { Command } from 'commander';
10
13
 
11
14
  async function writeBundle(outDir, hasHandlers) {
12
15
  const lines = [
@@ -721,4 +724,132 @@ async function buildManifest(options = {}) {
721
724
  };
722
725
  }
723
726
 
724
- export { buildManifest };
727
+ function collectValues(value, previous) {
728
+ return [...previous ?? [], value];
729
+ }
730
+ function collectRegex(value, previous) {
731
+ const next = previous ?? [];
732
+ next.push(new RegExp(value));
733
+ return next;
734
+ }
735
+ function toBuildOptions(options) {
736
+ const buildOptions = {
737
+ handlers: options.handlers !== false,
738
+ log: (message) => {
739
+ console.log(message);
740
+ }
741
+ };
742
+ if (options.dir && options.dir.length > 0) {
743
+ buildOptions.dir = options.dir;
744
+ }
745
+ if (options.out) {
746
+ buildOptions.outDir = options.out;
747
+ }
748
+ if (options.prefix) {
749
+ buildOptions.prefix = options.prefix;
750
+ }
751
+ if (options.include && options.include.length > 0) {
752
+ buildOptions.include = options.include;
753
+ }
754
+ if (options.exclude && options.exclude.length > 0) {
755
+ buildOptions.exclude = options.exclude;
756
+ }
757
+ return buildOptions;
758
+ }
759
+ function toServeOptions(options) {
760
+ const serveOptions = {
761
+ watch: options.watch !== false,
762
+ log: options.log !== false
763
+ };
764
+ if (options.dir && options.dir.length > 0) {
765
+ serveOptions.dir = options.dir;
766
+ }
767
+ if (options.prefix) {
768
+ serveOptions.prefix = options.prefix;
769
+ }
770
+ if (options.include && options.include.length > 0) {
771
+ serveOptions.include = options.include;
772
+ }
773
+ if (options.exclude && options.exclude.length > 0) {
774
+ serveOptions.exclude = options.exclude;
775
+ }
776
+ if (options.host) {
777
+ serveOptions.host = options.host;
778
+ }
779
+ if (typeof options.port === "string" && options.port.length > 0) {
780
+ const parsed = Number(options.port);
781
+ if (!Number.isFinite(parsed)) {
782
+ throw new TypeError(`Invalid port: ${options.port}`);
783
+ }
784
+ serveOptions.port = parsed;
785
+ }
786
+ if (typeof options.playground !== "undefined") {
787
+ serveOptions.playground = options.playground;
788
+ }
789
+ return serveOptions;
790
+ }
791
+ function createCli() {
792
+ const program = new Command();
793
+ program.name("mokup").description("Mock utilities for file-based routes.").showHelpAfterError();
794
+ program.command("build").description("Generate .mokup build output").option("-d, --dir <dir>", "Mock directory (repeatable)", collectValues).option("-o, --out <dir>", "Output directory (default: .mokup)").option("--prefix <prefix>", "URL prefix").option("--include <pattern>", "Include regex (repeatable)", collectRegex).option("--exclude <pattern>", "Exclude regex (repeatable)", collectRegex).option("--no-handlers", "Skip function handler output").action(async (options) => {
795
+ const buildOptions = toBuildOptions(options);
796
+ await buildManifest(buildOptions);
797
+ });
798
+ program.command("serve").description("Start a Node.js mock server").option("-d, --dir <dir>", "Mock directory (repeatable)", collectValues).option("--prefix <prefix>", "URL prefix").option("--include <pattern>", "Include regex (repeatable)", collectRegex).option("--exclude <pattern>", "Exclude regex (repeatable)", collectRegex).option("--host <host>", "Hostname (default: localhost)").option("--port <port>", "Port (default: 8080)").option("--no-watch", "Disable file watching").option("--no-playground", "Disable Playground").option("--no-log", "Disable logging").action(async (options) => {
799
+ const serveOptions = toServeOptions(options);
800
+ const host = serveOptions.host ?? "localhost";
801
+ const port = serveOptions.port ?? 8080;
802
+ const playgroundEnabled = serveOptions.playground !== false;
803
+ const playgroundPath = "/_mokup";
804
+ const server = await createFetchServer(serveOptions);
805
+ const nodeServer = serve(
806
+ {
807
+ fetch: server.fetch,
808
+ hostname: host,
809
+ port
810
+ },
811
+ (info) => {
812
+ const resolvedHost = typeof info === "string" ? host : info?.address ?? host;
813
+ const resolvedPort = typeof info === "string" ? port : info?.port ?? port;
814
+ console.log(`Mock server ready at http://${resolvedHost}:${resolvedPort}`);
815
+ if (playgroundEnabled) {
816
+ console.log(`Playground at http://${resolvedHost}:${resolvedPort}${playgroundPath}`);
817
+ }
818
+ }
819
+ );
820
+ const shutdown = async () => {
821
+ try {
822
+ if (server.close) {
823
+ await server.close();
824
+ }
825
+ await new Promise((resolve, reject) => {
826
+ nodeServer.close((error) => {
827
+ if (error) {
828
+ reject(error);
829
+ return;
830
+ }
831
+ resolve();
832
+ });
833
+ });
834
+ } finally {
835
+ process.exit(0);
836
+ }
837
+ };
838
+ process.on("SIGINT", shutdown);
839
+ process.on("SIGTERM", shutdown);
840
+ });
841
+ program.command("help").description("Show help").action(() => {
842
+ program.help();
843
+ });
844
+ return program;
845
+ }
846
+ async function runCli(argv = process.argv) {
847
+ const program = createCli();
848
+ if (argv.length <= 2) {
849
+ program.help();
850
+ return;
851
+ }
852
+ await program.parseAsync(argv);
853
+ }
854
+
855
+ export { buildManifest, createCli, runCli };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mokup/cli",
3
3
  "type": "module",
4
- "version": "0.2.0",
4
+ "version": "0.3.0",
5
5
  "description": "CLI for building mokup manifests and handlers.",
6
6
  "license": "MIT",
7
7
  "homepage": "https://mokup.icebreaker.top",
@@ -27,10 +27,11 @@
27
27
  "dist"
28
28
  ],
29
29
  "dependencies": {
30
- "esbuild": "^0.27.2",
31
- "jsonc-parser": "^3.3.1",
32
- "pathe": "^2.0.3",
33
- "@mokup/runtime": "0.1.0"
30
+ "@hono/node-server": "^1.19.9",
31
+ "commander": "^14.0.0",
32
+ "@mokup/runtime": "0.1.1",
33
+ "@mokup/server": "1.0.0",
34
+ "@mokup/shared": "0.1.0"
34
35
  },
35
36
  "devDependencies": {
36
37
  "@types/node": "^25.0.9",