@nowline/cli 0.5.1 → 0.7.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.
Files changed (50) hide show
  1. package/dist/cli/args.d.ts +17 -3
  2. package/dist/cli/args.d.ts.map +1 -1
  3. package/dist/cli/args.js +16 -1
  4. package/dist/cli/args.js.map +1 -1
  5. package/dist/cli/help.d.ts.map +1 -1
  6. package/dist/cli/help.js +42 -2
  7. package/dist/cli/help.js.map +1 -1
  8. package/dist/commands/mcp.d.ts +17 -0
  9. package/dist/commands/mcp.d.ts.map +1 -0
  10. package/dist/commands/mcp.js +50 -0
  11. package/dist/commands/mcp.js.map +1 -0
  12. package/dist/commands/render.d.ts +1 -1
  13. package/dist/commands/render.d.ts.map +1 -1
  14. package/dist/commands/render.js +149 -156
  15. package/dist/commands/render.js.map +1 -1
  16. package/dist/commands/serve.d.ts.map +1 -1
  17. package/dist/commands/serve.js +20 -12
  18. package/dist/commands/serve.js.map +1 -1
  19. package/dist/convert/parse-json.d.ts +1 -6
  20. package/dist/convert/parse-json.d.ts.map +1 -1
  21. package/dist/convert/parse-json.js +2 -33
  22. package/dist/convert/parse-json.js.map +1 -1
  23. package/dist/convert/printer.d.ts +1 -5
  24. package/dist/convert/printer.d.ts.map +1 -1
  25. package/dist/convert/printer.js +2 -333
  26. package/dist/convert/printer.js.map +1 -1
  27. package/dist/convert/schema.d.ts +1 -32
  28. package/dist/convert/schema.d.ts.map +1 -1
  29. package/dist/convert/schema.js +2 -76
  30. package/dist/convert/schema.js.map +1 -1
  31. package/dist/generated/version.d.ts +1 -1
  32. package/dist/generated/version.js +2 -2
  33. package/dist/index.js +4 -0
  34. package/dist/index.js.map +1 -1
  35. package/dist/resvg.wasm +0 -0
  36. package/man/nowline.1 +41 -3
  37. package/package.json +18 -13
  38. package/scripts/bun-entry.mjs +25 -0
  39. package/scripts/compile.mjs +11 -6
  40. package/scripts/copy-wasm.mjs +23 -0
  41. package/src/cli/args.ts +35 -4
  42. package/src/cli/help.ts +42 -2
  43. package/src/commands/mcp.ts +58 -0
  44. package/src/commands/render.ts +194 -177
  45. package/src/commands/serve.ts +30 -11
  46. package/src/convert/parse-json.ts +2 -57
  47. package/src/convert/printer.ts +2 -376
  48. package/src/convert/schema.ts +9 -105
  49. package/src/generated/version.ts +2 -2
  50. package/src/index.ts +3 -0
