@voxgig/apidef 8.15.2 → 8.17.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 (55) hide show
  1. package/dist/apidef.js +15 -0
  2. package/dist/apidef.js.map +1 -1
  3. package/dist/builder/entity/entity.js +8 -5
  4. package/dist/builder/entity/entity.js.map +1 -1
  5. package/dist/builder/entity/info.js +1 -1
  6. package/dist/builder/entity/info.js.map +1 -1
  7. package/dist/builder/flow.js +3 -3
  8. package/dist/builder/flow.js.map +1 -1
  9. package/dist/cli.js +8 -10
  10. package/dist/cli.js.map +1 -1
  11. package/dist/desc.d.ts +2 -0
  12. package/dist/guide/guide.d.ts +14 -1
  13. package/dist/guide/guide.js +106 -62
  14. package/dist/guide/guide.js.map +1 -1
  15. package/dist/guide/heuristic01.d.ts +9 -1
  16. package/dist/guide/heuristic01.js +290 -71
  17. package/dist/guide/heuristic01.js.map +1 -1
  18. package/dist/parse.d.ts +4 -1
  19. package/dist/parse.js +183 -133
  20. package/dist/parse.js.map +1 -1
  21. package/dist/refcount.d.ts +6 -0
  22. package/dist/refcount.js +215 -0
  23. package/dist/refcount.js.map +1 -0
  24. package/dist/transform/entity.d.ts +1 -1
  25. package/dist/transform/entity.js +12 -3
  26. package/dist/transform/entity.js.map +1 -1
  27. package/dist/transform/field.js +1 -1
  28. package/dist/transform/field.js.map +1 -1
  29. package/dist/transform/operation.js +1 -1
  30. package/dist/transform/operation.js.map +1 -1
  31. package/dist/transform/top.js +1 -1
  32. package/dist/transform/top.js.map +1 -1
  33. package/dist/tsconfig.tsbuildinfo +1 -1
  34. package/dist/utility.d.ts +3 -1
  35. package/dist/utility.js +85 -6
  36. package/dist/utility.js.map +1 -1
  37. package/model/{guide.aon → guide.aontu} +1 -1
  38. package/package.json +3 -3
  39. package/src/apidef.ts +20 -1
  40. package/src/builder/entity/entity.ts +8 -5
  41. package/src/builder/entity/info.ts +1 -1
  42. package/src/builder/flow.ts +3 -3
  43. package/src/cli.ts +8 -10
  44. package/src/desc.ts +3 -0
  45. package/src/guide/guide.ts +136 -71
  46. package/src/guide/heuristic01.ts +358 -90
  47. package/src/parse.ts +221 -126
  48. package/src/refcount.ts +244 -0
  49. package/src/transform/entity.ts +16 -5
  50. package/src/transform/field.ts +1 -1
  51. package/src/transform/operation.ts +1 -1
  52. package/src/transform/top.ts +1 -1
  53. package/src/types.ts +2 -2
  54. package/src/utility.ts +102 -6
  55. /package/model/{apidef.aon → apidef.aontu} +0 -0
@@ -2,7 +2,7 @@
2
2
 
3
3
  import Path from 'node:path'
4
4
 
5
- import { Jostraca, Project, File, Content, each } from 'jostraca'
5
+ import { each } from 'jostraca'
6
6
 
7
7
  import { Aontu } from 'aontu'
8
8
 
@@ -32,6 +32,7 @@ import {
32
32
  debugpathOn,
33
33
  formatJSONIC,
34
34
  relativizePath,
35
+ removeLegacyAon,
35
36
  } from '../utility'
36
37
 
37
38
 
@@ -46,47 +47,79 @@ const aontu = new Aontu()
46
47
 
47
48
 
48
49
 
