@nowline/cli 0.2.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/LICENSE +190 -0
- package/README.md +372 -0
- package/dist/cli/args.d.ts +54 -0
- package/dist/cli/args.d.ts.map +1 -0
- package/dist/cli/args.js +165 -0
- package/dist/cli/args.js.map +1 -0
- package/dist/cli/formats.d.ts +61 -0
- package/dist/cli/formats.d.ts.map +1 -0
- package/dist/cli/formats.js +153 -0
- package/dist/cli/formats.js.map +1 -0
- package/dist/cli/help.d.ts +3 -0
- package/dist/cli/help.d.ts.map +1 -0
- package/dist/cli/help.js +90 -0
- package/dist/cli/help.js.map +1 -0
- package/dist/cli/output-path.d.ts +57 -0
- package/dist/cli/output-path.d.ts.map +1 -0
- package/dist/cli/output-path.js +70 -0
- package/dist/cli/output-path.js.map +1 -0
- package/dist/commands/init.d.ts +20 -0
- package/dist/commands/init.d.ts.map +1 -0
- package/dist/commands/init.js +80 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/render.d.ts +15 -0
- package/dist/commands/render.d.ts.map +1 -0
- package/dist/commands/render.js +435 -0
- package/dist/commands/render.js.map +1 -0
- package/dist/commands/serve.d.ts +16 -0
- package/dist/commands/serve.d.ts.map +1 -0
- package/dist/commands/serve.js +287 -0
- package/dist/commands/serve.js.map +1 -0
- package/dist/convert/parse-json.d.ts +7 -0
- package/dist/convert/parse-json.d.ts.map +1 -0
- package/dist/convert/parse-json.js +34 -0
- package/dist/convert/parse-json.js.map +1 -0
- package/dist/convert/printer.d.ts +6 -0
- package/dist/convert/printer.d.ts.map +1 -0
- package/dist/convert/printer.js +334 -0
- package/dist/convert/printer.js.map +1 -0
- package/dist/convert/schema.d.ts +33 -0
- package/dist/convert/schema.d.ts.map +1 -0
- package/dist/convert/schema.js +77 -0
- package/dist/convert/schema.js.map +1 -0
- package/dist/core/parse.d.ts +24 -0
- package/dist/core/parse.d.ts.map +1 -0
- package/dist/core/parse.js +58 -0
- package/dist/core/parse.js.map +1 -0
- package/dist/diagnostics/adapt.d.ts +46 -0
- package/dist/diagnostics/adapt.d.ts.map +1 -0
- package/dist/diagnostics/adapt.js +109 -0
- package/dist/diagnostics/adapt.js.map +1 -0
- package/dist/diagnostics/format.d.ts +18 -0
- package/dist/diagnostics/format.d.ts.map +1 -0
- package/dist/diagnostics/format.js +41 -0
- package/dist/diagnostics/format.js.map +1 -0
- package/dist/diagnostics/index.d.ts +5 -0
- package/dist/diagnostics/index.d.ts.map +1 -0
- package/dist/diagnostics/index.js +5 -0
- package/dist/diagnostics/index.js.map +1 -0
- package/dist/diagnostics/json.d.ts +8 -0
- package/dist/diagnostics/json.d.ts.map +1 -0
- package/dist/diagnostics/json.js +24 -0
- package/dist/diagnostics/json.js.map +1 -0
- package/dist/diagnostics/model.d.ts +44 -0
- package/dist/diagnostics/model.d.ts.map +1 -0
- package/dist/diagnostics/model.js +2 -0
- package/dist/diagnostics/model.js.map +1 -0
- package/dist/diagnostics/text.d.ts +6 -0
- package/dist/diagnostics/text.d.ts.map +1 -0
- package/dist/diagnostics/text.js +43 -0
- package/dist/diagnostics/text.js.map +1 -0
- package/dist/generated/templates.d.ts +4 -0
- package/dist/generated/templates.d.ts.map +1 -0
- package/dist/generated/templates.js +9 -0
- package/dist/generated/templates.js.map +1 -0
- package/dist/generated/version.d.ts +11 -0
- package/dist/generated/version.d.ts.map +1 -0
- package/dist/generated/version.js +8 -0
- package/dist/generated/version.js.map +1 -0
- package/dist/i18n/locale.d.ts +56 -0
- package/dist/i18n/locale.d.ts.map +1 -0
- package/dist/i18n/locale.js +107 -0
- package/dist/i18n/locale.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/io/config.d.ts +2 -0
- package/dist/io/config.d.ts.map +1 -0
- package/dist/io/config.js +5 -0
- package/dist/io/config.js.map +1 -0
- package/dist/io/exit-codes.d.ts +12 -0
- package/dist/io/exit-codes.d.ts.map +1 -0
- package/dist/io/exit-codes.js +15 -0
- package/dist/io/exit-codes.js.map +1 -0
- package/dist/io/read.d.ts +13 -0
- package/dist/io/read.d.ts.map +1 -0
- package/dist/io/read.js +53 -0
- package/dist/io/read.js.map +1 -0
- package/dist/io/write.d.ts +32 -0
- package/dist/io/write.d.ts.map +1 -0
- package/dist/io/write.js +61 -0
- package/dist/io/write.js.map +1 -0
- package/dist/version.d.ts +13 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +20 -0
- package/dist/version.js.map +1 -0
- package/man/fr/nowline.1 +424 -0
- package/man/fr/nowline.5 +1864 -0
- package/man/nowline.1 +517 -0
- package/man/nowline.5 +1784 -0
- package/package.json +66 -0
- package/scripts/bundle-templates.mjs +105 -0
- package/scripts/compile.mjs +131 -0
- package/src/cli/args.ts +252 -0
- package/src/cli/formats.ts +207 -0
- package/src/cli/help.ts +92 -0
- package/src/cli/output-path.ts +98 -0
- package/src/commands/init.ts +99 -0
- package/src/commands/render.ts +566 -0
- package/src/commands/serve.ts +322 -0
- package/src/convert/parse-json.ts +57 -0
- package/src/convert/printer.ts +376 -0
- package/src/convert/schema.ts +105 -0
- package/src/core/parse.ts +93 -0
- package/src/diagnostics/adapt.ts +148 -0
- package/src/diagnostics/format.ts +70 -0
- package/src/diagnostics/index.ts +4 -0
- package/src/diagnostics/json.ts +30 -0
- package/src/diagnostics/model.ts +48 -0
- package/src/diagnostics/text.ts +62 -0
- package/src/generated/templates.ts +12 -0
- package/src/generated/version.ts +18 -0
- package/src/i18n/locale.ts +133 -0
- package/src/index.ts +60 -0
- package/src/io/config.ts +11 -0
- package/src/io/exit-codes.ts +18 -0
- package/src/io/read.ts +70 -0
- package/src/io/write.ts +94 -0
- package/src/version.ts +21 -0
|
@@ -0,0 +1,376 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { createNowlineServices, type NowlineFile, type NowlineServices } from '@nowline/core';
|
|
2
|
+
import { type LangiumDocument, URI } from 'langium';
|
|
3
|
+
import {
|
|
4
|
+
adaptLangiumDiagnostic,
|
|
5
|
+
adaptLexerError,
|
|
6
|
+
adaptParserError,
|
|
7
|
+
type LangiumLikeDiagnostic,
|
|
8
|
+
} from '../diagnostics/adapt.js';
|
|
9
|
+
import type { CliDiagnostic, DiagnosticSource } from '../diagnostics/model.js';
|
|
10
|
+
|
|
11
|
+
export interface ParseOptions {
|
|
12
|
+
validate?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ParseResult {
|
|
16
|
+
ast: NowlineFile;
|
|
17
|
+
document: LangiumDocument<NowlineFile>;
|
|
18
|
+
diagnostics: CliDiagnostic[];
|
|
19
|
+
/** True if any diagnostic (parse, lex, or semantic validation) has severity 'error'. */
|
|
20
|
+
hasErrors: boolean;
|
|
21
|
+
/** True if any *parser/lexer* diagnostic is present. Semantic validation errors don't count. */
|
|
22
|
+
hasParseErrors: boolean;
|
|
23
|
+
source: DiagnosticSource;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type Services = {
|
|
27
|
+
shared: ReturnType<typeof createNowlineServices>['shared'];
|
|
28
|
+
Nowline: NowlineServices;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
let cachedServices: Services | undefined;
|
|
32
|
+
let docCounter = 0;
|
|
33
|
+
|
|
34
|
+
export function getServices(): Services {
|
|
35
|
+
if (!cachedServices) cachedServices = createNowlineServices();
|
|
36
|
+
return cachedServices;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function parseSource(
|
|
40
|
+
contents: string,
|
|
41
|
+
filePath: string,
|
|
42
|
+
options: ParseOptions = {},
|
|
43
|
+
): 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], {
|
|
49
|
+
validation: options.validate ?? true,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const source: DiagnosticSource = { file: filePath, contents };
|
|
53
|
+
const diagnostics: CliDiagnostic[] = [];
|
|
54
|
+
const parseDiagnostics: CliDiagnostic[] = [];
|
|
55
|
+
|
|
56
|
+
for (const err of doc.parseResult.lexerErrors) {
|
|
57
|
+
const adapted = adaptLexerError(err, filePath);
|
|
58
|
+
parseDiagnostics.push(adapted);
|
|
59
|
+
diagnostics.push(adapted);
|
|
60
|
+
}
|
|
61
|
+
for (const err of doc.parseResult.parserErrors) {
|
|
62
|
+
const adapted = adaptParserError(err, filePath);
|
|
63
|
+
parseDiagnostics.push(adapted);
|
|
64
|
+
diagnostics.push(adapted);
|
|
65
|
+
}
|
|
66
|
+
for (const diag of doc.diagnostics ?? []) {
|
|
67
|
+
diagnostics.push(adaptLangiumDiagnostic(diag as LangiumLikeDiagnostic, filePath));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const hasErrors = diagnostics.some((d) => d.severity === 'error');
|
|
71
|
+
const hasParseErrors = parseDiagnostics.some((d) => d.severity === 'error');
|
|
72
|
+
return {
|
|
73
|
+
ast: doc.parseResult.value,
|
|
74
|
+
document: doc,
|
|
75
|
+
diagnostics,
|
|
76
|
+
hasErrors,
|
|
77
|
+
hasParseErrors,
|
|
78
|
+
source,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function uriFor(filePath: string): URI {
|
|
83
|
+
// Always mint a fresh URI for each parse. Re-using a URI across calls causes
|
|
84
|
+
// Langium's DocumentBuilder to mutate the prior document and/or rebind
|
|
85
|
+
// cross-references against an incompatible AST, which surfaces as spurious
|
|
86
|
+
// diagnostics when the CLI is embedded in a long-running process (tests,
|
|
87
|
+
// language server, etc.). The suffix ensures `build([doc])` is always a net
|
|
88
|
+
// new document.
|
|
89
|
+
if (filePath === '<stdin>') {
|
|
90
|
+
return URI.parse(`memory:///stdin-${++docCounter}.nowline`);
|
|
91
|
+
}
|
|
92
|
+
return URI.parse(`memory:///doc-${++docCounter}.nowline`);
|
|
93
|
+
}
|