@nowline/cli 0.12.1 → 0.13.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.
@@ -1,54 +1,33 @@
1
- import { createServer } from 'node:http';
2
- import { StdioServerTransport } from '@modelcontextprotocol/server/stdio';
3
- import { createMcpHttpHandler } from '@nowline/mcp/http';
4
- import { createMcpServer } from '@nowline/mcp/server';
1
+ import { InvalidPortError, startMcpServer } from '@nowline/mcp/start';
5
2
  import type { ParsedArgs } from '../cli/args.js';
3
+ import { CliError, ExitCode } from '../io/exit-codes.js';
6
4
  import { createLog } from '../io/log.js';
7
5
 
8
6
  /**
9
7
  * `nowline --mcp` handler.
10
8
  *
11
- * Starts a Model Context Protocol server sharing the same @nowline/mcp
12
- * server factory as `npx @nowline/mcp`.
9
+ * Starts the Model Context Protocol server through the same `startMcpServer`
10
+ * that `npx @nowline/mcp` calls, so `--root` (expansion and the binary-export
11
+ * delivery default), `--port` and the reported version behave identically.
13
12
  *
14
13
  * Transport selection:
15
14
  * stdio (default) — when --port is absent.
16
- * Streamable HTTP — when --port <N> is supplied; listens on localhost:<N>.
15
+ * Streamable HTTP — when --port <N> is supplied; listens on 127.0.0.1:<N>.
17
16
  *
18
17
  * Runs until the process receives SIGINT/SIGTERM or the client closes stdin.
19
18
  */
