mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-28 09:06:51 +00:00
feat(Parser): just-in-time YTNode generation (#310)
* refactor: merge NavigatableText into Text * fix(Text): data might not be object * refactor: remove GetParserByName from map * feat(Parser): just-in-time YTNode generation * refactor: cleanup YTNodeGenerator * fix: YTNode map imports * feat(YTNodeGenerator): primative types Add support for inferring primatives types * fix(YTNodeGenerator): NavigationEndpoint detection * fix(YTNodeGenerator): fix generated typescript Correct types and linting for generated typescript class * chore: update parsers after merge * feat: add support for object type inference * fix: object type def * docs: basic YTNodeGenerator explanation * docs: tsdoc for YTNodeGenerator * docs: update parser updating guide * fix: apply suggested changes * docs: accessing generated nodes
This commit is contained in:
@@ -3,9 +3,7 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const import_list = [];
|
||||
|
||||
const json = [];
|
||||
const misc_exports = [];
|
||||
const misc_imports = [];
|
||||
|
||||
glob.sync('../src/parser/classes/**/*.{js,ts}', { cwd: __dirname })
|
||||
.forEach((file) => {
|
||||
@@ -16,44 +14,26 @@ glob.sync('../src/parser/classes/**/*.{js,ts}', { cwd: __dirname })
|
||||
|
||||
if (is_misc) {
|
||||
const class_name = file.split('/').pop().replace('.js', '').replace('.ts', '');
|
||||
import_list.push(`import { default as ${class_name} } from './classes/${file}.js';`);
|
||||
misc_exports.push(class_name);
|
||||
misc_imports.push(`export { default as ${class_name} } from './classes/${file}.js';`);
|
||||
} else {
|
||||
import_list.push(`import { default as ${import_name} } from './classes/${file}.js';
|
||||
export { ${import_name} };`);
|
||||
json.push(import_name);
|
||||
import_list.push(`export { default as ${import_name} } from './classes/${file}.js';`);
|
||||
}
|
||||
});
|
||||
|
||||
fs.writeFileSync(
|
||||
path.resolve(__dirname, '../src/parser/map.ts'),
|
||||
path.resolve(__dirname, '../src/parser/nodes.ts'),
|
||||
`// This file was auto generated, do not edit.
|
||||
// See ./scripts/build-parser-map.js
|
||||
import { YTNodeConstructor } from './helpers.js';
|
||||
|
||||
${import_list.join('\n')}
|
||||
`
|
||||
);
|
||||
|
||||
const map: Record<string, YTNodeConstructor> = {
|
||||
${json.join(',\n ')}
|
||||
};
|
||||
fs.writeFileSync(
|
||||
path.resolve(__dirname, '../src/parser/misc.ts'),
|
||||
`// This file was auto generated, do not edit.
|
||||
// See ./scripts/build-parser-map.js
|
||||
|
||||
export const Misc = {
|
||||
${misc_exports.join(',\n ')}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param name - Name of the node to be parsed
|
||||
*/
|
||||
export default function GetParserByName(name: string) {
|
||||
const ParserConstructor = map[name];
|
||||
|
||||
if (!ParserConstructor) {
|
||||
const error = new Error(\`Module not found: \${name}\`);
|
||||
(error as any).code = 'MODULE_NOT_FOUND';
|
||||
throw error;
|
||||
}
|
||||
|
||||
return ParserConstructor;
|
||||
}
|
||||
${misc_imports.join('\n')}
|
||||
`
|
||||
);
|
||||
Reference in New Issue
Block a user