Getting Started

Installation

npm:

npm i -D @ts-ast-parser/core

yarn:

yarn add -D @ts-ast-parser/core

pnpm:

pnpm add -D @ts-ast-parser/core

How to use it

If you have a TypeScript project with a TSConfig file:

import { parseFromProject } from '@ts-ast-parser/core';

const {project, errors} = await parseFromProject(<options>);

if (errors.length > 0) {
    // Handle the errors
    process.exit(1);
}

const reflectedModules = project?.getModules() ?? [];

You can also provide the array of files:

import { parseFromFiles } from '@ts-ast-parser/core';

const {project, errors} = await parseFromFiles(['test1.ts', 'test2.ts'], <options>);

if (errors.length > 0) {
    // Handle the errors
    process.exit(1);
}

const reflectedModules = project?.getModules() ?? [];

Or you can provide a glob or an array of globs instead:

import { parseFromGlob } from '@ts-ast-parser/core';

const {project, errors} = await parseFromGlob('**/*.ts', <options>);

if (errors.length > 0) {
    // Handle the errors
    process.exit(1);
}

const reflectedModules = project?.getModules() ?? [];

Finally, there is also support for code snippets:

import { parseFromSource } from '@ts-ast-parser/core';

const {project, errors} = await parseFromSource('const foo = true;export { foo };', <options>);

if (errors.length > 0) {
    // Handle the errors
    process.exit(1);
}

const reflectedModules = project?.getModules() ?? [];

See the API Reference for more information around the parser functions.