20
19
  export async function mcpHandler({ args }: { args: ParsedArgs }): Promise<void> {
21
- // Without --root the server follows the client's MCP roots, else cwd.
22
- const root = args.root;
23
-
24
- const portStr = args.port;
25
- if (portStr !== undefined) {
26
- const port = parseInt(portStr, 10);
27
- if (!Number.isFinite(port) || port < 1 || port > 65535) {
28
- throw new Error(
29
- `nowline: --port must be a number between 1 and 65535 (got ${portStr}).`,
30
- );
31
- }
32
-
33
- const httpServer = createServer(
34
- createMcpHttpHandler(() => createMcpServer({ allowedRoot: root })),
35
- );
36
-
37
- await new Promise<void>((resolve) => {
38
- httpServer.listen(port, '127.0.0.1', () => {
39
- createLog(args.logLevel).notice(
40
- `nowline: MCP Streamable HTTP listening on http://127.0.0.1:${port}`,
41
- );
42
- });
43
- httpServer.on('close', resolve);
44
- process.on('SIGINT', () => httpServer.close());
45
- process.on('SIGTERM', () => httpServer.close());
46
- });
47
- } else {
48
- const transport = new StdioServerTransport();
49
- await createMcpServer({ allowedRoot: root }).connect(transport);
50
- await new Promise<void>((resolve) => {
51
- transport.onclose = resolve;
20
+ try {
21
+ await startMcpServer({
22
+ root: args.root,
23
+ port: args.port,
24
+ onListening: (url) =>
25
+ createLog(args.logLevel).notice(`nowline: MCP Streamable HTTP listening on ${url}`),
52
26
  });
27
+ } catch (err) {
28
+ if (err instanceof InvalidPortError) {
29
+ throw new CliError(ExitCode.InputError, `nowline: ${err.message}`);
30
+ }
31
+ throw err;
53
32
  }
54
33
  }
@@ -123,18 +123,18 @@ export async function renderHandler(options: RenderHandlerOptions): Promise<void
123
123
  noLinks: args.noLinks,
124
124
  strict: args.strict,
125
125
  assetRoot: cliOptions.assetRoot,
126
- // m2c format-specific options. The PDF settings parse in `produce`,
127
- // after validation, for every format.
126
+ // m2c format-specific options. The PDF settings and `--start` are
127
+ // checked in `produce`, after validation, for every format.
128
128
  pageSize: cliOptions.surface.pageSize,
129
129
  orientation: cliOptions.orientation,
130
130
  marginPt: cliOptions.margin,
131
- assertPdfSettingsValid: () => cliOptions.assertPdfSettingsValid(),
131
+ assertExportSettingsValid: () => cliOptions.assertExportSettingsValid(),
132
132
  fontSans: cliOptions.fontSans,
133
133
  fontMono: cliOptions.fontMono,
134
134
  headless: cliOptions.headless,
135
135
  useSystemFonts: cliOptions.useSystemFonts,
136
136
  scale: args.scale,
137
- start: args.start,
137
+ start: cliOptions.start,
138
138
  locale,
139
139
  operatorLocale: opLocale,
140
140
  resolvedLocale: resolved,
@@ -219,8 +219,8 @@ interface ProduceArgs {
219
219
  orientation?: PdfOrientation;
220
220
  /** Page margin in points. */
221
221
  marginPt?: number;
222
- /** Reports a bad PDF setting; run after validation so its error follows any diagnostics. */
223
- assertPdfSettingsValid: () => void;
222
+ /** Reports a bad PDF setting or `--start`; run after validation so its error follows any diagnostics. */
223
+ assertExportSettingsValid: () => void;
224
224
  fontSans?: string;
225
225
  fontMono?: string;
226
226
  headless: boolean;
@@ -299,7 +299,7 @@ async function produce(args: ProduceArgs): Promise<ProduceResult> {
299
299
  fonts = await resolveNodeFonts(args);
300
300
  }
301
301
 
302
- args.assertPdfSettingsValid();
302
+ args.assertExportSettingsValid();
303
303
 
304
304
  const kernelInputs: RenderInputs = {
305
305
  ...layoutInputs,
@@ -445,9 +445,13 @@ async function resolveNodeFonts(
445
445
  async function produceCanonicalNowline(args: ProduceArgs): Promise<string> {
446
446
  if (args.inputFormat === 'json') {
447
447
  const { ast } = parseNowlineJson(args.contents, args.displayPath);
448
+ args.assertExportSettingsValid();
448
449
  return printNowlineFile(ast);
449
450
  }
450
451
  const parsed = await parseAndValidate(args.contents, args);
452
+ // The format-specific settings don't apply here, but a bad one is the same
453
+ // usage error for every format.
454
+ args.assertExportSettingsValid();
451
455
  const doc = serializeToJson(parsed.document, args.contents);
452
456
  return printNowlineFile(doc.ast);
453
457
  }
package/src/core/parse.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import {
2
+ buildTransientDocument,
2
3
  collectDocumentDiagnostics,
3
4
  createNowlineServices,
4
5
  type NowlineFile,
@@ -41,11 +42,7 @@ export async function parseSource(
41
42
  filePath: string,
42
43
  options: ParseOptions = {},
43
44
  ): Promise<ParseResult> {
44
- const services = getServices();
45
- const uri = uriFor(filePath);
46
- const docFactory = services.shared.workspace.LangiumDocumentFactory;
47
- const doc = docFactory.fromString<NowlineFile>(contents, uri);
48
- await services.shared.workspace.DocumentBuilder.build([doc], {
45
+ const doc = await buildTransientDocument(getServices().shared, contents, uriFor(filePath), {
49
46
  validation: options.validate ?? true,
50
47
  });
51
48
 
@@ -1,6 +1,6 @@
1
1
  // Auto-generated by scripts/bundle-templates.mjs. Do not edit by hand.
2
2
 
3
- export const CLI_VERSION = "0.12.1";
3
+ export const CLI_VERSION = "0.13.0";
4
4
 
5
5
  export interface CliBuild {
6
6
  /** Short git SHA at build time, or empty when not in a git checkout. */
@@ -12,7 +12,7 @@ export interface CliBuild {
12
12
  }
13
13
 
14
14
  export const CLI_BUILD: CliBuild = {
15
- "sha": "fd731a2",
15
+ "sha": "348d8c6",
16
16
  "isRelease": true,
17
17
  "isDirty": false
18
18
  };