diff --git a/src/api-writer/glua-api-writer.ts b/src/api-writer/glua-api-writer.ts index 9508109..2cfe5fd 100644 --- a/src/api-writer/glua-api-writer.ts +++ b/src/api-writer/glua-api-writer.ts @@ -38,12 +38,17 @@ export const RESERVERD_KEYWORDS = new Set([ 'while' ]); +type IndexedWikiPage = { + index: number; + page: WikiPage; +}; + export class GluaApiWriter { private readonly writtenClasses: Set = new Set(); private readonly writtenLibraryGlobals: Set = new Set(); private readonly pageOverrides: Map = new Map(); - private readonly files: Map = new Map(); + private readonly files: Map = new Map(); constructor() { } @@ -274,32 +279,36 @@ export class GluaApiWriter { return api; } - public writePages(pages: WikiPage[], filePath: string) { + public writePages(pages: WikiPage[], filePath: string, index: number = 0) { if (!this.files.has(filePath)) this.files.set(filePath, []); - this.files.get(filePath)!.push(...pages); + + pages.forEach(page => { + this.files.get(filePath)!.push({index: index, page: page}); + }); } public getPages(filePath: string) { return this.files.get(filePath) ?? []; } - public makeApiFromPages(pages: WikiPage[]) { + public makeApiFromPages(pages: IndexedWikiPage[]) { let api = ""; - // First we write the "header" types - for (const page of pages.filter(x => isClass(x) || isLibrary(x))) { - api += this.writePage(page); - } + pages.sort((a, b) => a.index - b.index); - for (const page of pages.filter(x => !isClass(x) && !isLibrary(x))) { - api += this.writePage(page); - } + // First we write the "header" types + for (const page of pages.filter(x => isClass(x.page) || isLibrary(x.page) || isPanel(x.page))) { + api += this.writePage(page.page); + } + for (const page of pages.filter(x => !isClass(x.page) && !isLibrary(x.page) && !isPanel(x.page))) { + api += this.writePage(page.page); + } return api; } public writeToDisk() { - this.files.forEach((pages: WikiPage[], filePath: string) => { + this.files.forEach((pages: IndexedWikiPage[], filePath: string) => { let api = this.makeApiFromPages(pages); if (api.length > 0) { diff --git a/src/cli-scraper.ts b/src/cli-scraper.ts index baef814..5280ff0 100644 --- a/src/cli-scraper.ts +++ b/src/cli-scraper.ts @@ -90,10 +90,12 @@ async function startScrape() { console.log('Scraping all pages...'); let scrape_start = performance.now(); + let cur = 0; let queue: Promise[] = []; for (const pageIndex of pageIndexes) { const pageMarkupScraper = new WikiPageMarkupScraper(`${baseUrl}/${pageIndex.address}?format=text`); + const indexForThis = cur++; pageMarkupScraper.on('scraped', (url, pageMarkups) => { if (pageMarkups.length === 0) return; @@ -118,7 +120,7 @@ async function startScrape() { const moduleFile = path.join(baseDirectory, moduleName); // Write Lua API docs - writer.writePages(pageMarkups, path.join(baseDirectory, `${moduleName}.lua`)); + writer.writePages(pageMarkups, path.join(baseDirectory, `${moduleName}.lua`), indexForThis); // Write JSON data if (!fs.existsSync(moduleFile))