49
- function migrateLegacyGuide(fs: any, folder: string, guideprefix: string): boolean {
50
- const guidepath = Path.join(folder, 'guide', guideprefix + 'guide.aon')
51
- const legacyguide = Path.join(folder, 'guide', guideprefix + 'guide.aontu')
52
-
53
- if (fs.existsSync(guidepath) || !fs.existsSync(legacyguide)) {
54
- return false
55
- }
56
-
57
- let migrated = String(fs.readFileSync(legacyguide, 'utf8'))
58
- .replace(/@"@voxgig\/apidef\/model\/guide\.aontu"/g,
59
- '@"@voxgig/apidef/model/guide.aon"')
50
+ function migrateGuideIncludes(src: string, guideprefix: string): string {
51
+ let migrated = src
52
+ .replace(/@"@voxgig\/apidef\/model\/guide\.aon"/g,
53
+ '@"@voxgig/apidef/model/guide.aontu"')
60
54
 
61
55
  // The sibling include is written bare or with `./`; both name this file.
62
56
  for (const dir of ['', './']) {
63
57
  migrated = migrated
64
- .split('@"' + dir + guideprefix + 'base-guide.aontu"')
65
- .join('@"' + dir + guideprefix + 'base-guide.aon"')
58
+ .split('@"' + dir + guideprefix + 'base-guide.aon"')
59
+ .join('@"' + dir + guideprefix + 'base-guide.aontu"')
66
60
  }
67
61
 
68
- fs.writeFileSync(guidepath, migrated)
62
+ return migrated
63
+ }
64
+
65
+
66
+ // aontu refuses a bare sibling include, so it gains the `./` it needs.
67
+ function prefixGuideInclude(src: string, guideprefix: string): string {
68
+ return src
69
+ .split('@"' + guideprefix + 'base-guide.aontu"')
70
+ .join('@"./' + guideprefix + 'base-guide.aontu"')
71
+ }
72
+
73
+
74
+ // A `.aon` entry file is unresolvable: aontu reads only `.aontu` as source.
75
+ // So this renames AND rewrites both includes — a repair, not a convenience.
76
+ function migrateLegacyGuide(fs: any, folder: string, guideprefix: string): boolean {
77
+ const guidepath = Path.join(folder, 'guide', guideprefix + 'guide.aontu')
78
+ const legacyguide = Path.join(folder, 'guide', guideprefix + 'guide.aon')
79
+
80
+ if (fs.existsSync(guidepath) || !fs.existsSync(legacyguide)) {
81
+ return false
82
+ }
83
+
84
+ fs.writeFileSync(guidepath,
85
+ migrateGuideIncludes(String(fs.readFileSync(legacyguide, 'utf8')), guideprefix))
69
86
  try { fs.unlinkSync(legacyguide) } catch (_err: any) { }
70
87
 
71
88
  return true
72
89
  }
73
90
 
91
+
92
+ // A `.aontu` entry file may still include a `.aon` sibling, so the rename
93
+ // above never fires for it while its include still names an absent file.
94
+ function migrateLegacyGuideInclude(
95
+ fs: any, guidepath: string, guideprefix: string
96
+ ): boolean {
97
+ return rewriteGuide(fs, guidepath, (src) => migrateGuideIncludes(src, guideprefix))
98
+ }
99
+
100
+
74
101
  function migrateGuideIncludePrefix(
75
102
  fs: any, guidepath: string, guideprefix: string
103
+ ): boolean {
104
+ return rewriteGuide(fs, guidepath, (src) => prefixGuideInclude(src, guideprefix))
105
+ }
106
+
107
+
108
+ function rewriteGuide(
109
+ fs: any, guidepath: string, rewrite: (src: string) => string
76
110
  ): boolean {
77
111
  if (!fs.existsSync(guidepath)) {
78
112
  return false
79
113
  }
80
114
 
81
- const bare = '@"' + guideprefix + 'base-guide.aon"'
82
115
  const src = String(fs.readFileSync(guidepath, 'utf8'))
116
+ const migrated = rewrite(src)
83
117
 
84
- if (!src.includes(bare)) {
118
+ if (migrated === src) {
85
119
  return false
86
120
  }
87
121
 
88
- fs.writeFileSync(guidepath,
89
- src.split(bare).join('@"./' + guideprefix + 'base-guide.aon"'))
122
+ fs.writeFileSync(guidepath, migrated)
90
123
 
91
124
  return true
92
125
  }
@@ -94,6 +127,26 @@ function migrateGuideIncludePrefix(
94
127
 
95
128
 
96
129
 
130
+ function guideConflictMessage(
131
+ path: string, conflict: { line: number, text: string }
132
+ ): string {
133
+ return `@voxgig/apidef: guide: unresolved merge conflict at ${path}:${conflict.line}\n` +
134
+ ` ${conflict.text}\n` +
135
+ `Resolve the marked block in ${path}.`
136
+ }
137
+
138
+
139
+ function missingGuideMessage(path: string, guideprefix: string): string {
140
+ return `@voxgig/apidef: guide: ${path} defines no guide map; it needs the include ` +
141
+ `@"./${guideprefix}base-guide.aontu".`
142
+ }
143
+
144
+
145
+ function isPlainObject(val: any): boolean {
146
+ return null != val && 'object' === typeof val && !Array.isArray(val)
147
+ }
148
+
149
+
97
150
  function findConflict(src: string): { line: number, text: string } | null {
98
151
  const lines = String(src || '').split('\n')
99
152
 
@@ -115,7 +168,7 @@ async function buildGuide(ctx: ApiDefContext): Promise<any> {
115
168
  const folder = Path.resolve(ctx.opts.folder)
116
169
 
117
170
  try {
118
- const basejres = await buildBaseGuide(ctx)
171
+ await buildBaseGuide(ctx)
119
172
  }
120
173
  catch (err: any) {
121
174
  errs.push(err)
@@ -125,16 +178,23 @@ async function buildGuide(ctx: ApiDefContext): Promise<any> {
125
178
 
126
179
  let src = ''
127
180
  const guideprefix = null == ctx.opts.outprefix ? '' : ctx.opts.outprefix
128
- let guidepath = Path.join(folder, 'guide', guideprefix + 'guide.aon')
181
+ let guidepath = Path.join(folder, 'guide', guideprefix + 'guide.aontu')
129
182
 
130
183
  if (migrateLegacyGuide(ctx.fs, folder, guideprefix)) {
131
- log.info({ point: 'migrate-guide', note: 'guide.aontu -> guide.aon' })
184
+ log.info({ point: 'migrate-guide', note: 'guide.aon -> guide.aontu' })
185
+ }
186
+
187
+ if (migrateLegacyGuideInclude(ctx.fs, guidepath, guideprefix)) {
188
+ log.info({
189
+ point: 'migrate-guide-include',
190
+ note: 'base-guide.aon -> base-guide.aontu'
191
+ })
132
192
  }
133
193
 
134
194
  if (migrateGuideIncludePrefix(ctx.fs, guidepath, guideprefix)) {
135
195
  log.info({
136
196
  point: 'migrate-guide-prefix',
137
- note: 'base-guide.aon -> ./base-guide.aon'
197
+ note: 'base-guide.aontu -> ./base-guide.aontu'
138
198
  })
139
199
  }
140
200
 
@@ -154,35 +214,10 @@ async function buildGuide(ctx: ApiDefContext): Promise<any> {
154
214
 
155
215
  handleErrors(ctx, errs)
156
216
 
157
- const basepath = Path.join(folder, 'guide', guideprefix + 'base-guide.aon')
158
- for (const checkpath of [guidepath, basepath]) {
159
- let checksrc = ''
160
- try {
161
- checksrc = checkpath === guidepath ? src : String(ctx.fs.readFileSync(checkpath, 'utf8'))
162
- }
163
- catch (_err: any) {
164
- continue
165
- }
166
-
167
- const conflict = findConflict(checksrc)
168
- if (null != conflict) {
169
- errs.push(new Error(
170
- `@voxgig/apidef: guide: unresolved merge conflict at ${
171
- relativizePath(checkpath)}:${conflict.line}\n` +
172
- ` ${conflict.text}\n` +
173
- `A guide is merged, not overwritten, so an edit the regenerated base\n` +
174
- `guide contradicts is left for a human to settle. Resolve the marked\n` +
175
- `block` +
176
- // DELETING ONLY HELPS FOR THE BASE GUIDE. Regeneration rewrites that
177
- // file, while the top-level entry guide is the user's own and is read
178
- // back unchanged — so advising its deletion would send a reader in a
179
- // circle, failing this same check on the next build.
180
- (checkpath === basepath ?
181
- `, or delete ${guideprefix}base-guide.aon to regenerate it from the\n` +
182
- `specification and re-apply the edit afterwards.` :
183
- ` in ${relativizePath(checkpath)}.`)))
184
- break
185
- }
217
+ // Only the entry file: the base guide was just rewritten from the spec.
218
+ const conflict = findConflict(src)
219
+ if (null != conflict) {
220
+ errs.push(new Error(guideConflictMessage(relativizePath(guidepath), conflict)))
186
221
  }
187
222
 
188
223
  handleErrors(ctx, errs)
@@ -194,7 +229,7 @@ async function buildGuide(ctx: ApiDefContext): Promise<any> {
194
229
 
195
230
  const opts: any = {
196
231
  path: guidepath,
197
- errs,
232
+ errfs: ctx.fs,
198
233
  }
199
234
 
200
235
  if (ctx.fsInjected) {
@@ -203,7 +238,19 @@ async function buildGuide(ctx: ApiDefContext): Promise<any> {
203
238
 
204
239
  ctx.work.guideAontuFs = undefined !== opts.fs
205
240
 
206
- const guideModel = aontu.generate(src, opts)
241
+ // One AontuError carries every aontu failure, formatted: collect mode
242
+ // leaves a generation-time failure's message empty.
243
+ let guideModel: any
244
+ try {
245
+ guideModel = aontu.generate(src, opts)
246
+ }
247
+ catch (err: any) {
248
+ errs.push(err)
249
+ }
250
+
251
+ if (0 === errs.length && !isPlainObject(guideModel?.guide)) {
252
+ errs.push(new Error(missingGuideMessage(relativizePath(guidepath), guideprefix)))
253
+ }
207
254
 
208
255
  handleErrors(ctx, errs)
209
256
 
@@ -256,8 +303,10 @@ async function buildBaseGuide(ctx: ApiDefContext) {
256
303
  throw new Error('Unknown guide strategy: ' + ctx.opts.strategy)
257
304
  }
258
305
 
306
+ const guideprefix = null == ctx.opts.outprefix ? '' : ctx.opts.outprefix
307
+
259
308
  const guideBlocks = [
260
- '# Guide',
309
+ ...baseGuideHeader(guideprefix),
261
310
  '',
262
311
  'guide: {',
263
312
  ]
@@ -371,7 +420,7 @@ async function buildBaseGuide(ctx: ApiDefContext) {
371
420
  guideBlocks.push(
372
421
  ` # Deactivated by the heuristic` +
373
422
  (null == why ? '' : ` (${why})`) + `. Set` +
374
- ` \`active: true\` here in guide.aon to generate it as an entity.`)
423
+ ` \`active: true\` here in guide.aontu to generate it as an entity.`)
375
424
  guideBlocks.push(` active: *false`)
376
425
  }
377
426
 
@@ -392,25 +441,34 @@ async function buildBaseGuide(ctx: ApiDefContext) {
392
441
 
393
442
  ctx.note.guide = { base: guideSrc }
394
443
 
395
- const baseGuideFileName =
396
- (null == ctx.opts.outprefix ? '' : ctx.opts.outprefix) + 'base-guide.aon'
444
+ const guidefolder = Path.join(ctx.opts.folder, 'guide')
445
+ ctx.fs.mkdirSync(guidefolder, { recursive: true })
446
+ const basepath = Path.join(guidefolder, guideprefix + 'base-guide.aontu')
447
+ ctx.fs.writeFileSync(basepath, guideSrc)
448
+ removeLegacyAon(ctx.fs, ctx.log, basepath)
449
+ }
397
450
 
398
- const jostraca = Jostraca({
399
- folder: ctx.opts.folder + '/guide',
400
- now: ctx.spec.now,
401
- fs: () => ctx.fs,
402
- log: ctx.log,
403
- })
404
451
 
405
- const root = () => Project({ folder: '.' }, async () => {
406
- File({ name: baseGuideFileName }, () => Content(guideSrc))
407
- })
452
+ // The entry file a project writes once and owns; the CLI prints it.
453
+ function guideEntrySource(guideprefix: string): string[] {
454
+ return [
455
+ '# Guide entry file: put customizations below the includes. The base guide',
456
+ '# it includes is generated and overwritten on every build.',
457
+ '@"@voxgig/apidef/model/guide.aontu"',
458
+ '@"./' + guideprefix + 'base-guide.aontu"',
459
+ ]
460
+ }
408
461
 
409
- const jres = await jostraca.generate({
410
- existing: { txt: { merge: true } }
411
- }, root)
412
462
 
413
- return jres
463
+ // Written into every base guide, so a reader of the file learns where an
464
+ // edit belongs before making one there.
465
+ function baseGuideHeader(guideprefix: string): string[] {
466
+ return [
467
+ '# Generated from the API definition and overwritten on every build: do not',
468
+ '# edit this file. Put customizations in the guide entry file, which includes',
469
+ '# this one and overrides its defaults:',
470
+ '# ' + guideprefix + 'guide.aontu',
471
+ ]
414
472
  }
415
473
 
416
474
 
@@ -555,6 +613,13 @@ function validateBaseBuide(ctx: ApiDefContext, baseguide: any) {
555
613
 
556
614
 
557
615
  export {
616
+ migrateGuideIncludes,
617
+ prefixGuideInclude,
618
+ findConflict,
619
+ guideConflictMessage,
620
+ missingGuideMessage,
621
+ baseGuideHeader,
622
+ guideEntrySource,
558
623
  migrateLegacyGuide,
559
624
  migrateGuideIncludePrefix,
560
625
  buildGuide