#!/usr/bin/env node

const path = require('path');
const proc = require('child_process');
const startCase = require('lodash.startcase');

const baseDir = process.argv[2];

const files = proc.execFileSync(
  'find', [baseDir, '-type', 'f'], { encoding: 'utf8' }
).split('\n').filter(s => s !== '');

console.log('.API');

for (const file of files) {
  const doc = file.replace(baseDir, '');
  const title = path.parse(file).name;
  console.log(`* xref:${doc}[${startCase(title)}]`);
}

@@ -318,38 +318,48 @@
 );%0A%0A
-for (
 const 
-file of 
+links = files.map((
 file
-s
 )
+ =%3E
  %7B%0A  
+ 
+ 
 cons
@@ -393,16 +393,18 @@
 r, '');%0A
+  
   const 
@@ -438,22 +438,34 @@
 me;%0A
+%0A
   
-console.log(
+  return %7B%0A      xref: 
 %60* x
@@ -496,13 +496,304 @@
 itle)%7D%5D%60
+,%0A      title,%0A    %7D;%0A%7D);%0A%0A// Case-insensitive sort based on titles (so 'token/ERC20' gets sorted as 'erc20')%0Aconst sortedLinks = links.sort(function (a, b) %7B%0A  return a.title.toLowerCase().localeCompare(b.title.toLowerCase());%0A%7D);%0A%0Afor (const link of sortedLinks) %7B%0A   console.log(link.xref
 );%0A%7D%0A

Improve API docgen sorting