@@ -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';
@@ -1,105 +1,9 @@
1
- import type { NowlineFile } from '@nowline/core';
2
- import type { AstNode, CstNode, LangiumDocument } from 'langium';
3
-
4
- export const NOWLINE_SCHEMA_VERSION = '1';
5
-
6
- export interface NowlineDocument {
7
- $nowlineSchema: string;
8
- file: { uri: string; source: string };
9
- ast: JsonAstNode;
10
- }
11
-
12
- export interface Position {
13
- start: { line: number; column: number; offset: number };
14
- end: { line: number; column: number; offset: number };
15
- }
16
-
17
- export interface JsonAstNode {
18
- $type: string;
19
- $position?: Position;
20
- [key: string]: unknown;
21
- }
22
-
23
- export interface SerializeOptions {
24
- includePositions?: boolean;
25
- }
26
-
27
- // Keys that Langium adds to AST nodes that we don't want in the JSON form.
28
- const CONTAINER_KEYS = new Set(['$container', '$containerProperty', '$containerIndex']);
29
- // $cstNode / $document are runtime-only. $type and $position are emitted.
30
- const RUNTIME_KEYS = new Set(['$cstNode', '$document']);
31
-
32
- export function serializeToJson(
33
- document: LangiumDocument<NowlineFile>,
34
- source: string,
35
- options: SerializeOptions = {},
36
- ): NowlineDocument {
37
- const includePositions = options.includePositions ?? true;
38
- return {
39
- $nowlineSchema: NOWLINE_SCHEMA_VERSION,
40
- file: {
41
- uri: document.uri.toString(),
42
- source,
43
- },
44
- ast: serializeNode(document.parseResult.value, includePositions),
45
- };
46
- }
47
-
48
- function serializeNode(node: AstNode, includePositions: boolean): JsonAstNode {
49
- const out: JsonAstNode = { $type: node.$type };
50
- if (includePositions) {
51
- const pos = cstPosition(node.$cstNode);
52
- if (pos) out.$position = pos;
53
- }
54
- for (const [key, value] of Object.entries(node)) {
55
- if (key.startsWith('$')) continue;
56
- if (CONTAINER_KEYS.has(key) || RUNTIME_KEYS.has(key)) continue;
57
- out[key] = serializeValue(value, includePositions);
58
- }
59
- return out;
60
- }
61
-
62
- function serializeValue(value: unknown, includePositions: boolean): unknown {
63
- if (value === null || value === undefined) return value;
64
- if (Array.isArray(value)) {
65
- return value.map((v) => serializeValue(v, includePositions));
66
- }
67
- if (isAstNode(value)) {
68
- return serializeNode(value, includePositions);
69
- }
70
- if (typeof value === 'object') {
71
- const record = value as Record<string, unknown>;
72
- const out: Record<string, unknown> = {};
73
- for (const [k, v] of Object.entries(record)) {
74
- if (k.startsWith('$')) continue;
75
- if (CONTAINER_KEYS.has(k) || RUNTIME_KEYS.has(k)) continue;
76
- out[k] = serializeValue(v, includePositions);
77
- }
78
- return out;
79
- }
80
- return value;
81
- }
82
-
83
- function isAstNode(value: unknown): value is AstNode {
84
- return (
85
- value !== null &&
86
- typeof value === 'object' &&
87
- typeof (value as { $type?: unknown }).$type === 'string'
88
- );
89
- }
90
-
91
- function cstPosition(cst: CstNode | undefined): Position | undefined {
92
- if (!cst) return undefined;
93
- return {
94
- start: {
95
- line: cst.range.start.line + 1,
96
- column: cst.range.start.character + 1,
97
- offset: cst.offset,
98
- },
99
- end: {
100
- line: cst.range.end.line + 1,
101
- column: cst.range.end.character + 1,
102
- offset: cst.end,
103
- },
104
- };
105
- }
1
+ // Re-exported from @nowline/core. Kept here for backwards compatibility.
2
+ export {
3
+ type JsonAstNode,
4
+ NOWLINE_SCHEMA_VERSION,
5
+ type NowlineDocument,
6
+ type Position,
7
+ type SerializeOptions,
8
+ serializeToJson,
9
+ } from '@nowline/core';
@@ -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.5.1";
3
+ export const CLI_VERSION = "0.7.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": "fc40638",
15
+ "sha": "0f07e00",
16
16
  "isRelease": true,
17
17
  "isDirty": false
18
18
  };
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  import { parseArgv } from './cli/args.js';
3
3
  import { renderHelp, renderVersion } from './cli/help.js';
4
4
  import { initHandler } from './commands/init.js';
5
+ import { mcpHandler } from './commands/mcp.js';
5
6
  import { renderHandler } from './commands/render.js';
6
7
  import { serveHandler } from './commands/serve.js';
7
8
  import { CliError, ExitCode } from './io/exit-codes.js';
@@ -30,6 +31,8 @@ async function run(): Promise<number> {
30
31
  await initHandler({ args: parsed });
31
32
  } else if (parsed.mode === 'serve') {
32
33
  await serveHandler({ args: parsed });
34
+ } else if (parsed.mode === 'mcp') {
35
+ await mcpHandler({ args: parsed });
33
36
  } else {
34
37
  await renderHandler({ args: parsed });
35
38
  }