@nowline/cli 0.6.0 → 0.8.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 +1 @@
1
- {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/convert/schema.ts"],"names":[],"mappings":"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"}
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/convert/schema.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,OAAO,EAEH,sBAAsB,EAItB,eAAe,GAClB,MAAM,eAAe,CAAC"}
@@ -1,4 +1,4 @@
1
- export declare const CLI_VERSION = "0.6.0";
1
+ export declare const CLI_VERSION = "0.8.0";
2
2
  export interface CliBuild {
3
3
  /** Short git SHA at build time, or empty when not in a git checkout. */
4
4
  readonly sha: string;
@@ -1,7 +1,7 @@
1
1
  // Auto-generated by scripts/bundle-templates.mjs. Do not edit by hand.
2
- export const CLI_VERSION = "0.6.0";
2
+ export const CLI_VERSION = "0.8.0";
3
3
  export const CLI_BUILD = {
4
- "sha": "e2e4d9d",
4
+ "sha": "871752d",
5
5
  "isRelease": true,
6
6
  "isDirty": false
7
7
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nowline/cli",
3
- "version": "0.6.0",
4
- "description": "Nowline command-line interface validate, convert, init",
3
+ "version": "0.8.0",
4
+ "description": "Command-line tool to check, convert, and create Nowline roadmaps.",
5
5
  "license": "Apache-2.0",
6
6
  "engines": {
7
7
  "node": ">=22",
@@ -38,19 +38,19 @@
38
38
  "consola": "^3.4.2",
39
39
  "js-yaml": "^4.1.1",
40
40
  "langium": "~4.2.4",
41
- "@nowline/config": "0.6.0",
42
- "@nowline/export": "0.6.0",
43
- "@nowline/mcp": "0.6.0",
44
- "@nowline/export-html": "0.6.0",
45
- "@nowline/core": "0.6.0",
46
- "@nowline/export-core": "0.6.0",
47
- "@nowline/export-mermaid": "0.6.0",
48
- "@nowline/export-pdf": "0.6.0",
49
- "@nowline/export-png": "0.6.0",
50
- "@nowline/export-xlsx": "0.6.0",
51
- "@nowline/renderer": "0.6.0",
52
- "@nowline/export-msproj": "0.6.0",
53
- "@nowline/layout": "0.6.0"
41
+ "@nowline/config": "0.8.0",
42
+ "@nowline/core": "0.8.0",
43
+ "@nowline/mcp": "0.8.0",
44
+ "@nowline/export": "0.8.0",
45
+ "@nowline/export-core": "0.8.0",
46
+ "@nowline/export-html": "0.8.0",
47
+ "@nowline/export-msproj": "0.8.0",
48
+ "@nowline/export-png": "0.8.0",
49
+ "@nowline/export-pdf": "0.8.0",
50
+ "@nowline/export-mermaid": "0.8.0",
51
+ "@nowline/layout": "0.8.0",
52
+ "@nowline/export-xlsx": "0.8.0",
53
+ "@nowline/renderer": "0.8.0"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@resvg/resvg-wasm": "^2.6.2",
@@ -1,21 +1,58 @@
1
+ import { createServer } from 'node:http';
1
2
  import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
3
+ import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
2
4
  import { createMcpServer } from '@nowline/mcp/server';
3
5
  import type { ParsedArgs } from '../cli/args.js';
4
6
 
5
7
  /**
6
8
  * `nowline --mcp` handler.
7
9
  *
8
- * Starts a Model Context Protocol stdio server sharing the same @nowline/mcp
9
- * server factory as `npx @nowline/mcp`. Runs until the process receives
10
- * SIGINT/SIGTERM or the client closes stdin.
10
+ * Starts a Model Context Protocol server sharing the same @nowline/mcp
11
+ * server factory as `npx @nowline/mcp`.
12
+ *
13
+ * Transport selection:
14
+ * stdio (default) — when --port is absent.
15
+ * Streamable HTTP — when --port <N> is supplied; listens on localhost:<N>.
16
+ *
17
+ * Runs until the process receives SIGINT/SIGTERM or the client closes stdin.
11
18
  */
12
19
  export async function mcpHandler({ args }: { args: ParsedArgs }): Promise<void> {
13
20
  const root = args.root ?? process.cwd();
14
21
  const server = createMcpServer({ allowedRoot: root });
15
- const transport = new StdioServerTransport();
16
- await server.connect(transport);
17
- // Keep alive: the transport closes when stdin closes or the process is signalled.
18
- await new Promise<void>((resolve) => {
19
- transport.onclose = resolve;
20
- });
22
+
23
+ const portStr = args.port;
24
+ if (portStr !== undefined) {
25
+ const port = parseInt(portStr, 10);
26
+ if (!Number.isFinite(port) || port < 1 || port > 65535) {
27
+ throw new Error(
28
+ `nowline: --port must be a number between 1 and 65535 (got ${portStr}).`,
29
+ );
30
+ }
31
+
32
+ const transport = new StreamableHTTPServerTransport({
33
+ sessionIdGenerator: undefined,
34
+ });
35
+ await server.connect(transport);
36
+
37
+ const httpServer = createServer(async (req, res) => {
38
+ await transport.handleRequest(req, res);
39
+ });
40
+
41
+ await new Promise<void>((resolve) => {
42
+ httpServer.listen(port, '127.0.0.1', () => {
43
+ process.stderr.write(
44
+ `nowline: MCP Streamable HTTP listening on http://127.0.0.1:${port}\n`,
45
+ );
46
+ });
47
+ httpServer.on('close', resolve);
48
+ process.on('SIGINT', () => httpServer.close());
49
+ process.on('SIGTERM', () => httpServer.close());
50
+ });
51
+ } else {
52
+ const transport = new StdioServerTransport();
53
+ await server.connect(transport);
54
+ await new Promise<void>((resolve) => {
55
+ transport.onclose = resolve;
56
+ });
57
+ }
21
58
  }
@@ -1,57 +1,2 @@
1
- import { CliError, ExitCode } from '../io/exit-codes.js';
2
- import { type JsonAstNode, NOWLINE_SCHEMA_VERSION, type NowlineDocument } from './schema.js';
3
-
4
- export interface ParseJsonResult {
5
- document: NowlineDocument;
6
- ast: JsonAstNode;
7
- }
8
-
9
- export function parseNowlineJson(text: string, filePath: string): ParseJsonResult {
10
- let parsed: unknown;
11
- try {
12
- parsed = JSON.parse(text);
13
- } catch (err) {
14
- throw new CliError(
15
- ExitCode.ValidationError,
16
- `${filePath}: invalid JSON — ${err instanceof Error ? err.message : String(err)}`,
17
- );
18
- }
19
- const doc = parsed;
20
- if (!isRecord(doc)) {
21
- throw new CliError(
22
- ExitCode.ValidationError,
23
- `${filePath}: JSON root must be an object with $nowlineSchema and ast.`,
24
- );
25
- }
26
- const schema = doc.$nowlineSchema;
27
- if (typeof schema !== 'string') {
28
- throw new CliError(
29
- ExitCode.ValidationError,
30
- `${filePath}: missing "$nowlineSchema" at document root.`,
31
- );
32
- }
33
- if (schema !== NOWLINE_SCHEMA_VERSION) {
34
- throw new CliError(
35
- ExitCode.ValidationError,
36
- `${filePath}: unsupported $nowlineSchema "${schema}" (this CLI supports "${NOWLINE_SCHEMA_VERSION}").`,
37
- );
38
- }
39
- const ast = doc.ast;
40
- if (!isRecord(ast) || typeof ast.$type !== 'string') {
41
- throw new CliError(
42
- ExitCode.ValidationError,
43
- `${filePath}: document.ast must be an object with a "$type" field.`,
44
- );
45
- }
46
- if (ast.$type !== 'NowlineFile') {
47
- throw new CliError(
48
- ExitCode.ValidationError,
49
- `${filePath}: document.ast.$type must be "NowlineFile" (got "${String(ast.$type)}").`,
50
- );
51
- }
52
- return { document: doc as unknown as NowlineDocument, ast: ast as unknown as JsonAstNode };
53
- }
54
-
55
- function isRecord(value: unknown): value is Record<string, unknown> {
56
- return typeof value === 'object' && value !== null && !Array.isArray(value);
57
- }
1
+ // Re-exported from @nowline/core. Kept here for backwards compatibility.
2
+ export { type ParseJsonResult, parseNowlineJson } from '@nowline/core';
@@ -1,376 +1,2 @@
1
- import { CliError, ExitCode } from '../io/exit-codes.js';
2
- import type { JsonAstNode } from './schema.js';
3
-
4
- // Keyed-property canonical order. Keys not in this list sort alphabetically after.
5
- const KEY_ORDER = [
6
- 'date',
7
- 'effort',
8
- 'on',
9
- 'size',
10
- 'duration',
11
- 'status',
12
- 'owner',
13
- 'after',
14
- 'before',
15
- 'remaining',
16
- 'labels',
17
- 'style',
18
- 'link',
19
- 'author',
20
- 'start',
21
- 'scale',
22
- 'calendar',
23
- 'header-position',
24
- 'timeline-position',
25
- 'minor-grid',
26
- ];
27
-
28
- const INDENT = ' ';
29
-
30
- export interface PrintOptions {
31
- indent?: string;
32
- }
33
-
34
- export function printNowlineFile(ast: JsonAstNode, options: PrintOptions = {}): string {
35
- const printer = new Printer(options.indent ?? INDENT);
36
- printer.file(ast);
37
- return printer.toString();
38
- }
39
-
40
- class Printer {
41
- private readonly lines: string[] = [];
42
-
43
- constructor(private readonly indent: string) {}
44
-
45
- toString(): string {
46
- const text = this.lines.join('\n');
47
- return text.endsWith('\n') ? text : `${text}\n`;
48
- }
49
-
50
- file(file: JsonAstNode): void {
51
- assertType(file, 'NowlineFile');
52
- const directive = file.directive as JsonAstNode | undefined;
53
- if (directive) {
54
- const props = asArray(directive.properties);
55
- const tail = props.length > 0 ? ` ${props.map(renderProperty).join(' ')}` : '';
56
- this.line(0, `nowline ${getString(directive, 'version')}${tail}`);
57
- this.blank();
58
- }
59
- for (const inc of asArray(file.includes)) {
60
- this.include(inc);
61
- }
62
- if (asArray(file.includes).length > 0) this.blank();
63
- if (file.hasConfig) {
64
- this.line(0, 'config');
65
- this.blank();
66
- for (const entry of asArray(file.configEntries)) {
67
- this.configEntry(entry);
68
- this.blank();
69
- }
70
- }
71
- if (file.roadmapDecl) {
72
- this.roadmap(file.roadmapDecl as JsonAstNode);
73
- this.blank();
74
- }
75
- for (const entry of asArray(file.roadmapEntries)) {
76
- this.roadmapEntry(entry, 0);
77
- }
78
- }
79
-
80
- include(inc: JsonAstNode): void {
81
- assertType(inc, 'IncludeDeclaration');
82
- const path = getString(inc, 'path');
83
- const options = asArray(inc.options)
84
- .map((o) => `${getString(o, 'key')}:${getString(o, 'value')}`)
85
- .join(' ');
86
- const tail = options ? ` ${options}` : '';
87
- this.line(0, `include ${JSON.stringify(path)}${tail}`);
88
- }
89
-
90
- configEntry(entry: JsonAstNode): void {
91
- switch (entry.$type) {
92
- case 'ScaleBlock':
93
- return this.blockDecl('scale', asArray(entry.properties));
94
- case 'CalendarBlock':
95
- return this.blockDecl('calendar', asArray(entry.properties));
96
- case 'StyleDeclaration':
97
- return this.styleDecl(entry);
98
- case 'DefaultDeclaration':
99
- return this.defaultDecl(entry);
100
- default:
101
- throw new CliError(
102
- ExitCode.ValidationError,
103
- `Unknown config entry type: ${String(entry.$type)}`,
104
- );
105
- }
106
- }
107
-
108
- blockDecl(keyword: string, properties: JsonAstNode[]): void {
109
- this.line(0, keyword);
110
- for (const p of properties) {
111
- this.line(1, renderBlockProperty(p));
112
- }
113
- }
114
-
115
- styleDecl(entry: JsonAstNode): void {
116
- assertType(entry, 'StyleDeclaration');
117
- const header = declarationHeader('style', entry, []);
118
- this.line(0, header);
119
- for (const p of asArray(entry.properties)) {
120
- this.line(1, renderBlockProperty(p));
121
- }
122
- }
123
-
124
- defaultDecl(entry: JsonAstNode): void {
125
- assertType(entry, 'DefaultDeclaration');
126
- const entityType = getString(entry, 'entityType');
127
- const props = renderProperties(asArray(entry.properties));
128
- const tail = props ? ` ${props}` : '';
129
- this.line(0, `default ${entityType}${tail}`);
130
- }
131
-
132
- roadmap(decl: JsonAstNode): void {
133
- assertType(decl, 'RoadmapDeclaration');
134
- this.line(0, declarationHeader('roadmap', decl, asArray(decl.properties)));
135
- }
136
-
137
- roadmapEntry(entry: JsonAstNode, depth: number): void {
138
- switch (entry.$type) {
139
- case 'PersonDeclaration':
140
- return this.simpleEntity('person', entry, depth);
141
- case 'TeamDeclaration':
142
- return this.team(entry, depth);
143
- case 'AnchorDeclaration':
144
- return this.simpleEntity('anchor', entry, depth);
145
- case 'SizeDeclaration':
146
- return this.simpleEntity('size', entry, depth);
147
- case 'StatusDeclaration':
148
- return this.simpleEntity('status', entry, depth);
149
- case 'LabelDeclaration':
150
- return this.simpleEntity('label', entry, depth);
151
- case 'MilestoneDeclaration':
152
- return this.simpleEntity('milestone', entry, depth);
153
- case 'FootnoteDeclaration':
154
- return this.simpleEntity('footnote', entry, depth);
155
- case 'SwimlaneDeclaration':
156
- return this.swimlane(entry, depth);
157
- default:
158
- throw new CliError(
159
- ExitCode.ValidationError,
160
- `Unknown roadmap entry type: ${String(entry.$type)}`,
161
- );
162
- }
163
- }
164
-
165
- simpleEntity(keyword: string, entry: JsonAstNode, depth: number): void {
166
- this.line(depth, declarationHeader(keyword, entry, asArray(entry.properties)));
167
- this.maybeDescription(entry, depth + 1);
168
- }
169
-
170
- team(entry: JsonAstNode, depth: number): void {
171
- this.line(depth, declarationHeader('team', entry, asArray(entry.properties)));
172
- for (const child of asArray(entry.content)) {
173
- this.teamContent(child, depth + 1);
174
- }
175
- }
176
-
177
- teamContent(node: JsonAstNode, depth: number): void {
178
- if (node.$type === 'PersonMemberRef') {
179
- this.line(depth, `person ${getString(node, 'ref')}`);
180
- return;
181
- }
182
- if (node.$type === 'TeamDeclaration') {
183
- return this.team(node, depth);
184
- }
185
- if (node.$type === 'PersonDeclaration') {
186
- return this.simpleEntity('person', node, depth);
187
- }
188
- if (node.$type === 'DescriptionDirective') {
189
- return this.descriptionDirective(node, depth);
190
- }
191
- throw new CliError(
192
- ExitCode.ValidationError,
193
- `Unknown team content type: ${String(node.$type)}`,
194
- );
195
- }
196
-
197
- swimlane(entry: JsonAstNode, depth: number): void {
198
- this.line(depth, declarationHeader('swimlane', entry, asArray(entry.properties)));
199
- for (const child of asArray(entry.content)) {
200
- this.swimlaneContent(child, depth + 1);
201
- }
202
- }
203
-
204
- swimlaneContent(node: JsonAstNode, depth: number): void {
205
- switch (node.$type) {
206
- case 'ItemDeclaration':
207
- this.simpleEntity('item', node, depth);
208
- return;
209
- case 'ParallelBlock':
210
- this.parallelBlock(node, depth);
211
- return;
212
- case 'GroupBlock':
213
- this.groupBlock(node, depth);
214
- return;
215
- case 'DescriptionDirective':
216
- this.descriptionDirective(node, depth);
217
- return;
218
- default:
219
- throw new CliError(
220
- ExitCode.ValidationError,
221
- `Unknown swimlane content type: ${String(node.$type)}`,
222
- );
223
- }
224
- }
225
-
226
- parallelBlock(entry: JsonAstNode, depth: number): void {
227
- this.line(depth, declarationHeader('parallel', entry, asArray(entry.properties)));
228
- for (const child of asArray(entry.content)) {
229
- this.swimlaneContent(child, depth + 1);
230
- }
231
- }
232
-
233
- groupBlock(entry: JsonAstNode, depth: number): void {
234
- this.line(depth, declarationHeader('group', entry, asArray(entry.properties)));
235
- for (const child of asArray(entry.content)) {
236
- this.swimlaneContent(child, depth + 1);
237
- }
238
- }
239
-
240
- descriptionDirective(node: JsonAstNode, depth: number): void {
241
- assertType(node, 'DescriptionDirective');
242
- this.line(depth, `description ${JSON.stringify(getString(node, 'text'))}`);
243
- }
244
-
245
- maybeDescription(entry: JsonAstNode, depth: number): void {
246
- const desc = entry.description as JsonAstNode | undefined;
247
- if (desc) this.descriptionDirective(desc, depth);
248
- }
249
-
250
- private line(depth: number, text: string): void {
251
- this.lines.push(this.indent.repeat(depth) + text);
252
- }
253
-
254
- private blank(): void {
255
- if (this.lines.length === 0) return;
256
- if (this.lines[this.lines.length - 1] === '') return;
257
- this.lines.push('');
258
- }
259
- }
260
-
261
- function declarationHeader(keyword: string, entry: JsonAstNode, properties: JsonAstNode[]): string {
262
- const id = entry.name as string | undefined;
263
- const title = entry.title as string | undefined;
264
- const parts = [keyword];
265
- if (id) parts.push(id);
266
- if (title) parts.push(JSON.stringify(title));
267
- const props = renderProperties(properties);
268
- if (props) parts.push(props);
269
- return parts.join(' ');
270
- }
271
-
272
- function renderProperties(properties: JsonAstNode[]): string {
273
- return orderProperties(properties).map(renderProperty).join(' ');
274
- }
275
-
276
- function orderProperties(properties: JsonAstNode[]): JsonAstNode[] {
277
- const indexOf = new Map(KEY_ORDER.map((k, i) => [k, i] as const));
278
- return [...properties].sort((a, b) => {
279
- const ak = normalizeKey(getString(a, 'key'));
280
- const bk = normalizeKey(getString(b, 'key'));
281
- const ai = indexOf.get(ak);
282
- const bi = indexOf.get(bk);
283
- if (ai !== undefined && bi !== undefined) return ai - bi;
284
- if (ai !== undefined) return -1;
285
- if (bi !== undefined) return 1;
286
- return ak.localeCompare(bk);
287
- });
288
- }
289
-
290
- function renderProperty(prop: JsonAstNode): string {
291
- const key = normalizeKey(getString(prop, 'key'));
292
- const values = asStringArray(prop.values);
293
- const value = prop.value as string | undefined;
294
- if (values.length >= 2) {
295
- return `${key}:[${values.map(formatAtom).join(', ')}]`;
296
- }
297
- if (values.length === 1) {
298
- return `${key}:${formatAtom(values[0])}`;
299
- }
300
- if (value !== undefined && value !== '') {
301
- return `${key}:${formatAtom(value)}`;
302
- }
303
- return `${key}:`;
304
- }
305
-
306
- // Block-style property (one per line, rendered as `key: value`). Used inside
307
- // indented blocks like `scale`, `calendar`, and `style`. Uses formatAtom so
308
- // that quoted / template strings such as `label: "W{n}"` survive a round-trip.
309
- function renderBlockProperty(prop: JsonAstNode): string {
310
- const key = normalizeKey(getString(prop, 'key'));
311
- const values = asStringArray(prop.values);
312
- const value = prop.value as string | undefined;
313
- if (values.length >= 2) {
314
- return `${key}: [${values.map(formatAtom).join(', ')}]`;
315
- }
316
- if (values.length === 1) {
317
- return `${key}: ${formatAtom(values[0])}`;
318
- }
319
- if (value !== undefined && value !== '') {
320
- return `${key}: ${formatAtom(value)}`;
321
- }
322
- return `${key}:`;
323
- }
324
-
325
- function normalizeKey(key: string): string {
326
- return key.endsWith(':') ? key.slice(0, -1) : key;
327
- }
328
-
329
- const URL_RE = /^https?:\/\//;
330
- const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
331
- const DURATION_RE = /^\d+(?:\.\d+)?[dwmqy]$/;
332
- const PERCENTAGE_RE = /^\d+%$/;
333
- const HEX_COLOR_RE = /^#[0-9a-fA-F]{3,8}$/;
334
- const INTEGER_RE = /^\d+$/;
335
- const ID_RE = /^[a-zA-Z_][a-zA-Z0-9_-]*$/;
336
-
337
- function formatAtom(atom: string): string {
338
- if (
339
- URL_RE.test(atom) ||
340
- DATE_RE.test(atom) ||
341
- DURATION_RE.test(atom) ||
342
- PERCENTAGE_RE.test(atom) ||
343
- HEX_COLOR_RE.test(atom) ||
344
- INTEGER_RE.test(atom) ||
345
- ID_RE.test(atom)
346
- ) {
347
- return atom;
348
- }
349
- // Already-quoted string survives a round trip through JSON.stringify by re-quoting once.
350
- if (atom.startsWith('"') && atom.endsWith('"')) return atom;
351
- return JSON.stringify(atom);
352
- }
353
-
354
- function asArray(value: unknown): JsonAstNode[] {
355
- if (Array.isArray(value)) return value as JsonAstNode[];
356
- return [];
357
- }
358
-
359
- function asStringArray(value: unknown): string[] {
360
- if (!Array.isArray(value)) return [];
361
- return value.filter((v): v is string => typeof v === 'string');
362
- }
363
-
364
- function getString(node: JsonAstNode | Record<string, unknown>, key: string): string {
365
- const value = (node as Record<string, unknown>)[key];
366
- return typeof value === 'string' ? value : '';
367
- }
368
-
369
- function assertType(node: JsonAstNode, expected: string): void {
370
- if (node.$type !== expected) {
371
- throw new CliError(
372
- ExitCode.ValidationError,
373
- `Expected $type "${expected}", got "${String(node.$type)}"`,
374
- );
375
- }
376
- }
1
+ // Re-exported from @nowline/core. Kept here for backwards compatibility.
2
+ export { type PrintOptions, printNowlineFile } from '@nowline/core';