-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
- Loading branch information
Showing
11 changed files
with
120 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
export * as constants from './constants.mjs'; | ||
export { default as generators } from './generators/index.mjs'; | ||
export { default as createGenerator } from './generators.mjs'; | ||
export * from './loader.mjs'; | ||
export * from './loaders/markdown.mjs'; | ||
export * from './loaders/javascript.mjs'; | ||
export { default as createMetadata } from './metadata.mjs'; | ||
export * from './parser.mjs'; | ||
export * from './parsers/markdown.mjs'; | ||
export * from './parsers/javascript.mjs'; | ||
export { default as createQueries } from './queries.mjs'; | ||
export { default as createNodeReleases } from './releases.mjs'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
import { readFile } from 'node:fs/promises'; | ||
import { extname } from 'node:path'; | ||
|
||
import { globSync } from 'glob'; | ||
import { VFile } from 'vfile'; | ||
|
||
/** | ||
* This creates a "loader" for loading Javascript source files into VFiles. | ||
*/ | ||
const createLoader = () => { | ||
/** | ||
* Loads the JavaScript source files and transforms them into VFiles | ||
* | ||
* @param {string | Array<string>} searchPath | ||
*/ | ||
const loadFiles = searchPath => { | ||
const resolvedFiles = globSync(searchPath).filter( | ||
filePath => extname(filePath) === '.js' | ||
); | ||
|
||
return resolvedFiles.map(async filePath => { | ||
const fileContents = await readFile(filePath, 'utf-8'); | ||
|
||
return new VFile({ path: filePath, value: fileContents }); | ||
}); | ||
}; | ||
|
||
return { loadFiles }; | ||
}; | ||
|
||
export default createLoader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
import * as acorn from 'acorn'; | ||
|
||
/** | ||
* Creates a Javascript source parser for a given source file | ||
*/ | ||
const createParser = () => { | ||
/** | ||
* Parses a given JavaScript file into an ESTree AST representation of it | ||
* | ||
* @param {import('vfile').VFile | Promise<import('vfile').VFile>} sourceFile | ||
* @returns {Promise<JsProgram>} | ||
*/ | ||
const parseJsSource = async sourceFile => { | ||
// We allow the API doc VFile to be a Promise of a VFile also, | ||
// hence we want to ensure that it first resolves before we pass it to the parser | ||
const resolvedSourceFile = await Promise.resolve(sourceFile); | ||
|
||
if (typeof resolvedSourceFile.value !== 'string') { | ||
throw new TypeError( | ||
`expected resolvedSourceFile.value to be string but got ${typeof resolvedSourceFile.value}` | ||
); | ||
} | ||
|
||
const res = acorn.parse(resolvedSourceFile.value, { | ||
allowReturnOutsideFunction: true, | ||
ecmaVersion: 'latest', | ||
locations: true, | ||
}); | ||
|
||
return { | ||
...res, | ||
path: resolvedSourceFile.path, | ||
}; | ||
}; | ||
|
||
/** | ||
* Parses multiple JavaScript files into ESTree ASTs by wrapping parseJsSource | ||
* | ||
* @param {Array<import('vfile').VFile | Promise<import('vfile').VFile>>} apiDocs List of API doc files to be parsed | ||
* @returns {Promise<Array<JsProgram>>} | ||
*/ | ||
const parseJsSources = async apiDocs => { | ||
// We do a Promise.all, to ensure that each API doc is resolved asynchronously | ||
// but all need to be resolved first before we return the result to the caller | ||
const resolvedApiDocEntries = await Promise.all(apiDocs.map(parseJsSource)); | ||
|
||
return resolvedApiDocEntries; | ||
}; | ||
|
||
return { parseJsSource, parseJsSources }; | ||
}; | ||
|
||
export default createParser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters